rulp 0.0.22 → 0.0.23
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/helpers/log.rb +18 -65
- data/lib/rulp/rulp.rb +19 -13
- data/test/test_helper.rb +4 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6efb7eb7ffab874e21b74ad4c7e7c8288bd9a1e7
|
4
|
+
data.tar.gz: 31f181cdbb431e6b9e22e86fd555a83f73d773e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d626aeecb10b6223689f447ce7489a607fe1cc832dba72879442c3b7d84d3e5f9844587c708eefcec61f7fe775ba65180b93890a19ea19414596f56a1b7f0458
|
7
|
+
data.tar.gz: d47b81d18089719e3fefc9848cf8806a8d4e11dd79c3dc5f9cb9f15a3b233d153240f6dbc6851777c98cf9c694395aa7ce11001da1cd16462e9ed26e5c584a4e
|
data/lib/helpers/log.rb
CHANGED
@@ -1,83 +1,36 @@
|
|
1
|
-
|
2
|
-
# Basic logger module.
|
3
|
-
# Allows logging in the format
|
4
|
-
#
|
5
|
-
# "This is a string".log(:debug)
|
6
|
-
# "Oh no!".log(:error)
|
7
|
-
#
|
8
|
-
# Log level is set as follows.
|
9
|
-
# Rulp::Logger::level = :debug
|
10
|
-
#
|
11
|
-
##
|
12
|
-
module Rulp
|
13
|
-
module Logger
|
14
|
-
DEBUG = 5
|
15
|
-
INFO = 4
|
16
|
-
WARN = 3
|
17
|
-
ERROR = 2
|
18
|
-
OFF = 1
|
19
|
-
|
20
|
-
LEVELS = {
|
21
|
-
debug: DEBUG,
|
22
|
-
info: INFO,
|
23
|
-
warn: WARN,
|
24
|
-
error: ERROR,
|
25
|
-
off: OFF
|
26
|
-
}
|
1
|
+
require 'logger'
|
27
2
|
|
28
|
-
|
29
|
-
|
30
|
-
@@level = value
|
31
|
-
end
|
3
|
+
module Rulp
|
4
|
+
module Log
|
32
5
|
|
33
|
-
def
|
34
|
-
@@
|
6
|
+
def print_solver_outputs=(print)
|
7
|
+
@@print_solver_outputs = print
|
35
8
|
end
|
36
9
|
|
37
|
-
def
|
38
|
-
@@
|
10
|
+
def print_solver_outputs
|
11
|
+
@@print_solver_outputs
|
39
12
|
end
|
40
13
|
|
41
|
-
def
|
42
|
-
@@
|
14
|
+
def log_level=(level)
|
15
|
+
@@log_level = level
|
43
16
|
end
|
44
17
|
|
45
|
-
def
|
46
|
-
|
47
|
-
puts("[#{colorize(level)}] #{message}")
|
48
|
-
end
|
18
|
+
def log_level
|
19
|
+
@@log_level || Logger::DEBUG
|
49
20
|
end
|
50
21
|
|
51
|
-
def
|
52
|
-
if
|
53
|
-
|
54
|
-
when :debug
|
55
|
-
Pry::Helpers::Text.cyan(level)
|
56
|
-
when :info
|
57
|
-
Pry::Helpers::Text.green(level)
|
58
|
-
when :warn
|
59
|
-
Pry::Helpers::Text.magenta(level)
|
60
|
-
when :error
|
61
|
-
Pry::Helpers::Text.red(level)
|
62
|
-
end
|
63
|
-
else
|
64
|
-
level
|
22
|
+
def log(level, message)
|
23
|
+
if level >= self.log_level
|
24
|
+
self.logger.add(level, message)
|
65
25
|
end
|
66
26
|
end
|
67
27
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
class ::String
|
72
|
-
def log(level)
|
73
|
-
Logger::log(level, self)
|
74
|
-
end
|
28
|
+
def logger=(logger)
|
29
|
+
@@logger = logger
|
75
30
|
end
|
76
31
|
|
77
|
-
|
78
|
-
|
79
|
-
Logger::log(level, self.join("#{sep}[#{level}] "))
|
80
|
-
end
|
32
|
+
def logger
|
33
|
+
@@logger = Logger.new(STDOUT)
|
81
34
|
end
|
82
35
|
end
|
83
36
|
end
|
data/lib/rulp/rulp.rb
CHANGED
@@ -10,6 +10,7 @@ require_relative "../helpers/log"
|
|
10
10
|
|
11
11
|
require 'set'
|
12
12
|
require 'open3'
|
13
|
+
require 'logger'
|
13
14
|
|
14
15
|
GLPK = "glpsol"
|
15
16
|
SCIP = "scip"
|
@@ -18,6 +19,9 @@ GUROBI = "gurobi_cl"
|
|
18
19
|
|
19
20
|
module Rulp
|
20
21
|
attr_accessor :expressions
|
22
|
+
extend Rulp::Log
|
23
|
+
self.print_solver_outputs = true
|
24
|
+
|
21
25
|
MIN = "Minimize"
|
22
26
|
MAX = "Maximize"
|
23
27
|
|
@@ -33,6 +37,7 @@ module Rulp
|
|
33
37
|
GUROBI => Gurobi,
|
34
38
|
}
|
35
39
|
|
40
|
+
|
36
41
|
def self.Glpk(lp, opts={})
|
37
42
|
lp.solve_with(GLPK, opts)
|
38
43
|
end
|
@@ -50,12 +55,12 @@ module Rulp
|
|
50
55
|
end
|
51
56
|
|
52
57
|
def self.Max(objective_expression)
|
53
|
-
"Creating maximization problem"
|
58
|
+
Rulp.log(Logger::INFO, "Creating maximization problem")
|
54
59
|
Problem.new(Rulp::MAX, objective_expression)
|
55
60
|
end
|
56
61
|
|
57
62
|
def self.Min(objective_expression)
|
58
|
-
"Creating minimization problem"
|
63
|
+
Rulp.log(Logger::INFO, "Creating minimization problem")
|
59
64
|
Problem.new(Rulp::MIN, objective_expression)
|
60
65
|
end
|
61
66
|
|
@@ -68,9 +73,9 @@ module Rulp
|
|
68
73
|
|
69
74
|
def self.exec(command)
|
70
75
|
result = ""
|
71
|
-
Open3.popen2e("#{command} #{Rulp
|
76
|
+
Open3.popen2e("#{command} #{Rulp.print_solver_outputs ? "" : "> /dev/null 2>&1"}") do | inp, out, thr|
|
72
77
|
out.each do |line|
|
73
|
-
|
78
|
+
Rulp.log(Logger::DEBUG, line)
|
74
79
|
result << line
|
75
80
|
end
|
76
81
|
end
|
@@ -90,13 +95,13 @@ module Rulp
|
|
90
95
|
end
|
91
96
|
|
92
97
|
def [](*constraints)
|
93
|
-
"Got constraints"
|
98
|
+
Rulp.log(Logger::INFO, "Got constraints")
|
94
99
|
constraints.flatten!
|
95
|
-
"Flattened constraints"
|
100
|
+
Rulp.log(Logger::INFO, "Flattened constraints")
|
96
101
|
@constraints.concat(constraints)
|
97
|
-
"Joint constraints"
|
102
|
+
Rulp.log(Logger::INFO, "Joint constraints")
|
98
103
|
@variables.merge(constraints.flat_map(&:variables).uniq)
|
99
|
-
"Extracted variables"
|
104
|
+
Rulp.log(Logger::INFO, "Extracted variables")
|
100
105
|
self
|
101
106
|
end
|
102
107
|
|
@@ -152,22 +157,23 @@ module Rulp
|
|
152
157
|
end
|
153
158
|
|
154
159
|
def solve_with(type, options={})
|
160
|
+
|
155
161
|
filename = get_output_filename
|
156
162
|
solver = SOLVERS[type].new(filename, options)
|
157
163
|
|
158
|
-
"Writing problem"
|
164
|
+
Rulp.log(Logger::INFO, "Writing problem")
|
159
165
|
IO.write(filename, self)
|
160
166
|
|
161
167
|
Rulp.exec("open #{filename}") if options[:open_definition]
|
162
168
|
|
163
|
-
"Solving problem"
|
169
|
+
Rulp.log(Logger::INFO, "Solving problem")
|
164
170
|
self.trace, time = _profile{ solver.solve }
|
165
171
|
|
166
172
|
Rulp.exec("open #{solver.outfile}") if options[:open_solution]
|
167
173
|
|
168
|
-
"Solver took #{time}"
|
174
|
+
Rulp.log(Logger::DEBUG, "Solver took #{time}")
|
169
175
|
|
170
|
-
"Parsing result"
|
176
|
+
Rulp.log(Logger::INFO, "Parsing result")
|
171
177
|
solver.store_results(@variables)
|
172
178
|
|
173
179
|
solver.remove_lp_file if options[:remove_lp_file]
|
@@ -175,7 +181,7 @@ module Rulp
|
|
175
181
|
|
176
182
|
self.result = @objective_expression.evaluate
|
177
183
|
|
178
|
-
"Objective: #{result}\n#{@variables.map{|v|[v.name, "=", v.value].join(' ') if v.value}.compact.join("\n")}"
|
184
|
+
Rulp.log(Logger::DEBUG, "Objective: #{result}\n#{@variables.map{|v|[v.name, "=", v.value].join(' ') if v.value}.compact.join("\n")}")
|
179
185
|
return self.result
|
180
186
|
end
|
181
187
|
|
data/test/test_helper.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require_relative "../lib/rulp"
|
2
|
+
require 'logger'
|
2
3
|
|
3
4
|
gem "minitest"
|
4
5
|
require "minitest/autorun"
|
5
6
|
|
6
|
-
Rulp::
|
7
|
-
Rulp::
|
7
|
+
Rulp::log_level = Logger::UNKNOWN
|
8
|
+
Rulp::print_solver_outputs = false
|
8
9
|
|
9
10
|
def each_solver
|
10
11
|
[:scip, :cbc, :glpk, :gurobi].each do |solver|
|
@@ -12,7 +13,7 @@ def each_solver
|
|
12
13
|
if Rulp::solver_exists?(solver)
|
13
14
|
yield(solver)
|
14
15
|
else
|
15
|
-
"Couldn't find solver #{solver}"
|
16
|
+
Rulp::log(Logger::INFO, "Couldn't find solver #{solver}")
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
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.
|
4
|
+
version: 0.0.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wouter Coppieters
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple Ruby LP description DSL
|
14
14
|
email: wc@pico.net.nz
|