quicopt.pyomo¶
pyomo ¶
quicopt.pyomo — a Pyomo model → a Quicopt Program.
Reads the model at Pyomo's expression-tree level: the single active objective and
each constraint are walked into Quicopt's expression graph, variable bounds and
domain into VarDecl, and each constraint into a Zero/Nonneg row. The
result is a flat Program (one scalar VarDecl per Pyomo VarData; Pyomo
has already expanded indexed components).
An operator the service does not support raises, rather than being approximated or dropped — so a model that imports is a model that means what it says.
Example
Maximize 3x + 5y subject to x + 2y ≤ 5, with 0 ≤ x ≤ 4 and y
binary:
import pyomo.environ as pyo
from quicopt import Client
m = pyo.ConcreteModel()
m.x = pyo.Var(bounds=(0, 4))
m.y = pyo.Var(domain=pyo.Binary)
m.cap = pyo.Constraint(expr=m.x + 2 * m.y <= 5)
m.obj = pyo.Objective(expr=3 * m.x + 5 * m.y, sense=pyo.maximize)
result = Client(api_key="…").solve(m) # the import below happens inside
print(result.status, result.objective) # optimal 14.0
print(result.solution) # {'x1': 3.0, 'x2': 1.0}
Client.solve takes the Pyomo 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.
Note what the solution is keyed by: x1/x2, not m.x/m.y.
Variables are named positionally, because Pyomo has already expanded indexed
components and declaration order is what is left to identify them by.
import_model ¶
Convert a Pyomo ConcreteModel into a Quicopt Program.
Each VarData becomes a scalar VarDecl (x{i} in declaration order)
carrying its bounds and domain (Binary→BINARY, integer domains→
INTEGER, else CONTINUOUS); the active objective and every constraint
become Quicopt expressions. Requires exactly one active objective; an absent
Pyomo variable bound means unbounded in that direction (±Inf). A variable that
is fixed but carries no value raises: its pin has no value to pin to, and the ±Inf
reading of an absent bound would turn it into a free variable instead.
A variable given a distribution (see quicopt.stochastic) is not a decision
at all: it leaves the variable list, becomes a named declaration in
Program.sources, and its every use becomes a reference to that name. The
scenario count and seed ride along as model data. Names are assigned across
all variables before the random ones are set aside, so which variables are
random never renumbers the rest. A model that declares no uncertainty imports
to exactly the Program it always did.
A permutation, partition or lookup table (see quicopt.structured) becomes a
named declaration in Program.structures or Program.params. Its items,
slots, parts and positions count from 0 in the model and from 1 in the
Program, which is how the service counts them; the shift is made here. A
variable marked as 0 or within its bounds becomes SEMICONTINUOUS or
SEMIINTEGER, and the model's blocks become Program.blocks.
Pyomo's Expr_if(IF=c, THEN=a, ELSE=b) is read when c is one
comparison (<=, <, >=, > or ==); a range such as
pyo.inequality(1, x, 3) raises, and is written as two nested Expr_if.
Source code in src/quicopt/pyomo.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |