更改测试案例

This commit is contained in:
794757862@qq.com 2022-09-17 17:36:32 +08:00
parent 922e6e9288
commit 3f56751906
2 changed files with 18 additions and 13 deletions

View File

@ -12,15 +12,16 @@ import java.util.Map;
import java.util.Random;
public class GameRobotTest {
private static DynamicProgramming dynamicProgramming = new DynamicProgramming();
private static DynamicProgramming dynamicProgramming = new DynamicProgramming();//智能机器人
//游戏机器人测试
//这是一个寻找宝藏的游戏地图是一个6*6大小的格子地图游戏里有1个宝藏三个炸弹每次宝藏和炸弹刷新的位置随机
//一个小人从地图的随机一个位置出发共走六步如果能拿到四个宝藏中的指定宝藏就成功
// 如果五步没有拿到宝藏就失败或者五步之内撞到地图边缘同样也是失败如果碰到炸弹同样失败
//一个小人从地图的随机一个位置出发共走六步如果能拿到指定宝藏就成功
// 如果六步没有拿到宝藏就失败或者六步之内撞到地图边缘同样也是失败如果碰到炸弹同样失败
public static void main(String[] args) throws Exception {
dynamicProgramming.setGaMa(0.9);//取值范围(0-1)值越低越注重短期收益值越高越注重长期收益
dynamicProgramming.setMaxTimes(500);//取值范围正整数值越低速度越快精度越低值越大速度越慢精度越高
dynamicProgramming.setValueTh(0.0001);//取值范围很小的正数值越低精度越大速度越慢值越大精度越小速度越快
//dynamicProgramming.setGaMa(0.9);//取值范围(0-1)值越低越注重短期收益值越高越注重长期收益
//dynamicProgramming.setMaxTimes(500);//取值范围正整数值越低速度越快精度越低值越大速度越慢精度越高
//dynamicProgramming.setValueTh(0.0001);//取值范围很小的正数值越低精度越大速度越慢值越大精度越小速度越快
GameConfig gameConfig = new GameConfig();
init(gameConfig);
List<int[]> prizeList = gameConfig.getPrizeList();//生成随机四个奖品坐标
@ -28,9 +29,12 @@ public class GameRobotTest {
for (int i = 0; i < size; i++) {
System.out.println("i==" + i + ",cood==" + Arrays.toString(prizeList.get(i)));
}
dynamicProgramming.gameStart();//先跑数据
dynamicProgramming.gameStart();//探索
dynamicProgramming.strategyStudy();//研究策略
Matrix matrix = dynamicProgramming.getValueMatrix();
//List<ValueFunction> valueFunctions = dynamicProgramming.getValueFunction();//返回一个多维的获取价值函数
//List<Integer> actionList = dynamicProgramming.getBestAction(new int[]{1, 2});//获取下一步行动
//返回一个动作id集合
Matrix matrix = dynamicProgramming.getValueMatrix();//返回一个二维状态主键可打印的价值函数
System.out.println(matrix.getString());
}
@ -38,6 +42,7 @@ public class GameRobotTest {
Random random = new Random();
//初始化随机奖品位置
List<int[]> prizeList = gameConfig.getPrizeList();//生成随机四个奖品坐标
int[] cup = new int[]{random.nextInt(3) + 3, random.nextInt(3) + 3};
for (int i = 0; i < 4; i++) {
switch (i) {
case 0:
@ -47,20 +52,20 @@ public class GameRobotTest {
prizeList.add(new int[]{random.nextInt(3) + 3, random.nextInt(3)});
break;
case 2:
prizeList.add(new int[]{random.nextInt(3) + 3, random.nextInt(3) + 3});
prizeList.add(cup);
break;
case 3:
prizeList.add(new int[]{random.nextInt(3), random.nextInt(3) + 3});
break;
}
}
//加载状态
//加载状态 生成地图
List<DynamicState> dynamicStateList = dynamicProgramming.getDynamicStateList();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
int[] stateId = new int[]{i, j};
DynamicState dynamicState = new DynamicState(stateId);
//dynamicState.setFinish(isFinish(gameConfig, stateId));//设置终结态
dynamicState.setFinish(isFinish(gameConfig, stateId));//设置终结态
dynamicStateList.add(dynamicState);
}
}

View File

@ -22,7 +22,7 @@ public class Left extends Action {//向左移动
super.setActionId(actionId);
}
@Override
@Override//动作 参数是 执行该动作的状态
protected int[] action(int[] stateId) {//左移动作
int x = stateId[0];//横坐标
int y = stateId[1];//纵坐标
@ -36,7 +36,7 @@ public class Left extends Action {//向左移动
return new int[]{x, y};
}
@Override
@Override//该动作带来的收益
protected int getProfit(int[] stateId) {//左移后的惩罚或者奖励值设置
int profit = 0;
if (stateId[0] == 0) {//如果左移会撞墙给惩罚-10