-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspy.py
More file actions
148 lines (131 loc) · 6.66 KB
/
Copy pathspy.py
File metadata and controls
148 lines (131 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import random
#"Guess" at a good defense baseline, trying to assign 1.8 soldiers per victory point.
baseline = [0, 2, 4, 6, 7, 9, 11, 13, 14, 16, 18]
# Generated from massScore(createAlternates()), the "winning" defense
trueBestDefense = [0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
# Generated from calculateWinOptions().
# Saved here for ease.
win_options = [[0, 0, 0, 0, 0, 0, 0, 0.5, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0.5, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0.5, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0.5, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0.5, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0.5, 0, 1, 0, 1, 1], [0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1], [0, 0, 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1, 1, 1, 0, 1, 0, 1, 0, 0, 1], [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]]
def createAlternates():
possibilities = [baseline];
for castle in range(1, 10):
newPossibilities = []
for modification in xrange(-2, 3):
for possibility in possibilities:
newStrength = possibility[castle] + modification
if possibility[castle - 1] <= newStrength:
npossibility = list(possibility)
npossibility[castle] = newStrength
newPossibilities.append(npossibility)
possibilities = newPossibilities
print castle, len(possibilities)
# Just add up to 100 for the final castle guard
newPossibilities = []
for possibility in possibilities:
possibility[10] = 100 - sum(possibility[:-1])
newPossibilities.append(possibility)
print "created: ", len(newPossibilities)
#a = random.choice(newPossibilities)
#b = random.choice(newPossibilities)
#print a, sum(a)
#print b, sum(b)
return newPossibilities
def massScore(defenses):
print "Mass scoring: ", len(defenses)
bestScore = 0
winners = []
for defense in defenses:
score = scoreDefense(defense)
if bestScore < score:
bestScore = score
winners = [defense]
elif bestScore == score:
winners.append(defense)
print len(winners)
print bestScore
a = random.choice(winners)
b = random.choice(winners)
c = random.choice(winners)
print a, b, c
def scoreDefense(defense):
minSoldiers = 100
winningOptions = []
for win_option in win_options:
totalSoldiers = 0
for index in xrange(1, 11):
if win_option[index] == 1:
totalSoldiers += defense[index] + 1
elif win_option[index] == 0.5:
totalSoldiers += defense[index]
if totalSoldiers < minSoldiers:
minSoldiers = totalSoldiers
winningOptions = [win_option]
elif totalSoldiers == minSoldiers:
winningOptions.append(win_option)
print minSoldiers
print len(winningOptions)
return minSoldiers
def calculateScore(poss):
score = 0
for a in xrange(len(poss)):
score += poss[a] * a
return score
def hasTie(poss):
return 0.5 in poss
def hasWin(poss):
return 1 in poss
def canBeReduced(poss):
posscopy = list(poss)
if hasTie(poss):
lowTie = poss.index(0.5)
posscopy[lowTie] = 0
else:
lowWin = poss.index(1)
posscopy[lowWin] = 0.5
# If we would win anyway despite dropping a bit,
# this win is "inefficient".
return calculateScore(posscopy) >= 28
def calculateWinOptions():
possibilities = [[0]];
for castle in range(1, 11):
newPossibilities = []
for possibility in possibilities:
alreadyHasTie = hasTie(possibility)
alreadyHasWin = hasWin(possibility)
score = calculateScore(possibility)
npossibility = list(possibility)
npossibility.append(0)
newPossibilities.append(npossibility)
if score < 28:
if not alreadyHasTie and not alreadyHasWin:
npossibility = list(possibility)
npossibility.append(0.5)
newPossibilities.append(npossibility)
score = calculateScore(npossibility)
if score < 28:
npossibility = list(possibility)
npossibility.append(1)
newPossibilities.append(npossibility)
else:
npossibility = list(possibility)
npossibility.append(1)
newPossibilities.append(npossibility)
possibilities = newPossibilities
# filter out losses
filtered = [x for x in possibilities if calculateScore(x) >= 28]
# Filter out inefficient wins
irreduceable = [ x for x in filtered if not canBeReduced(x)]
irLen = len(irreduceable)
q = random.choice(irreduceable)
w = random.choice(irreduceable)
e = random.choice(irreduceable)
print q, calculateScore(q)
print w, calculateScore(w)
print e, calculateScore(e)
print fixedLen, irLen
print irreduceable
if __name__ == "__main__":
#calculateWinOptions()
#scoreDefense(baseline)
scoreDefense(trueBestDefense)
#massScore(createAlternates())