-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalTweaker.java
131 lines (114 loc) · 4.2 KB
/
FinalTweaker.java
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
/*
* TDPSPlayer - a naive learned-function MCTS poker squares player
* Copyright (C) 2016 James Harris
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Random;
public class FinalTweaker {
public static void main(String[] args) throws ClassNotFoundException, IOException {
long seed = 3;
Random r = new Random(seed);
TDPlayer player = new TDPlayer();
String curBestFile = "Player_0.1_258000_73.962.dat";
//player.estimator = loadFromFile(curBestFile);
player.learnRate = 0.1;
//player.learnRate = 2.1544346900318823E-4;
double maxSoFar = Double.NEGATIVE_INFINITY;
int numTrials = 10000;
PokerSquaresPointSystem sys = PokerSquaresPointSystem.getAmericanPointSystem();
String curFile = "start.dat";
saveToFile(player.estimator, curFile);
for (long trial = 0; true ;trial += numTrials) {
System.out.printf("%16d %.17f", trial, player.learnRate);
double lR = player.learnRate;
double sameMean = runNGames(r, sys, player, numTrials);
BPANNE sameEst = player.estimator;
System.out.printf(" %.16f", sameMean);
player.estimator = loadFromFile(curFile);
player.learnRate = lR * 2;
double addMean = runNGames(r, sys, player, numTrials);
BPANNE addEst = player.estimator;
System.out.printf(" %.16f", addMean);
player.estimator = loadFromFile(curFile);
player.learnRate = lR / 10;
double subMean = runNGames(r, sys, player, numTrials);
BPANNE subEst = player.estimator;
System.out.printf(" %.16f", subMean);
double mean;
if (addMean > sameMean && addMean > subMean) {
player.learnRate = lR * 1.5;
player.estimator = addEst;
mean = addMean;
} else if (subMean > addMean && subMean > sameMean) {
player.learnRate = lR / 2;
player.estimator = subEst;
mean = subMean;
} else {
player.learnRate = lR;
player.estimator = sameEst;
mean = sameMean;
}
System.out.printf(" %.16f\n", mean);
if (mean > maxSoFar) {
maxSoFar = mean;
saveToFile(player.estimator, "best_" + maxSoFar + "_" + player.learnRate + "_" + trial + ".dat");
}
curFile = mean + "_" + player.learnRate + "_" + trial + ".dat";
saveToFile(player.estimator, curFile);
}
}
private static double runNGames(Random r, PokerSquaresPointSystem sys, TDPlayer player, int numTrials) {
long sum = 0;
PokerSquares ps = new PokerSquares(player, sys);
for (int j = 0; j < numTrials; j++) {
ps.verbose = false;
ps.setSeed(r.nextLong());
sum += ps.play();
}
double mean = sum / (double) numTrials;
return mean;
}
public static void saveToFile(Serializable s, String filename) {
try
{
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(s);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
public static BPANNE loadFromFile(String filename) throws IOException, ClassNotFoundException {
FileInputStream fileIn = null;
ObjectInputStream in = null;
try
{
fileIn = new FileInputStream(filename);
in = new ObjectInputStream(fileIn);
return (BPANNE) in.readObject();
} finally {
if (in != null)
in.close();
if (fileIn != null)
fileIn.close();
}
}
}