-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightLearner.java
81 lines (65 loc) · 2.06 KB
/
WeightLearner.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
import java.util.Arrays;
public class WeightLearner {
public static final int NUMHEURISTICS = 4;
public static double delta = 0.1;
// Returns an array of vectors where changes have been applied onto each weight
public static double[][] generateNewVectors(double[][] newVectors, double[] vector, boolean reduceDelta) {
double d = calculateDelta(reduceDelta);
for (int i = 0; i < NUMHEURISTICS*2; i++) {
double[] newVector = new double[NUMHEURISTICS];
// Deduct instead of add
if (i == NUMHEURISTICS) {
d *= -1;
}
for (int j = 0; j < NUMHEURISTICS; j++) {
// Change a single weight
if (i % NUMHEURISTICS == j) {
newVector[j] = vector[j] + d;
} else {
newVector[j] = vector[j];
}
}
newVectors[i] = newVector;
}
return newVectors;
}
// Reduce delta by half
public static double calculateDelta(boolean reduceDelta) {
if (reduceDelta) {
delta /= 2;
}
return delta;
}
public static final double randomMin = -1;
public static final double randomMax = 1;
// Generates a random vector to run the checks
public static double[] generateRandomVector() {
double[] vector = new double[NUMHEURISTICS];
for (int i = 0; i < NUMHEURISTICS; i++) {
vector[i] = randomMin + (randomMax-randomMin)*Math.random();
}
return vector;
}
public static void main(String[] args) {
boolean continueClimb = true;
boolean reduceDelta = false;
double[] randomVector = generateRandomVector();
double[][] newVectors = new double[NUMHEURISTICS*2][NUMHEURISTICS];
generateNewVectors(newVectors, randomVector, reduceDelta);
int bestVector = 0;
int bestScore = Integer.MIN_VALUE;
while(continueClimb) {
for (int i = 0; i < NUMHEURISTICS*2; i++) {
PlayerSkeleton p = new PlayerSkeleton(newVectors[i]);
int score = p.playGame();
if (bestScore < score) {
bestVector = i;
}
System.out.println("Score: " + score);
}
double[] bestVec = newVectors[bestVector].clone();
System.out.println(Arrays.toString(bestVec));
generateNewVectors(newVectors, bestVec, reduceDelta);
}
}
}