rulp 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmU0YjdmMjllMmU1YWJlMWM1MDNiMmM1OTc5YjNlMmYxN2ViMjg3OA==
4
+ MzVkMDQ0N2U5ZTg3NGNiMGQ3N2ZlODAyNDljY2U4MDNlMDVkOGRhMg==
5
5
  data.tar.gz: !binary |-
6
- MjM3MDZmYmY4NmZjNzI1ZmYzOTk3ZjYzNTQxMDAzYTk2OGFjY2FmMg==
6
+ MGI2M2ZiYWVlZWYzMmUzZDM2NjU4NzZlNGQ2Y2M4YzU4YTU0NGE5Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NGY4YTJjOWNkOWNiNDNlMjU1ZjM0Njg0ZGE4YzFkYjhkMTYxMTg0ODMzMjQx
10
- M2NiOWYyMWI5YTc4NDZlOWQ2ZmZmYzZlYzAxMjM0OTAyMjRiOGY1ZmJlOGVh
11
- Mjc4MjRiMTQ4OGZiZWQ3NjMxOTg5NGY2NmFhZmU3YzJiZjNmZGE=
9
+ NGQxOWZlNDZkNTU2ZjA1ZTM1M2RkMTIxYmY0ZTExOWE5YjMyODRhNjVlZDhj
10
+ MGI5YWZiM2MwMTU5ZTcyNDQ2MmI1NWU2NzVmZjNhYzViNmE4MGY5MjcxZjZh
11
+ YjJjZTM3YTUzZmZiYWE5MGJiNTNlZjBjNWFiYzNjOTg5NTgzZDc=
12
12
  data.tar.gz: !binary |-
13
- YmVjYWJiNzFhZDliZmM4MWNlMDAzMjk4OGM2MGQ5ZGMzNDBiODZkNzk5MjM4
14
- Yjk3M2RmZWRiYjVhNmJlMDY5ZTUwOTAzNWEwMGM4YWIzNzQwNzIyMDhjYjIx
15
- MTZlZTYzY2M0NzY1M2YzYWI5NzZhZGViZDcyMzY0MjYwYzI2MGM=
13
+ NmYwM2Q1NmFmODk1OGI3OGNjNDRmNDU5OTRhMDRiZGE5ZDJmYzAzZTI3YTQw
14
+ MzNiMTRhZjdlOTQzZWE2NDE4ZGMwOWI0YzM5MTFjZjAyM2M0MjM4M2YyZjZl
15
+ Yzc0OWI5NGYxYjdiZTRhMzUxMmM1ODYxZGI3ZWU1NDM3ODBjOWE=
@@ -15,6 +15,7 @@ SCIP = "scip"
15
15
  CBC = "cbc"
16
16
  PSCIP = "pscip"
17
17
  PCBC = "pcbc"
18
+ GUROBI = "gurobi_cl"
18
19
 
19
20
  module Rulp
20
21
  attr_accessor :expressions
@@ -22,37 +23,47 @@ module Rulp
22
23
  MAX = "Maximize"
23
24
 
24
25
  GLPK = ::GLPK
26
+ GUROBI = ::GUROBI
25
27
  SCIP = ::SCIP
26
28
  CBC = ::CBC
27
29
  PSCIP = ::PSCIP
28
30
  PCBC = ::PCBC
29
31
 
30
32
  SOLVERS = {
31
- GLPK => Glpk,
32
- SCIP => Scip,
33
- PSCIP => PScip,
34
- PCBC => PCbc,
35
- CBC => Cbc
33
+ GLPK => Glpk,
34
+ SCIP => Scip,
35
+ PSCIP => PScip,
36
+ PCBC => PCbc,
37
+ CBC => Cbc,
38
+ GUROBI => Gurobi,
36
39
  }
37
40
 
38
- def self.Glpk(lp)
39
- lp.solve_with(GLPK) rescue nil
41
+ def self.Glpk(lp, opts={})
42
+ lp.solve_with(GLPK, opts) rescue nil
40
43
  end
41
44
 
42
- def self.Cbc(lp)
43
- lp.solve_with(CBC) rescue nil
45
+ def self.Cbc(lp, opts={})
46
+ lp.solve_with(CBC, opts)
44
47
  end
