-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLController.java
78 lines (63 loc) · 1.8 KB
/
RLController.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
/* module for cat and mouse game. used by applet, very specific parameters
used by cat and mouse game applet. controls RLearner. */
import javax.swing.*;
public class RLController extends Thread {
public RLearner learner;
SwingApplet a;
public int epochswaiting = 0, epochsdone = 0, totaldone = 0;
long delay;
int UPDATE_EPOCHS = 100;
public boolean newInfo;
public RLController(SwingApplet a, RLWorld world, long waitperiod) {
// create RLearner
learner = new RLearner(world);
this.a = a;
delay = waitperiod;
}
public void run() {
try {
while(true) {
if (epochswaiting > 0) {
System.out.println("Running "+epochswaiting+" epochs");
learner.running = true;
while(epochswaiting > 0) {
epochswaiting--;
epochsdone++;
learner.runEpoch();
if (epochswaiting % UPDATE_EPOCHS == 0)
SwingUtilities.invokeLater(a);
}
totaldone += epochsdone;
epochsdone = 0;
learner.running = false;
newInfo = true;
// inform applet we're finished
SwingUtilities.invokeLater(a);
}
sleep(delay);
}
} catch (InterruptedException e) {
System.out.println("Controller interrupted.");
}
}
public void setEpisodes(int episodes) {
System.out.println("Setting "+episodes+" episodes");
this.epochswaiting += episodes;
}
public void stopLearner() {
System.out.println("Stopping learner.");
newInfo = false;
epochswaiting = 0;
totaldone += epochsdone;
epochsdone = 0;
// inform applet we're finished
SwingUtilities.invokeLater(a);
learner.running = false;
}
public synchronized RLPolicy resetLearner() {
totaldone = 0;
epochsdone = 0;
epochswaiting = 0;
return learner.newPolicy();
}
}