apply code improvements suggested by intelliJ

This commit is contained in:
2020-04-05 14:44:48 +02:00
parent 94ad976a1f
commit 9d1f8dfd46
15 changed files with 45 additions and 66 deletions
@@ -1,7 +1,10 @@
package core;
import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Implementation of a discrete action space.
@@ -18,6 +21,7 @@ public class ListDiscreteActionSpace<A extends Enum> implements DiscreteActionSp
actions = new ArrayList<>();
}
@SafeVarargs
public ListDiscreteActionSpace(A... actions){
this.actions = new ArrayList<>(Arrays.asList(actions));
}
@@ -21,7 +21,7 @@ public class MonteCarloControlEGreedy<A extends Enum> extends EpisodicLearning<A
private Map<Pair<State, A>, Double> returnSum;
private Map<Pair<State, A>, Integer> returnCount;
private boolean isEveryVisit;
private final boolean isEveryVisit;
public MonteCarloControlEGreedy(Environment<A> environment, DiscreteActionSpace<A> actionSpace, float discountFactor, float epsilon, int delay, boolean useEveryVisit) {
@@ -34,7 +34,7 @@ public class QLearningOffPolicyTDControl<A extends Enum> extends EpisodicLearnin
}
StepResultEnvironment envResult = null;
Map<A, Double> actionValues = null;
Map<A, Double> actionValues;
sumOfRewards = 0;
while(envResult == null || !envResult.isDone()) {
@@ -39,6 +39,7 @@ public class RLController<A extends Enum> implements LearningListener {
protected int prevDelay;
protected volatile boolean printNextEpisode;
@SafeVarargs
public RLController(Environment<A> env, Method method, A... actions) {
setEnvironment(env);
setMethod(method);
@@ -102,9 +103,7 @@ public class RLController<A extends Enum> implements LearningListener {
if(learning.isCurrentlyLearning()){
((EpisodicLearning<A>) learning).learnMoreEpisodes(nrOfEpisodes);
}else{
new Thread(() -> {
((EpisodicLearning<A>) learning).learn(nrOfEpisodes);
}).start();
new Thread(() -> ((EpisodicLearning<A>) learning).learn(nrOfEpisodes)).start();
}
} else {
throw new RuntimeException("Triggering onLearnMoreEpisodes on non-episodic learning!");
@@ -179,7 +178,7 @@ public class RLController<A extends Enum> implements LearningListener {
public void onEpisodeEnd(List<Double> rewardHistory) {
latestRewardsHistory = rewardHistory;
if(printNextEpisode) {
System.out.println("Episode " + ((EpisodicLearning<A>) learning).getCurrentEpisode() + " Latest Reward: " + rewardHistory.get(rewardHistory.size() - 1));
System.out.println("Episode " + learning.getCurrentEpisode() + " Latest Reward: " + rewardHistory.get(rewardHistory.size() - 1));
System.out.println("Eps/sec: " + ((EpisodicLearning<A>) learning).getEpisodePerSecond());
printNextEpisode = false;
}
@@ -13,6 +13,7 @@ import java.util.List;
public class RLControllerGUI<A extends Enum> extends RLController<A> implements ViewListener {
private LearningView learningView;
@SafeVarargs
public RLControllerGUI(Environment<A> env, Method method, A... actions) {
super(env, method, actions);
}
@@ -102,7 +103,7 @@ public class RLControllerGUI<A extends Enum> extends RLController<A> implements
@Override
public void onLearningEnd() {
super.onLearningEnd();
onSaveState(method.toString() + System.currentTimeMillis() / 1000 + (learning instanceof EpisodicLearning ? "e" + ((EpisodicLearning) learning).getCurrentEpisode() : ""));
onSaveState(method.toString() + System.currentTimeMillis() / 1000 + (learning instanceof EpisodicLearning ? "e" + learning.getCurrentEpisode() : ""));
SwingUtilities.invokeLater(() -> learningView.updateRewardGraph(latestRewardsHistory));
}
}
@@ -85,9 +85,7 @@ public class LearningInfoPanel extends JPanel {
add(learnMoreEpisodesButton);
}
showQTableButton = new JButton("Show Q-Table");
showQTableButton.addActionListener(e -> {
viewListener.onShowQTable();
});
showQTableButton.addActionListener(e -> viewListener.onShowQTable());
add(drawEnvironmentCheckbox);
add(smoothGraphCheckbox);
add(last100Checkbox);