or-tools 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,85 +0,0 @@
1
- module ORTools
2
- class LinearExpr
3
- def solution_value
4
- coeffs.sum { |var, coeff| var.solution_value * coeff }
5
- end
6
-
7
- def coeffs
8
- coeffs = Hash.new(0.0)
9
- stack = [[1.0, self]]
10
- while stack.any?
11
- current_multiplier, current_expression = stack.pop
12
-
13
- # skip empty LinearExpr for backwards compatibility
14
- next if current_expression.instance_of?(LinearExpr)
15
-
16
- current_expression.add_self_to_coeff_map_or_stack(coeffs, current_multiplier, stack)
17
- end
18
- coeffs
19
- end
20
-
21
- def +(expr)
22
- SumArray.new([self, expr])
23
- end
24
-
25
- def -(expr)
26
- SumArray.new([self, -expr])
27
- end
28
-
29
- def *(other)
30
- if is_a?(Constant)
31
- ProductCst.new(other, @val)
32
- else
33
- ProductCst.new(self, other)
34
- end
35
- end
36
-
37
- def /(cst)
38
- ProductCst.new(self, 1.0 / other)
39
- end
40
-
41
- def -@
42
- ProductCst.new(self, -1)
43
- end
44
-
45
- def ==(arg)
46
- if arg.is_a?(Numeric)
47
- LinearConstraint.new(self, arg, arg)
48
- else
49
- LinearConstraint.new(self - arg, 0.0, 0.0)
50
- end
51
- end
52
-
53
- def >=(arg)
54
- if arg.is_a?(Numeric)
55
- LinearConstraint.new(self, arg, Float::INFINITY)
56
- else
57
- LinearConstraint.new(self - arg, 0.0, Float::INFINITY)
58
- end
59
- end
60
-
61
- def <=(arg)
62
- if arg.is_a?(Numeric)
63
- LinearConstraint.new(self, -Float::INFINITY, arg)
64
- else
65
- LinearConstraint.new(self - arg, -Float::INFINITY, 0.0)
66
- end
67
- end
68
-
69
- def to_s
70
- "(empty)"
71
- end
72
-
73
- def inspect
74
- "#<#{self.class.name} #{to_s}>"
75
- end
76
-
77
- def coerce(other)
78
- if other.is_a?(Numeric)
79
- [Constant.new(other), self]
80
- else
81
- raise TypeError, "#{self.class} can't be coerced into #{other.class}"
82
- end
83
- end
84
- end
85
- end
@@ -1,11 +0,0 @@
1
- module ORTools
2
- class MPVariable < LinearExpr
3
- def add_self_to_coeff_map_or_stack(coeffs, multiplier, stack)
4
- coeffs[self] += multiplier
5
- end
6
-
7
- def to_s
8
- name
9
- end
10
- end
11
- end
@@ -1,35 +0,0 @@
1
- module ORTools
2
- class ProductCst < LinearExpr
3
- attr_reader :expr, :coef
4
-
5
- def initialize(expr, coef)
6
- @expr = cast_to_lin_exp(expr)
7
- # TODO improve message
8
- raise TypeError, "expected numeric" unless coef.is_a?(Numeric)
9
- @coef = coef
10
- end
11
-
12
- def to_s
13
- if @coef == -1
14
- "-#{@expr}"
15
- else
16
- expr = @expr.to_s
17
- if expr.include?("+") || expr.include?("-")
18
- expr = "(#{expr})"
19
- end
20
- "#{@coef} * #{expr}"
21
- end
22
- end
23
-
24
- def add_self_to_coeff_map_or_stack(coeffs, multiplier, stack)
25
- current_multiplier = multiplier * @coef
26
- if current_multiplier
27
- stack << [current_multiplier, @expr]
28
- end
29
- end
30
-
31
- def cast_to_lin_exp(v)
32
- v.is_a?(Numeric) ? Constant.new(v) : v
33
- end
34
- end
35
- end
@@ -1,29 +0,0 @@
1
- module ORTools
2
- class SatIntVar
3
- include ComparisonOperators
4
-
5
- def *(other)
6
- SatLinearExpr.new([[self, other]])
7
- end
8
-
9
- def +(other)
10
- SatLinearExpr.new([[self, 1], [other, 1]])
11
- end
12
-
13
- def -(other)
14
- SatLinearExpr.new([[self, 1], [-other, 1]])
15
- end
16
-
17
- def -@
18
- SatLinearExpr.new([[self, -1]])
19
- end
20
-
21
- def to_s
22
- name
23
- end
24
-
25
- def inspect
26
- "#<#{self.class.name} #{to_s}>"
27
- end
28
- end
29
- end
@@ -1,59 +0,0 @@
1
- module ORTools
2
- class SatLinearExpr
3
- include ComparisonOperators
4
-
5
- attr_reader :vars
6
-
7
- def initialize(vars = [])
8
- @vars = vars
9
- end
10
-
11
- def +(other)
12
- add(other, 1)
13
- end
14
-
15
- def -(other)
16
- add(other, -1)
17
- end
18
-
19
- def *(other)
20
- if vars.size == 1
21
- self.class.new([[vars[0][0], vars[0][1] * other]])
22
- else
23
- raise ArgumentError, "Multiplication not allowed here"
24
- end
25
- end
26
-
27
- def to_s
28
- vars.map do |v|
29
- k = v[0]
30
- k = k.respond_to?(:name) ? k.name : k.to_s
31
- if v[1] == 1
32
- k
33
- else
34
- "#{k} * #{v[1]}"
35
- end
36
- end.join(" + ").sub(" + -", " - ")
37
- end
38
-
39
- def inspect
40
- "#<#{self.class.name} #{to_s}>"
41
- end
42
-
43
- private
44
-
45
- def add(other, sign)
46
- other_vars =
47
- case other
48
- when SatLinearExpr
49
- other.vars
50
- when BoolVar, SatIntVar, Integer
51
- [[other, 1]]
52
- else
53
- raise ArgumentError, "Unsupported type: #{other.class.name}"
54
- end
55
-
56
- self.class.new(vars + other_vars.map { |a, b| [a, sign * b] })
57
- end
58
- end
59
- end
@@ -1,23 +0,0 @@
1
- module ORTools
2
- class SumArray < LinearExpr
3
- attr_reader :array
4
-
5
- def initialize(array)
6
- @array = array.map { |v| cast_to_lin_exp(v) }
7
- end
8
-
9
- def add_self_to_coeff_map_or_stack(coeffs, multiplier, stack)
10
- @array.reverse_each do |arg|
11
- stack << [multiplier, arg]
12
- end
13
- end
14
-
15
- def cast_to_lin_exp(v)
16
- v.is_a?(Numeric) ? Constant.new(v) : v
17
- end
18
-
19
- def to_s
20
- "#{@array.map(&:to_s).reject { |v| v == "0" }.join(" + ")}".gsub(" + -", " - ")
21
- end
22
- end
23
- end