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 (see order), 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.



210
211
212
213
214
215
216
217
218
219
# File 'lib/quicopt/model.rb', line 210

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

#domain ⇒ Symbol (readonly)

Returns :CONTINUOUS, :INTEGER, :BINARY, or one of the two domains that also allow 0: :SEMICONTINUOUS (0 or any value within the bounds) and :SEMIINTEGER (0 or a whole number within them).

Returns:

  • (Symbol) —

    :CONTINUOUS, :INTEGER, :BINARY, or one of the two domains that also allow 0: :SEMICONTINUOUS (0 or any value within the bounds) and :SEMIINTEGER (0 or a whole number within them)



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

def domain
  @domain
end

#index ⇒ Integer (readonly)

Returns the variable's position in its model.

Returns:

  • (Integer) —

    the variable's position in its model



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

def index
  @index
end

#lower ⇒ Float (readonly)

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

Returns:

  • (Float) —

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



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

def lower
  @lower
end

#model ⇒ Model (readonly)

Returns the model that declared this variable.

Returns:

  • (Model) —

    the model that declared this variable



194
195
196
# File 'lib/quicopt/model.rb', line 194

def model
  @model
end

#name ⇒ String (readonly)

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

Returns:

  • (String) —

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



196
197
198
# File 'lib/quicopt/model.rb', line 196

def name
  @name
end

#start ⇒ Float (readonly)

Returns the starting point handed to the solver.

Returns:

  • (Float) —

    the starting point handed to the solver



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

def start
  @start
end

#upper ⇒ Float (readonly)

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

Returns:

  • (Float) —

    the upper bound (may be Float::INFINITY)



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

def upper
  @upper
end

Instance Method Details

#active ⇒ Expression

Whether the variable is in use: 1 when its value is not 0, else 0. Made for a variable declared with semi: true, which is 0 or within its bounds: a delivery of either nothing or 10 to 25 units, say. A fee per delivery is then

fee * ship.active

and a model that is otherwise linear stays one the service solves exactly.

Returns:



236
237
238
# File 'lib/quicopt/model.rb', line 236

def active
  Conditions.term(:indicator, to_expr)
end

#eql?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


256
257
258
# File 'lib/quicopt/model.rb', line 256

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

#hash ⇒ Object

Hashes by identity, to match eql?.



261
262
263
# File 'lib/quicopt/model.rb', line 261

def hash
  object_id.hash
end

#inspect ⇒ String

Returns name, domain and bounds.

Returns:

  • (String) —

    name, domain and bounds



271
272
273
# File 'lib/quicopt/model.rb', line 271

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

#order ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Where this variable sorts among its model's terms: before every read of a permutation or lookup table, then by index.



244
245
246
# File 'lib/quicopt/model.rb', line 244

def order
  [0, @index]
end

#to_expr ⇒ Expression

Returns this variable as a degree-1 expression.

Returns:

  • (Expression) —

    this variable as a degree-1 expression



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

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

#to_s ⇒ String

Returns the variable's name.

Returns:

  • (String) —

    the variable's name



266
267
268
# File 'lib/quicopt/model.rb', line 266

def to_s
  @name
end

#variables ⇒ Array<Variable>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns this variable alone.

Returns:

  • (Array<Variable>) —

    this variable alone



250
251
252
# File 'lib/quicopt/model.rb', line 250

def variables
  [self]
end