adopt MVC pattern and add real time graph interface
This commit is contained in:
@@ -2,26 +2,62 @@ package core.algo;
|
||||
|
||||
import core.DiscreteActionSpace;
|
||||
import core.Environment;
|
||||
import core.LearningConfig;
|
||||
import core.StateActionTable;
|
||||
import core.listener.LearningListener;
|
||||
import core.policy.Policy;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
public abstract class Learning<A extends Enum> {
|
||||
protected Policy<A> policy;
|
||||
protected DiscreteActionSpace<A> actionSpace;
|
||||
protected StateActionTable<A> stateActionTable;
|
||||
protected Environment<A> environment;
|
||||
protected float discountFactor;
|
||||
@Setter
|
||||
protected float epsilon;
|
||||
protected Set<LearningListener> learningListeners;
|
||||
@Setter
|
||||
protected int delay;
|
||||
|
||||
public Learning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, float epsilon){
|
||||
public Learning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, float epsilon, int delay){
|
||||
this.environment = environment;
|
||||
this.actionSpace = actionSpace;
|
||||
this.discountFactor = discountFactor;
|
||||
this.epsilon = epsilon;
|
||||
}
|
||||
public Learning(Environment<A> environment, DiscreteActionSpace<A> actionSpace){
|
||||
this(environment, actionSpace, 1.0f, 0.1f);
|
||||
this.delay = delay;
|
||||
learningListeners = new HashSet<>();
|
||||
}
|
||||
|
||||
public abstract void learn(int nrOfEpisodes, int delay);
|
||||
public Learning(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, float epsilon){
|
||||
this(environment, actionSpace, discountFactor, epsilon, LearningConfig.DEFAULT_DELAY);
|
||||
}
|
||||
|
||||
public Learning(Environment<A> environment, DiscreteActionSpace<A> actionSpace){
|
||||
this(environment, actionSpace, LearningConfig.DEFAULT_DISCOUNT_FACTOR, LearningConfig.DEFAULT_EPSILON, LearningConfig.DEFAULT_DELAY);
|
||||
}
|
||||
|
||||
public abstract void learn(int nrOfEpisodes);
|
||||
|
||||
public void addListener(LearningListener learningListener){
|
||||
learningListeners.add(learningListener);
|
||||
}
|
||||
|
||||
protected void dispatchEpisodeEnd(double sum){
|
||||
for(LearningListener l: learningListeners) {
|
||||
l.onEpisodeEnd(sum);
|
||||
}
|
||||
}
|
||||
|
||||
protected void dispatchEpisodeStart(){
|
||||
for(LearningListener l: learningListeners){
|
||||
l.onEpisodeStart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,22 +34,21 @@ public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void learn(int nrOfEpisodes, int delay) {
|
||||
public void learn(int nrOfEpisodes) {
|
||||
|
||||
Map<Pair<State, A>, Double> returnSum = new HashMap<>();
|
||||
Map<Pair<State, A>, Integer> returnCount = new HashMap<>();
|
||||
|
||||
State startingState = environment.reset();
|
||||
for(int i = 0; i < nrOfEpisodes; ++i) {
|
||||
List<StepResult<A>> episode = new ArrayList<>();
|
||||
State state = environment.reset();
|
||||
double rewardSum = 0;
|
||||
double sumOfRewards = 0;
|
||||
for(int j=0; j < 10; ++j){
|
||||
Map<A, Double> actionValues = stateActionTable.getActionValues(state);
|
||||
A chosenAction = policy.chooseAction(actionValues);
|
||||
StepResultEnvironment envResult = environment.step(chosenAction);
|
||||
State nextState = envResult.getState();
|
||||
rewardSum += envResult.getReward();
|
||||
sumOfRewards += envResult.getReward();
|
||||
episode.add(new StepResult<>(state, chosenAction, envResult.getReward()));
|
||||
|
||||
if(envResult.isDone()) break;
|
||||
@@ -57,13 +56,14 @@ public class MonteCarloOnPolicyEGreedy<A extends Enum> extends Learning<A> {
|
||||
state = nextState;
|
||||
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.printf("Episode %d \t Reward: %f \n", i, rewardSum);
|
||||
dispatchEpisodeEnd(sumOfRewards);
|
||||
System.out.printf("Episode %d \t Reward: %f \n", i, sumOfRewards);
|
||||
Set<Pair<State, A>> stateActionPairs = new HashSet<>();
|
||||
|
||||
for(StepResult<A> sr: episode){
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package core.algo;
|
||||
|
||||
public enum Method {
|
||||
MC_ONPOLICY_EGREEDY, TD_ONPOLICY
|
||||
}
|
||||
Reference in New Issue
Block a user