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("=========================");
}
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("=========================");
}
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);
}
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);
}
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);
}
Aggregations