-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaustralia.py
81 lines (64 loc) · 2.59 KB
/
australia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Australia:
def __init__(self):
self.states = ('WA', 'NT', 'SA', 'QLD', 'NSW', 'ACT', 'VIC', 'TAS')
self.colours = ('R', 'G', 'B')
self.values = {state:'' for state in self.states}
self.constraints = {'WA': ('NT', 'SA'),
'NT': ('WA', 'SA', 'QLD'),
'SA' : ('WA', 'NT', 'QLD', 'NSW', 'VIC'),
'QLD': ('NT', 'SA', 'NSW'),
'NSW': ('SA', 'QLD', 'ACT', 'VIC'),
'ACT': ('NSW'),
'VIC': ('SA', 'NSW'),
'TAS': ()}
def get_init_state(self):
return AustraliaEnvState(self, self.values.copy())
def is_solved(self, env_state):
for colour in env_state.values():
if colour not in self.colours:
return False
if not self.check_all_constraints(env_state):
return False
return True
def check_all_constraints(self, env_state):
for state in self.states:
if not self.check_constraints(env_state, state):
return False
return True
def check_constraints(self, env_state, state):
state_colour = env_state[state]
# TODO MAKE DYNAMIC
if state == 'ACT':
if state_colour == env_state[self.constraints[state]]:
return False
else:
for neighbour in self.constraints[state]:
if state_colour == env_state[neighbour]:
return False
return True
class AustraliaEnvState:
def __init__ (self, env, colouring: dict, parent=None):
self.env = env
self.state = colouring
self.parent = parent
def get_successors(self):
next_state = None
for state, colour in self.state.items():
if not colour:
next_state = state
break
if next_state is None:
return [self]
successors = []
for colour in self.env.colours:
new_colouring = self.state.copy()
new_colouring[next_state] = colour
new_state = AustraliaEnvState(self.env, new_colouring, self)
successors.append(new_state)
return successors
def get_representation(self):
return ''.join([value for value in self.state.values()])
def __repr__(self):
return str(self.state)
def __str__(self):
return str(self.state)