ruby-cbc 0.3.2 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby-cbc/ilp/objective.rb +1 -1
- data/lib/ruby-cbc/ilp/term.rb +6 -6
- data/lib/ruby-cbc/ilp/term_array.rb +12 -8
- data/lib/ruby-cbc/model.rb +14 -0
- data/lib/ruby-cbc/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03dc702beca044d1c5b6e9b2319ad40e0b0933dd
|
4
|
+
data.tar.gz: a50b241db4d74b0068005e5709461f6272b791aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a774982b0538e3f9d80f1c2743279174fea6ba83f3ec30526eda0e5b7b66da38737b82299c861f69a88037153c52511903a394760d9a772a8ef6ae2ccebdb27
|
7
|
+
data.tar.gz: 420fb9e01cd239819c0b224ec607959f2e59c59886175171424e8d46445866850a5ee1b003c6be0e3f8417a5796ec6641ab7b762b57c4fd9f16ac50bc89f39a0
|
@@ -8,7 +8,7 @@ module Ilp
|
|
8
8
|
def initialize(terms, objective_function = MAXIMIZE)
|
9
9
|
@terms = terms
|
10
10
|
@terms = Ilp::Term.new(@terms) if @terms.is_a? Ilp::Var
|
11
|
-
@terms = Ilp::TermArray.new(@terms) if @terms.is_a? Ilp::Term
|
11
|
+
@terms = Ilp::TermArray.new([@terms]) if @terms.is_a? Ilp::Term
|
12
12
|
@terms.normalize!
|
13
13
|
cste = @terms.send(:pop_constant)
|
14
14
|
puts "Removing constant [#{cste}] in objective" if cste != 0
|
data/lib/ruby-cbc/ilp/term.rb
CHANGED
@@ -9,23 +9,23 @@ module Ilp
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def +(vars)
|
12
|
-
Ilp::TermArray.new(self) + vars
|
12
|
+
Ilp::TermArray.new([self]) + vars
|
13
13
|
end
|
14
14
|
|
15
15
|
def -(vars)
|
16
|
-
Ilp::TermArray.new(self) - vars
|
16
|
+
Ilp::TermArray.new([self]) - vars
|
17
17
|
end
|
18
18
|
|
19
19
|
def ==(vars)
|
20
|
-
Ilp::TermArray.new(self) == vars
|
20
|
+
Ilp::TermArray.new([self]) == vars
|
21
21
|
end
|
22
22
|
|
23
23
|
def <=(vars)
|
24
|
-
Ilp::TermArray.new(self) <= vars
|
24
|
+
Ilp::TermArray.new([self]) <= vars
|
25
25
|
end
|
26
26
|
|
27
27
|
def >=(vars)
|
28
|
-
Ilp::TermArray.new(self) >= vars
|
28
|
+
Ilp::TermArray.new([self]) >= vars
|
29
29
|
end
|
30
30
|
|
31
31
|
|
@@ -35,7 +35,7 @@ module Ilp
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def coerce(num)
|
38
|
-
[Ilp::TermArray.new(self), num]
|
38
|
+
[Ilp::TermArray.new([self]), num]
|
39
39
|
end
|
40
40
|
|
41
41
|
def to_s
|
@@ -4,7 +4,7 @@ module Ilp
|
|
4
4
|
|
5
5
|
attr_accessor :terms
|
6
6
|
|
7
|
-
def initialize(
|
7
|
+
def initialize(terms)
|
8
8
|
@terms = terms
|
9
9
|
end
|
10
10
|
|
@@ -21,7 +21,7 @@ module Ilp
|
|
21
21
|
else
|
22
22
|
raise ArgumentError, "Argument is not allowed: #{vars} of type #{vars.class}"
|
23
23
|
end
|
24
|
-
TermArray.new(
|
24
|
+
TermArray.new(new_terms)
|
25
25
|
end
|
26
26
|
|
27
27
|
def -(vars)
|
@@ -31,20 +31,24 @@ module Ilp
|
|
31
31
|
def *(mult)
|
32
32
|
raise ArgumentError, 'Argument is not numeric' unless mult.is_a? Numeric
|
33
33
|
new_terms = terms.map { |term| term * mult }
|
34
|
-
TermArray.new(
|
34
|
+
TermArray.new(new_terms)
|
35
35
|
end
|
36
36
|
|
37
37
|
# cste + nb * var + nb * var...
|
38
38
|
def normalize!
|
39
|
-
constant = @terms.select{ |t| t.is_a? Numeric }.
|
39
|
+
constant = @terms.select{ |t| t.is_a? Numeric }.reduce(:+)
|
40
40
|
hterms = @terms.select{ |t| t.is_a? Ilp::Term }.group_by(&:var)
|
41
41
|
@terms = []
|
42
42
|
constant ||= 0
|
43
43
|
@terms << constant
|
44
|
-
hterms.
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
reduced = hterms.map do |v, ts|
|
45
|
+
if ts.count == 1
|
46
|
+
ts.first
|
47
|
+
else
|
48
|
+
ts.reduce(Ilp::Term.new(v, 0)) { |v1, v2| v1.mult += v2.mult; v1 }
|
49
|
+
end
|
50
|
+
end.reject { |term| term.mult == 0 }
|
51
|
+
@terms.concat reduced
|
48
52
|
self
|
49
53
|
end
|
50
54
|
|
data/lib/ruby-cbc/model.rb
CHANGED
@@ -4,6 +4,20 @@ module Cbc
|
|
4
4
|
|
5
5
|
INF = 1.0 / 0.0 # Useful for ranges
|
6
6
|
|
7
|
+
def self.add_all(variables)
|
8
|
+
to_add = variables.map do |variable|
|
9
|
+
case variable
|
10
|
+
when Numeric, Ilp::Term
|
11
|
+
variable
|
12
|
+
when Ilp::Var
|
13
|
+
Ilp::Term.new(variable)
|
14
|
+
else
|
15
|
+
raise 'Not a variable, a term or a numeric'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
Ilp::TermArray.new(to_add)
|
19
|
+
end
|
20
|
+
|
7
21
|
class Model
|
8
22
|
|
9
23
|
|
data/lib/ruby-cbc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-cbc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillaume Verger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -161,3 +161,4 @@ signing_key:
|
|
161
161
|
specification_version: 4
|
162
162
|
summary: Wrapper around Cbc Linear Programming Solver
|
163
163
|
test_files: []
|
164
|
+
has_rdoc:
|