Wolfsheep improved

This commit is contained in:
madmaurice 2015-04-29 06:57:52 +02:00
parent d0881dabdf
commit 98d5c95973

View file

@ -17,8 +17,9 @@ class Model:
return ",".join(self.left) + " <-> " + ",".join(self.right)
class Simulator:
invalid_states = [ set({Model.Wolf,Model.Sheep}), set({Model.Sheep, Model.Cabbage}) ]
def simulate(self, state, stack=[]):
#stack.append(state)
stack = stack + [state]
if state.right == set([Model.Man,Model.Wolf,Model.Sheep,Model.Cabbage]):
return stack
@ -32,12 +33,11 @@ class Simulator:
nextStates.append( Model(state.left | set([Model.Man, obj]) , state.right - set([Model.Man,obj]) ) )
nextStates = list(filter(
lambda s: s not in stack and not(
( (set([Model.Wolf,Model.Sheep]) <= s.left or set([Model.Sheep, Model.Cabbage]) <= s.left) and Model.Man not in s.left ) or
( (set([Model.Wolf,Model.Sheep]) <= s.right or set([Model.Sheep, Model.Cabbage]) <= s.right) and Model.Man not in s.right )
lambda s: not(
s in stack or
any([ any([ invalid_state <= m for invalid_state in Simulator.invalid_states ]) and Model.Man not in m for m in [s.left,s.right] ])
)
, nextStates)) #Remove invalid and previous states.
#print("%s: %s\nstack: %s" % (state,nextStates,stack))
for s in nextStates:
m = self.simulate(s,stack)