-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiverMain_RSValueIterationNxN.m
More file actions
102 lines (73 loc) · 3.4 KB
/
Copy pathRiverMain_RSValueIterationNxN.m
File metadata and controls
102 lines (73 loc) · 3.4 KB
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
99
100
101
102
%% Main file to invoke River Domain
%% Using RS Value Iteration
function RiverMain_RSValueIterationNxN
ks = [0.99 0.8 0.5 0 -0.5 -0.8 -0.99]; %sets risk values (-1,1) which k<0 is expected a prone policy and k>0 is averse in k=0 is equal VA
for w=1:length(ks)
limit = 250000;
isDinamicRisk = true; %set dinamic value
isDinamicAlpha = false; %set dinamic alpha
k = ks(w); %factor of risk in (-1,1) range -1 is prone attitude +1 is averse 0 is neutral
alpha = (1 +abs(k))^-1; %stepsize
epsilon = 0.00001; %precision to stop planning algorithm
if isDinamicAlpha == true
ini_alpha = 1;
else
ini_alpha = alpha;
end
%if Dinamic is enable initial value is used after next value is used
if isDinamicRisk == true
ini_k = 0;
ini_epsilon = 0.1;
ini_alpha = (1 +abs(ini_k))^-1;
else
ini_k = k;
ini_epsilon = epsilon;
end
scenario = false; %true (+) scenario [1 in goal and 0 otherwise] or false (-) scenario [0 in goal and -1 otherwise]
p = 0.2; %probability to move in river ProbMoveInsideRiver
nA = 4; %number of actions (N,S,W,E)
%size of grid instance
Nx = 10; Ny = 7;
gamma = 0.99; %discount parameter for convergence
if (scenario)
scene = '(+)';
else
scene = '(-)';
end
%directory for save files
mkdir_if_not_exist(strcat('Data/',scene,'/','gamma=',num2str(gamma),'/',num2str(Nx),'x',num2str(Ny)));
agentStorage = strcat('Data/',scene,'/','gamma=',num2str(gamma),'/',num2str(Nx),'x',num2str(Ny),'/','Agent','_','RS','_','VI','_',num2str(Nx),'x',num2str(Ny),'_','k=',num2str(k),'_','p=',num2str(p),'_e=0.00001.mat'); %local for storage instance for agents simulated (could be reused in a new execution)
print = strcat('Data/',scene,'/','gamma=',num2str(gamma),'/',num2str(Nx),'x',num2str(Ny),'/','Print','_','RS','_','VI','_',num2str(Nx),'x',num2str(Ny),'_','k=',num2str(k),'_','p=',num2str(p),'_e=0.00001.txt'); %local for storage instance for agents simulated (could be reused in a new execution)
%vinculates set of information for agent
A = setAgent(agentStorage, print, epsilon, alpha, gamma, k, ini_k, ini_alpha, ini_epsilon, isDinamicRisk, isDinamicAlpha, limit);
%vinculates all probabilities and transiction of river matrix
M = setRiver(Nx, Ny, p, nA, scenario);
%instance
agent = RSValueIteration(A, A.epsilon, A.gamma, A.alpha, M, A.k); %vinculates instance for RSValueIteration
%time counting
t=0; e=0;
t = cputime;
agent.RSValuation(); %call for RSValueIteration
e = cputime - t;
%save agent after planning
save(A.agentStorage,'agent','e','M','A');
end
end
function mkdir_if_not_exist(dirpath)
if dirpath(end) ~= '/', dirpath = [dirpath '/']; end
if (exist(dirpath, 'dir') == 0), mkdir(dirpath); end
end
function A = setAgent(agentStorage, print, epsilon, alpha, gamma, k, ini_k, ini_alpha, ini_epsilon, isDinamicRisk, isDinamicAlpha, limit)
A.agentStorage = agentStorage;
A.print = print;
A.epsilon = epsilon;
A.alpha = alpha;
A.gamma = gamma;
A.k = k;
A.ini_k = ini_k;
A.ini_alpha = ini_alpha;
A.ini_epsilon = ini_epsilon;
A.isDinamicRisk = isDinamicRisk;
A.isDinamicAlpha = isDinamicAlpha;
A.limit = limit;
end