-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue_Iteration.m
More file actions
29 lines (26 loc) · 1.01 KB
/
Value_Iteration.m
File metadata and controls
29 lines (26 loc) · 1.01 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
function [v, pi] = Value_Iteration(MDP, gamma)
%% Description: Value-Interation algorithm in MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input: MDP Markov decision process
% gamma discount factor (γ)
%
% Output: v state-value function (v-function)
% pi optimal strategy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Number of states
n = length(MDP.States);
%% Initialize state-value function (v-function) and strategy
v = zeros(1, n);
pi = zeros(1, n);
%% Loop until convergence
delta = inf; % Initialize delta for convergence check
while delta > 0.001
delta = 0;
for s = 1:n
v_old = v(s);
q_sa = sum(MDP.T(s,:,:) .* (MDP.R(s,:,:) + gamma * v), 2); % Vectorized action-value function (q-function)
[v(s), pi(s)] = max(q_sa); % Update the value function and policy simultaneously
delta = max(delta, abs(v_old - v(s))); % Update delta for convergence check
end
end
end