From 7f18a66e9840d5713b43cf344531e239716a30f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20L=C3=B6wenstrom?= Date: Thu, 12 Dec 2019 00:20:38 +0100 Subject: [PATCH] add random policy test --- .gitignore | 4 ++ gradle/wrapper/gradle-wrapper.properties | 3 +- src/test/java/DummyAction.java | 6 +++ src/test/java/PolicyTest.java | 50 ++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/test/java/DummyAction.java create mode 100644 src/test/java/PolicyTest.java diff --git a/.gitignore b/.gitignore index 5c3489b..25d1e1c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 44e7c4d..39329ec 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/src/test/java/DummyAction.java b/src/test/java/DummyAction.java new file mode 100644 index 0000000..c804165 --- /dev/null +++ b/src/test/java/DummyAction.java @@ -0,0 +1,6 @@ +public enum DummyAction { + UP, + DOWN, + RIGHT, + LEFT +} diff --git a/src/test/java/PolicyTest.java b/src/test/java/PolicyTest.java new file mode 100644 index 0000000..5dfda16 --- /dev/null +++ b/src/test/java/PolicyTest.java @@ -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 createActionValueMap(){ + Map 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 randomPolicy = new RandomPolicy<>(); + Map dummyActionValues = createActionValueMap(); + + // dummy action space to choose an action from + List actionList = new ArrayList<>(dummyActionValues.keySet()); + // list of 100 random selected actions in a row + List 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)); + } + } +}