Class: MIPPeR::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/mipper/variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lb, ub, coeff, type, name = nil) ⇒ Variable

Returns a new instance of Variable.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mipper/variable.rb', line 6

def initialize(lb, ub, coeff, type, name = nil)
  @lower_bound = lb
  @upper_bound = ub
  @coefficient = coeff
  @type = type
  @name = name
  @constraints = []

  # These will be populated when this is added to a model
  @model = nil
  @index = nil
end

Instance Attribute Details

#coefficientObject (readonly)

Returns the value of attribute coefficient.



4
5
6
# File 'lib/mipper/variable.rb', line 4

def coefficient
  @coefficient
end

#constraintsObject

Returns the value of attribute constraints.



3
4
5
# File 'lib/mipper/variable.rb', line 3

def constraints
  @constraints
end

#indexObject

Returns the value of attribute index.



3
4
5
# File 'lib/mipper/variable.rb', line 3

def index
  @index
end

#lower_boundObject

Returns the value of attribute lower_bound.



4
5
6
# File 'lib/mipper/variable.rb', line 4

def lower_bound
  @lower_bound
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/mipper/variable.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mipper/variable.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/mipper/variable.rb', line 4

def type
  @type
end

#upper_boundObject

Returns the value of attribute upper_bound.



4
5
6
# File 'lib/mipper/variable.rb', line 4

def upper_bound
  @upper_bound
end

Instance Method Details

#*(coeff) ⇒ Object

Create a LinExpr consisting of a single term which is this variable multiplied by a constant



50
51
52
53
54
# File 'lib/mipper/variable.rb', line 50

def *(coeff)
  fail TypeError unless coeff.is_a? Numeric

  LinExpr.new({ self => coeff })
end

#+(other) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/mipper/variable.rb', line 56

def +(other)
  case other
  when LinExpr
    other + self * 1.0
  when Variable
    LinExpr.new({self => 1.0, other => 1.0})
  else
    fail TypeError
  end
end

#inspectObject

Produce the name of the variable and the value if the model is solved



68
69
70
71
72
73
74
75
76
# File 'lib/mipper/variable.rb', line 68

def inspect
  if @model && @model.status == :optimized
    value = self.value
  else
    value = '?'
  end

  "#{@name} = #{value}"
end

#valueObject

Get the final value of this variable



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mipper/variable.rb', line 32

def value
  # Model must be solved to have a value
  return nil unless @model && @model.status == :optimized

  value = @model.variable_value self

  case @type
  when :integer
    value.round
  when :binary
    [false, true][value.round]
  else
    value
  end
end