-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoubleIntegrator.m
158 lines (128 loc) · 4.66 KB
/
DoubleIntegrator.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
classdef DoubleIntegrator < LinearSystem
methods
function obj = DoubleIntegrator
obj = obj@LinearSystem([0,1;0,0],[0;1],[],[],eye(2),[]);
obj = setInputLimits(obj,-1,1);
obj = setOutputFrame(obj,getStateFrame(obj));
end
function g = mintime_cost(obj,x,u)
g = ((x(1,:) ~= 0) | (x(2,:) ~= 0));
end
function g = lqr_cost(obj,x,u)
Q=eye(2); R = 10;
g = x'*Q*x + u'*R*u;
end
function v = constructVisualizer(obj)
function draw(t,x)
blockx = [-1, -1, 1, 1, -1];
blocky = [0, 0.5, 0.5, 0, 0];
% draw the mass
brickcolor=[.75 .6 .5];
fill(blockx+repmat(x(1),1,5),blocky,brickcolor);
faintline=[.6 .8 .65]*1.1;
plot(min(blockx)+[0 0],[-5 5],'k:','Color',faintline);
plot(max(blockx)+[0 0],[-5 5],'k:','Color',faintline);
% draw the ground
line([-5, 5], [0, 0],'Color',[.3 .5 1],'LineWidth',1);
axis equal;
axis([-5 5 -1 2]);
%grid on
end
v = FunctionHandleVisualizer(getOutputFrame(obj),@draw);
end
end
methods (Static = true)
function runValueIteration
p = DoubleIntegrator;
options.gamma = .9999; % discount factor
options.dt = .01;
costfun = @mintime_cost;
% costfun = @lqr_cost;
xbins = {[-3:.2:3],[-4:.2:4]};
ubins = linspace(p.umin,p.umax,9);
mdp = MarkovDecisionProcess.discretizeSystem(p,costfun,xbins,ubins,options);
function drawfun(J,PI)
figure(2); clf;
n1=length(xbins{1});
n2=length(xbins{2});
subplot(2,1,1);imagesc(xbins{1},xbins{2},reshape(ubins(PI),n1,n2)');
axis xy;
xlabel('q');
ylabel('qdot');
title('u(x)');
subplot(2,1,2);imagesc(xbins{1},xbins{2},reshape(J,n1,n2)');
axis xy;
xlabel('q');
ylabel('qdot');
title('J(x)');
drawnow;
% pause;
end
valueIteration(mdp,0.0001,@drawfun);
% add colorbars (it's expensive to do it inside the loop)
figure(2);
subplot(2,1,1); colorbar;
subplot(2,1,2); colorbar;
end
function runDirtran
% Simple example of Direct Transcription trajectory optimization
% create the system
plant = DoubleIntegrator;
% set up a direct transcription problem with N knot points and bounds on
% the duration
N = 20;
prog = DirtranTrajectoryOptimization(plant,N,[0.1 10]);
% add the initial value constraint
x0 = [-2; -2];
prog = addStateConstraint(prog,ConstantConstraint(x0),1);
% add the final value constraint
xf = [0;0];
prog = addStateConstraint(prog,ConstantConstraint(xf),N);
% add the cost function g(dt,x,u) = 1*dt
function [g,dg] = cost(dt,x,u)
g = dt; dg = [1,0*x',0*u']; % see geval.m for our gradient format
end
prog = addRunningCost(prog,@cost);
% add a display function to draw the trajectory on every iteration
function displayStateTrajectory(t,x,u)
plot(x(1,:),x(2,:),'b.-','MarkerSize',10);
axis([-5,1,-2.5,2.5]); axis equal;
drawnow;
end
prog = addTrajectoryDisplayFunction(prog,@displayStateTrajectory);
% solve the optimization problem (with 2 sec as the initial guess for
% the duration)
prog.solveTraj(2);
end
function runDircol
% Simple example of Direct Collocation trajectory optimization
% create the system
plant = DoubleIntegrator;
% set up a direct transcription problem with N knot points and bounds on
% the duration
N = 20;
prog = DircolTrajectoryOptimization(plant,N,[0.1 10]);
% add the initial value constraint
x0 = [-2; -2];
prog = addStateConstraint(prog,BoundingBoxConstraint(x0,x0),1);
% add the final value constraint
xf = [0;0];
prog = addStateConstraint(prog,BoundingBoxConstraint(xf,xf),N);
% add the cost function g(dt,x,u) = 1*dt
function [g,dg] = cost(dt,x,u)
g = dt; dg = [1,0*x',0*u']; % see geval.m for our gradient format
end
prog = addRunningCost(prog,@cost);
% add a display function to draw the trajectory on every iteration
function displayStateTrajectory(t,x,u)
plot(x(1,:),x(2,:),'b.-','MarkerSize',10);
axis([-5,1,-2.5,2.5]); axis equal;
drawnow;
end
prog = addTrajectoryDisplayFunction(prog,@displayStateTrajectory);
% solve the optimization problem (with 2 sec as the initial guess for
% the duration)
prog.solveTraj(2);
end
end
end