/
second_2.py
244 lines (168 loc) · 11.9 KB
/
second_2.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import logic
import itertools
KB = logic.PropKB()
#initial state
KB.tell(logic.expr("At_0(C1, SFO)"))
KB.tell(logic.expr("~At_0(C1, JFK)"))
KB.tell(logic.expr("At_0(C2, JFK)"))
KB.tell(logic.expr("~At_0(C2, SFO)"))
KB.tell(logic.expr("At_0(P1, SFO)"))
KB.tell(logic.expr("~At_0(P1, JFK)"))
KB.tell(logic.expr("At_0(P2, JFK)"))
KB.tell(logic.expr("~At_0(P2, SFO)"))
KB.tell(logic.expr("~In_0(C1, P1)"))
KB.tell(logic.expr("~In_0(C2, P1)"))
KB.tell(logic.expr("~In_0(C1, P2)"))
KB.tell(logic.expr("~In_0(C2, P2)"))
moves = 6
#first preconditions
for i in range(0,moves):
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P1) | At_"+str(i)+"(C1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P1) | At_"+str(i)+"(P1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P2) | At_"+str(i)+"(C1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P2) | At_"+str(i)+"(P2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P1) | At_"+str(i)+"(C1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P1) | At_"+str(i)+"(P1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P2) | At_"+str(i)+"(C1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P2) | At_"+str(i)+"(P2, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P1) | At_"+str(i)+"(C2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P1) | At_"+str(i)+"(P1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P2) | At_"+str(i)+"(C2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P2) | At_"+str(i)+"(P2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P1) | At_"+str(i)+"(C2, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P1) | At_"+str(i)+"(P1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P2) | At_"+str(i)+"(C2, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P2) | At_"+str(i)+"(P2, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P1) | In_"+str(i)+"(C1, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P1) | At_"+str(i)+"(P1, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P2) | In_"+str(i)+"(C1, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P2) | At_"+str(i)+"(P2, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P1) | In_"+str(i)+"(C1, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P1) | At_"+str(i)+"(P1, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P2) | In_"+str(i)+"(C1, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P2) | At_"+str(i)+"(P2, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P1) | In_"+str(i)+"(C2, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P1) | At_"+str(i)+"(P1, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P2) | In_"+str(i)+"(C2, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P2) | At_"+str(i)+"(P2, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P1) | In_"+str(i)+"(C2, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P1) | At_"+str(i)+"(P1, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P2) | In_"+str(i)+"(C2, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P2) | At_"+str(i)+"(P2, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, JFK, SFO) | At_"+str(i)+"(P1, JFK)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, SFO, JFK) | At_"+str(i)+"(P1, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, JFK, SFO) | At_"+str(i)+"(P2, JFK)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, SFO, JFK) | At_"+str(i)+"(P2, SFO)"))
#second positive effects
for i in range(0, moves):
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P1) | In_"+str(i+1)+"(C1, P1)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P2) | In_"+str(i+1)+"(C1, P2)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P1) | In_"+str(i+1)+"(C1, P1)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P2) | In_"+str(i+1)+"(C1, P2)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P1) | In_"+str(i+1)+"(C2, P1)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P2) | In_"+str(i+1)+"(C2, P2)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P1) | In_"+str(i+1)+"(C2, P1)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P2) | In_"+str(i+1)+"(C2, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P1) | At_"+str(i+1)+"(C1, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P2) | At_"+str(i+1)+"(C1, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P1) | At_"+str(i+1)+"(C1, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P2) | At_"+str(i+1)+"(C1, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P1) | At_"+str(i+1)+"(C2, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P2) | At_"+str(i+1)+"(C2, JFK)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P1) | At_"+str(i+1)+"(C2, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P2) | At_"+str(i+1)+"(C2, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, JFK, SFO) | At_"+str(i+1)+"(P1, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, SFO, JFK) | At_"+str(i+1)+"(P1, JFK)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, JFK, SFO) | At_"+str(i+1)+"(P2, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, SFO, JFK) | At_"+str(i+1)+"(P2, JFK)"))
#third negative effects
for i in range(0,moves):
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P1) | ~At_"+str(i+1)+"(C1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, JFK, P2) | ~At_"+str(i+1)+"(C1, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P1) | ~At_"+str(i+1)+"(C1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C1, SFO, P2) | ~At_"+str(i+1)+"(C1, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P1) | ~At_"+str(i+1)+"(C2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, JFK, P2) | ~At_"+str(i+1)+"(C2, JFK)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P1) | ~At_"+str(i+1)+"(C2, SFO)"))
KB.tell(logic.expr("~Load_"+str(i)+"(C2, SFO, P2) | ~At_"+str(i+1)+"(C2, SFO)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P1) | ~In_"+str(i+1)+"(C1, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, JFK, P2) | ~In_"+str(i+1)+"(C1, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P1) | ~In_"+str(i+1)+"(C1, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C1, SFO, P2) | ~In_"+str(i+1)+"(C1, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P1) | ~In_"+str(i+1)+"(C2, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, JFK, P2) | ~In_"+str(i+1)+"(C2, P2)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P1) | ~In_"+str(i+1)+"(C2, P1)"))
KB.tell(logic.expr("~Unload_"+str(i)+"(C2, SFO, P2) | ~In_"+str(i+1)+"(C2, P2)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, JFK, SFO) | ~At_"+str(i+1)+"(P1, JFK)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P1, SFO, JFK) | ~At_"+str(i+1)+"(P1, SFO)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, JFK, SFO) | ~At_"+str(i+1)+"(P2, JFK)"))
KB.tell(logic.expr("~Fly_"+str(i)+"(P2, SFO, JFK) | ~At_"+str(i+1)+"(P2, SFO)"))
#fourth from false to true
for i in range(0,moves):
KB.tell(logic.expr("At_"+str(i)+"(C1, JFK) | ~At_"+str(i+1)+"(C1, JFK) | Unload_"+str(i)+"(C1, JFK, P1) | Unload_"+str(i)+"(C1, JFK, P2)"))
KB.tell(logic.expr("At_"+str(i)+"(C2, JFK) | ~At_"+str(i+1)+"(C2, JFK) | Unload_"+str(i)+"(C2, JFK, P1) | Unload_"+str(i)+"(C2, JFK, P2)"))
KB.tell(logic.expr("At_"+str(i)+"(C1, SFO) | ~At_"+str(i+1)+"(C1, SFO) | Unload_"+str(i)+"(C1, SFO, P1) | Unload_"+str(i)+"(C1, SFO, P2)"))
KB.tell(logic.expr("At_"+str(i)+"(C2, SFO) | ~At_"+str(i+1)+"(C2, SFO) | Unload_"+str(i)+"(C2, SFO, P1) | Unload_"+str(i)+"(C2, SFO, P2)"))
KB.tell(logic.expr("At_"+str(i)+"(P1, JFK) | ~At_"+str(i+1)+"(P1, JFK) | Fly_"+str(i)+"(P1, SFO, JFK)"))
KB.tell(logic.expr("At_"+str(i)+"(P2, JFK) | ~At_"+str(i+1)+"(P2, JFK) | Fly_"+str(i)+"(P2, SFO, JFK)"))
KB.tell(logic.expr("At_"+str(i)+"(P1, SFO) | ~At_"+str(i+1)+"(P1, SFO) | Fly_"+str(i)+"(P1, JFK, SFO)"))
KB.tell(logic.expr("At_"+str(i)+"(P2, SFO) | ~At_"+str(i+1)+"(P2, SFO) | Fly_"+str(i)+"(P2, JFK, SFO)"))
KB.tell(logic.expr("In_"+str(i)+"(C1, P1) | ~In_"+str(i+1)+"(C1, P1) | Load_"+str(i)+"(C1, JFK, P1) | Load_"+str(i)+"(C1, SFO, P1)"))
KB.tell(logic.expr("In_"+str(i)+"(C1, P2) | ~In_"+str(i+1)+"(C1, P2) | Load_"+str(i)+"(C1, JFK, P2) | Load_"+str(i)+"(C1, SFO, P2)"))
KB.tell(logic.expr("In_"+str(i)+"(C2, P1) | ~In_"+str(i+1)+"(C2, P1) | Load_"+str(i)+"(C2, JFK, P1) | Load_"+str(i)+"(C2, SFO, P1)"))
KB.tell(logic.expr("In_"+str(i)+"(C2, P2) | ~In_"+str(i+1)+"(C2, P2) | Load_"+str(i)+"(C2, JFK, P2) | Load_"+str(i)+"(C2, SFO, P2)"))
#fifth from true to false
for i in range(0,moves):
KB.tell(logic.expr("~At_"+str(i)+"(C1, SFO) | At_"+str(i+1)+"(C1, SFO) | Load_"+str(i)+"(C1, SFO, P1) | Load_"+str(i)+"(C1, SFO, P2)"))
KB.tell(logic.expr("~At_"+str(i)+"(C2, SFO) | At_"+str(i+1)+"(C2, SFO) | Load_"+str(i)+"(C2, SFO, P1) | Load_"+str(i)+"(C2, SFO, P2)"))
KB.tell(logic.expr("~At_"+str(i)+"(C1, JFK) | At_"+str(i+1)+"(C1, JFK) | Load_"+str(i)+"(C1, JFK, P1) | Load_"+str(i)+"(C1, JFK, P2)"))
KB.tell(logic.expr("~At_"+str(i)+"(C2, JFK) | At_"+str(i+1)+"(C2, JFK) | Load_"+str(i)+"(C2, JFK, P1) | Load_"+str(i)+"(C2, JFK, P2)"))
KB.tell(logic.expr("~At_"+str(i)+"(P1, SFO) | At_"+str(i+1)+"(P1, SFO) | Fly_"+str(i)+"(P1, SFO, JFK)"))
KB.tell(logic.expr("~At_"+str(i)+"(P2, SFO) | At_"+str(i+1)+"(P2, SFO) | Fly_"+str(i)+"(P2, SFO, JFK)"))
KB.tell(logic.expr("~At_"+str(i)+"(P1, JFK) | At_"+str(i+1)+"(P1, JFK) | Fly_"+str(i)+"(P1, JFK, SFO)"))
KB.tell(logic.expr("~At_"+str(i)+"(P2, JFK) | At_"+str(i+1)+"(P2, JFK) | Fly_"+str(i)+"(P2, JFK, SFO)"))
KB.tell(logic.expr("~In_"+str(i)+"(C1, P1) | In_"+str(i+1)+"(C1, P1) | Unload_"+str(i)+"(C1, JFK, P1) | Unload_"+str(i)+"(C1, SFO, P1)"))
KB.tell(logic.expr("~In_"+str(i)+"(C2, P1) | In_"+str(i+1)+"(C2, P1) | Unload_"+str(i)+"(C2, JFK, P1) | Unload_"+str(i)+"(C2, SFO, P1)"))
KB.tell(logic.expr("~In_"+str(i)+"(C1, P2) | In_"+str(i+1)+"(C1, P2) | Unload_"+str(i)+"(C1, JFK, P2) | Unload_"+str(i)+"(C1, SFO, P2)"))
KB.tell(logic.expr("~In_"+str(i)+"(C2, P2) | In_"+str(i+1)+"(C2, P2) | Unload_"+str(i)+"(C2, JFK, P2) | Unload_"+str(i)+"(C2, SFO, P2)"))
#list of all possible actions
#actions without timestamp, will be added later
actions = ["Load_(C1, JFK, P1)","Load_(C1, JFK, P2)","Load_(C1, SFO, P1)","Load_(C1, SFO, P2)",
"Load_(C2, JFK, P1)","Load_(C2, JFK, P2)","Load_(C2, SFO, P1)","Load_(C2, SFO, P2)",
"Unload_(C1, JFK, P1)","Unload_(C1, JFK, P2)","Unload_(C1, SFO, P1)","Unload_(C1, SFO, P2)",
"Unload_(C2, JFK, P1)","Unload_(C2, JFK, P2)","Unload_(C2, SFO, P1)","Unload_(C2, SFO, P2)",
"Fly_(P1, JFK, SFO)","Fly_(P1, SFO, JFK)","Fly_(P2, JFK, SFO)","Fly_(P2, SFO, JFK)"]
#sixth, only one action can take place
for i in range(0,moves):
for elem in itertools.combinations(actions, 2):
KB.tell(logic.expr("~"+elem[0].replace("_","_"+str(i))+" | ~"+elem[1].replace("_","_"+str(i))))
#seventh, one action must take place
for i in range(0,moves):
seventh = ""
for elem in actions:
seventh += elem.replace("_","_"+str(i)) + " | "
seventh = seventh[:-2]
KB.tell(logic.expr(seventh))
#goal
# KB.tell(logic.expr("Fly_0(P1, SFO, JFK)"))
KB.tell(logic.expr("At_6(P1, JFK) & At_6(C1, JFK)"))
KB.tell(logic.expr("At_6(C2, SFO) & At_6(P2, SFO)"))
#some manual cnf
string = ""
for elem in KB.clauses:
# print(elem)
# elem = logic.to_cnf(str(elem))
string = string + str(elem) + " & "
string = string[:-2]
action_stubs = ["Load", "Unload", "Fly"]
#print only true values
answer = logic.dpll_satisfiable(logic.expr(string))
#print out only true actions, leave out facts
if answer != False:
for elem in answer:
if answer[elem]:
if any(sub in str(elem) for sub in action_stubs):
print(str(elem)+ " : " +str(answer[elem]))
else:
print(answer)
# print(logic.dpll_satisfiable(logic.expr(string)))