quicopt.mathopt¶
mathopt ¶
quicopt.mathopt — an OR-Tools MathOpt model → a Quicopt Program.
For a model you already have as an OR-Tools
MathOpt <https://developers.google.com/optimization/math_opt>_ model.
It walks a ModelProto — the objective offset + Σ cᵢxᵢ + Σ_{i≤j} qᵢⱼxᵢxⱼ
and each lbᵣ ≤ Σ A[r,j]xⱼ ≤ ubᵣ row — into Quicopt's expression graph, with
variable bounds and integrality into VarDecl and each row into
Zero/Nonneg. ±Inf bounds pass straight through and mean "unbounded in that
direction"; a one-sided range drops its infinite side.
What is supported is LP / MILP / (unconstrained binary-quadratic) QUBO — the kinds of model the service handles. A MathOpt construct outside that (quadratic / second-order-cone / SOS / indicator constraints, multiple objectives) raises, rather than being approximated or dropped.
Requires the optional [mathopt] extra (pip install -e '.[mathopt]').
Example
Maximize 3x + 5y subject to x + 2y ≤ 5, with 0 ≤ x ≤ 4 and y
binary:
from ortools.math_opt.python import mathopt
from quicopt import Client
model = mathopt.Model(name="mix")
x = model.add_variable(lb=0.0, ub=4.0, name="x")
y = model.add_binary_variable(name="y")
model.add_linear_constraint(x + 2 * y <= 5.0)
model.maximize(3 * x + 5 * y)
result = Client().solve(model) # the import below happens inside
print(result.status, result.objective) # optimal 14.0
print(result.solution) # {'x': 3.0, 'y': 1.0}
Client.solve takes the MathOpt model as it
stands and calls this module on the way out, so the import is not a step to
perform — reach for import_model only to hold the Program itself. It
accepts either the high-level mathopt.Model above or the ModelProto
it exports.
The solution is keyed by the MathOpt names, so it reads back against the model that was written.
import_model ¶
Convert an OR-Tools MathOpt model into a Quicopt Program.
model is a ModelProto or a high-level mathopt.Model (exported to
its proto). Variables are named by their MathOpt name, falling back to x{id}
— matching the service's own naming, so solutions line up. A binary variable
(integer with [0,1] bounds) maps to BINARY, other integers to
INTEGER, the rest to CONTINUOUS.