Class: Rulp::Problem

Inherits:
Object show all
Defined in:
lib/rulp/rulp.rb

Instance Method Summary collapse

Constructor Details

#initialize(objective, objective_expression) ⇒ Problem

Returns a new instance of Problem.



68
69
70
71
72
73
74
# File 'lib/rulp/rulp.rb', line 68

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, *args) ⇒ Object



91
92
93
# File 'lib/rulp/rulp.rb', line 91

def method_missing(method_name, *args)
  self.call(method_name, *args)
end

Instance Method Details

#[](*constraints) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/rulp/rulp.rb', line 76

def [](*constraints)
  "Got constraints".log(:info)
  constraints.flatten!
  "Flattened constraints".log(:info)
  @constraints.concat(constraints)
  "Joint constraints".log(:info)
  @variables.merge(constraints.flat_map(&:variables).uniq)
  "Extracted variables".log(:info)
  self
end

#bitsObject



118
119
120
121
# File 'lib/rulp/rulp.rb', line 118

def bits
  bits = @variables.select{|x| x.kind_of?(BV) }.join(" ")
  return "\nBinary\n #{bits}" if(bits.length > 0)
end

#boundsObject



123
124
125
126
127
128
# File 'lib/rulp/rulp.rb', line 123

def bounds
  @variables.map{|var|
    next unless var.bounds
    " #{var.bounds}"
  }.compact.join("\n")
end

#call(using = nil, options = {}) ⇒ Object



100
101
102
# File 'lib/rulp/rulp.rb', line 100

def call(using=nil, options={})
  Rulp.send(self.solver(using), self, options)
end

#constraintsObject



104
105
106
107
108
109
110
111
# File 'lib/rulp/rulp.rb', line 104

def constraints
  return  "0 #{@variables.first} = 0" if @constraints.length == 0
  constraints_str = " "
  @constraints.each.with_index{|constraint, i|
    constraints_str << " c#{i}: #{constraint}\n"
  }
  constraints_str
end

#get_output_filenameObject



130
131
132
# File 'lib/rulp/rulp.rb', line 130

def get_output_filename
  "/tmp/rulp-#{Random.rand(0..1000)}.lp"
end

#inspectObject



163
164
165
# File 'lib/rulp/rulp.rb', line 163

def inspect
  to_s
end

#integersObject



113
114
115
116
# File 'lib/rulp/rulp.rb', line 113

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



134
135
136
# File 'lib/rulp/rulp.rb', line 134

def output(filename=choose_file)
  IO.write(filename, self)
end

#solve(opts = {}) ⇒ Object



87
88
89
# File 'lib/rulp/rulp.rb', line 87

def solve(opts={})
  Rulp.send(self.solver, self, opts)
end

#solve_with(type, options = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rulp/rulp.rb', line 138

def solve_with(type, options={})
  filename = get_output_filename
  solver = SOLVERS[type].new(filename, options)

  "Writing problem".log(:info)
  IO.write(filename, self)

  `open #{filename}` if options[:open_definition]

  "Solving problem".log(:info)
  _, time = _profile{ solver.solve }

  `open #{solver.outfile}` if options[:open_solution]

  "Solver took #{time}".log(:debug)

  "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



95
96
97
98
# File 'lib/rulp/rulp.rb', line 95

def solver(solver=nil)
  solver = solver || ENV["SOLVER"] || "Scip"
  solver = solver[0].upcase + solver[1..-1].downcase
end

#to_sObject



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rulp/rulp.rb', line 177

def to_s
  %Q(
#{'  '*0}#{@objective}
#{'  '*0} obj: #{@objective_expression}
#{'  '*0}Subject to
#{'  '*0}#{constraints}
#{'  '*0}Bounds
#{'  '*0}#{bounds}#{integers}#{bits}
#{'  '*0}End
  )
end

#write(output) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/rulp/rulp.rb', line 167

def write(output)
  output.puts "#{@objective}"
  output.puts " obj: #{@objective_expression}"
  output.puts "Subject to"
  output.puts "#{constraints}"
  output.puts "Bounds"
  output.puts "#{bounds}#{integers}#{bits}"
  output.puts "End"
end