distinguish learning and episodic learning, enable fast-learning without drawing every step to reduce lag

- repainting every step on no time delay will certainly freeze the app, so "fast-learning" will disable it, only refreshing current episode label
- Added new abstract class "Episodic Learning". Maybe just use an interface instead?! Important because TD learning is not episodic, needs another way to represent the rewards received (maybe mean of last X rewards or sth)
- Opening two JFrames, one with learning infos and one with environment
This commit is contained in:
2019-12-21 00:23:09 +01:00
parent 7db5a2af3b
commit 34e7e3fdd6
14 changed files with 188 additions and 75 deletions
+5
View File
@@ -0,0 +1,5 @@
package core.algo;
public interface Episodic {
int getCurrentEpisode();
}
@@ -0,0 +1,29 @@
package core.algo;
import core.DiscreteActionSpace;
import core.Environment;
public abstract class EpisodicLearning<A extends Enum> extends Learning<A> implements Episodic{
protected int currentEpisode;
public EpisodicLearning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, int delay) {
super(environment, actionSpace, discountFactor, delay);
}
public EpisodicLearning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor) {
super(environment, actionSpace, discountFactor);
}
public EpisodicLearning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, int delay) {
super(environment, actionSpace, delay);
}
public EpisodicLearning(Environment<A> environment, DiscreteActionSpace<A> actionSpace) {
super(environment, actionSpace);
}
@Override
public int getCurrentEpisode(){
return currentEpisode;
}
}
+6 -2
View File
@@ -9,8 +9,6 @@ import core.policy.Policy;
import lombok.Getter;
import lombok.Setter;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -68,4 +66,10 @@ public abstract class Learning<A extends Enum> {
l.onEpisodeStart();
}
}
protected void dispatchStepEnd(){
for(LearningListener l: learningListeners){
l.onStepEnd();
}
}
}
@@ -1,10 +1,9 @@
package core.algo.mc;
import core.*;
import core.algo.Learning;
import core.algo.EpisodicLearning;
import core.policy.EpsilonGreedyPolicy;
import javafx.util.Pair;
import lombok.Setter;
import java.util.*;
@@ -26,11 +25,11 @@ import java.util.*;
* How to encounter this problem?
* @param <A>
*/
public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
public class MonteCarloOnPolicyEGreedy<A extends Enum> extends EpisodicLearning<A> {
public MonteCarloOnPolicyEGreedy(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, float epsilon, int delay) {
super(environment, actionSpace, discountFactor, delay);
currentEpisode = 0;
this.policy = new EpsilonGreedyPolicy<>(epsilon);
this.stateActionTable = new StateActionHashTable<>(this.actionSpace);
}
@@ -47,8 +46,15 @@ public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
Map<Pair<State, A>, Integer> returnCount = new HashMap<>();
for(int i = 0; i < nrOfEpisodes; ++i) {
++currentEpisode;
List<StepResult<A>> episode = new ArrayList<>();
State state = environment.reset();
dispatchEpisodeStart();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
double sumOfRewards = 0;
for(int j=0; j < 10; ++j){
Map<A, Double> actionValues = stateActionTable.getActionValues(state);
@@ -67,6 +73,7 @@ public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
} catch (InterruptedException e) {
e.printStackTrace();
}
dispatchStepEnd();
}
dispatchEpisodeEnd(sumOfRewards);
@@ -100,4 +107,9 @@ public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
}
}
}
@Override
public int getCurrentEpisode() {
return currentEpisode;
}
}