-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticNetsAgent.py
193 lines (152 loc) · 5.59 KB
/
SemanticNetsAgent.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import copy
import time
class AnimalsState:
def __init__(self, wolves, sheep):
self.wolves = wolves
self.sheep = sheep
def is_valid(self):
if self.sheep >= 0 and self.wolves >= 0 \
and (self.sheep == 0 or self.sheep >= self.wolves):
return True
else:
return False
def __hash__(self):
return hash((self.wolves, self.sheep)) ### hash function
class BoatState:
def __init__(self, boat):
self.boat = boat
self.parent = None
# def __hash__(self):
# return hash((self.boat, self.parent))
def generate_neighbors(self):
neighbors = []
temp_state = copy.deepcopy(self.boat)
if self.boat['position'] == 'L': ### define next states and current states for boat
current_pos = 'L'
across_current_pos = 'R'
elif self.boat['position'] == 'R':
current_pos = 'R'
across_current_pos = 'L'
## move two wolves, then two sheep, etc
## then
if temp_state[current_pos].wolves >= 2:
temp_state[current_pos].wolves = temp_state[current_pos].wolves - 2
temp_state[across_current_pos].wolves = temp_state[across_current_pos].wolves + 2
temp_state['position'] = across_current_pos
if temp_state[current_pos].is_valid() and temp_state[across_current_pos].is_valid(): ### check if it's illegal
boat_state = BoatState(temp_state) ## create a new state using the temp state if it is legal
boat_state.parent = self ## parent inherets current
neighbors.append(boat_state) ### save in list
temp_state = copy.deepcopy(self.boat)
if temp_state[current_pos].sheep >= 2:
temp_state[current_pos].sheep = temp_state[current_pos].sheep - 2
temp_state[across_current_pos].sheep = temp_state[across_current_pos].sheep + 2
temp_state['position'] = across_current_pos
if temp_state[current_pos].is_valid() and temp_state[across_current_pos].is_valid():
boat_state = BoatState(temp_state)
boat_state.parent = self
neighbors.append(boat_state)
temp_state = copy.deepcopy(self.boat)
if temp_state[current_pos].wolves >= 1:
temp_state[current_pos].wolves = temp_state[current_pos].wolves - 1
temp_state[across_current_pos].wolves = temp_state[across_current_pos].wolves + 1
temp_state['position'] = across_current_pos
if temp_state[current_pos].is_valid() and temp_state[across_current_pos].is_valid():
boat_state = BoatState(temp_state)
boat_state.parent = self
neighbors.append(boat_state)
temp_state = copy.deepcopy(self.boat)
if temp_state[current_pos].sheep >= 1:
temp_state[current_pos].sheep = temp_state[current_pos].sheep - 1
temp_state[across_current_pos].sheep = temp_state[across_current_pos].sheep + 1
temp_state['position'] = across_current_pos
if temp_state[current_pos].is_valid() and temp_state[across_current_pos].is_valid():
boat_state = BoatState(temp_state)
boat_state.parent = self
neighbors.append(boat_state)
temp_state = copy.deepcopy(self.boat)
if temp_state[current_pos].sheep >= 1 and temp_state[current_pos].wolves >= 1:
temp_state[current_pos].sheep = temp_state[current_pos].sheep - 1
temp_state[across_current_pos].sheep = temp_state[across_current_pos].sheep + 1
temp_state[current_pos].wolves = temp_state[current_pos].wolves - 1
temp_state[across_current_pos].wolves = temp_state[across_current_pos].wolves + 1
temp_state['position'] = across_current_pos
if temp_state[current_pos].is_valid() and temp_state[across_current_pos].is_valid():
boat_state = BoatState(temp_state)
boat_state.parent = self
neighbors.append(boat_state)
return neighbors ## returns potential neighbors to be searched through
def goal(state):
if state.boat['L'].sheep == 0 and state.boat['L'].wolves == 0 and state.boat['position'] == 'R':
return True
else:
return False
def bfs(current):
end = AnimalsState(0, 0)
root = {'L': current, 'R': end, 'position': 'L'}
seen = set()
q = list()
queue = [BoatState(root)]
path = []
count = 0
for state in queue:
count+=1
if goal(state):
path = [state]
return state
while state.parent:
path.insert(0, state.parent)
state = state.parent
break
if(count > 15000):
return []
queue.extend(state.generate_neighbors())
return []
def is_valid_state(state):
if state != None:
if state.sheep >= state.wolves \
and (state.sheep + state.wolves) > 0:
return True
else:
return False
class SemanticNetsAgent:
def __init__(self):
#If you want to do any initial processing, add it here.
pass
def solve(self, initial_sheep, initial_wolves):
#Add your code here! Your solve method should receive
#the initial number of sheep and wolves as integers,
#and return a list of 2-tuples that represent the moves
#required to get all sheep and wolves from the left
# start = time.time()
moves = list()
initial_state = AnimalsState(initial_wolves,initial_sheep)
if is_valid_state(initial_state):
print('Solving...')
state = bfs(initial_state)
if(state == []):
return []
list1 = list()
list2=list()
path = [state]
if(type(state) is tuple):
return (0,0)
else:
while state.parent:
state = state.parent
path.append(state)
for p in reversed(path):
left = (p.boat['L'].sheep,p.boat['L'].wolves)
right = (p.boat['R'].sheep,p.boat['R'].wolves)
sheep = p.boat['L'].sheep
wolves = p.boat['L'].wolves
list1.append(sheep)
list2.append(wolves)
sheep_change = [abs(x - list1[i - 1]) for i, x in enumerate(list1)][1:]
wolves_change = [abs(x - list2[i - 1]) for i, x in enumerate(list2)][1:]
moves = list(zip(sheep_change,wolves_change))
else:
moves = []
return moves
t = SemanticNetsAgent()
# print(t.solve(4,4))