Search in sources :

Example 1 with JavaRandomizer

use of aima.core.util.JavaRandomizer in project aima-java by aimacode.

the class LearningDemo method passiveTDAgentDemo.

public static void passiveTDAgentDemo() {
    System.out.println("======================");
    System.out.println("DEMO: Passive-TD-Agent");
    System.out.println("======================");
    System.out.println("Figure 21.5");
    System.out.println("-----------");
    CellWorld<Double> cw = CellWorldFactory.createCellWorldForFig17_1();
    CellWorldEnvironment cwe = new CellWorldEnvironment(cw.getCellAt(1, 1), cw.getCells(), MDPFactory.createTransitionProbabilityFunctionForFigure17_1(cw), new JavaRandomizer());
    Map<Cell<Double>, CellWorldAction> fixedPolicy = new HashMap<Cell<Double>, CellWorldAction>();
    fixedPolicy.put(cw.getCellAt(1, 1), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(1, 2), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(1, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(2, 1), CellWorldAction.Left);
    fixedPolicy.put(cw.getCellAt(2, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(3, 1), CellWorldAction.Left);
    fixedPolicy.put(cw.getCellAt(3, 2), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(3, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(4, 1), CellWorldAction.Left);
    PassiveTDAgent<Cell<Double>, CellWorldAction> ptda = new PassiveTDAgent<Cell<Double>, CellWorldAction>(fixedPolicy, 0.2, 1.0);
    cwe.addAgent(ptda);
    output_utility_learning_rates(ptda, 20, 500, 100, 1);
    System.out.println("=========================");
}
Also used : CellWorldAction(aima.core.environment.cellworld.CellWorldAction) HashMap(java.util.HashMap) PassiveTDAgent(aima.core.learning.reinforcement.agent.PassiveTDAgent) JavaRandomizer(aima.core.util.JavaRandomizer) CellWorldEnvironment(aima.core.learning.reinforcement.example.CellWorldEnvironment) Cell(aima.core.environment.cellworld.Cell)

Example 2 with JavaRandomizer

use of aima.core.util.JavaRandomizer in project aima-java by aimacode.

the class LearningDemo method qLearningAgentDemo.

public static void qLearningAgentDemo() {
    System.out.println("======================");
    System.out.println("DEMO: Q-Learning-Agent");
    System.out.println("======================");
    CellWorld<Double> cw = CellWorldFactory.createCellWorldForFig17_1();
    CellWorldEnvironment cwe = new CellWorldEnvironment(cw.getCellAt(1, 1), cw.getCells(), MDPFactory.createTransitionProbabilityFunctionForFigure17_1(cw), new JavaRandomizer());
    QLearningAgent<Cell<Double>, CellWorldAction> qla = new QLearningAgent<Cell<Double>, CellWorldAction>(MDPFactory.createActionsFunctionForFigure17_1(cw), CellWorldAction.None, 0.2, 1.0, 5, 2.0);
    cwe.addAgent(qla);
    output_utility_learning_rates(qla, 20, 10000, 500, 20);
    System.out.println("=========================");
}
Also used : CellWorldAction(aima.core.environment.cellworld.CellWorldAction) JavaRandomizer(aima.core.util.JavaRandomizer) CellWorldEnvironment(aima.core.learning.reinforcement.example.CellWorldEnvironment) QLearningAgent(aima.core.learning.reinforcement.agent.QLearningAgent) Cell(aima.core.environment.cellworld.Cell)

Example 3 with JavaRandomizer

use of aima.core.util.JavaRandomizer in project aima-java by aimacode.

the class LearningDemo method output_utility_learning_rates.

//
// PRIVATE METHODS
//
private static void output_utility_learning_rates(ReinforcementAgent<Cell<Double>, CellWorldAction> reinforcementAgent, int numRuns, int numTrialsPerRun, int rmseTrialsToReport, int reportEveryN) {
    if (rmseTrialsToReport > (numTrialsPerRun / reportEveryN)) {
        throw new IllegalArgumentException("Requesting to report too many RMSE trials, max allowed for args is " + (numTrialsPerRun / reportEveryN));
    }
    CellWorld<Double> cw = CellWorldFactory.createCellWorldForFig17_1();
    CellWorldEnvironment cwe = new CellWorldEnvironment(cw.getCellAt(1, 1), cw.getCells(), MDPFactory.createTransitionProbabilityFunctionForFigure17_1(cw), new JavaRandomizer());
    cwe.addAgent(reinforcementAgent);
    Map<Integer, List<Map<Cell<Double>, Double>>> runs = new HashMap<Integer, List<Map<Cell<Double>, Double>>>();
    for (int r = 0; r < numRuns; r++) {
        reinforcementAgent.reset();
        List<Map<Cell<Double>, Double>> trials = new ArrayList<Map<Cell<Double>, Double>>();
        for (int t = 0; t < numTrialsPerRun; t++) {
            cwe.executeTrial();
            if (0 == t % reportEveryN) {
                Map<Cell<Double>, Double> u = reinforcementAgent.getUtility();
                if (null == u.get(cw.getCellAt(1, 1))) {
                    throw new IllegalStateException("Bad Utility State Encountered: r=" + r + ", t=" + t + ", u=" + u);
                }
                trials.add(u);
            }
        }
        runs.put(r, trials);
    }
    StringBuilder v4_3 = new StringBuilder();
    StringBuilder v3_3 = new StringBuilder();
    StringBuilder v1_3 = new StringBuilder();
    StringBuilder v1_1 = new StringBuilder();
    StringBuilder v3_2 = new StringBuilder();
    StringBuilder v2_1 = new StringBuilder();
    for (int t = 0; t < (numTrialsPerRun / reportEveryN); t++) {
        // Use the last run
        Map<Cell<Double>, Double> u = runs.get(numRuns - 1).get(t);
        v4_3.append((u.containsKey(cw.getCellAt(4, 3)) ? u.get(cw.getCellAt(4, 3)) : 0.0) + "\t");
        v3_3.append((u.containsKey(cw.getCellAt(3, 3)) ? u.get(cw.getCellAt(3, 3)) : 0.0) + "\t");
        v1_3.append((u.containsKey(cw.getCellAt(1, 3)) ? u.get(cw.getCellAt(1, 3)) : 0.0) + "\t");
        v1_1.append((u.containsKey(cw.getCellAt(1, 1)) ? u.get(cw.getCellAt(1, 1)) : 0.0) + "\t");
        v3_2.append((u.containsKey(cw.getCellAt(3, 2)) ? u.get(cw.getCellAt(3, 2)) : 0.0) + "\t");
        v2_1.append((u.containsKey(cw.getCellAt(2, 1)) ? u.get(cw.getCellAt(2, 1)) : 0.0) + "\t");
    }
    StringBuilder rmseValues = new StringBuilder();
    for (int t = 0; t < rmseTrialsToReport; t++) {
        // Calculate the Root Mean Square Error for utility of 1,1
        // for this trial# across all runs
        double xSsquared = 0;
        for (int r = 0; r < numRuns; r++) {
            Map<Cell<Double>, Double> u = runs.get(r).get(t);
            Double val1_1 = u.get(cw.getCellAt(1, 1));
            if (null == val1_1) {
                throw new IllegalStateException("U(1,1,) is not present: r=" + r + ", t=" + t + ", runs.size=" + runs.size() + ", runs(r).size()=" + runs.get(r).size() + ", u=" + u);
            }
            xSsquared += Math.pow(0.705 - val1_1, 2);
        }
        double rmse = Math.sqrt(xSsquared / runs.size());
        rmseValues.append(rmse);
        rmseValues.append("\t");
    }
    System.out.println("Note: You may copy and paste the following lines into a spreadsheet to generate graphs of learning rate and RMS error in utility:");
    System.out.println("(4,3)" + "\t" + v4_3);
    System.out.println("(3,3)" + "\t" + v3_3);
    System.out.println("(1,3)" + "\t" + v1_3);
    System.out.println("(1,1)" + "\t" + v1_1);
    System.out.println("(3,2)" + "\t" + v3_2);
    System.out.println("(2,1)" + "\t" + v2_1);
    System.out.println("RMSeiu" + "\t" + rmseValues);
}
Also used : HashMap(java.util.HashMap) JavaRandomizer(aima.core.util.JavaRandomizer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CellWorldEnvironment(aima.core.learning.reinforcement.example.CellWorldEnvironment) HashMap(java.util.HashMap) Map(java.util.Map) Cell(aima.core.environment.cellworld.Cell)

Example 4 with JavaRandomizer

use of aima.core.util.JavaRandomizer in project aima-java by aimacode.

the class MonteCarloLocalizationApp method initialize.

/**
	 * Creates a {@link GenericMonteCarloLocalization2DApp} and stores it in {@code app}.<br/>
	 * In addition the corresponding {@link IRobotGui} is created and stored in {@code robotGui}. The function {@code robotGui.destructRobot()} will be called when the application window closes to allow closing any open connections gracefully.
	 */
protected void initialize() {
    SimpleSettingsListener settingsListener = new SimpleSettingsListener(settingsGui);
    settingsListener.createSettings();
    AnglePanel angles = new AnglePanel(RANGE_READING_ANGLES_TITLE);
    settingsGui.registerSpecialSetting(RANGE_READING_ANGLES_KEY, angles);
    MclCartesianPlot2D<SimplePose, SimpleMove, AbstractRangeReading> map = new MclCartesianPlot2D<SimplePose, SimpleMove, AbstractRangeReading>(new SVGGroupParser(), new SVGGroupParser(), new SimplePoseFactory(), new SimpleRangeReadingFactory());
    VirtualRobot robot = new VirtualRobot(map);
    robotGui = new VirtualRobotGui(robot);
    MonteCarloLocalization<SimplePose, Angle, SimpleMove, AbstractRangeReading> mcl = new MonteCarloLocalization<SimplePose, Angle, SimpleMove, AbstractRangeReading>(map, new JavaRandomizer());
    app = new GenericMonteCarloLocalization2DApp<SimplePose, SimpleMove, SimpleRangeReading>(mcl, map, robot, robotGui, settingsGui);
    angles.setChangeListener((ChangeListener) robotGui);
    settingsListener.setMap(map);
    settingsListener.setMcl(mcl);
    settingsListener.setRobot(robot);
}
Also used : SimpleRangeReadingFactory(aima.gui.swing.applications.robotics.simple.SimpleRangeReadingFactory) VirtualRobotGui(aima.gui.swing.applications.robotics.simple.VirtualRobotGui) VirtualRobot(aima.gui.swing.applications.robotics.simple.VirtualRobot) AnglePanel(aima.gui.swing.applications.robotics.components.AnglePanel) SimplePoseFactory(aima.gui.swing.applications.robotics.simple.SimplePoseFactory) JavaRandomizer(aima.core.util.JavaRandomizer) SVGGroupParser(aima.core.util.math.geom.SVGGroupParser) SimpleSettingsListener(aima.gui.swing.applications.robotics.simple.SimpleSettingsListener) SimpleRangeReading(aima.gui.swing.applications.robotics.simple.SimpleRangeReading) Angle(aima.core.robotics.impl.datatypes.Angle) SimplePose(aima.gui.swing.applications.robotics.simple.SimplePose) AbstractRangeReading(aima.core.robotics.impl.datatypes.AbstractRangeReading) MclCartesianPlot2D(aima.core.robotics.impl.map.MclCartesianPlot2D) SimpleMove(aima.gui.swing.applications.robotics.simple.SimpleMove) MonteCarloLocalization(aima.core.robotics.MonteCarloLocalization)

Example 5 with JavaRandomizer

use of aima.core.util.JavaRandomizer in project aima-java by aimacode.

the class PassiveADPAgentTest method setUp.

@Before
public void setUp() {
    cw = CellWorldFactory.createCellWorldForFig17_1();
    cwe = new CellWorldEnvironment(cw.getCellAt(1, 1), cw.getCells(), MDPFactory.createTransitionProbabilityFunctionForFigure17_1(cw), new JavaRandomizer());
    Map<Cell<Double>, CellWorldAction> fixedPolicy = new HashMap<Cell<Double>, CellWorldAction>();
    fixedPolicy.put(cw.getCellAt(1, 1), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(1, 2), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(1, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(2, 1), CellWorldAction.Left);
    fixedPolicy.put(cw.getCellAt(2, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(3, 1), CellWorldAction.Left);
    fixedPolicy.put(cw.getCellAt(3, 2), CellWorldAction.Up);
    fixedPolicy.put(cw.getCellAt(3, 3), CellWorldAction.Right);
    fixedPolicy.put(cw.getCellAt(4, 1), CellWorldAction.Left);
    padpa = new PassiveADPAgent<Cell<Double>, CellWorldAction>(fixedPolicy, cw.getCells(), cw.getCellAt(1, 1), MDPFactory.createActionsFunctionForFigure17_1(cw), new ModifiedPolicyEvaluation<Cell<Double>, CellWorldAction>(10, 1.0));
    cwe.addAgent(padpa);
}
Also used : CellWorldAction(aima.core.environment.cellworld.CellWorldAction) HashMap(java.util.HashMap) JavaRandomizer(aima.core.util.JavaRandomizer) CellWorldEnvironment(aima.core.learning.reinforcement.example.CellWorldEnvironment) Cell(aima.core.environment.cellworld.Cell) ModifiedPolicyEvaluation(aima.core.probability.mdp.impl.ModifiedPolicyEvaluation) Before(org.junit.Before)

Aggregations

JavaRandomizer (aima.core.util.JavaRandomizer)10 Cell (aima.core.environment.cellworld.Cell)9 CellWorldEnvironment (aima.core.learning.reinforcement.example.CellWorldEnvironment)9 HashMap (java.util.HashMap)7 CellWorldAction (aima.core.environment.cellworld.CellWorldAction)6 Map (java.util.Map)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PassiveADPAgent (aima.core.learning.reinforcement.agent.PassiveADPAgent)1 PassiveTDAgent (aima.core.learning.reinforcement.agent.PassiveTDAgent)1 QLearningAgent (aima.core.learning.reinforcement.agent.QLearningAgent)1 ModifiedPolicyEvaluation (aima.core.probability.mdp.impl.ModifiedPolicyEvaluation)1 MonteCarloLocalization (aima.core.robotics.MonteCarloLocalization)1 AbstractRangeReading (aima.core.robotics.impl.datatypes.AbstractRangeReading)1 Angle (aima.core.robotics.impl.datatypes.Angle)1 MclCartesianPlot2D (aima.core.robotics.impl.map.MclCartesianPlot2D)1 SVGGroupParser (aima.core.util.math.geom.SVGGroupParser)1 AnglePanel (aima.gui.swing.applications.robotics.components.AnglePanel)1 SimpleMove (aima.gui.swing.applications.robotics.simple.SimpleMove)1