-
Notifications
You must be signed in to change notification settings - Fork 4
/
linfun1.m
141 lines (121 loc) · 2.99 KB
/
linfun1.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
clear all;
close all;
clc;
NUM_STATES = 162;
MAX_EP = 1000;
NUM_FAILED_STATES = 1;
NUM_ACTIONS = 2;
Q = zeros(NUM_STATES+NUM_FAILED_STATES,NUM_ACTIONS);
ep = 1;
samerep = 0;
gamma = 0.992; % late reward contribution
alpha = 0.07; % learning rate
lambda = 0; % eligibility decay
epsilon = 0; % amount of randomness/greediness
% states - 1 to 162+1 (failed state)
% actions - 1 to 2
thetaPlot = 0;
xplot = 0;
fail = 0;
jv = 0;
% APPROX
w = zeros(4,2);
while (ep < MAX_EP)
% init S and A
%S = 82;
%realS = [0,0,0,0];
xSA = [0,0,0,0]; % First 4 for action 0;
%[qp,A] = max(Q(S,:));%eGreedy(Q(S,:),epsilon);
% End of Episode flag
EOE = 0;
%qHat = xSA*w;
% Episodic loop
c = 0;
j = 0;
%thetaPlot = 0;
while (~EOE)
c = c + 1;
% Take action:
%[Qpi, A] = eGreedy(Q(S,:),epsilon); %max(Q(Snew,:));
A = xSA*w(:,2) > xSA*w(:,1);
qHat = xSA*w(:,A+1);
%[R,Snew,realSnew] = takeAction(realS,S,A);
xSAnew = takeAction2(xSA,A+1);
%disp('---');
thetaPlot(end+1) = xSAnew(1);
xplot(end +1) = xSAnew(3);
% Choose Anew using some<greedy> policy
% [Qpi, Anew] = eGreedy(Q(Snew,:),epsilon); %max(Q(Snew,:));
%[Qpigr, Anew] = max(Q(Snew,:));
% Reward and Eligibility for taking a step
if (abs(xSAnew(1)) > 12*pi/180 || abs(xSAnew(3)) > 2.4)
%Snew = 82;
R = -1;
w(:,A+1) = w(:,A+1) + alpha.*(R - qHat).*xSA';
fail = 1;
else
Anew = xSAnew*w(:,2) > xSAnew*w(:,1);
% APPROX value function
qHatNew = xSAnew*w(:,Anew+1);
R = 0;
w(:,A+1) = w(:,A+1) + alpha.*(R + gamma.*qHatNew - qHat).*xSA';
%disp('w is: ');
%disp(w);
end
% del = R + gamma*Q(Snew,Anew)-Q(S,A);
%Q(S,A) = Q(S,A) + alpha*(R + gamma*max(Q(Snew,:))-Q(S,A));
% if S == Snew
% jv = jv + 1;
% disp(jv);
% else
% jv = 0;
% end
%S = Snew;
%realS = realSnew;
xSA = xSAnew;
qHat = qHatNew;
%disp(c);
% Check if End of Episode
% if (S == NUM_STATES+NUM_FAILED_STATES || c > 10000)
if (fail == 1 || c > 100000)
EOE = 1;
fail = 0;
% figure(1);
% plot(0,0,'r*');
end
end
figure(3);
plot(1:c,thetaPlot(1:c),'-ob');
thetaPlot = 0;
fprintf('Trial %d was %d steps. \n',ep,c);
figure(4);
plot(1:c,xplot(1:c),'-og');
xplot = 0;
fprintf('Trial %d was %d steps. \n',ep,c);
% if ep == 75
% pause(10);
% end
if (c > 100000)
fprintf('Balanced for %d steps. \n',c);
break;
end
ep = ep + 1;
figure(2);
plot(ep,c,'r*');
hold on;
c = 0;
end
ep = ep + 1;
figure(2);
plot(ep,c,'r*');
hold on;
% Update all Qs
% if samerep == 0
% E(S,A) = E(S,A) + 1;
% for s = 1:NUM_STATES
% for a = 1:NUM_ACTIONS
% Q(s,a) = Q(s,a) + alpha*del*E(s,a);
% E(s,a) = gamma*lambda*E(s,a);
% end
% end
% end