![]() |
Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
|
A solver independent library for modeling optimization problems.
Example use to model the optimization problem:
max 2.0 * x + y
s.t. x + y <= 1.5
x in {0.0, 1.0}
y in [0.0, 2.5]
model = mathopt.Model(name='my_model')
x = model.add_binary_variable(name='x')
y = model.add_variable(lb=0.0, ub=2.5, name='y')
# We can directly use linear combinations of variables ...
model.add_linear_constraint(x + y <= 1.5, name='c')
# ... or build them incrementally.
objective_expression = 0
objective_expression += 2 * x
objective_expression += y
model.maximize(objective_expression)
# May raise a RuntimeError on invalid input or internal solver errors.
result = mathopt.solve(model, mathopt.SolverType.GSCIP)
if result.termination.reason not in (mathopt.TerminationReason.OPTIMAL,
mathopt.TerminationReason.FEASIBLE):
raise RuntimeError(f'model failed to solve: {result.termination}')
print(f'Objective value: {result.objective_value()}')
print(f'Value for variable x: {result.variable_values()[x]}')
Classes | |
| class | Model |
| class | UpdateTracker |