1 | function [model,changed] = bilinearize(model) |
---|
2 | |
---|
3 | % Assume we don't do anything |
---|
4 | changed = 0; |
---|
5 | |
---|
6 | % Are there really any non-quadratic terms? |
---|
7 | if any(model.variabletype > 2) |
---|
8 | % Bugger... |
---|
9 | changed = 1; |
---|
10 | |
---|
11 | % Find a higher order term |
---|
12 | first_polynomial = find(model.variabletype == 3); |
---|
13 | model = fixbounds(model); |
---|
14 | first_polynomial = first_polynomial(1); |
---|
15 | powers = model.monomtable(first_polynomial,:); |
---|
16 | if nnz(powers) == 1 |
---|
17 | model = univariate_bilinearize(model,first_polynomial,powers); |
---|
18 | else |
---|
19 | model = multivariable_bilinearize(model,first_polynomial,powers); |
---|
20 | end |
---|
21 | % |
---|
22 | % % Find inverses etc |
---|
23 | % first_sigmonial = find(model.variabletype == 4); |
---|
24 | % if ~isempty(first_sigmonial) |
---|
25 | % first_sigmonial = first_sigmonial(1); |
---|
26 | % powers = model.monomtable(first_sigmonial,:); |
---|
27 | % if any(powers ~= fix(powers) |
---|
28 | % error('model class not supported') |
---|
29 | % else |
---|
30 | % powers_new(powers>0) = 0; |
---|
31 | % powers_new(powers>0) = 0; |
---|
32 | % end |
---|
33 | % end |
---|
34 | end |
---|
35 | |
---|
36 | function model = univariate_bilinearize(model,first_polynomial,powers); |
---|
37 | |
---|
38 | % Fix initial? |
---|
39 | fix_initials = ~isempty(model.x0); |
---|
40 | |
---|
41 | % variable^power |
---|
42 | variable = find(powers); |
---|
43 | p1 = floor(powers(variable)/2); |
---|
44 | p2 = ceil(powers(variable)/2); |
---|
45 | |
---|
46 | powers_1 = powers;powers_1(variable) = p1; |
---|
47 | powers_2 = powers;powers_2(variable) = p2; |
---|
48 | |
---|
49 | % Only recursive if power>4 |
---|
50 | switch p1+p2 |
---|
51 | case 3 |
---|
52 | [model,index2] = findoradd(model,powers_2); |
---|
53 | % Now define new variable y, replace x^3 with x*y, and add |
---|
54 | % constraint y == x^2 |
---|
55 | model.monomtable(end+1,end+1) = 1; |
---|
56 | model.variabletype(end+1) = 0; |
---|
57 | model.monomtable(first_polynomial,variable) = 1; |
---|
58 | model.monomtable(first_polynomial,end) = 1; |
---|
59 | model.variabletype(first_polynomial) = 1; |
---|
60 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
61 | model.K.f = model.K.f + 1; |
---|
62 | model.F_struc(1,end+1) = 1; |
---|
63 | model.F_struc(1,1+index2) = -1; |
---|
64 | model.c(end+1) = 0; |
---|
65 | model.Q(end+1,end+1) = 0; |
---|
66 | if fix_initials |
---|
67 | model.x0(end+1) = initial(model.x0,powers_2); |
---|
68 | % model.x0(end+1) = 0; |
---|
69 | end |
---|
70 | bound = powerbound(model.lb,model.ub,powers_2); |
---|
71 | model.lb(end+1) = -bound; |
---|
72 | model.ub(end+1) = bound; |
---|
73 | |
---|
74 | case 4 |
---|
75 | [model,index2] = findoradd(model,powers_2); |
---|
76 | model.monomtable(end+1,end+1) = 1; |
---|
77 | model.variabletype(end+1) = 0; |
---|
78 | model.monomtable(first_polynomial,variable) = 0; |
---|
79 | model.monomtable(first_polynomial,end) = 2; |
---|
80 | model.variabletype(first_polynomial) = 2; |
---|
81 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
82 | model.K.f = model.K.f + 1; |
---|
83 | model.F_struc(1,end+1) = 1; |
---|
84 | model.F_struc(1,1+index2) = -1; |
---|
85 | model.c(end+1) = 0; |
---|
86 | if fix_initials |
---|
87 | model.x0(end+1) = initial(model.x0,powers_2); |
---|
88 | % model.x0(end+1) = 0; |
---|
89 | end |
---|
90 | model.Q(end+1,end+1) = 0; |
---|
91 | bound = powerbound(model.lb,model.ub,powers_2); |
---|
92 | model.lb(end+1) = 0; |
---|
93 | model.ub(end+1) = bound; |
---|
94 | otherwise |
---|
95 | [model,index1] = findoradd(model,powers_1); |
---|
96 | [model,index2] = findoradd(model,powers_2); |
---|
97 | model.monomtable(end+1,end+1) = 1; |
---|
98 | model.monomtable(end+1,end+1) = 1; |
---|
99 | model.variabletype(end+1) = 0; |
---|
100 | model.variabletype(end+1) = 0; |
---|
101 | model.monomtable(first_polynomial,variable) = 0; |
---|
102 | model.monomtable(first_polynomial,end) = 1; |
---|
103 | model.monomtable(first_polynomial,end-1) = 1; |
---|
104 | model.variabletype(first_polynomial) = 1; |
---|
105 | |
---|
106 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
107 | model.K.f = model.K.f + 1; |
---|
108 | model.F_struc(1,end+1) = 1; |
---|
109 | model.F_struc(1,1+index1) = -1; |
---|
110 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
111 | model.K.f = model.K.f + 1; |
---|
112 | model.F_struc(1,end+1) = 1; |
---|
113 | model.F_struc(1,1+index2) = -1; |
---|
114 | |
---|
115 | model.c(end+1) = 0; |
---|
116 | model.Q(end+1,end+1) = 0; |
---|
117 | model.c(end+1) = 0; |
---|
118 | model.Q(end+1,end+1) = 0; |
---|
119 | |
---|
120 | if fix_initials |
---|
121 | x0 = model.x0; |
---|
122 | model.x0(end+1) = initial(x0,powers_1); |
---|
123 | model.x0(end+1) = initial(x0,powers_2); |
---|
124 | % model.x0(end+1) = 0; |
---|
125 | % model.x0(end+1) = 0; |
---|
126 | end |
---|
127 | bound1 = powerbound(model.lb,model.ub,powers_1); |
---|
128 | bound2 = powerbound(model.lb,model.ub,powers_2), |
---|
129 | |
---|
130 | model.lb(end+1) = -bound1; |
---|
131 | model.ub(end+1) = bound1; |
---|
132 | model.lb(end+1) = -bound2; |
---|
133 | model.ub(end+1) = bound2; |
---|
134 | end |
---|
135 | model = bilinearize(model); |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | function model = multivariable_bilinearize(model,first_polynomial,powers); |
---|
141 | |
---|
142 | % Fix initial? |
---|
143 | fix_initials = ~isempty(model.x0); |
---|
144 | |
---|
145 | variables = find(powers); |
---|
146 | mid = floor(length(variables)/2); |
---|
147 | variables_1 = variables(1:mid); |
---|
148 | variables_2 = variables(mid+1:end); |
---|
149 | powers_1 = powers; |
---|
150 | powers_2 = powers; |
---|
151 | powers_1(variables_2) = 0; |
---|
152 | powers_2(variables_1) = 0; |
---|
153 | |
---|
154 | [model,index1] = findoradd(model,powers_1); |
---|
155 | if sum(powers_1)>1 |
---|
156 | model.monomtable(end+1,end+1) = 1; |
---|
157 | pos1 = size(model.monomtable,2); |
---|
158 | model.variabletype(end+1) = 0; |
---|
159 | bound = powerbound(model.lb,model.ub,powers_1); |
---|
160 | model.lb(end+1) = -bound; |
---|
161 | model.ub(end+1) = bound; |
---|
162 | if fix_initials;model.x0(end+1) = initial(model.x0,powers_1);end |
---|
163 | end |
---|
164 | |
---|
165 | [model,index2] = findoradd(model,powers_2); |
---|
166 | if sum(powers_2)>1 |
---|
167 | model.monomtable(end+1,end+1) = 1; |
---|
168 | pos2 = size(model.monomtable,2); |
---|
169 | model.variabletype(end+1) = 0; |
---|
170 | bound = powerbound(model.lb,model.ub,powers_2); |
---|
171 | model.lb(end+1) = -bound; |
---|
172 | model.ub(end+1) = bound; |
---|
173 | if fix_initials;model.x0(end+1) = initial(model.x0,powers_2);end |
---|
174 | end |
---|
175 | |
---|
176 | model.monomtable(first_polynomial,:) = 0; |
---|
177 | if sum(powers_1)>1 |
---|
178 | model.monomtable(first_polynomial,pos1) = 1; |
---|
179 | else |
---|
180 | model.monomtable(first_polynomial,variables_1) = 1; |
---|
181 | end |
---|
182 | if sum(powers_2)>1 |
---|
183 | model.monomtable(first_polynomial,pos2) = 1; |
---|
184 | else |
---|
185 | model.monomtable(first_polynomial,variables_2) = 1; |
---|
186 | end |
---|
187 | model.variabletype(first_polynomial) = 1; |
---|
188 | |
---|
189 | if sum(powers_1)>1 |
---|
190 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
191 | model.K.f = model.K.f + 1; |
---|
192 | model.F_struc(1,end+1) = 1; |
---|
193 | model.F_struc(1,1+index1) = -1; |
---|
194 | model.c(end+1) = 0; |
---|
195 | model.Q(end+1,end+1) = 0; |
---|
196 | end |
---|
197 | |
---|
198 | if sum(powers_2)>1 |
---|
199 | model.F_struc = [zeros(1,size(model.F_struc,2));model.F_struc]; |
---|
200 | model.K.f = model.K.f + 1; |
---|
201 | model.F_struc(1,end+1) = 1; |
---|
202 | model.F_struc(1,1+index2) = -1; |
---|
203 | model.c(end+1) = 0; |
---|
204 | model.Q(end+1,end+1) = 0; |
---|
205 | end |
---|
206 | model = bilinearize(model); |
---|
207 | |
---|
208 | |
---|
209 | function [model,index1] = findoradd(model,powers_1); |
---|
210 | |
---|
211 | if length(powers_1) < size(model.monomtable,2) |
---|
212 | powers_1(size(model.monomtable,1)) = 0; |
---|
213 | end |
---|
214 | |
---|
215 | index1 = findrows(model.monomtable,powers_1); |
---|
216 | |
---|
217 | if isempty(index1) |
---|
218 | model.monomtable = [model.monomtable;powers_1]; |
---|
219 | model.monomtable(end,end+1) = 0; |
---|
220 | index1 = size(model.monomtable,1); |
---|
221 | model.c(end+1) = 0; |
---|
222 | model.Q(end+1,end+1) = 0; |
---|
223 | model.F_struc(1,end+1) = 0; |
---|
224 | bound = powerbound(model.lb,model.ub,powers_1); |
---|
225 | model.lb(end+1) = -bound; |
---|
226 | model.ub(end+1) = bound; |
---|
227 | if ~isempty(model.x0) |
---|
228 | model.x0(end+1) = 0; |
---|
229 | end |
---|
230 | switch sum(powers_1) |
---|
231 | case 1 |
---|
232 | model.variabletype(end+1) = 0; |
---|
233 | case 2 |
---|
234 | model.variabletype(end+1) = 2; |
---|
235 | otherwise |
---|
236 | model.variabletype(end+1) = 3; |
---|
237 | end |
---|
238 | end |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | function model = fixbounds(model); |
---|
243 | polynomials = find(model.variabletype > 0); |
---|
244 | LU = max([abs(model.lb) abs(model.ub)],[],2); |
---|
245 | for i = 1:length(polynomials) |
---|
246 | j = polynomials(i); |
---|
247 | if j<=length(model.lb) |
---|
248 | vars = find(model.monomtable(j,:)); |
---|
249 | bound = 1; |
---|
250 | for k = 1:length(vars) |
---|
251 | bound = bound * LU(vars(k))^model.monomtable(j,vars(k)); |
---|
252 | end |
---|
253 | model.lb(j) = max(model.lb(j),-bound); |
---|
254 | model.ub(j) = min(model.ub(j),bound); |
---|
255 | end |
---|
256 | end |
---|
257 | |
---|
258 | function bound = powerbound(lb,ub,powers) |
---|
259 | LU = max([abs(lb) abs(ub)],[],2); |
---|
260 | vars = find(powers); |
---|
261 | bound = 1; |
---|
262 | for k = 1:length(vars) |
---|
263 | bound = bound * LU(vars(k))^powers(vars(k)); |
---|
264 | end |
---|
265 | |
---|
266 | function z = initial(x0,powers) |
---|
267 | z = 1; |
---|
268 | vars = find(powers); |
---|
269 | for k = 1:length(vars) |
---|
270 | z = z * x0(vars(k))^powers(vars(k)); |
---|
271 | end |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | |
---|