#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define Actions_Num 6
#define Target 5
#define Episode 600000
#define gamma 0.8
const int INF = 0x3f3f3f3f;
int max(int* p, int m) {
int Max = *p;
for (int i = 1; i < m; i++) if (*(p + i) > Max)
Max = *(p + i);
return Max;
}
int main() {
int Q[6][6] = { 0 };
for (int i = 0; i < 6; i++) Q[i][i] = -1;
int R[6][6] = {
{ -1, -1, -1, -1, 0, -1},
{-1, -1, -1, 0, -1, 100.0},
{ -1, -1, -1, 0, -1, -1},
{-1, 0, 0, -1, 0, -1},
{0, -1, -1, 0, -1, 100.0},
{ -1, 0, -1, -1, 0, 100.0 }
};
int Start_Room, Cur_Sta, Cur_Act, Nex_Sta;
for (int i = 0; i < Episode; i++) {
srand(time(NULL));
Cur_Sta = Start_Room = rand() % 5; // 取0~4进行位置初始化.
while (Cur_Sta != Target) {
Cur_Act = rand() % 6;// 取0~5进行行动位置初始化.
int Cur_Reward = R[Cur_Sta][Cur_Act]; // 保存当前行动奖赏值.
if (Cur_Reward >= 0) { // 目标位置可以行走.
Nex_Sta = Cur_Act; // 保存下一状态.
Q[Cur_Sta][Cur_Act] = Cur_Reward + gamma * max(&Q[Nex_Sta][0], 6);
Cur_Sta = Nex_Sta;
}
else Q[Cur_Sta][Cur_Act] = Cur_Reward;
}
}
Start_Room = rand() % 5; // 取0~4进行位置初始化.
Cur_Sta = Start_Room; // 保存当前位置
int step = 0;
while (Cur_Sta != Target) {
int Max = -INF;
for (int i = 0; i < 6; i++) if (Max < Q[Cur_Sta][i])
Max = Q[Cur_Sta][i];
for (int j = 0; j < 6; j++) if (Max == Q[Cur_Sta][j])
Nex_Sta = j;
printf("\n Agent 由 %d 号房间移动到了 %d 号房间\n", Cur_Sta, Nex_Sta);
Cur_Sta = Nex_Sta;
step++;
}
printf("\n Agent 在 %d 号房间开始移动了 %d 步到达了目标房间\n", Start_Room, step);
}
Q-Learing(房间移动) - (1)
最新推荐文章于 2024-07-09 16:07:52 发布