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)}')
Metadata
Release files for codableopt 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| codableopt-0.1.2.tar.gz | 93.4 kB | Without provenance | Details for codableopt-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| codableopt-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for codableopt-0.1.2-py3-none-any.whl |
Total release size: 172.8 kB
Release files / codableopt-0.1.2.tar.gz
| Download URL | codableopt-0.1.2.tar.gz |
|---|---|
| Size | 93.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9277badb1d842410283d8e059f9da2b67022e0b688d1e6f36b1863fcca299571
|
|
BLAKE2b-256 checksum How to use checksums |
35b824dfad4b3a6c5a40bd62ef28b213147be9f7ddac1466a8b772555b36f758
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / codableopt-0.1.2-py3-none-any.whl
| Download URL | codableopt-0.1.2-py3-none-any.whl |
|---|---|
| Size | 79.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
881788ecad6048db122e96e4e728b139acb0e7efc80039271aeb0f34f1e9a778
|
|
BLAKE2b-256 checksum How to use checksums |
1286c5213cc2e0885848c7f0048788fe2f92f4505d774dd8ebcff018b38c3066
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for codableopt 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.