-
Notifications
You must be signed in to change notification settings - Fork 23
/
ANN_RL.m
142 lines (99 loc) · 3.36 KB
/
ANN_RL.m
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
% REINFORCEMENT LEARNING CONTROL OF TWO TANK LIQUID LEVEL SYSTEM
% Dr. Mathew Mithra Noel
% School of Electrical Engineering
% Vellore Institute of Technology
% Control of a nonlinear liquid level system using a new artificial neural network based reinforcement learning approach,
% Applied Soft Computing, Volume 23, 2014, Pages 444-451, ISSN 1568-4946, https://doi.org/10.1016/j.asoc.2014.06.037.
% (http://www.sciencedirect.com/science/article/pii/S1568494614003111)
clear all;
clc;
% Define final desired goal state
global state_desired;
state_desired= 7;
% Flow to Tank 2 is not controlled and hence set to zero
global Q2;
Q2=0;
% Discretize state space
global h1;
global h2;
h1=linspace(0,10,15);
h2=h1;
global delta;
delta= (h1(2)-h1(1))/2;
% Discretize action space
global action;
Q1=linspace(0,20,10);
N1 = length(h1);
N2 = length(h2);
% Initialize policy and value.
pibest = zeros(N1,N2);
gamma =0.99;
% Set the initial guess for V(s) to be zero for each state s.
V = zeros(N1,N2);
policy = zeros(N1,N2);
% Compute the optimal value function using the Value Iteration algorithm.
for runs=1:1000
for m=1:N1
for n=1:N2
for p =1:length(Q1)
% Take all possible actions.
action = Q1(p);
snext = [h1(m); h2(n)]+ 0.1*tank(0,[h1(m); h2(n)]);
% Compute the closest discretized state.
[r,s] = closest(snext);
nextV(p)=V(r,s);
end
[Vbest,bestind] = max(nextV);
% Improve value function estimate using Bellman's equation.
V(m,n)= Reward([h1(m); h2(n)] ) + gamma*Vbest ;
end
end
end
% Compute the optimal policy from the optimal value function.
for m=1:N1
for n=1:N2
% Take all possible actions.
for p =1:length(Q1)
action = Q1(p);
snext = [h1(m); h2(n)]+ 0.1*tank(0,[h1(m); h2(n)]);
% Compute the closest discretized state.
[r,s] = closest(snext);
nextV(p)=V(r,s);
end
[Vbest,bestind] = max(nextV);
pibest(m,n) = Q1(bestind);
end
end
%train a feedforward neural net to approximate pbest
p=1;
targetQ = zeros(1,length(h1)*length(h2));
input_states = zeros(2,length(h1)*length(h2));
for m=1:length(h1)
for n=1:length(h2)
input_states(:,p)= [h1(m) ; h2(n)];
targetQ(p) = pibest(m,n);
p=p+1;
end
end
net = feedforwardnet(1);
net=init(net);
[net,tr] = train(net,input_states,targetQ);
N = 100;
state=[1 0]; %Initial state
states = zeros(N,2);
states(1,:)= state ;
Ts = 0.1; % Define time between control actions.
% Simulate the system with the optimal control policy.
for n=2:N
% Use the optimal action learnt by the ANN
action = net(state');
%Simulate the system for one time step.
[t,y]=ode45(@tank,[0 Ts],state);
state = real(y(end,:));
states(n,:) = state;
end
% Plot time history of states with optimal policy.
time = (1:length(states))*Ts;
plot(time,states);
xlabel('time (s)');
ylabel('state: liquid levels h_1 and h_2');