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. 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.

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)


414
415
416
417
418
419
420
# File 'lib/quicopt/model.rb', line 414

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:



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

def expr
  @expr
end

#senseSymbol (readonly)

Returns :le, :ge or :eq.

Returns:

  • (Symbol)

    :le, :ge or :eq



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

def sense
  @sense
end

Class Method Details

.build(lhs, rhs, sense) ⇒ Object

Build a row from the two sides of a comparison.



410
411
412
# File 'lib/quicopt/model.rb', line 410

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:



425
426
427
# File 'lib/quicopt/model.rb', line 425

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

#boundFloat

The right-hand side after moving the constant across.

Returns:

  • (Float)


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

def bound
  -@expr.constant
end

#inspectString

Returns the row, tagged with the class.

Returns:

  • (String)

    the row, tagged with the class



442
443
444
# File 'lib/quicopt/model.rb', line 442

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

#to_sObject

Renders the row as body <op> bound.



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

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