45
48
 
46
- def self.Scip(lp)
47
- lp.solve_with(SCIP) rescue nil
49
+ def self.Scip(lp, opts={})
50
+ lp.solve_with(SCIP, opts) rescue nil
48
51
  end
49
52
 
50
- def self.Pcbc(lp)
51
- lp.solve_with(PCBC) rescue nil
53
+ def self.Pcbc(lp, opts={})
54
+ lp.solve_with(PCBC, opts) rescue nil
52
55
  end
53
56
 
54
- def self.Pscip(lp)
55
- lp.solve_with(PSCIP) rescue nil
57
+ def self.Pscip(lp, opts={})
58
+ lp.solve_with(PSCIP, opts) rescue nil
59
+ end
60
+
61
+ def self.Pscip(lp, opts={})
62
+ lp.solve_with(PSCIP, opts) rescue nil
63
+ end
64
+
65
+ def self.Gurobi(lp, opts={})
66
+ lp.solve_with(GUROBI, opts) rescue nil
56
67
  end
57
68
 
58
69
  def self.Max(objective_expression)
@@ -86,12 +97,12 @@ module Rulp
86
97
  self
87
98
  end
88
99
 
89
- def solve
90
- Rulp.send(self.solver, self)
100
+ def solve(opts={})
101
+ Rulp.send(self.solver, self, opts)
91
102
  end
92
103
 
93
- def method_missing(method_name)
94
- self.call(method_name)
104
+ def method_missing(method_name, *args)
105
+ self.call(method_name, *args)
95
106
  end
96
107
 
97
108
  def solver(solver=nil)
@@ -99,8 +110,8 @@ module Rulp
99
110
  solver = solver[0].upcase + solver[1..-1].downcase
100
111
  end
101
112
 
102
- def call(using=nil)
103
- Rulp.send(self.solver(using), self)
113
+ def call(using=nil, options={})
114
+ Rulp.send(self.solver(using), self, options)
104
115
  end
105
116
 
106
117
  def constraints
@@ -139,17 +150,19 @@ module Rulp
139
150
  IO.write(filename, self)
140
151
  end
141
152
 
142
- def solve_with(type, open_definition=false, open_solution=false)
153
+ def solve_with(type, options={})
143
154
  filename = get_output_filename
144
- solver = SOLVERS[type].new(filename)
155
+ solver = SOLVERS[type].new(filename, options)
145
156
 
146
157
  "Writing problem".log(:info)
147
158
  IO.write(filename, self)
148
159
 
149
- `open #{filename}` if open_definition
160
+ `open #{filename}` if options[:open_definition]
150
161
 
151
162
  "Solving problem".log(:info)
152
- _, time = _profile{ solver.solve(open_solution) }
163
+ _, time = _profile{ solver.solve(options) }
164
+
165
+ `open #{solver.outfile}` if options[:open_solution]
153
166
 
154
167
  "Solver took #{time}".log(:debug)
155
168
 
@@ -56,7 +56,7 @@ module Rulp
56
56
  [
57
57
  self.gt,
58
58
  self.gt ? "<=" : nil,
59
- self.name,
59
+ self.to_s,
60
60
  self.lt ? "<=" : nil,
61
61
  self.lt
62
62
  ].compact.join(" ")
@@ -1,7 +1,9 @@
1
1
  class Cbc < Solver
2
- def solve(open_solution=false)
3
- system("#{executable} #{@filename} branch solution #{@outfile}")
4
- `open #{@outfile}` if open_solution
2
+ def solve(options)
3
+ command = "#{executable} #{@filename} %s branch solution #{@outfile}"
4
+ command %= options[:gap] ? "ratio #{options[:gap]}":""
5
+ system(command)
6
+ `open #{@outfile}` if options[:open_solution]
5
7
  end
6
8
 
7
9
  def self.executable
@@ -1,7 +1,8 @@
1
1
  class Glpk < Solver
2
- def solve(open_solution=false)
3
- system("#{executable} --lp #{@filename} --write #{@outfile}")
4
- `open #{@outfile}` if open_solution
2
+ def solve(options)
3
+ command = "#{executable} --lp #{@filename} %s --cuts --write #{@outfile}"
4
+ command %= options[:gap] ? "--mipgap #{options[:gap]}" : ""
5
+ system(command)
5
6
  end
