#include #include "smaze.h" #define SmoothMove 0 char World[VGrids][HGrids] = { " OG", " O O ", "S O O ", " O ", " O ", " " }; Agent A; short Startx, Starty; unsigned long Step, GoalCount; draw_view(flag) { extern int DisplayOn; if (DisplayOn) { set_step_label(Step); draw_world(flag); } else if (Step % ShowStepInterval == 0) set_step_label(Step); } domain_reset() { int x, y; Step = GoalCount = 0; for (y = 0; y < VGrids; y ++) for (x = 0; x < HGrids; x ++) if (World[y][x] == W_Start) { Startx = x; Starty = y; break; } A.x = Startx; A.y = Starty; brain_reset(); draw_view(0); } static void new_position(x,y,a,newx,newy) short x, y, a, *newx, *newy; { *newx = x; *newy = y; switch (a) { case ActionUp: if (y > 0) (*newy)--; break; case ActionDown: if (y < VGrids-1) (*newy)++; break; case ActionLeft: if (x > 0) (*newx)--; break; case ActionRight: if (x < HGrids-1) (*newx)++; } } one_step() { policy(); new_position(A.x,A.y,A.a,&A.newx,&A.newy); A.reward = 0.; switch (World[A.newy][A.newx]) { case W_Obstacle: A.newx = A.x; A.newy = A.y; break; case W_Goal: GoalCount ++; displayMonitor(); A.reward = 1.; A.newx = Startx; A.newy = Starty; } learn(); Step ++; draw_view(SmoothMove); A.x = A.newx; A.y = A.newy; }