Class: Quicopt::Constraint

Inherits:
Object
  • Object
show all
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. The lowering in Quicopt::Wire splits that into the body f (the variable terms) and the numeric bound (the constant, moved to the other side), which is the shape the wire's function-in-set rows take.

Constant Summary collapse

SENSES =

The comparison operator each sense renders as.

{ le: "<=", ge: ">=", eq: "==" }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr, sense) ⇒ Constraint

Returns a new instance of Constraint.

Raises:

  • (ArgumentError)


410
411
412
413
414
415
416
# File 'lib/quicopt/model.rb', line 410

def initialize(expr, sense)
  raise ArgumentError, "unknown constraint sense #{sense.inspect}" unless SENSES.key?(sense)

  @expr = expr
  @sense = sense
  freeze
end

Instance Attribute Details

#exprExpression (readonly)

Returns the residual lhs - rhs.

Returns:



401
402
403
# File 'lib/quicopt/model.rb', line 401

def expr
  @expr
end

#senseSymbol (readonly)

Returns :le, :ge or :eq.

Returns:

  • (Symbol)

    :le, :ge or :eq



403
404
405
# File 'lib/quicopt/model.rb', line 403

def sense
  @sense
end

Class Method Details

.build(lhs, rhs, sense) ⇒ Object

Build a row from the two sides of a comparison.



406
407
408
# File 'lib/quicopt/model.rb', line 406

def self.build(lhs, rhs, sense)
  new(Expression.cast(lhs).plus(Expression.cast(rhs).scale(-1.0)), sense)
end

Instance Method Details

#bodyExpression

The row body: the variable terms alone, with the constant moved to bound.

Returns:



421
422
423
# File 'lib/quicopt/model.rb', line 421

def body
  Expression.new(linear: @expr.linear, quadratic: @expr.quadratic)
end

#boundFloat

The right-hand side after moving the constant across.

Returns:

  • (Float)


428
429
430
# File 'lib/quicopt/model.rb', line 428

def bound
  -@expr.constant
end

#inspectString

Returns the row, tagged with the class.

Returns:

  • (String)

    the row, tagged with the class



438
439
440
# File 'lib/quicopt/model.rb', line 438

def inspect
  "#<Quicopt::Constraint #{self}>"
end

#to_sObject

Renders the row as body <op> bound.



433
434
435
# File 'lib/quicopt/model.rb', line 433

def to_s
  "#{body} #{SENSES[@sense]} #{Format.number(bound)}"
end