65 lines
2.1 KiB
Java
65 lines
2.1 KiB
Java
package aima.core.probability.mdp;
|
|
|
|
import java.util.Map;
|
|
|
|
import org.junit.Assert;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
|
|
import aima.core.environment.cellworld.Cell;
|
|
import aima.core.environment.cellworld.CellWorld;
|
|
import aima.core.environment.cellworld.CellWorldAction;
|
|
import aima.core.environment.cellworld.CellWorldFactory;
|
|
import aima.core.probability.example.MDPFactory;
|
|
import aima.core.probability.mdp.MarkovDecisionProcess;
|
|
import aima.core.probability.mdp.search.ValueIteration;
|
|
|
|
/**
|
|
* @author Ravi Mohan
|
|
* @author Ciaran O'Reilly
|
|
*
|
|
*/
|
|
public class ValueIterationTest {
|
|
public static final double DELTA_THRESHOLD = 1e-3;
|
|
|
|
private CellWorld<Double> cw = null;
|
|
private MarkovDecisionProcess<Cell<Double>, CellWorldAction> mdp = null;
|
|
private ValueIteration<Cell<Double>, CellWorldAction> vi = null;
|
|
|
|
@Before
|
|
public void setUp() {
|
|
cw = CellWorldFactory.createCellWorldForFig17_1();
|
|
mdp = MDPFactory.createMDPForFigure17_3(cw);
|
|
vi = new ValueIteration<Cell<Double>, CellWorldAction>(1.0);
|
|
}
|
|
|
|
@Test
|
|
public void testValueIterationForFig17_3() {
|
|
Map<Cell<Double>, Double> U = vi.valueIteration(mdp, 0.0001);
|
|
|
|
Assert.assertEquals(0.705, U.get(cw.getCellAt(1, 1)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(0.762, U.get(cw.getCellAt(1, 2)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(0.812, U.get(cw.getCellAt(1, 3)), DELTA_THRESHOLD);
|
|
|
|
Assert.assertEquals(0.655, U.get(cw.getCellAt(2, 1)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(0.868, U.get(cw.getCellAt(2, 3)), DELTA_THRESHOLD);
|
|
|
|
Assert.assertEquals(0.611, U.get(cw.getCellAt(3, 1)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(0.660, U.get(cw.getCellAt(3, 2)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(0.918, U.get(cw.getCellAt(3, 3)), DELTA_THRESHOLD);
|
|
|
|
Assert.assertEquals(0.388, U.get(cw.getCellAt(4, 1)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(-1.0, U.get(cw.getCellAt(4, 2)), DELTA_THRESHOLD);
|
|
Assert.assertEquals(1.0, U.get(cw.getCellAt(4, 3)), DELTA_THRESHOLD);
|
|
|
|
for (int j = 3; j >= 1; j--) {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 1; i <= 4; i++) {
|
|
sb.append(U.get(cw.getCellAt(i, j)));
|
|
sb.append(" ");
|
|
}
|
|
System.out.println(sb.toString());
|
|
}
|
|
}
|
|
}
|