-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev_array.m
189 lines (165 loc) · 5.77 KB
/
ev_array.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
% uses the array form of the variables for clarity
%% Define the problem parameters
clear, close all, clc
clear('yalmip');
N = 15; % prediction horizon
Ts = 1; % sample time
C = 2; % number of cars
n_nu = 5; % number of nodes (>= to the # of stations)
capacity_nu = 1*ones(n_nu,1); % vector of vehicle capacity per station
Ec = 5; % Power threshold (switches power to decrementing)
E_max = 15; %kWh (battery capacity)
E_min = 0;
d_max = 100;
Pmax = 45; %kW (max charging power)
P_drive_test = .5; % kW
P_charge_test = 1; % kW
P_drive_test = .5; % kW (driving power discharge)
P_charge_test = 5; % kW (charging rate)
car_capacity = 10;
proximity_margin = .95; % the equality constraints never hold exactly
final_margin = .95; % final state won't be exactly the end point due to discrete dynamics
%% Run the feas_traj function to get the sequences (ordered-sets) of edges
% % to traverse
% % Initial nodes (nu)
%
% % Define connected, undirected graph G = (V,E,A) i.e. highways that go
% both waysa
%
% traj = feas_traj(C)
% Create feas_traj function based on A* search / Dijkstra's
% returns X no. of shortest time paths w/o traffic (best case)
% Before developing the above, use a basic problem
traj.num_cars = 2; % scalar indicating the number of cars on the network
traj.edges = {4,3}; % cell array indicating the numbers of edges traversed
traj.max_edges = 4;
% representing G with adjacency list here
traj.sequence = {[1 3 4 5],[2 4 5]}; % cell array of the node sequence
traj.distances = {[0 25 10 30], [0 15 30]}; % cell array of the edge weights
traj.cum_distances = {[0 25 35 65], [0 15 45]};
n_nodes = 5;
A = [0 1 1 0 0;
1 0 0 1 0;
1 0 0 1 1;
0 1 1 0 1;
0 0 1 1 0];
E = [0 30 25 0 0;
0 0 0 15 0;
0 0 0 10 45;
0 0 0 0 30;
0 0 0 0 0];
E = E + E';
m_x = [E_min; -1e-10];
M_x = [E_max; d_max];
M_beta_var = [1 1 1]';
m_leg = -15; % min and max distances for edges
M_leg = 30;
x0 = [E_max; 0];
%% Set up the MIQP problem (YALMIP)
% x{car}(state,k)
x = sdpvar(repmat(2,1,C),repmat(N+1,1,C));
y = binvar(C,N);
gam = binvar(C,N);
% beta_var{car}(mode,k)
beta_var = binvar(repmat(3,1,C),repmat(N+1,1,C));
delta_dist = binvar(C,N);
% define the dynamical constraints
constraints = [];
for k = 1:N
for c = 1:C
% define the dynamics in big-M
constraints = [constraints,...
(m_x - M_x).*beta_var{c}(1,k) <= - x{c}(:,k+1) + x{c}(:,k) + [P_charge_test; 0]*Ts;
(m_x - M_x).*beta_var{c}(1,k) <= x{c}(:,k+1) - x{c}(:,k) - [P_charge_test; 0]*Ts;
(m_x - M_x).*beta_var{c}(2,k) <= - x{c}(:,k+1) + x{c}(:,k);
(m_x - M_x).*beta_var{c}(2,k) <= x{c}(:,k+1) - x{c}(:,k);
(m_x - M_x).*beta_var{c}(3,k) <= - x{c}(:,k+1) + x{c}(:,k) + [-P_drive_test; 5];
(m_x - M_x).*beta_var{c}(3,k) <= x{c}(:,k+1) - x{c}(:,k) - [-P_drive_test; 5],...
beta_var{c}(1,k) + beta_var{c}(2,k) + beta_var{c}(3,k) == 2
];
% define the switching logic, i(t)
constraints = [constraints,...
0 <= beta_var{c}(1,k) <= M_beta_var(1)*abs(2 - gam(c,k) - y(c,k));
0 <= beta_var{c}(2,k) <= M_beta_var(2)*abs(1 - gam(c,k) - y(c,k));
0 <= beta_var{c}(3,k) <= M_beta_var(3)*abs(0 - gam(c,k) - y(c,k))];
% add the constraints on gamma (at a node?)
for j = 2:traj.distances{c}-1
check_location = (2-proximity_margin)*traj.distances{c}(j-1)...
<= x{c}(2,k) <= ...
proximity_margin*traj.distances{c}(j);
% constraints = [constraints,...
% 0 <= gam(c,k) <= M_gam*(1-check_location)
% ];
end
% time invariant box constraints on the state
constraints = [constraints,...
m_x <= x{c}(:,k+1) <= M_x];
end
end
initial_conditions = [];
terminal_constraints = [];
for c = 1:C
initial_conditions = [initial_conditions,...
x{c}(:,1) == x0,...
m_x <= x{c}(:,1) <= M_x];
k = N+1;
terminal_constraints = [terminal_constraints, ...
beta_var{c}(1,k) + beta_var{c}(2,k) + beta_var{c}(3,k) == 2,...
x{c}(2,N+1) >= final_margin*traj.cum_distances{c}(end)];
end
constraints = [constraints, ...
initial_conditions, terminal_constraints];
figure(3); subplot(121);
plot(constraints,x{1}(1,:),[],[],sdpsettings('relax',1));
xlabel('Car #');
ylabel('Time step');
zlabel('Energy');
subplot(122);
plot(constraints,x{1}(2,:),[],[],sdpsettings('relax',1))
xlabel('Car #');
ylabel('Time step');
zlabel('Distance');
options = sdpsettings('verbose',0,'solver','gurobi');
obj = (100-x{1}(2,N+1))^2 +(100-x{2}(2,N+1))^2;
p = optimize(constraints,[],options);
%% Show the results
if p.problem == 1
p
error('Infeasible');
elseif p.problem ~= 0
p
error('The above went wrong');
else
p
states = cell(C,1);
for k = 1:N+1
for c = 1:C
states{c} = [states{c},value(x{c}(:,k))];
end
end
figure(1);
for c = 1
subplot(121);
plot(states{c}(1,:),'-x'); title('Energy vs time');
legend(['Car ' num2str(c)]); hold on
subplot(122);
plot(states{c}(2,:),'-x'); title('Distance vs time');
legend(['Car ' num2str(c)]); hold on
end
figure(2);
for k = 1:N+1
if value(beta_var{1}(1,k)) == false
v_k = 3;
elseif value(beta_var{1}(2,k)) == false
v_k = 2;
else
v_k = 1;
end
scatter(k,v_k,'kx'); hold on
title('State: Car 1'); grid on
axis([1 N+1 0 3]);
end
legend('3: Charging','2: Waiting','1: Driving');
disp('Decision-making logic');
gamma__y = [value(gam(1,:))',value(y(1,:))']
end