[gecoder-users] Reification question
David Minor
daveminor at gmail.com
Fri Jul 4 18:04:59 EDT 2008
I get "no value is assigned" when trying to optimize this. With the
reification syntactic sugar I'm trying to set a variable to say 0 or
100000 based on the value of another variable. Is what I have correct usage?
This program is essentially trying to find the best poker hand,
excluding straights and flushes, given a set of cards.
----------------------
require 'gecoder'
class FindBest < Gecode::Model
def int_var_array(s,d,k=nil) (@vars ||= {})[k] = super(s,d) end
def int_var(d,k=nil) (@vars ||= {})[k] = super(d) end
def [](k) @vars[k] end
def initialize(c)
# decision variables for c
int_var_array(c.length, 0..1, :c)
# must include 5 of c
self[:c].inject { |s,n| s+n }.must == 5
# make arrays for each value in c
r = Hash.new {|h,k| h[k]=[]}
c.each_index { |i| r[c[i]] << self[:c][i] }
# create values based on number of each c value
rvalue = [] and r.each { |rnum,arr|
int_var(0..4, rnum).must == wrap_enum(arr).inject { |s,n| s+n }
(int_var([0,100000+rnum], "q#{rnum}").must == 0) |
(self[rnum].must == 4)
(int_var([0,10000+rnum], "t#{rnum}").must == 0) |
(self[rnum].must == 3)
(int_var([0,1000+rnum], "p#{rnum}").must == 0) | (self[rnum].must
== 2)
(int_var([0,rnum], "h#{rnum}").must == 0) | (self[rnum].must == 1)
rvalue << self["q#{rnum}"] << self["t#{rnum}"] <<
self["p#{rnum}"] << self["h#{rnum}"]
}
int_var(0..100200,:rvalue).must == wrap_enum(rvalue).inject { |s,n|
s+n }
branch_on self[:c]
end
end
# c holds up to 4 of each number 0-12
c = [12, 11, 10, 9, 8, 1, 1]
finder = FindBest.new(c)
finder.optimize! { |model, glb| model[:rvalue].must > glb[:rvalue].value }
# should yield 12, 11, 10, 1, 1
finder[:c].values.each_with_index { |v,i| puts ["", c[i]][v] }
--
_________________
David Minor
More information about the gecoder-users
mailing list