e4dee3ff609d77cd6993cd41e4f71ea06111ef5b
[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                 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":0, "RIGHT":0, "UP":0, "DOWN":0}
48                         
49
50                         for target in self.enemyUnits:
51                                 if target == unit:
52                                         continue
53                                 path = PathFinder().pathFind((unit.x, unit.y), (target.x, target.y), self.board)
54                                 if path == False or len(path) == 0:
55                                         continue
56                                 #moveList.append({"unit":unit, "direction":path[0], "score":self.CalculateScore(unit, target, path)})
57                                 scores[path[0]] += self.CalculateScore(unit, target, path)
58
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]})
62                         
63                         
64
65                 if len(moveList) <= 0:
66                         print "NO_MOVE"
67                         return True
68
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:
72                 #       print "NO_MOVE"
73                 #       return True
74
75                 
76                 print str(moveList[0]["unit"].x) + " " + str(moveList[0]["unit"].y) + " " + moveList[0]["direction"]
77                 return True
78                                 
79                         
80         def tailFactor(self, pathLength):
81                 #if pathLength >= len(self.tailFactors) or pathLength <= 0:
82                 #       return 0.0
83                 #return self.tailFactors[pathLength]
84                 #return 0.5 * (1.0 + pow(pathLength, 0.75))
85                 return 1.0 / pathLength
86
87
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]]):
91                         return -1000.0
92
93                 total = 0.0
94                 count = 0.0
95                 for rank in ranks:
96                         prob = self.rankProbability(defender, rank)                     
97                         if prob > 0.0:
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))
100                                 count += 1
101                                 
102                 
103                 #if count > 1:
104                 #       total = total / count + self.riskScore(attacker.rank)
105
106
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]:
110                         total = total / 100
111                 #sys.stderr.write("Total score for " + str(attacker) + " vs. " + str(defender) + " is " + str(total) + "\n")
112                 return total
113
114         def combatScore(self, attackerRank, defenderRank, pathLength):
115                 if defenderRank == 'F':
116                         return 1.0
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)
123
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)
129
130         def killScore(self, defenderRank):
131                 return self.killScores[defenderRank]
132
133         def bombScore(self, attackerRank):
134                 if attackerRank == '8':
135                         return 1.0
136                 else:
137                         return self.suicideScore(attackerRank)
138
139         def suicideScore(self, attackerRank):
140                 return self.suicideScores[attackerRank]
141
142         def riskScore(self, attackerRank):
143                 return self.riskScores[attackerRank]
144
145         def rankProbability(self, target, targetRank):
146                 if targetRank == '+' or targetRank == '?':
147                         return 0.0
148                 if target.rank == targetRank:
149                         return 1.0
150                 elif target.rank != '?':
151                         return 0.0
152
153                 total = 0.0
154                 for rank in ranks:
155                         if rank == '+' or rank == '?':
156                                 continue
157                         elif rank == 'F' or rank == 'B':
158                                 if target.lastMoved < 0:
159                                         total += self.hiddenEnemies[rank]
160                         else:
161                                 total += self.hiddenEnemies[rank]
162
163                 if total == 0.0:
164                         return 0.0
165                 return float(float(self.hiddenEnemies[targetRank]) / float(total))
166         
167
168         
169
170                                 
171                                 
172                 
173 if __name__ == "__main__":
174         vixen = Vixen()
175         if vixen.Setup():
176                 while vixen.MoveCycle():
177                         pass
178

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