3 #NOTE: The -u option is required for unbuffered stdin/stdout.
4 # If stdin/stdout are buffered, the manager program will not recieve any messages and assume that the agent has timed out.
7 vixen.py - A sample Stratego AI for the UCC Programming Competition 2012
9 Written in python, the slithery language
11 author Sam Moore (matches) [SZM]
12 website http://matches.ucc.asn.au/stratego
14 git git.ucc.asn.au/progcomp2012.git
17 from basic_python import *
24 " Python based AI, improves upon Asmodeus by taking into account probabilities, and common paths "
26 BasicAI.__init__(self)
29 #self.bombScores = {'1' : -0.9 , '2' : -0.8 , '3' : -0.5 , '4' : 0.1, '5' : 0.1, '6' : 0.3, '7' : 0.7, '8' : 1 , '9' : 0.6, 's' : 0}
30 #self.bombScores = {'1' : -0.9 , '2' : -0.8 , '3' : -0.5 , '4' : -0.5, '5' : -0.4, '6' : -0.5, '7' : -0.2, '8' : 1.0 , '9' : -0.1, 's' : -0.2}
31 self.suicideScores = {'1' : -0.8 , '2' : -0.6 , '3' : -0.5, '4' : -0.25, '5' : -0.2, '6' : 0.0, '7' : 0.1, '8' : -1.0 , '9' : 0.0, 's' : -0.4}
32 self.killScores = {'1' : 1.0 , '2' : 0.9 , '3' : 0.9 , '4' : 0.8, '5' : 0.8, '6' : 0.8, '7' : 0.8, '8' : 0.9 , '9' : 0.7, 's' : 1.0}
33 self.riskScores = {'1' : -0.3, '2' : -0.3, '3' : 0.0, '4': 0.4, '5': 0.6, '6': 0.7, '7':0.8, '8': 0.0, '9' : 1.0, 's' : 0.1}
37 """ Implements Setup part of protocol. Always uses the same setup. Override to create custom setups """
38 #sys.stderr.write("BasicAI Setup here...\n");
39 setup = sys.stdin.readline().split(' ')
41 sys.stderr.write("BasicAI setup fails, expected 4 tokens, got " + str(len(setup)) + " "+str(setup) + "\n")
42 self.colour = setup[0]
43 self.opponentName = setup[1]
44 self.width = int(setup[2])
45 self.height = int(setup[3])
46 for x in range(0, self.width):
48 for y in range(0, self.height):
49 self.board[x].append(None)
51 #flagPosition = random.choice((
52 #fakeFlag = random.choice((
53 if self.colour == "RED":
54 print "FB8sB479B8\nBB31555583\n6724898974\n967B669999"
55 elif self.colour == "BLUE":
56 print "967B669999\n6724898974\nBB31555583\nFB8sB479B8"
61 #sys.stderr.write("Vixen MakingMove...\n")
62 " Over-rides the default BasicAI.MakeMove function "
65 for unit in self.units:
66 if unit.mobile() == False:
69 scores = {"LEFT":None, "RIGHT":None, "UP":None, "DOWN":None}
71 for target in self.enemyUnits:
74 path = PathFinder().pathFind((unit.x, unit.y), (target.x, target.y), self.board)
75 if path == False or len(path) == 0:
77 if scores[path[0]] == None:
80 scores[path[0]] += self.CalculateScore(unit, target, path)
82 for d in scores.keys():
86 if len(scores.items()) > 0:
87 bestScore = sorted(scores.items(), key = lambda e : e[1], reverse=True)[0]
88 moveList.append({"unit":unit, "direction":bestScore[0], "score":bestScore[1]})
92 if len(moveList) <= 0:
96 moveList.sort(key = lambda e : e["score"], reverse=True)
97 #sys.stderr.write("vixen - best move: " + str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"] + " [ score = " + str(moveList[0]["score"]) + " ]\n")
98 #if moveList[0]["score"] == 0:
103 print str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"]
107 def tailFactor(self, pathLength):
108 #if pathLength >= len(self.tailFactors) or pathLength <= 0:
110 #return self.tailFactors[pathLength]
111 #return 0.5 * (1.0 + pow(pathLength, 0.75))
112 return 1.0 / pathLength
115 def CalculateScore(self, attacker, defender, path):
116 p = move(attacker.x, attacker.y, path[0], 1)
117 if p[0] < 0 or p[0] >= len(self.board) or p[1] < 0 or p[1] >= len(self.board[p[0]]):
123 prob = self.rankProbability(defender, rank)
125 #sys.stderr.write(" " + str(attacker.rank) + " vs. " + str(rank) + " [" + str(prob) + "] score " + str(self.combatScore(attacker.rank, rank, len(path))) + "\n")
126 total += prob * self.combatScore(attacker.rank, rank, len(path))
131 # total = total / count + self.riskScore(attacker.rank)
134 total = total * self.tailFactor(len(path))
135 #HACK - Prevent "oscillating" by decreasing the value of backtracks
136 if len(path) > 1 and len(attacker.positions) > 1 and attacker.positions[1][0] == p[0] and attacker.positions[1][1] == p[1]:
138 #sys.stderr.write("Total score for " + str(attacker) + " vs. " + str(defender) + " is " + str(total) + "\n")
141 def combatScore(self, attackerRank, defenderRank, pathLength):
142 if defenderRank == 'F':
144 elif defenderRank == 'B':
145 return self.bombScore(attackerRank)
146 elif defenderRank == 's' and attackerRank == '1' and pathLength == 2:
147 return self.suicideScore(attackerRank)
148 elif defenderRank == '1' and attackerRank == 's' and pathLength != 2:
149 return self.killScore(attackerRank)
151 if valuedRank(attackerRank) > valuedRank(defenderRank):
152 return self.killScore(defenderRank)
153 elif valuedRank(attackerRank) < valuedRank(defenderRank):
154 return self.suicideScore(attackerRank)
155 return self.killScore(defenderRank) + self.suicideScore(attackerRank)
157 def killScore(self, defenderRank):
158 return self.killScores[defenderRank]
160 def bombScore(self, attackerRank):
161 if attackerRank == '8':
164 return self.suicideScore(attackerRank)
166 def suicideScore(self, attackerRank):
167 return self.suicideScores[attackerRank]
169 def riskScore(self, attackerRank):
170 return self.riskScores[attackerRank]
172 def rankProbability(self, target, targetRank):
173 if targetRank == '+' or targetRank == '?':
175 if target.rank == targetRank:
177 elif target.rank != '?':
182 if rank == '+' or rank == '?':
184 elif rank == 'F' or rank == 'B':
185 if target.lastMoved < 0:
186 total += self.hiddenEnemies[rank]
188 total += self.hiddenEnemies[rank]
192 return float(float(self.hiddenEnemies[targetRank]) / float(total))
200 if __name__ == "__main__":
203 while vixen.MoveCycle():