Hae Lee wrote:
Andreas Launila wrote:
The simplest way to solve this problem in Gecode/R is probably to use
set variables:
[...]
Hi there; thanks so much! Qq (quick question): How would I modify this
so that it lists each possible combination surrounded within their own
bracket sets?
That is covered in
http://gecoder.rubyforge.org/documentation/searching_for_solutions.html
. There doesn't exist a convenience method for it, so one has to
formulate the problem as a class:
require 'rubygems'
require 'gecoder'
class ArraySumProblem
include Gecode::Mixin
def initialize
weights = [2429.63, 497.87, 51.96, 59.43, 138.4, 66.22, 28.74,
1.75, 2075.13, 556.14, 112.56, 116.5, 84.41, 55.97, 139.07,
24.46]
# Convert to integers.
weights.map!{ |x| (x*100).floor }
selected_weights_is_a set_var(, weights)
selected_weights.sum.must == 343578
branch_on selected_weights
end
end
ArraySumProblem.new.each_solution do |solution|
p solution.selected_weights.value.map{ |x| x.to_f / 100 }
end
Output:
[1.75, 112.56, 116.5, 138.4, 139.07, 497.87, 2429.63]
[24.46, 28.74, 51.96, 59.43, 66.22, 138.4, 139.07, 497.87, 2429.63]
If you are planning to solve several instances of the problem then you
probably want to modify the above so that the weights and target are
sent in through the constructor.
···
--
Andreas Launila