-
Notifications
You must be signed in to change notification settings - Fork 1
/
ia.py
executable file
·310 lines (226 loc) · 6.94 KB
/
ia.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import operator
from utils import *
from wrapper import Protocol
from Comment import comment, bcolors
import time
from random import randint
# import Protocol
ANT_ECLAIREUR = 0
ANT_RAMASSEUR = 1
PH_NEED_RECHARGE = 30
STAMINA_NEED_EAT = 100
DISTANCE_NEED_PUT_PH = 90
def antIA(ant):
# ANT PROGRAM
# time.sleep(1)
if ant.type == ANT_ECLAIREUR:
comment("ANT_ECLAIREUR", bcolors.OKGREEN)
lastIdPaht = []
lastIdPaht.append(0)
for ph in ant.arrSeePheromone:
lastIdPaht.append(ph["type"])
idPathStart = max(lastIdPaht)
if ant.stamina < STAMINA_NEED_EAT and ant.food > 0:
ant.say("ant-eat")
ant.eat(1)
return
gotFood = (ant.m2 == 1 or ant.m2 == 2)
needMove = (ant.m2 == 1)
if idPathStart > ant.m1 and not gotFood:
ant.say("SUICIDE")
ant.suicide()
return
# NEED STAMINA
if ant.stamina < STAMINA_NEED_EAT:
ant.say("ON BOUFFE, ON NEED DE LA STAMINA")
ant.suicide()
return
phs = ant.arrSeePheromone
# needRechargePhs = les phs qui sont < PH_NEED_RECHARGE
nearest = compareKey("area", phs, operator.eq, "NEAR")
needRefuel = compareKey("persistance", nearest, operator.lt, PH_NEED_RECHARGE)
# if len(needRefuel) > 0:
# ant.say("LE PHEROMONE A BESOIN DE SE FAIRE RECHARGER")
# ant.rechargePheromone(needRefuel[0]["id"])
# return
# farPh = la phs la plus loin
# HOME RETURN
id_min = 0
type_min = 99999999999
for ph in ant.arrSeePheromone:
if type_min > ph["type"]:
id_min = ph["id"]
type_min = ph["type"]
if gotFood:
if ant.arrSeeNest and ant.arrSeeNest[0]["area"] == 'FAR':
ant.say("ON SE DIRIGE VERS LE BERCAIL")
ant.moveTo(ant.arrSeeNest[0]["id"])
return
if ant.arrSeeNest and ant.arrSeeNest[0]["area"] == 'NEAR':
ant.say("BERCAIL")
ant.nest(ant.arrSeeNest[0]["id"])
return
ant.say("ON SE DIRIGE VERS LE CHEMIN DU RETOUR")
if needMove:
ant.setMemory(ant.m1, 2)
ant.moveTo(id_min)
else :
ant.setMemory(ant.m1, 1)
nearestPhero = [ x for x in ant.arrSeePheromone if x["area"] == "NEAR"]
if not nearestPhero:
ant.setMemory(ant.m1, 2)
ant.moveTo(id_min)
else:
a = minMaxKey("dist",nearestPhero,min)
ant.rechargePheromone(a)
ant.commitMemory()
return
# partie calcul min distance
ant.say("test")
phs = [ x for x in phs if x["dist"] < DISTANCE_NEED_PUT_PH ]
ant.say("phs" + str(phs))
if not phs:
ant.say("ON A BESOIN DE PLACER UN PHEROMONE, INIT")
ant.putPheromone(idPathStart + 1)
return
# ant.say("idPathStart" + str(idPathStart))
# ant.say("arrSeePheromone" + str(ant.arrSeePheromone))
ant.say("arrSeeFood" + str(ant.arrSeeFood))
if ant.arrSeeFood:
nearestFoodSrc = [ x for x in ant.arrSeeFood if x["area"] == 'NEAR']
if nearestFoodSrc:
ant.say("ON RECUPERE DE LA BOUFFE")
ant.collect(nearestFoodSrc[0]["id"], min(nearestFoodSrc[0]["amount"], ant.FOOD_MAX) - 1)
ant.setMemory(idPathStart + 10, 2)
ant.commitMemory()
return
else:
ant.say("ON SE DIRIGE VERS LA BOUFFE")
ant.moveTo(ant.arrSeeFood[0]["id"])
return
# s = sorted(ant.arrSeePheromone, key = lambda x: (x["persistance"], x["type"]))
# if s and randint(0, 1000) < 2:
# ant.moveTo(s[0]["id"])
# return
# ant.say("arrSeePheromone ==" + str(ant.arrSeePheromone))
ant.explore()
ant.say("ON A RIEN TROUVE, ON EXPLORE")
return
elif ant.type == ANT_RAMASSEUR:
comment("ANT_RAMASSEUR", bcolors.OKGREEN)
lastIdPaht = []
lastIdPaht.append(0)
for ph in ant.arrSeePheromone:
lastIdPaht.append(ph["persistance"])
idPathStart = max(lastIdPaht)
comment("idPathStart: " + str(idPathStart))
gotFood = (ant.m2 == 1 or ant.m2 == 2)
needMove = (ant.m2 == 1)
# if not gotFood:
# ant.say("SUICIDE")
# ant.suicide()
# return
# NEED STAMINA
if ant.stamina < STAMINA_NEED_EAT:
ant.say("ON BOUFFE, ON NEED DE LA STAMINA")
ant.eat(1)
return
phs = ant.arrSeePheromone
# needRechargePhs = les phs qui sont < PH_NEED_RECHARGE
nearest = compareKey("area", phs, operator.eq, "NEAR")
needRefuel = compareKey("persistance", nearest, operator.lt, PH_NEED_RECHARGE)
# if len(needRefuel) > 0:
# ant.say("LE PHEROMONE A BESOIN DE SE FAIRE RECHARGER")
# ant.rechargePheromone(needRefuel[0]["id"])
# return
# farPh = la phs la plus loin
# HOME RETURN
id_min = 0
pers_min = 99999999999
for ph in ant.arrSeePheromone:
if pers_min > ph["persistance"]:
id_min = ph["id"]
pers_min = ph["persistance"]
if gotFood:
if ant.arrSeeNest and ant.arrSeeNest[0]["area"] == 'FAR':
ant.say("ON SE DIRIGE VERS LE BERCAIL")
ant.moveTo(ant.arrSeeNest[0]["id"])
return
if ant.arrSeeNest and ant.arrSeeNest[0]["area"] == 'NEAR':
ant.say("BERCAIL")
ant.nest(ant.arrSeeNest[0]["id"])
return
ant.say("ON SE DIRIGE VERS LE CHEMIN DU RETOUR")
if needMove:
ant.setMemory(ant.m1, 2)
ant.moveTo(pers_min)
else :
ant.setMemory(ant.m1, 1)
nearestPhero = [ x for x in ant.arrSeePheromone if x["area"] == "NEAR"]
if not nearestPhero:
ant.setMemory(ant.m1, 2)
ant.moveTo(pers_min)
else:
a = minMaxKey("dist",nearestPhero,min)
ant.rechargePheromone(a)
ant.say("arrSeeFood" + str(ant.arrSeeFood))
if ant.arrSeeFood:
nearestFoodSrc = [ x for x in ant.arrSeeFood if x["area"] == 'NEAR']
if nearestFoodSrc:
ant.say("ON RECUPERE DE LA BOUFFE")
ant.collect(nearestFoodSrc[0]["id"], min(nearestFoodSrc[0]["amount"], ant.FOOD_MAX))
ant.setMemory(ant.m1, 2)
ant.commitMemory()
return
else:
ant.say("ON SE DIRIGE VERS LA BOUFFE")
ant.moveTo(ant.arrSeeFood[0]["id"])
return
ant.moveTo(id_min)
return
return
NB_ANT_CREATED = 0
KILL_AT_PH = 1
def nestIA(nest):
# NEST PROGRAM
comment(str(nest), bcolors.OKBLUE)
if nest.memory[KILL_AT_PH] == 0:
nest.memory[KILL_AT_PH] = 70
nest.commitMemory()
if nest.arrAnt:
comment("capitalize create rama", bcolors.OKGREEN)
nest.memory[KILL_AT_PH] = int(nest.arrAnt[0]["m1"] * 1.3)
nest.commitMemory()
nest.newAnt(ANT_RAMASSEUR)
return
if nest.arrAntType:
nest.antOut(nest.arrAntType[0]["type"], 3, nest.memory[KILL_AT_PH], 0)
return
if nest.memory[NB_ANT_CREATED] < 1:
comment("capitalize create ant", bcolors.OKGREEN)
nest.memory[NB_ANT_CREATED] += 2
nest.commitMemory()
nest.newAnt(ANT_ECLAIREUR)
return
elif randint(0, 200) < 1:
comment("random create ant", bcolors.OKGREEN)
nest.memory[NB_ANT_CREATED] = 1
nest.memory[KILL_AT_PH] = int(nest.memory[KILL_AT_PH]+1 * 1.5)
nest.commitMemory()
nest.newAnt(ANT_ECLAIREUR)
return
# elif randint(0, 10) < 1:
# comment("random create ant", bcolors.OKGREEN)
# nest.memory[NB_ANT_CREATED] = 1
# nest.memory[KILL_AT_PH] = int(nest.memory[KILL_AT_PH]+1 * 1.5)
# nest.commitMemory()
# nest.newAnt(ANT_RAMASSEUR)
# return
while True:
nameEntity, entity = Protocol.readInput()
comment(nameEntity)
if nameEntity == 'ANT':
antIA(entity)
elif nameEntity == 'NEST':
nestIA(entity)
Protocol.exit()