/
fourmis.py
executable file
·153 lines (104 loc) · 4.19 KB
/
fourmis.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import lib.PyratApi as api
import lib.shortestPaths as sp
import lib.travelHeuristics as th
import lib.utils as ut
import lib.antColonyOptimization as aco
import lib.connard as connard
import time
import operator
BOT_NAME = "Antbot"
# Global variable for general behaviour
GLOBALPATH = []
ACTUALPATH = []
GOALLOCATION = (-1,-1)
METAGRAPH = {}
FORMICMETAGRAPH = {}
BESTPATHES = {}
MOVING = False
EATENCOINS = []
PERCENTTIMEALLOWEDFORANTSBEGINNING = 0.95
PERCENTTIMEALLOWEDFORANTS = 0.80
ESTIMATEDTIMEMAIN = 0.01
def chooseNextCoins (fmg, elLoc):
try :
# Just order by pheromone:
nodesList = list (fmg[elLoc].items ())
except KeyError :
# Told them there's an error :
raise AntError
nodesList.sort (key = operator.itemgetter(1))
return nodesList
# This function should not return anything, but should be used for a short preprocessing
def initializationCode (mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) :
t0 = time.time()
# First we protect ourself from killing
connard.preventFromKilling ()
# We add global variables
global METAGRAPH
global BESTPATHES
global FORMICMETAGRAPH
global GLOBALPATH
global ACTUALPATH
global GOALLOCATION
# Let's define global variables
METAGRAPH, BESTPATHES = th.generateMetaGraph(mazeMap, playerLocation, coins)
GOALLOCATION = playerLocation
t1 = time.time()
# ut.timeline ("Computed meta-graph and best pathes", t0, t0, t1)
# Now let's send a looooot of ants
FORMICMETAGRAPH = aco.generateFormicMetaGraph (METAGRAPH, playerLocation, (timeAllowed-(t1-t0))*PERCENTTIMEALLOWEDFORANTSBEGINNING)
t2 = time.time()
# ut.timeline ("Computed formic-meta-graph", t0, t1, t2)
# Ready !
# This is where you should write your code to determine the next direction
def determineNextMove (mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) :
t0 = time.time ()
# Travel heuristics variables
global METAGRAPH
global FORMICMETAGRAPH
global BESTPATHES
# Pathes variables
global GLOBALPATH
global ACTUALPATH
global GOALLOCATION
# General variables
global MOVING
global EATENCOINS
# We update eatenCoins except playerLocation
EATENCOINS = ut.updateCoinsWoPlayerLoc (METAGRAPH, EATENCOINS, coins, playerLocation)
if GOALLOCATION in EATENCOINS:
ACTUALPATH = th.findNearestCoin(mazeMap, playerLocation, coins)
GOALLOCATION = ACTUALPATH[0]
api.debug("Thief ! We go there : "+str(GOALLOCATION))
ACTUALPATH.pop ()
newMetaGraph = ut.metaGraphWithoutEaten (METAGRAPH, EATENCOINS)
# Let's send some ant. Not too much
t1 = time.time ()
FORMICMETAGRAPH = aco.generateFormicMetaGraph (newMetaGraph, GOALLOCATION, (timeAllowed-(t1-t0)-ESTIMATEDTIMEMAIN)*PERCENTTIMEALLOWEDFORANTS, FORMICMETAGRAPH)
if MOVING :
# Plus de chemin ou pièce bouffée, on s'arrete.
if not ACTUALPATH :
MOVING = False
EATENCOINS.append (playerLocation)
if not MOVING :
# We choose the next coin with aco :
GOALLOCATION = chooseNextCoins(FORMICMETAGRAPH, playerLocation)[0][0]
# Get next path
try :
ACTUALPATH = list (BESTPATHES[playerLocation][GOALLOCATION])
except KeyError: # The path doesn't exist, let's calculate one:
ACTUALPATH = sp.shortestWay (mazeMap, playerLocation, GOALLOCATION)
ACTUALPATH.pop () # Get rid of the first position wich should be actualPosition
MOVING = True
# Let's go !
nextPos = ACTUALPATH.pop()
return ut.convertPosesToDir(nextPos, playerLocation, mazeMap)
####
if __name__ == "__main__" :
# We let technical stuff happen
(mazeWidth, mazeHeight, mazeMap, preparationTime, turnTime, playerLocation, opponentLocation, coins, gameIsOver) = api.initGame(BOT_NAME)
initializationCode(mazeWidth, mazeHeight, mazeMap, preparationTime, playerLocation, opponentLocation, coins)
# Here magic happens
api.mainLoop (determineNextMove, mazeWidth, mazeHeight, mazeMap, turnTime)