-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwa_bounds.m
192 lines (163 loc) · 4.68 KB
/
pwa_bounds.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
190
191
function PWABounds = pwa_bounds(nlfun, Option)
% Nonlinear function: nlfun.Handle
% Vertices: nlfun.X
% Domain: nlfun.Domain
% Resolution: nlfun.Resolution
% Parameters: nlfun.Param
% The linearization point: nlfun.xstar
% The linear approximation: nlfun.AbarLin
% nlfun.Index = Index of the variables in the domain of nonlinearity
% nlfun.YIndex = Index of the nonlinear entries
if nargin == 1,
Option.ObjFun = 'L2';
Option.Type = 'UB'; % 'UB','AP'
Option.SafetyBound = 0.0; %
else
if ~isfield(Option,'ObjFun'),
Option.ObjFun = 'L2';
end
if ~isfield(Option,'Type'),
Option.Type = 'UB'; % 'LB','UB'
end
if ~isfield(Option,'SafetyBound'),
Option.SafetyBound = 0.0;
end
end
f = nlfun.Handle; % Specify the handle of the nonlinear function
a=[];
b=[];
for i=1:length(nlfun.Domain),
a=[a; nlfun.Domain{i}(1)]; % Set start point of the domain
b=[b; nlfun.Domain{i}(end)]; % Set start point of the domain
end
N = nlfun.Resolution; % The resolution of sampling
X = nlfun.X; % Vertices of the partition
if isfield(nlfun,'xcl'),
nlfun.xstar = nlfun.xcl;
end
if ~ismember(nlfun.xstar',X,'rows'),
X = [X; nlfun.xstar'];
end
[m n] = size(X);
if n == 1,
X = sort(X);
end
if isfield(nlfun,'Param'),
Param = nlfun.Param; % Parameters of the nonlinear function
elseif isfield(nlfun,'Parameters'),
Param = nlfun.Parameters; % Parameters of the nonlinear function
else
Param = [];
end
for i = 1:n,
if a(i) >= b(i), % Check ?
eval(['error(''nlfun.domain(1,' num2str(i) ') should be less than nlfun.domain(2,' num2str(i) ').'');'])
end
end
try
Y = pwa_nlfun(nlfun,a,Param);
catch
error(['The function can not be evaluated.']);
end
% Create fine mesh
W = pwa_grid(a,b,N);
Z = pwa_nlfun(nlfun,W,Param);
p = size(W,1);
q = size(Z,2);
% Ver = ver('MATLAB');
% if str2num(Ver.Version(1)) == 7,
% T = delaunayn(X,{'Qt','Qbb','Qc','Qz'});
% else
% T = delaunayn(X);
% end
dt = DelaunayTri(X);
T = dt.Triangulation;
numt = size(T,1); % Determine number of triangulations (cells)
% Optimization problem
constraints = set([]);
for i = 1:numt,
Abar{i} = sdpvar(q,n+1,'full'); % PWA approximation parameters
end
% The approximation is exact at xstar
if isfield(nlfun,'xstar'),
% if n == 2,
% t = tsearch(X(:,1),X(:,2),T,nlfun.xstar(1),nlfun.xstar(2));
% else
% t = tsearchn(X,T,nlfun.xstar');
% end
t = pointLocation(dt,nlfun.xstar');
constraints = constraints + set(Abar{t}*[nlfun.xstar;1] == pwa_nlfun(nlfun,nlfun.xstar',Param)',['Abar x_cl_bar =f(x_cl)']);
end
% Continuity constraints
if numt > 1,
for i = 1:m,
Ti = find(any((T==i)'));
if length(Ti) > 1,
for j = 1:length(Ti)-1,
k = j+1;
constraints = constraints + set((Abar{Ti(k)}-Abar{Ti(j)})*[X(i,:) 1]'==0,['(Abar' num2str(Ti(k)) '-Abar' num2str(Ti(j)) ')*[X(' num2str(i) ',:) 1]''=0']);
end
end
end
end
% Error
% if n == 2,
% t = tsearch(X(:,1),X(:,2),T,W(:,1),W(:,2));
% else
% t = tsearchn(X,T,W);
% end
t = pointLocation(dt,W);
% Objective
if ~isfield(Option,'ObjFun'),
Option.ObjFun = 'L2';
end
Err=[];
if strcmp(Option.ObjFun,'L2'),
Obj = 0;
for i = 1:p,
if n>1,
if strcmp(Option.Type,'LB'),
constraints = constraints + set(Z(i,:)'>Abar{t(i)}*[W(i,:) 1]','Z>AbarWbar');
elseif strcmp(Option.Type,'UB'),
constraints = constraints + set(Z(i,:)'<Abar{t(i)}*[W(i,:) 1]','Z<AbarWbar');
end
else,
if strcmp(Option.Type,'LB'),
constraints = constraints + set(Z(i,:)'>Abar{t(i)}*[W(i,:) 1]','Z>AbarWbar');
elseif strcmp(Option.Type,'UB'),
constraints = constraints + set(Z(i,:)'<Abar{t(i)}*[W(i,:) 1]','Z<AbarWbar');
end
end
Error = Z(i,:)'-Abar{t(i)}*[W(i,:) 1]';
Obj = Obj + sum(sum(Error.*Error));
Err = [Err; Error'];
end
end
% Solve the optimization problem
solvesdp(constraints,Obj,sdpsettings('usex0',1,'shift',1e-7)); %,sdpsettings('solver','SEDUMI'));
checkset(constraints);
for i = 1:length(Abar),
Abar{i} = double(Abar{i});
end
Obj = double(Obj);
Err = double(Err);
Y = [];
for i = 1:m,
J = find(any((T==i)'));
Y = [Y; (Abar{J(1)}*[X(i,:) 1]')'];
end
PWABounds.Abar = Abar;
PWABounds.Obj = Obj;
PWABounds.X = X;
PWABounds.Y = Y;
PWABounds.W = W;
PWABounds.Z = Z;
PWABounds.T = T;
PWABounds.dt = dt;
PWABounds.Err = Err;
function y = sat(x)
if abs(x)<1,
y=x;
else,
y = sign(x);
end