Class: ORTools::SatLinearExpr

Inherits:
Object
  • Object
show all
Includes:
ComparisonOperators
Defined in:
lib/or_tools/sat_linear_expr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = []) ⇒ SatLinearExpr

Returns a new instance of SatLinearExpr.



7
8
9
# File 'lib/or_tools/sat_linear_expr.rb', line 7

def initialize(vars = [])
  @vars = vars
end

Instance Attribute Details

#varsObject (readonly)

Returns the value of attribute vars.



5
6
7
# File 'lib/or_tools/sat_linear_expr.rb', line 5

def vars
  @vars
end

Instance Method Details

#*(other) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/or_tools/sat_linear_expr.rb', line 19

def *(other)
  if vars.size == 1
    self.class.new([[vars[0][0], vars[0][1] * other]])
  else
    raise ArgumentError, "Multiplication not allowed here"
  end
end

#+(other) ⇒ Object



11
12
13
# File 'lib/or_tools/sat_linear_expr.rb', line 11

def +(other)
  add(other, 1)
end

#-(other) ⇒ Object



15
16
17
# File 'lib/or_tools/sat_linear_expr.rb', line 15

def -(other)
  add(other, -1)
end

#inspectObject



39
40
41
# File 'lib/or_tools/sat_linear_expr.rb', line 39

def inspect
  "#<#{self.class.name} #{to_s}>"
end

#to_sObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/or_tools/sat_linear_expr.rb', line 27

def to_s
  vars.map do |v|
    k = v[0]
    k = k.respond_to?(:name) ? k.name : k.to_s
    if v[1] == 1
      k
    else
      "#{k} * #{v[1]}"
    end
  end.join(" + ").sub(" + -", " - ")
end