Class: Quicopt::Model
- Inherits:
-
Object
- Object
- Quicopt::Model
- Defined in:
- lib/quicopt/model.rb
Overview
An optimization model: variables, one objective, and constraint rows.
m = Quicopt::Model.new
x = m.int_var(0, 4, "x")
y = m.bin_var("y")
m.maximize(3 * x + 2 * y)
m.add(x + y <= 5)
You build the model; to_program / encode turn it into the message the
service reads, and Quicopt::Client#solve sends it.
Instance Attribute Summary collapse
-
#constraints ⇒ Array<Constraint>
readonly
The constraint rows, in insertion order.
-
#objective ⇒ Expression
readonly
The objective (the zero expression until set).
-
#sense ⇒ String
readonly
"min"or"max". -
#variables ⇒ Array<Variable>
readonly
The declared variables, in declaration order.
Instance Method Summary collapse
-
#add(constraint) ⇒ Constraint
Add a constraint row built by
<=,>=or==. -
#bin_var(name = nil, start: nil) ⇒ Variable
Declare a binary variable.
-
#encode ⇒ String
Turn this model into the message the service reads and encode it.
-
#initialize ⇒ Model
constructor
A new instance of Model.
-
#inspect ⇒ String
A one-line summary: variable and row counts.
-
#int_var(lower = nil, upper = nil, name = nil, start: nil) ⇒ Variable
Declare an integer variable.
-
#maximize(expr) ⇒ Expression
Maximize
expr. -
#minimize(expr) ⇒ Expression
Minimize
expr. -
#num_var(lower = nil, upper = nil, name = nil, start: nil) ⇒ Variable
Declare a continuous variable.
-
#to_program ⇒ Quicopt::Modeler::V1::Program
Turn this model into the structured message the service reads.
-
#to_s ⇒ Object
Renders the whole model: objective, rows, then declarations.
-
#variable(name) ⇒ Variable?
Look a variable up by name.
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
467 468 469 470 471 472 473 |
# File 'lib/quicopt/model.rb', line 467 def initialize @variables = [] @by_name = {} @constraints = [] @objective = Expression.new @sense = "min" end |
Instance Attribute Details
#constraints ⇒ Array<Constraint> (readonly)
Returns the constraint rows, in insertion order.
461 462 463 |
# File 'lib/quicopt/model.rb', line 461 def constraints @constraints end |
#objective ⇒ Expression (readonly)
Returns the objective (the zero expression until set).
463 464 465 |
# File 'lib/quicopt/model.rb', line 463 def objective @objective end |
#sense ⇒ String (readonly)
Returns "min" or "max".
465 466 467 |
# File 'lib/quicopt/model.rb', line 465 def sense @sense end |
#variables ⇒ Array<Variable> (readonly)
Returns the declared variables, in declaration order.
459 460 461 |
# File 'lib/quicopt/model.rb', line 459 def variables @variables end |
Instance Method Details
#add(constraint) ⇒ Constraint
Add a constraint row built by <=, >= or ==.
535 536 537 538 539 540 541 542 543 |
# File 'lib/quicopt/model.rb', line 535 def add(constraint) unless constraint.is_a?(Constraint) raise TypeError, "add expects a constraint such as `x + y <= 5`, got #{constraint.class}" end check_ownership(constraint.expr) @constraints << constraint constraint end |
#bin_var(name = nil, start: nil) ⇒ Variable
Declare a binary variable.
504 505 506 |
# File 'lib/quicopt/model.rb', line 504 def bin_var(name = nil, start: nil) declare(:BINARY, 0.0, 1.0, name, start) end |
#encode ⇒ String
Turn this model into the message the service reads and encode it.
555 556 557 |
# File 'lib/quicopt/model.rb', line 555 def encode Wire.encode(self) end |
#inspect ⇒ String
Returns a one-line summary: variable and row counts.
571 572 573 |
# File 'lib/quicopt/model.rb', line 571 def inspect "#<Quicopt::Model #{@variables.size} vars, #{@constraints.size} constraints>" end |
#int_var(lower = nil, upper = nil, name = nil, start: nil) ⇒ Variable
Declare an integer variable.
Bounds of exactly [0, 1] declare a :BINARY variable rather than an
integer one — the same rule the Python and Julia clients apply, so the same
model means the same thing whichever language wrote it.
494 495 496 497 498 499 |
# File 'lib/quicopt/model.rb', line 494 def int_var(lower = nil, upper = nil, name = nil, start: nil) lo = coerce_bound(lower, -Float::INFINITY) hi = coerce_bound(upper, Float::INFINITY) domain = lo.zero? && hi == 1.0 ? :BINARY : :INTEGER declare(domain, lo, hi, name, start) end |
#maximize(expr) ⇒ Expression
Maximize expr.
525 526 527 |
# File 'lib/quicopt/model.rb', line 525 def maximize(expr) set_objective(expr, "max") end |
#minimize(expr) ⇒ Expression
Minimize expr.
518 519 520 |
# File 'lib/quicopt/model.rb', line 518 def minimize(expr) set_objective(expr, "min") end |
#num_var(lower = nil, upper = nil, name = nil, start: nil) ⇒ Variable
Declare a continuous variable.
483 484 485 |
# File 'lib/quicopt/model.rb', line 483 def num_var(lower = nil, upper = nil, name = nil, start: nil) declare(:CONTINUOUS, lower, upper, name, start) end |
#to_program ⇒ Quicopt::Modeler::V1::Program
Turn this model into the structured message the service reads.
548 549 550 |
# File 'lib/quicopt/model.rb', line 548 def to_program Wire.program(self) end |
#to_s ⇒ Object
Renders the whole model: objective, rows, then declarations.
560 561 562 563 564 565 566 567 568 |
# File 'lib/quicopt/model.rb', line 560 def to_s lines = ["#{@sense} #{@objective}"] lines << "subject to" unless @constraints.empty? @constraints.each { |c| lines << " #{c}" } @variables.each do |v| lines << " #{v.name} in #{v.domain} [#{Format.number(v.lower)}, #{Format.number(v.upper)}]" end lines.join("\n") end |
#variable(name) ⇒ Variable?
Look a variable up by name.
511 512 513 |
# File 'lib/quicopt/model.rb', line 511 def variable(name) @by_name[name.to_s] end |