Optimization problem meta-heuristics solver for easy modeling.
Project description
codable-model-optimizer
Optimization problem meta-heuristics solver for easy modeling.
Installation
Use pip
$ pip install codableopt
Use setup.py
# Master branch
$ git clone https://github.com/recruit-tech/codable-model-optimizer
$ python3 setup.py install
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.2.tar.gz
(93.4 kB
view details)
Built Distribution
File details
Details for the file codableopt-0.1.2.tar.gz
.
File metadata
- Download URL: codableopt-0.1.2.tar.gz
- Upload date:
- Size: 93.4 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 | 9277badb1d842410283d8e059f9da2b67022e0b688d1e6f36b1863fcca299571 |
|
MD5 | eeb8567fc8438e6cabcb9c00114e20b6 |
|
BLAKE2b-256 | 35b824dfad4b3a6c5a40bd62ef28b213147be9f7ddac1466a8b772555b36f758 |
File details
Details for the file codableopt-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: codableopt-0.1.2-py3-none-any.whl
- Upload date:
- Size: 79.4 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 | 881788ecad6048db122e96e4e728b139acb0e7efc80039271aeb0f34f1e9a778 |
|
MD5 | 1b91818840062457e9f224ee6128a83b |
|
BLAKE2b-256 | 1286c5213cc2e0885848c7f0048788fe2f92f4505d774dd8ebcff018b38c3066 |