Module: ORTools::Utils

Defined in:
lib/or_tools/utils.rb

Class Method Summary collapse

Class Method Details

.index_constraint(constraint) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/or_tools/utils.rb', line 3

def self.index_constraint(constraint)
  raise ArgumentError, "Expected Comparison" unless constraint.is_a?(Comparison)

  left = index_expression(constraint.left, check_linear: true)
  right = index_expression(constraint.right, check_linear: true)

  const = right.delete(nil).to_f - left.delete(nil).to_f
  right.each do |k, v|
    left[k] -= v
  end

  [left, constraint.op, const]
end

.index_expression(expression, check_linear: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/or_tools/utils.rb', line 17

def self.index_expression(expression, check_linear: true)
  vars = Hash.new(0)
  case expression
  when Numeric
    vars[nil] += expression
  when Constant
    vars[nil] += expression.value
  when Variable
    vars[expression] += 1
  when Product
    if check_linear && expression.left.vars.any? && expression.right.vars.any?
      raise ArgumentError, "Nonlinear"
    end
    vars = index_product(expression.left, expression.right)
  when Expression
    expression.parts.each do |part|
      index_expression(part, check_linear: check_linear).each do |k, v|
        vars[k] += v
      end
    end
  else
    raise TypeError, "Unsupported type"
  end
  vars
end

.index_product(left, right) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/or_tools/utils.rb', line 43

def self.index_product(left, right)
  # normalize
  types = [Constant, Variable, Product, Expression]
  if types.index { |t| left.is_a?(t) } > types.index { |t| right.is_a?(t) }
    left, right = right, left
  end

  vars = Hash.new(0)
  case left
  when Constant
    vars = index_expression(right)
    vars.transform_values! { |v| v * left.value }
  when Variable
    case right
    when Variable
      vars[quad_key(left, right)] = 1
    when Product
      index_expression(right).each do |k, v|
        case k
        when Array
          raise Error, "Non-quadratic"
        when Variable
          vars[quad_key(left, k)] = v
        else # nil
          raise "Bug?"
        end
      end
    else
      right.parts.each do |part|
        index_product(left, part).each do |k, v|
          vars[k] += v
        end
      end
    end
  when Product
    index_expression(left).each do |lk, lv|
      index_expression(right).each do |rk, rv|
        if lk.is_a?(Variable) && rk.is_a?(Variable)
          vars[quad_key(lk, rk)] = lv * rv
        else
          raise "todo"
        end
      end
    end
  else # Expression
    left.parts.each do |lp|
      right.parts.each do |rp|
        index_product(lp, rp).each do |k, v|
          vars[k] += v
        end
      end
    end
  end
  vars
end

.quad_key(left, right) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/or_tools/utils.rb', line 99

def self.quad_key(left, right)
  if left.object_id <= right.object_id
    [left, right]
  else
    [right, left]
  end
end