Optimization problem meta-heuristics solver for easy modeling.
Project description
Optimization problem meta-heuristics solver for easy modeling.
Installation
Use setup.py
# Master branch
$ git clone https://github.com/recruit-tech/codable-model-optimizer
$ python3 setup.py install
Use pip
# Master branch
$ pip3 install git+https://github.com/recruit-tech/codable-model-optimizer
# Specific tag (or branch, commit hash)
$ pip3 install git+https://github.com/recruit-tech/codable-model-optimizer@v0.1.0
Example Usage
Sample1
import numpy as np
from codableopt import *
# set problem
problem = Problem(is_max_problem=True)
# define variables
x = IntVariable(name='x', lower=np.double(0), upper=np.double(5))
y = DoubleVariable(name='y', lower=np.double(0.0), upper=None)
z = CategoryVariable(name='z', categories=['a', 'b', 'c'])
# define objective function
def objective_function(var_x, var_y, var_z, parameters):
obj_value = parameters['coef_x'] * var_x + parameters['coef_y'] * var_y
if var_z == 'a':
obj_value += 10.0
elif var_z == 'b':
obj_value += 8.0
else:
# var_z == 'c'
obj_value -= 3.0
return obj_value
# set objective function and its arguments
problem += Objective(objective=objective_function,
args_map={'var_x': x,
'var_y': y,
'var_z': z,
'parameters': {'coef_x': -3.0, 'coef_y': 4.0}})
# define constraint
problem += 2 * x + 4 * y + 2 * (z == 'a') + 3 * (z == ('b', 'c')) <= 8
problem += 2 * x - y + 2 * (z == 'b') > 3
print(problem)
solver = OptSolver()
# generate optimization methods to be used within the solver
method = PenaltyAdjustmentMethod(steps=40000)
answer, is_feasible = solver.solve(problem, method)
print(f'answer:{answer}, answer_is_feasible:{is_feasible}')
Sample2
import random
from itertools import combinations
from codableopt import Problem, Objective, CategoryVariable, OptSolver, PenaltyAdjustmentMethod
# define distance generating function
def generate_distances(args_place_names):
generated_distances = {}
for point_to_point in combinations(['start'] + args_place_names, 2):
distance_value = random.randint(20, 40)
generated_distances[point_to_point] = distance_value
generated_distances[tuple(reversed(point_to_point))] = distance_value
for x in ['start'] + args_place_names:
generated_distances[(x, x)] = 0
return generated_distances
# generate TSP problem
PLACE_NUM = 30
destination_names = [f'destination_{no}' for no in range(PLACE_NUM)]
place_names = [f'P{no}' for no in range(PLACE_NUM)]
distances = generate_distances(place_names)
destinations = [CategoryVariable(name=destination_name, categories=place_names)
for destination_name in destination_names]
# set problem
problem = Problem(is_max_problem=False)
# define objective function
def calc_distance(var_destinations, para_distances):
return sum([para_distances[(x, y)] for x, y in zip(
['start'] + var_destinations, var_destinations + ['start'])])
# set objective function and its arguments
problem += Objective(objective=calc_distance,
args_map={'var_destinations': destinations, 'para_distances': distances})
# define constraint
# constraint formula that always reaches all points at least once
for place_name in place_names:
problem += sum([(destination == place_name) for destination in destinations]) >= 1
# optimization implementation
solver = OptSolver(round_times=4, debug=True, debug_unit_step=1000)
method = PenaltyAdjustmentMethod(steps=10000, delta_to_update_penalty_rate=0.9)
answer, is_feasible = solver.solve(problem, method, n_jobs=-1)
print(f'answer_is_feasible:{is_feasible}')
root = ["start"] + [answer[root] for root in destination_names] + ["start"]
print(f'root: {" -> ".join(root)}')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
codableopt-0.1.1.tar.gz
(87.3 kB
view details)
Built Distribution
File details
Details for the file codableopt-0.1.1.tar.gz
.
File metadata
- Download URL: codableopt-0.1.1.tar.gz
- Upload date:
- Size: 87.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7d5fb1ab11591de4f69f0c8858c2f476d1bf5e509fdeab4c0740f6594ebb443 |
|
MD5 | cecf4ce38b78567594fbcbfdd23633b2 |
|
BLAKE2b-256 | 8ffb1e2ed7c9c9eab8b008bc7aa6064165fb9a52230a2346b4ce282fb34891be |
File details
Details for the file codableopt-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: codableopt-0.1.1-py3-none-any.whl
- Upload date:
- Size: 55.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89ee106db0bbd264b9da9a0ad51c2c3567dafd089caa7b80ef7e9575fcd076b |
|
MD5 | 7d0cbbf88f61747ddbd842157a81ff1d |
|
BLAKE2b-256 | a764c849fb42ee8efbcf3ae7b55e9a5bb75fbba2e6adf28dbc0451b62954dc16 |