Class: Quicopt::Variable

Inherits:
Object
  • Object
show all
Includes:
Operators
Defined in:
lib/quicopt/model.rb

Overview

A decision variable, owned by the Model that created it.

index is the variable's position in its model; it also fixes the canonical order of a quadratic term's two factors, so x * y and y * x land on the same coefficient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Operators

#!=, #*, #**, #+, #+@, #-, #-@, #/, #<=, #==, #>=, #coerce

Constructor Details

#initialize(model, name, domain, lower, upper, start, index) ⇒ Variable

Returns a new instance of Variable.



186
187
188
189
190
191
192
193
194
195
# File 'lib/quicopt/model.rb', line 186

def initialize(model, name, domain, lower, upper, start, index)
  @model = model
  @name = name
  @domain = domain
  @lower = lower
  @upper = upper
  @start = start
  @index = index
  freeze
end

Instance Attribute Details

#domainSymbol (readonly)

Returns :CONTINUOUS, :INTEGER or :BINARY.

Returns:

  • (Symbol)

    :CONTINUOUS, :INTEGER or :BINARY



176
177
178
# File 'lib/quicopt/model.rb', line 176

def domain
  @domain
end

#indexInteger (readonly)

Returns the variable's position in its model.

Returns:

  • (Integer)

    the variable's position in its model



184
185
186
# File 'lib/quicopt/model.rb', line 184

def index
  @index
end

#lowerFloat (readonly)

Returns the lower bound (may be -Float::INFINITY).

Returns:

  • (Float)

    the lower bound (may be -Float::INFINITY)



178
179
180
# File 'lib/quicopt/model.rb', line 178

def lower
  @lower
end

#modelModel (readonly)

Returns the model that declared this variable.

Returns:

  • (Model)

    the model that declared this variable



172
173
174
# File 'lib/quicopt/model.rb', line 172

def model
  @model
end

#nameString (readonly)

Returns the wire name; solutions come back keyed by it.

Returns:

  • (String)

    the wire name; solutions come back keyed by it



174
175
176
# File 'lib/quicopt/model.rb', line 174

def name
  @name
end

#startFloat (readonly)

Returns the starting point handed to the solver.

Returns:

  • (Float)

    the starting point handed to the solver



182
183
184
# File 'lib/quicopt/model.rb', line 182

def start
  @start
end

#upperFloat (readonly)

Returns the upper bound (may be Float::INFINITY).

Returns:

  • (Float)

    the upper bound (may be Float::INFINITY)



180
181
182
# File 'lib/quicopt/model.rb', line 180

def upper
  @upper
end

Instance Method Details

#eql?(other) ⇒ Boolean

Identity equality, kept separate from == (which builds a constraint) so variables stay usable as Hash and Set keys.

Returns:

  • (Boolean)


204
205
206
# File 'lib/quicopt/model.rb', line 204

def eql?(other)
  equal?(other)
end

#hashObject

Hashes by identity, to match eql?.



209
210
211
# File 'lib/quicopt/model.rb', line 209

def hash
  object_id.hash
end

#inspectString

Returns name, domain and bounds.

Returns:

  • (String)

    name, domain and bounds



219
220
221
# File 'lib/quicopt/model.rb', line 219

def inspect
  "#<Quicopt::Variable #{@name} #{@domain} [#{@lower}, #{@upper}]>"
end

#to_exprExpression

Returns this variable as a degree-1 expression.

Returns:

  • (Expression)

    this variable as a degree-1 expression



198
199
200
# File 'lib/quicopt/model.rb', line 198

def to_expr
  Expression.new(linear: { self => 1.0 })
end

#to_sString

Returns the variable's wire name.

Returns:

  • (String)

    the variable's wire name



214
215
216
# File 'lib/quicopt/model.rb', line 214

def to_s
  @name
end