6
7
 
7
8
  def self.executable
@@ -0,0 +1,25 @@
1
+ class Gurobi < Solver
2
+ def solve(options)
3
+ command = "gurobi_cl ResultFile=#{@outfile} #{@filename}"
4
+ command %= options[:gap] ? "MipGap=#{options[:gap]}":""
5
+ system(command)
6
+ end
7
+
8
+ def self.executable
9
+ :cbc
10
+ end
11
+
12
+ def store_results(variables)
13
+ rows = IO.read(@outfile).split("\n")
14
+ objective = rows[0].split(/\s+/)[-1].to_f
15
+ vars_by_name = {}
16
+ rows[1..-1].each do |row|
17
+ cols = row.strip.split(/\s+/)
18
+ vars_by_name[cols[0].to_s] = cols[1].to_f
19
+ end
20
+ variables.each do |var|
21
+ var.value = vars_by_name[var.to_s].to_f
22
+ end
23
+ return objective
24
+ end
25
+ end
@@ -1,7 +1,8 @@
1
1
  class PCbc < Solver
2
- def solve(open_solution=false)
3
- system("#{executable} #{@filename} threads 8 branch solution #{@outfile}")
4
- `open #{@outfile}` if open_solution
2
+ def solve(options)
3
+ command = "#{executable} #{@filename} %s threads 8 branch solution #{@outfile}"
4
+ command %= options[:gap] ? "ratio #{options[:gap]}":""
5
+ system(command)
5
6
  end
6
7
 
7
8
  def self.executable
@@ -4,11 +4,10 @@ class PScip < Solver
4
4
  :fscip
5
5
  end
6
6
 
7
- def solve(open_solution=false)
7
+ def solve(options)
8
8
  command = "touch /tmp/fscip_params; rm #{@outfile}; #{executable} /tmp/fscip_params #{@filename} -fsol #{@outfile}"
9
9
  command += " -s ./scip.set" if File.exists?("./scip.set")
10
10
  system(command)
11
- `open #{@outfile}` if open_solution
12
11
  end
13
12
 
14
13
  def store_results(variables)
@@ -1,12 +1,10 @@
1
1
  class Scip < Solver
2
-
3
2
  def self.executable
4
3
  :scip
5
4
  end
6
5
 
7
- def solve(open_solution=false)
6
+ def solve(options)
8
7
  system("rm #{@outfile}; #{executable} -f #{@filename} -l #{@outfile} -s ./scip.set")
9
- `open #{@outfile}` if open_solution
10
8
  end
11
9
 
12
10
  def store_results(variables)
@@ -1,5 +1,8 @@
1
1
  class Solver
2
- def initialize(filename)
2
+ attr_reader :options, :outfile
3
+
4
+ def initialize(filename, options)
5
+ @options = options
3
6
  @filename = filename
4
7
  @outfile = get_output_filename
5
8
  raise StandardError.new("Couldn't find solver #{executable}!") if `which #{executable}`.length == 0
@@ -32,3 +35,4 @@ require_relative 'scip'
32
35
  require_relative 'glpk'
33
36
  require_relative 'pscip'
34
37
  require_relative 'pcbc'
38
+ require_relative 'gurobi'
@@ -13,6 +13,7 @@ require_relative 'test_helper'
13
13
 
14
14
  class SaveToFile < Minitest::Test
15
15
  def setup
16
+ LV::clear
16
17
  given[ X_i >= 0, Y_i >= 0, Z_i >= 0 ]
17
18
  @objective = 10 * X_i + 6 * Y_i + 4 * Z_i
18
19
  @problem = Rulp::Max( @objective ) [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple Ruby LP description DSL
14
14
  email: wc@pico.net.nz
@@ -34,6 +34,7 @@ files:
34
34
  - lib/rulp/rulp_initializers.rb
35
35
  - lib/solvers/cbc.rb
36
36
  - lib/solvers/glpk.rb
37
+ - lib/solvers/gurobi.rb
37
38
  - lib/solvers/pcbc.rb
38
39
  - lib/solvers/pscip.rb
39
40
  - lib/solvers/scip.rb