add Random-, Greedy and EGreedy-Policy and first implementation of monte carlo method
- fixed bug regarding wrong generation of hashCode. hashCodes needs to be equal across equal objects. Compute hashCode on final states once and return this value instead of computing it every time .hashCode() gets called. -
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package core.policy;
|
||||
|
||||
import core.RNG;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* To prevent the agent from getting stuck only using the "best" action
|
||||
* according to the current learning history, this policy
|
||||
* will take random action with the probability of epsilon.
|
||||
* (random action space includes the best action as well)
|
||||
*
|
||||
* @param <A> Discrete Action Enum
|
||||
*/
|
||||
public class EpsilonGreedyPolicy<A extends Enum> implements Policy<A>{
|
||||
private float epsilon;
|
||||
private RandomPolicy<A> randomPolicy;
|
||||
private GreedyPolicy<A> greedyPolicy;
|
||||
|
||||
public EpsilonGreedyPolicy(float epsilon){
|
||||
this.epsilon = epsilon;
|
||||
randomPolicy = new RandomPolicy<>();
|
||||
greedyPolicy = new GreedyPolicy<>();
|
||||
}
|
||||
@Override
|
||||
public A chooseAction(Map<A, Double> actionValues) {
|
||||
if(RNG.getRandom().nextFloat() < epsilon){
|
||||
// Take random action
|
||||
return randomPolicy.chooseAction(actionValues);
|
||||
}else{
|
||||
// Take the action with the highest value
|
||||
return greedyPolicy.chooseAction(actionValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package core.policy;
|
||||
|
||||
import core.RNG;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GreedyPolicy<A extends Enum> implements Policy<A> {
|
||||
|
||||
@Override
|
||||
public A chooseAction(Map<A, Double> actionValues) {
|
||||
if(actionValues.size() == 0) throw new RuntimeException("Empty actionActionValues set");
|
||||
|
||||
Double highestValueAction = null;
|
||||
|
||||
List<A> equalHigh = new ArrayList<>();
|
||||
|
||||
for(Map.Entry<A, Double> actionValue : actionValues.entrySet()){
|
||||
System.out.println(actionValue.getKey()+ " " + actionValue.getValue() );
|
||||
if(highestValueAction == null || highestValueAction < actionValue.getValue()){
|
||||
highestValueAction = actionValue.getValue();
|
||||
equalHigh.clear();
|
||||
equalHigh.add(actionValue.getKey());
|
||||
}else if(highestValueAction.equals(actionValue.getValue())){
|
||||
equalHigh.add(actionValue.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
return equalHigh.get(RNG.getRandom().nextInt(equalHigh.size()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package core.policy;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface Policy<A extends Enum> {
|
||||
A chooseAction(Map<A, Double> actionValues);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package core.policy;
|
||||
|
||||
import core.RNG;
|
||||
import java.util.Map;
|
||||
|
||||
public class RandomPolicy<A extends Enum> implements Policy<A>{
|
||||
@Override
|
||||
public A chooseAction(Map<A, Double> actionValues) {
|
||||
int idx = RNG.getRandom().nextInt(actionValues.size());
|
||||
int i = 0;
|
||||
for(A action : actionValues.keySet()){
|
||||
if(i++ == idx) return action;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user