This is a not very serious example that shows how to use domino from scratch to solve a problem. It is illustrative of creating your own type of particles, a pair score, particle states for DOMINO, a decorator, and using subset filter tables with DOMINO.
28 def evaluate(self, pair, accum):
29 price0 = Price(pair[0]).get_price()
30 price1 = Price(pair[1]).get_price()
33 def _do_get_inputs(self, m, pis):
34 return [m.get_particle(i)
for i
in pis]
36 class PriceStates(domino.ParticleStates):
38 def __init__(self, prices):
40 domino.ParticleStates.__init__(self)
42 def load_particle_state(self, i, particle):
44 pr.set_price(self.prices[i])
46 def get_number_of_particle_states(self):
47 return len(self.prices)
53 def __init__(self, p):
54 if not self.particle_is_instance(p):
55 self.setup_particle(p)
58 def setup_particle(self, p):
59 p.add_attribute(self.price_key, 0)
63 return self.particle.get_value(self.price_key)
65 def set_price(self, x):
66 self.particle.set_value(self.price_key, int(x))
68 def particle_is_instance(self, p):
69 return p.has_attribute(self.price_key)
73 def get_total_price(states_table, subset, assignment):
75 for i, p
in enumerate(subset):
76 price_states = states_table.get_particle_states(p)
77 price_states.load_particle_state(assignment[i], p)
78 price = Price(p).get_price()
83 def print_assignment(states_table, subset, assignment):
85 print "########## solution assignment", assignment
86 for i, p
in enumerate(subset):
87 price_states = states_table.get_particle_states(p)
88 price_states.load_particle_state(assignment[i], p)
89 price = Price(p).get_price()
90 print p.get_name(),
"price", price
96 prices = [100, 200, 400, 600, 800 ]
102 states_table = domino.ParticleStatesTable()
103 sampler = domino.DominoSampler(model,states_table)
104 all_possible_states = PriceStates(prices)
108 for i
in range(n_girls):
113 states_table.set_particle_states(p, all_possible_states)
119 n_dresses = random.randrange(1,len(prices)+1)
122 selection = random.sample(prices, n_dresses)
123 allowed_states_indices = [prices.index(price)
for price
in selection]
124 print p.get_name(),
"prices selected",selection,
"indices", allowed_states_indices
125 list_states_table = domino.ListSubsetFilterTable(states_table)
126 list_states_table.set_allowed_states(p, allowed_states_indices)
127 sampler.add_subset_filter_table(list_states_table)
130 for z
in xrange(n_edges):
132 i = random.randrange(0,n_girls)
133 j = random.randrange(0,n_girls)
136 score = SumPricePairScore()
137 r = core.PairRestraint(score, friends)
138 model.add_restraint(r)
140 ft = domino.ExclusionSubsetFilterTable()
142 sampler.add_subset_filter_table(ft)
144 subset = states_table.get_subset()
145 solutions = sampler.get_sample_assignments(subset)
147 if len(solutions) == 0:
148 print "There are no solutions to the problem"
151 best_solution = solutions[0]
152 for assignment
in solutions:
153 total_price = get_total_price(states_table, subset, assignment)
154 if(total_price > most_expensive):
155 most_expensive = total_price
156 best_solution = assignment
157 print " There are", len(solutions),
"possible solutions"
158 print "=================> BEST SOLUTION <=============="
159 print_assignment(states_table, subset, best_solution)