Class: Quicopt::Constraint
- Inherits:
-
Object
- Object
- Quicopt::Constraint
- Defined in:
- lib/quicopt/model.rb
Overview
One constraint row: a residual expression compared against zero.
expr is lhs - rhs, so the row is expr <= 0, expr >= 0 or expr == 0.
Quicopt::Wire then splits that into the body (the variable terms) and the
numeric bound (the constant, moved to the other side), which is the form
Quicopt sends.
A comparison built with <, > or != is not a row — Model#add refuses it
— but it can be turned into a 0/1 expression with Quicopt.holds.
Constant Summary collapse
- SENSES =
The comparison operator each sense renders as.
{ le: "<=", ge: ">=", eq: "==", lt: "<", gt: ">", ne: "!=" }.freeze
- ROWS =
The senses a constraint row can have.
%i[le ge eq].freeze
Instance Attribute Summary collapse
-
#expr ⇒ Expression
readonly
The residual
lhs - rhs. -
#sense ⇒ Symbol
readonly
:le,:geor:eqfor a row;:lt,:gtor:nefor a comparison onlyQuicopt.holdstakes.
Class Method Summary collapse
-
.build(lhs, rhs, sense) ⇒ Object
Build a row from the two sides of a comparison.
Instance Method Summary collapse
-
#body ⇒ Expression
The row body: the variable terms alone, with the constant moved to
bound. -
#bound ⇒ Float
The right-hand side after moving the constant across.
-
#initialize(expr, sense) ⇒ Constraint
constructor
A new instance of Constraint.
-
#inspect ⇒ String
The row, tagged with the class.
-
#to_s ⇒ Object
Renders the row as
body <op> bound.
Constructor Details
#initialize(expr, sense) ⇒ Constraint
Returns a new instance of Constraint.
484 485 486 487 488 489 490 |
# File 'lib/quicopt/model.rb', line 484 def initialize(expr, sense) raise ArgumentError, "unknown constraint sense #{sense.inspect}" unless SENSES.key?(sense) @expr = expr @sense = sense freeze end |
Instance Attribute Details
#expr ⇒ Expression (readonly)
Returns the residual lhs - rhs.
474 475 476 |
# File 'lib/quicopt/model.rb', line 474 def expr @expr end |
#sense ⇒ Symbol (readonly)
Returns :le, :ge or :eq for a row; :lt, :gt or :ne for
a comparison only Quicopt.holds takes.
477 478 479 |
# File 'lib/quicopt/model.rb', line 477 def sense @sense end |
Class Method Details
.build(lhs, rhs, sense) ⇒ Object
Build a row from the two sides of a comparison.
480 481 482 |
# File 'lib/quicopt/model.rb', line 480 def self.build(lhs, rhs, sense) new(Expression.cast(lhs).plus(Expression.cast(rhs).scale(-1.0)), sense) end |
Instance Method Details
#body ⇒ Expression
The row body: the variable terms alone, with the constant moved to bound.
495 496 497 |
# File 'lib/quicopt/model.rb', line 495 def body Expression.new(linear: @expr.linear, quadratic: @expr.quadratic) end |
#bound ⇒ Float
The right-hand side after moving the constant across.
502 503 504 |
# File 'lib/quicopt/model.rb', line 502 def bound -@expr.constant end |
#inspect ⇒ String
Returns the row, tagged with the class.
512 513 514 |
# File 'lib/quicopt/model.rb', line 512 def inspect "#<Quicopt::Constraint #{self}>" end |