-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_con_fun.m
81 lines (58 loc) · 1.87 KB
/
obj_con_fun.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
function [h_obj_fun, h_con_fun, h_sim_fun] = obj_con_fun(data, par)
h_obj_fun = @obj_fun;
h_con_fun = @con_fun;
h_sim_fun = @sim_fun;
ode_options = odeset('Jacobian', data.h_Fjac);
X_local = [];
y = []; t = []; u = []; T = [];
nDisStates = data.nDisStates;
nDisControls = data.nDisControls;
statesSize = data.statesSize;
y_dis = []; u_dis = [];
y_interval_end = [];
function [L, L_grad] = obj_fun(X)
if ~isequal(X, X_local)
[t, y, u, y_interval_end] = sim_fun(X);
X_local = X;
end
% Objective
L = y(end, end);
% Optional gradient (set 'SpecifyObjectiveGradient' to true)
if nargout > 1
delta = sqrt(eps);
for i = 1:length(X)
X_dist = X;
X_dist(i) = X_dist(i) + delta;
[~, y_dist] = sim_fun(X_dist);
% Cost gradient
L_grad(i) = (y_dist(end, end) - L) / delta;
end
end
end
function [c, ceq] = con_fun(X)
if ~isequal(X, X_local)
[t, y, u, y_interval_end] = sim_fun(X);
X_local = X;
end
%Continuity constraints
ceq = reshape(y_dis(:,2:end) - y_interval_end', [], 1);
c = [];
end
function [t, y, u, y_interval_end] = sim_fun(X)
% Get distcrete controls and states
u_dis = X(1:nDisControls);
T = X(nDisControls + 1);
intervalTimes = linspace(0, T, nDisStates);
y_dis = reshape(X(nDisControls+2:end), statesSize);
% Simulate system
y = []; t = [];
for n = 1:nDisStates-1
[t_interval, y_interval] = ode45(@(t, y) ODEFUN(t, y, u_dis, T, par, data), [intervalTimes(n) intervalTimes(n+1)], [y_dis(:,n); 0], ode_options);
y_interval_end(n,:) = y_interval(end,1:5);
y = [y; y_interval];
t = [t; t_interval];
end
% Interpolate u
u = interp1(linspace(0,T,data.nDisControls), u_dis, t, data.interpMethod);
end
end