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
13 email progcomp@ucc.asn.au or matches@ucc.asn.au
14 git git.ucc.asn.au/progcomp2012.git
17 from basic_python import *
23 " Python based AI, improves upon Asmodeus by taking into account probabilities, and common paths "
25 BasicAI.__init__(self)
28 #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}
29 #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}
30 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}
31 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}
32 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}
39 #sys.stderr.write("Vixen MakingMove...\n")
40 " Over-rides the default BasicAI.MakeMove function "
43 for unit in self.units:
44 if unit.mobile() == False:
47 scores = {"LEFT":0, "RIGHT":0, "UP":0, "DOWN":0}
50 for target in self.enemyUnits:
53 path = PathFinder().pathFind((unit.x, unit.y), (target.x, target.y), self.board)
54 if path == False or len(path) == 0:
56 #moveList.append({"unit":unit, "direction":path[0], "score":self.CalculateScore(unit, target, path)})
57 scores[path[0]] += self.CalculateScore(unit, target, path)
59 bestScore = sorted(scores.items(), key = lambda e : e[1], reverse=True)[0]
60 if bestScore[1] > -100.0:
61 moveList.append({"unit":unit, "direction":bestScore[0], "score":bestScore[1]})
65 if len(moveList) <= 0:
69 moveList.sort(key = lambda e : e["score"], reverse=True)
70 #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")
71 #if moveList[0]["score"] == 0:
76 print str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"]
80 def tailFactor(self, pathLength):
81 #if pathLength >= len(self.tailFactors) or pathLength <= 0:
83 #return self.tailFactors[pathLength]
84 #return 0.5 * (1.0 + pow(pathLength, 0.75))
85 return 1.0 / pathLength
88 def CalculateScore(self, attacker, defender, path):
89 p = move(attacker.x, attacker.y, path[0], 1)
90 if p[0] < 0 or p[0] >= len(self.board) or p[1] < 0 or p[1] >= len(self.board[p[0]]):
96 prob = self.rankProbability(defender, rank)
98 #sys.stderr.write(" " + str(attacker.rank) + " vs. " + str(rank) + " [" + str(prob) + "] score " + str(self.combatScore(attacker.rank, rank, len(path))) + "\n")
99 total += prob * self.combatScore(attacker.rank, rank, len(path))
104 # total = total / count + self.riskScore(attacker.rank)
107 total = total * self.tailFactor(len(path))
108 #HACK - Prevent "oscillating" by decreasing the value of backtracks
109 if len(path) > 1 and len(attacker.positions) > 1 and attacker.positions[1][0] == p[0] and attacker.positions[1][1] == p[1]:
111 #sys.stderr.write("Total score for " + str(attacker) + " vs. " + str(defender) + " is " + str(total) + "\n")
114 def combatScore(self, attackerRank, defenderRank, pathLength):
115 if defenderRank == 'F':
117 elif defenderRank == 'B':
118 return self.bombScore(attackerRank)
119 elif defenderRank == 's' and attackerRank == '1' and pathLength == 2:
120 return self.suicideScore(attackerRank)
121 elif defenderRank == '1' and attackerRank == 's' and pathLength != 2:
122 return self.killScore(attackerRank)
124 if valuedRank(attackerRank) > valuedRank(defenderRank):
125 return self.killScore(defenderRank)
126 elif valuedRank(attackerRank) < valuedRank(defenderRank):
127 return self.suicideScore(attackerRank)
128 return self.killScore(defenderRank) + self.suicideScore(attackerRank)
130 def killScore(self, defenderRank):
131 return self.killScores[defenderRank]
133 def bombScore(self, attackerRank):
134 if attackerRank == '8':
137 return self.suicideScore(attackerRank)
139 def suicideScore(self, attackerRank):
140 return self.suicideScores[attackerRank]
142 def riskScore(self, attackerRank):
143 return self.riskScores[attackerRank]
145 def rankProbability(self, target, targetRank):
146 if targetRank == '+' or targetRank == '?':
148 if target.rank == targetRank:
150 elif target.rank != '?':
155 if rank == '+' or rank == '?':
157 elif rank == 'F' or rank == 'B':
158 if target.lastMoved < 0:
159 total += self.hiddenEnemies[rank]
161 total += self.hiddenEnemies[rank]
165 return float(float(self.hiddenEnemies[targetRank]) / float(total))
173 if __name__ == "__main__":
176 while vixen.MoveCycle():