-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratmaze.c
98 lines (84 loc) · 1.76 KB
/
ratmaze.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "maze.h"
#include "maze_markov.h"
#include "maze_solver_value_iteration.h"
#include "maze_solver_policy_iteration.h"
#include "maze_solver_qlearning.h"
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
struct maze *m;
struct square s;
struct element *e;
struct maze_markov_decision_process *mdp;
int tmp;
if (argc < 2)
{
printf( "usage: %s <algorithm> [Q-learling limit]\n"
"\t[0] Value Iteration\n"
"\t[1] Policy Iteration\n"
"\t[2] Q-learning\n",
argv[0]
);
exit(EXIT_SUCCESS);
}
m = maze_create(5,3);
e = (__typeof__(e)) malloc(sizeof(struct element));
s.element = e;
e->name = 'R';
e->type = MAZE_ELEM_TYPE_INIT;
maze_add(m,&s,0,0);
s.element = 0;
maze_add(m,&s,0,1);
e->name = 'G';
e->reward = 1;
e->type = MAZE_ELEM_TYPE_GOAL;
s.element = e;
maze_add(m,&s,0,2);
s.element = 0;
maze_add(m,&s,1,2);
e->name = 'F';
e->reward = 10;
e->type = MAZE_ELEM_TYPE_GOAL;
s.element = e;
maze_add(m,&s,2,0);
s.element = 0;
maze_add(m,&s,2,1);
maze_add(m,&s,2,2);
maze_add(m,&s,3,2);
maze_add(m,&s,4,0);
maze_add(m,&s,4,1);
maze_add(m,&s,4,2);
maze_display(m);
printf("\n");
/*
MAZE_ELEMENT_MOVE_UP(m,e);
MAZE_ELEMENT_MOVE_UP(m,e);
MAZE_ELEMENT_MOVE_RIGHT(m,e);
MAZE_ELEMENT_MOVE_LEFT(m,e);
MAZE_ELEMENT_MOVE_LEFT(m,e);
MAZE_ELEMENT_MOVE_LEFT(m,e);
maze_display(m);
*/
mdp = maze_markov_decision_process_create(m);
maze_markov_decision_process_display(mdp);
if ((tmp = atoi(argv[1])) == 0)
maze_solver_vi_perform(mdp);
else if (tmp == 1)
maze_solver_pi_perform(mdp);
else
{
maze_solver_qlearning_perform(
mdp,
(argv[2]
? atoi(argv[2])
: MAZE_SOLVER_QLEARNING_DEFAULT_ITER
)
);
}
free(e);
maze_delete(m);
maze_markov_decision_process_destroy(mdp);
return 0;
}