Class: Rulp::Problem
Instance Method Summary
collapse
Constructor Details
#initialize(objective, objective_expression) ⇒ Problem
Returns a new instance of Problem.
61
62
63
64
65
66
67
|
# File 'lib/rulp/rulp.rb', line 61
def initialize(objective, objective_expression)
@objective = objective
@variables = Set.new
@objective_expression = objective_expression.kind_of?(LV) ? 1 * objective_expression : objective_expression
@variables.merge(@objective_expression.variables)
@constraints = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
79
80
81
|
# File 'lib/rulp/rulp.rb', line 79
def method_missing(method_name)
self.call(method_name)
end
|
Instance Method Details
#[](*constraints) ⇒ Object
69
70
71
72
73
|
# File 'lib/rulp/rulp.rb', line 69
def [](*constraints)
@constraints.concat(constraints)
@variables.merge(constraints.map(&:variables).flatten)
self
end
|
108
109
110
111
|
# File 'lib/rulp/rulp.rb', line 108
def bits
bits = @variables.select{|x| x.kind_of?(BV) }.join(" ")
return "\nBinary\n #{bits}" if(bits.length > 0)
end
|
113
114
115
116
117
118
|
# File 'lib/rulp/rulp.rb', line 113
def bounds
@variables.map{|var|
next unless var.bounds
" #{var.bounds}"
}.compact.join("\n")
end
|
#call(using = nil) ⇒ Object
88
89
90
|
# File 'lib/rulp/rulp.rb', line 88
def call(using=nil)
Rulp.send(self.solver(using), self)
end
|
#constraints ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/rulp/rulp.rb', line 92
def constraints
constraints_str = @constraints.map.with_index{|constraint, i|
" c#{i}: #{constraint}"
}.join("\n").strip
if constraints_str.empty?
"0 #{@variables.first} = 0"
else
" #{constraints_str}"
end
end
|
#get_output_filename ⇒ Object
120
121
122
|
# File 'lib/rulp/rulp.rb', line 120
def get_output_filename
"/tmp/rulp-#{Random.rand(0..1000)}.lp"
end
|
151
152
153
|
# File 'lib/rulp/rulp.rb', line 151
def inspect
to_s
end
|
103
104
105
106
|
# File 'lib/rulp/rulp.rb', line 103
def integers
ints = @variables.select{|x| x.kind_of?(IV) }.join(" ")
return "\nGeneral\n #{ints}" if(ints.length > 0)
end
|
#output(filename = choose_file) ⇒ Object
Also known as:
save
124
125
126
|
# File 'lib/rulp/rulp.rb', line 124
def output(filename=choose_file)
IO.write(filename, self)
end
|
75
76
77
|
# File 'lib/rulp/rulp.rb', line 75
def solve
Rulp.send(self.solver, self)
end
|
#solve_with(type, open_definition = false, open_solution = false) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/rulp/rulp.rb', line 128
def solve_with(type, open_definition=false, open_solution=false)
filename = get_output_filename
solver = SOLVERS[type].new(filename)
"Writing problem".log(:info)
IO.write(filename, self)
`open #{filename}` if open_definition
"Solving problem".log(:info)
_, time = _profile{ solver.solve(open_solution) }
"Solver took #{time}".log(:info)
"Parsing result".log(:info)
solver.store_results(@variables)
result = @objective_expression.evaluate
"Objective: #{result}\n#{@variables.map{|v|[v.name, "=", v.value].join(' ') if v.value}.compact.join("\n")}".log(:debug)
return result
end
|
#solver(solver = nil) ⇒ Object
83
84
85
86
|
# File 'lib/rulp/rulp.rb', line 83
def solver(solver=nil)
solver = solver || ENV["SOLVER"] || "Scip"
solver = solver[0].upcase + solver[1..-1].downcase
end
|
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/rulp/rulp.rb', line 155
def to_s
%Q(
#{' '*0}#{@objective}
#{' '*0} obj: #{@objective_expression}
#{' '*0}Subject to
#{' '*0}#{constraints}
#{' '*0}Bounds
#{' '*0}#{bounds}#{integers}#{bits}
#{' '*0}End
)
end
|