669df8362057e136c6ff239991de6c432233d9d7
[progcomp2012.git] / agents / vixen / vixen.py
1 #!/usr/bin/python -u
2
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.
5
6 '''
7  vixen.py - A sample Stratego AI for the UCC Programming Competition 2012
8
9  Written in python, the slithery language 
10
11  author Sam Moore (matches) [SZM]
12  website http://matches.ucc.asn.au/stratego
13  email [email protected] or [email protected]
14  git git.ucc.asn.au/progcomp2012.git
15 '''
16
17 from basic_python import *
18 from path import *
19
20
21
22 class Vixen(BasicAI):
23         " Python based AI, improves upon Asmodeus by taking into account probabilities, and common paths "
24         def __init__(self):
25                 BasicAI.__init__(self)
26                 
27                 
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}
33
34
35
36                         
37
38         def MakeMove(self):
39                 #sys.stderr.write("Vixen MakingMove...\n")
40                 " Over-rides the default BasicAI.MakeMove function "
41
42                 moveList = []
43                 for unit in self.units:
44                         if unit.mobile() == False:
45                                 continue
46
47                         scores = {"LEFT":None, "RIGHT":None, "UP":None, "DOWN":None}
48                         
49                         for target in self.enemyUnits:
50                                 if target == unit:
51                                         continue
52                                 path = PathFinder().pathFind((unit.x, unit.y), (target.x, target.y), self.board)
53                                 if path == False or len(path) == 0:
54                                         continue
55                                 if scores[path[0]] == None:
56                                         scores[path[0]] = 0 
57
58                                 scores[path[0]] += self.CalculateScore(unit, target, path)
59
60                         for d in scores.keys():
61                                 if scores[d] == None:
62                                         del scores[d]
63
64                         if len(scores.items()) > 0: 
65                                 bestScore = sorted(scores.items(), key = lambda e : e[1], reverse=True)[0]
66                                 moveList.append({"unit":unit, "direction":bestScore[0], "score":bestScore[1]})
67                         
68                         
69
70                 if len(moveList) <= 0:
71                         print "NO_MOVE"
72                         return True
73
74                 moveList.sort(key = lambda e : e["score"], reverse=True)
75                 #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")
76                 #if moveList[0]["score"] == 0:
77                 #       print "NO_MOVE"
78                 #       return True
79
80                 
81                 print str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"]
82                 return True
83                                 
84                         
85         def tailFactor(self, pathLength):
86                 #if pathLength >= len(self.tailFactors) or pathLength <= 0:
87                 #       return 0.0
88                 #return self.tailFactors[pathLength]
89                 #return 0.5 * (1.0 + pow(pathLength, 0.75))
90                 return 1.0 / pathLength
91
92
93         def CalculateScore(self, attacker, defender, path):
94                 p = move(attacker.x, attacker.y, path[0], 1)
95                 if p[0] < 0 or p[0] >= len(self.board) or p[1] < 0 or p[1] >= len(self.board[p[0]]):
96                         return -1000.0
97
98                 total = 0.0
99                 count = 0.0
100                 for rank in ranks:
101                         prob = self.rankProbability(defender, rank)                     
102                         if prob > 0.0:
103                                 #sys.stderr.write("     " + str(attacker.rank) + " vs. " + str(rank) + " [" + str(prob) + "] score " + str(self.combatScore(attacker.rank, rank, len(path))) + "\n")
104                                 total += prob * self.combatScore(attacker.rank, rank, len(path))
105                                 count += 1
106                                 
107                 
108                 #if count > 1:
109                 #       total = total / count + self.riskScore(attacker.rank)
110
111
112                 total = total * self.tailFactor(len(path))
113                 #HACK - Prevent "oscillating" by decreasing the value of backtracks
114                 if len(path) > 1 and len(attacker.positions) > 1 and attacker.positions[1][0] == p[0] and attacker.positions[1][1] == p[1]:
115                         total = total / 100
116                 #sys.stderr.write("Total score for " + str(attacker) + " vs. " + str(defender) + " is " + str(total) + "\n")
117                 return total
118
119         def combatScore(self, attackerRank, defenderRank, pathLength):
120                 if defenderRank == 'F':
121                         return 1.0
122                 elif defenderRank == 'B':
123                         return self.bombScore(attackerRank)
124                 elif defenderRank == 's' and attackerRank == '1' and pathLength == 2:
125                         return self.suicideScore(attackerRank)
126                 elif defenderRank == '1' and attackerRank == 's' and pathLength != 2:
127                         return self.killScore(attackerRank)
128
129                 if valuedRank(attackerRank) > valuedRank(defenderRank):
130                         return self.killScore(defenderRank)
131                 elif valuedRank(attackerRank) < valuedRank(defenderRank):
132                         return self.suicideScore(attackerRank)
133                 return self.killScore(defenderRank) + self.suicideScore(attackerRank)
134
135         def killScore(self, defenderRank):
136                 return self.killScores[defenderRank]
137
138         def bombScore(self, attackerRank):
139                 if attackerRank == '8':
140                         return 1.0
141                 else:
142                         return self.suicideScore(attackerRank)
143
144         def suicideScore(self, attackerRank):
145                 return self.suicideScores[attackerRank]
146
147         def riskScore(self, attackerRank):
148                 return self.riskScores[attackerRank]
149
150         def rankProbability(self, target, targetRank):
151                 if targetRank == '+' or targetRank == '?':
152                         return 0.0
153                 if target.rank == targetRank:
154                         return 1.0
155                 elif target.rank != '?':
156                         return 0.0
157
158                 total = 0.0
159                 for rank in ranks:
160                         if rank == '+' or rank == '?':
161                                 continue
162                         elif rank == 'F' or rank == 'B':
163                                 if target.lastMoved < 0:
164                                         total += self.hiddenEnemies[rank]
165                         else:
166                                 total += self.hiddenEnemies[rank]
167
168                 if total == 0.0:
169                         return 0.0
170                 return float(float(self.hiddenEnemies[targetRank]) / float(total))
171         
172
173         
174
175                                 
176                                 
177                 
178 if __name__ == "__main__":
179         vixen = Vixen()
180         if vixen.Setup():
181                 while vixen.MoveCycle():
182                         pass
183

UCC git Repository :: git.ucc.asn.au