Fixed bug in simulate.py + hacky fix for bug in vixen
[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 progcomp@ucc.asn.au or matches@ucc.asn.au
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                 #sys.stderr.write("Vixen initialised...\n")
26                 BasicAI.__init__(self)
27                 
28                 
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}
34
35
36
37                         
38
39         def MakeMove(self):
40                 #sys.stderr.write("Vixen MakingMove...\n")
41                 " Over-rides the default BasicAI.MakeMove function "
42
43                 moveList = []
44                 for unit in self.units:
45                         if unit.mobile() == False:
46                                 continue
47
48                         scores = {"LEFT":0, "RIGHT":0, "UP":0, "DOWN":0}
49                         
50
51                         for target in self.enemyUnits:
52                                 if target == unit:
53                                         continue
54                                 path = PathFinder().pathFind((unit.x, unit.y), (target.x, target.y), self.board)
55                                 if path == False or len(path) == 0:
56                                         continue
57                                 #moveList.append({"unit":unit, "direction":path[0], "score":self.CalculateScore(unit, target, path)})
58                                 scores[path[0]] += self.CalculateScore(unit, target, path)
59
60                         bestScore = sorted(scores.items(), key = lambda e : e[1], reverse=True)[0]
61                         moveList.append({"unit":unit, "direction":bestScore[0], "score":bestScore[1]})
62                         
63
64                 if len(moveList) <= 0:
65                         print "NO_MOVE"
66                         return True
67
68                 moveList.sort(key = lambda e : e["score"], reverse=True)
69                 #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")
70                 #if moveList[0]["score"] == 0:
71                 #       print "NO_MOVE"
72                 #       return True
73
74                 
75                 print str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"]
76                 return True
77                                 
78                         
79         def tailFactor(self, pathLength):
80                 #if pathLength >= len(self.tailFactors) or pathLength <= 0:
81                 #       return 0.0
82                 #return self.tailFactors[pathLength]
83                 #return 0.5 * (1.0 + pow(pathLength, 0.75))
84                 return 1.0 / pathLength
85
86
87         def CalculateScore(self, attacker, defender, path):
88                 p = move(attacker.x, attacker.y, path[0], 1)
89                 if p[0] < 0 or p[0] >= len(self.board) or p[1] < 0 or p[1] >= len(self.board[p[0]]):
90                         return -100.0
91
92                 total = 0.0
93                 count = 0.0
94                 for rank in ranks:
95                         prob = self.rankProbability(defender, rank)                     
96                         if prob > 0.0:
97                                 #sys.stderr.write("     " + str(attacker.rank) + " vs. " + str(rank) + " [" + str(prob) + "] score " + str(self.combatScore(attacker.rank, rank, len(path))) + "\n")
98                                 total += prob * self.combatScore(attacker.rank, rank, len(path))
99                                 count += 1
100                                 
101                 
102                 #if count > 1:
103                 #       total = total / count + self.riskScore(attacker.rank)
104
105
106                 total = total * self.tailFactor(len(path))
107                 #HACK - Prevent "oscillating" by decreasing the value of backtracks
108                 if len(path) > 1 and len(attacker.positions) > 1 and attacker.positions[1][0] == p[0] and attacker.positions[1][1] == p[1]:
109                         total = total / 100
110                 #sys.stderr.write("Total score for " + str(attacker) + " vs. " + str(defender) + " is " + str(total) + "\n")
111                 return total
112
113         def combatScore(self, attackerRank, defenderRank, pathLength):
114                 if defenderRank == 'F':
115                         return 1.0
116                 elif defenderRank == 'B':
117                         return self.bombScore(attackerRank)
118                 elif defenderRank == 's' and attackerRank == '1' and pathLength == 2:
119                         return self.suicideScore(attackerRank)
120                 elif defenderRank == '1' and attackerRank == 's' and pathLength != 2:
121                         return self.killScore(attackerRank)
122
123                 if valuedRank(attackerRank) > valuedRank(defenderRank):
124                         return self.killScore(defenderRank)
125                 elif valuedRank(attackerRank) < valuedRank(defenderRank):
126                         return self.suicideScore(attackerRank)
127                 return self.killScore(defenderRank) + self.suicideScore(attackerRank)
128
129         def killScore(self, defenderRank):
130                 return self.killScores[defenderRank]
131
132         def bombScore(self, attackerRank):
133                 if attackerRank == '8':
134                         return 1.0
135                 else:
136                         return self.suicideScore(attackerRank)
137
138         def suicideScore(self, attackerRank):
139                 return self.suicideScores[attackerRank]
140
141         def riskScore(self, attackerRank):
142                 return self.riskScores[attackerRank]
143
144         def rankProbability(self, target, targetRank):
145                 if targetRank == '+' or targetRank == '?':
146                         return 0.0
147                 if target.rank == targetRank:
148                         return 1.0
149                 elif target.rank != '?':
150                         return 0.0
151
152                 total = 0.0
153                 for rank in ranks:
154                         if rank == '+' or rank == '?':
155                                 continue
156                         elif rank == 'F' or rank == 'B':
157                                 if target.lastMoved < 0:
158                                         total += self.hiddenEnemies[rank]
159                         else:
160                                 total += self.hiddenEnemies[rank]
161
162                 if total == 0.0:
163                         return 0.0
164                 return float(float(self.hiddenEnemies[targetRank]) / float(total))
165         
166
167         
168
169                                 
170                                 
171                 
172 if __name__ == "__main__":
173         vixen = Vixen()
174         if vixen.Setup():
175                 while vixen.MoveCycle():
176                         pass
177

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