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



189
190
191
192
193
194
195
196
197
198
# File 'lib/quicopt/model.rb', line 189

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



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

def domain
  @domain
end

#indexInteger (readonly)

Returns the variable's position in its model.

Returns:

  • (Integer)

    the variable's position in its model



187
188
189
# File 'lib/quicopt/model.rb', line 187

def index
  @index
end

#lowerFloat (readonly)

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

Returns:

  • (Float)

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



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

def lower
  @lower
end

#modelModel (readonly)

Returns the model that declared this variable.

Returns:

  • (Model)

    the model that declared this variable



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

def model
  @model
end

#nameString (readonly)

Returns the variable's name; solutions come back keyed by it.

Returns:

  • (String)

    the variable's name; solutions come back keyed by it



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

def name
  @name
end

#startFloat (readonly)

Returns the starting point handed to the solver.

Returns:

  • (Float)

    the starting point handed to the solver



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

def start
  @start
end

#upperFloat (readonly)

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

Returns:

  • (Float)

    the upper bound (may be Float::INFINITY)



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

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)


207
208
209
# File 'lib/quicopt/model.rb', line 207

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

#hashObject

Hashes by identity, to match eql?.



212
213
214
# File 'lib/quicopt/model.rb', line 212

def hash
  object_id.hash
end

#inspectString

Returns name, domain and bounds.

Returns:

  • (String)

    name, domain and bounds



222
223
224
# File 'lib/quicopt/model.rb', line 222

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



201
202
203
# File 'lib/quicopt/model.rb', line 201

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

#to_sString

Returns the variable's name.

Returns:

  • (String)

    the variable's name



217
218
219
# File 'lib/quicopt/model.rb', line 217

def to_s
  @name
end