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.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr, sense) ⇒ Constraint

Returns a new instance of Constraint.

Raises:

  • (ArgumentError)


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.

Returns:



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.

Returns:

  • (Symbol) —

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

Returns:



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.

Returns:

  • (Float)


502
503
504
# File 'lib/quicopt/model.rb', line 502

def bound
  -@expr.constant
end

#inspect ⇒ String

Returns the row, tagged with the class.

Returns:

  • (String) —

    the row, tagged with the class



512
513
514
# File 'lib/quicopt/model.rb', line 512

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

#to_s ⇒ Object

Renders the row as body <op> bound.



507
508
509
# File 'lib/quicopt/model.rb', line 507

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