-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbc-calc-risk-odds.m
executable file
·127 lines (84 loc) · 3.24 KB
/
bc-calc-risk-odds.m
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
(* Risk (or conquerclub.com) w/ strategy one: assault until one of:
1. You win
2. You have fewer than 4 troops
*)
(* never attack with 3 troops or fewer *)
p[n_,m_] := 0 /; n<=3
(* if defense has no troops left, you've won *)
p[n_,0] := 1
(* if only one defender ... *)
p[n_,1] := 855/1296 + 441/1296*p[n-1,1]
(* chance of winning when defense has at least 2 courtesy
http://www.strategygamenetwork.com/statistics.html#q9 *)
p[n_,m_] := 2890/7776*p[n,m-2] + 2611/7776*p[n-1,m-1] + 2275/7776*p[n-2,m] /;
m>=2
(* However, the above assumes you will always auto-assault. If you
don't, you can stop assaulting when the odds turn against you. In
other words, strategy two is assault until one of:
1. You win
2. You have fewer than 4 troops
3. p[n,m] < .5
*)
(* Note: .5 could be .75 [or whatever] if you want to be safe *)
(* pf says you give up if odds are less than 50% *)
(* intentionally using soft equals here *)
pf[p_] = If[p<.5,0,p]
(* now the probabilities for this new strategy p1 *)
(* same base conditions *)
p1[n_,m_] := 0 /; n<=3
(* if defense has no troops left, you've won *)
p1[n_,0] := 1
(* if only one defender... *)
p1[n_,1] := 855/1296 + 441/1296*pf[p[n-1,1]]
(* and the basic case *)
p1[n_,m_] := 2890/7776*pf[p[n,m-2]] +
2611/7776*pf[p[n-1,m-1]] +
2275/7776*pf[p[n-2,m]] /; m>=2
(* of course, we're now using strategy 0's odds for strategy 1. This
leads us to ... strategy 2 *)
(* now the probabilities for this new strategy p2 *)
(* same base conditions *)
p2[n_,m_] := 0 /; n<=3
(* if defense has no troops left, you've won *)
p2[n_,0] := 1
(* if only one defender... *)
p2[n_,1] := 855/1296 + 441/1296*pf[p1[n-1,1]]
(* and the basic case *)
p2[n_,m_] := 2890/7776*pf[p1[n,m-2]] +
2611/7776*pf[p1[n-1,m-1]] +
2275/7776*pf[p1[n-2,m]] /; m>=2
(* of course, this leads us to strategy 3... ad infinitum *)
(* so we create q[x,n,m], the probability of winning with strategy x *)
(* base conditions from above *)
(* the foo := foo = something is a Mathematica efficiency thing *)
(* never attack with 3 troops or fewer *)
q[x_,n_,m_] := (q[x,n,m] = 0) /; n<=3
(* WAS: q[x_,n_,m_] := 0 /; n<=3 *)
(* FAIL: q[x_,n_,m_] := q[x,n,m] = 0 /; n<=3 *)
(* FAIL: (q[x_,n_,m_] := q[x,n,m] = 0) /; n<=3 *)
(* if defense has no troops left, you've won *)
q[x_,n_,0] := q[x,n,0] = 1
(* if only one defender ... *)
q[x_,n_,1] := q[x,n,1] = 855/1296 + 441/1296*pf[q[x-1,n-1,1]]
(* q0 is just the basic strategy *)
q[0,n_,m_] := q[0,n,m] = p[n,m]
(* general case *)
q[x_,n_,m_] := (q[x,n,m] = 2890/7776*pf[q[x-1,n,m-2]] +
2611/7776*pf[q[x-1,n-1,m-1]] +
2275/7776*pf[q[x-1,n-2,m]]) /; m>=2
(* the pointwise limit; really no reason for it to remember values
since it's not recursive *)
r[n_,m_] := r[n,m] = Module[{x=1},
While[q[x,n,m] != q[x-1,n,m], x++];
q[x,n,m]
]
(* My Mathematica has problems w/ graphics, so I must do this *)
showit := Module[{},
Export["/tmp/math.jpg",%, ImageSize->{800,600}]; Run["display /tmp/math.jpg&"]]
(*** JFF GRAPHING (want to avoid loading this every time)
ListPlot[Table[q[x,11,6],{x,0,20}], AxesOrigin->{0,0}, PlotJoined->True,
PlotRange -> All]
Table[q[10,n,m],{n,3,23},{m,1,23}]
Table[q[x,12,7],{x,1,20}]
Table[r[n,m],{n,4,10},{m,1,10}]
***)