add random policy test

This commit is contained in:
Jan Löwenstrom 2019-12-12 00:20:38 +01:00
parent 584d6a1246
commit 7f18a66e98
4 changed files with 62 additions and 1 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
.idea/refo.iml
.idea/misc.xml
.idea/modules.xml
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm

View File

@ -1,5 +1,6 @@
#Wed Dec 11 23:10:59 CET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip

View File

@ -0,0 +1,6 @@
public enum DummyAction {
UP,
DOWN,
RIGHT,
LEFT
}

View File

@ -0,0 +1,50 @@
import core.RNG;
import core.policy.RandomPolicy;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PolicyTest {
private Map<DummyAction, Double> createActionValueMap(){
Map<DummyAction, Double> actionValues = new HashMap<>();
actionValues.put(DummyAction.UP, 1d);
actionValues.put(DummyAction.DOWN, 2d);
actionValues.put(DummyAction.LEFT, 3d);
actionValues.put(DummyAction.RIGHT, 4d);
return actionValues;
}
/**
* Test if the random policy generates true random results
* based upon the static RNG class.
*/
@Test
public void RandomPolicyTest(){
RandomPolicy<DummyAction> randomPolicy = new RandomPolicy<>();
Map<DummyAction, Double> dummyActionValues = createActionValueMap();
// dummy action space to choose an action from
List<DummyAction> actionList = new ArrayList<>(dummyActionValues.keySet());
// list of 100 random selected actions in a row
List<DummyAction> chosenActions = new ArrayList<>();
// randomly selecting an action based upon an index generated from RNG class
RNG.setSeed(123);
for(int i = 0; i < 100; ++i){
int randomPos = RNG.getRandom().nextInt(actionList.size());
chosenActions.add(actionList.get(randomPos));
}
// check if all randomly chosen action in the implementation rely
// on RNG static class
RNG.setSeed(123);
for(DummyAction action : chosenActions){
assert (action == randomPolicy.chooseAction(dummyActionValues));
}
}
}