1 | function [prob,problem] = yalmip2geometric(options,F_struc,c,Q,K,ub,lb,mt,linear_variables,extended_variables); |
---|
2 | |
---|
3 | problem = 0; |
---|
4 | prob = []; |
---|
5 | h = []; |
---|
6 | A = []; % powers in objective/inequalities |
---|
7 | b = []; % coefficients in objective/inequalities |
---|
8 | G = []; % powers in equalities |
---|
9 | h = []; % coefficients in equalities |
---|
10 | |
---|
11 | % ********************************************************** |
---|
12 | % Initial sanity check for posynomial problem structure |
---|
13 | % ********************************************************** |
---|
14 | if any(ub<0) |
---|
15 | problem = -4; |
---|
16 | return |
---|
17 | end |
---|
18 | |
---|
19 | % ********************************************************** |
---|
20 | % Setup data related to objective |
---|
21 | % ********************************************************** |
---|
22 | vars_in_objective = find(c); |
---|
23 | A = mt(vars_in_objective,linear_variables); |
---|
24 | b = c(vars_in_objective); |
---|
25 | map_pos = 0; |
---|
26 | map = repmat(map_pos,length(b),1); |
---|
27 | |
---|
28 | % ********************************************************** |
---|
29 | % Invert negative monomial, or this is not a posynomial |
---|
30 | % ********************************************************** |
---|
31 | if any(b<0) |
---|
32 | if nnz(b)==1 |
---|
33 | b(find(b)) = -1/b(find(b)); |
---|
34 | A = -A; |
---|
35 | else |
---|
36 | problem = -4; |
---|
37 | return |
---|
38 | end |
---|
39 | end |
---|
40 | |
---|
41 | % ********************************************************** |
---|
42 | % Setup data related to inequalities sum(?x^?) > 0 |
---|
43 | % Loop through all inequalities, find the element with |
---|
44 | % positive coefficient, divide by this term. |
---|
45 | % ********************************************************** |
---|
46 | mte = blkdiag(0,mt); % Extend the monomial table with the monomial x^0 |
---|
47 | for j = 1+K.f:size(F_struc,1); |
---|
48 | k = find(F_struc(j,:)>0); |
---|
49 | if length(k) == 1 |
---|
50 | vars_in_c = find(F_struc(j,:)); |
---|
51 | Atemp = mte(vars_in_c,linear_variables+1); |
---|
52 | Atemp = Atemp - repmat(mte(k,linear_variables+1),size(Atemp,1),1); |
---|
53 | btemp = reshape(-F_struc(j,vars_in_c)/F_struc(j,k),length(vars_in_c),1); |
---|
54 | removed = find(sum(abs(Atemp),2)==0); |
---|
55 | Atemp(removed,:) = []; |
---|
56 | btemp(removed) = []; |
---|
57 | if length(btemp) > 0 |
---|
58 | A = [A;Atemp]; |
---|
59 | b = [b;btemp]; |
---|
60 | map_pos = map_pos + 1; |
---|
61 | % map = [map;repmat(map_pos,length(btemp),1)]; |
---|
62 | map = [map;map_pos*ones(length(btemp),1)]; |
---|
63 | end |
---|
64 | else |
---|
65 | if all(F_struc(j,:)>=0) |
---|
66 | % Redundant x+y > 1 |
---|
67 | elseif all(F_struc(j,:)<=0) |
---|
68 | % Infeasible x+y < -1 |
---|
69 | problem = 1; |
---|
70 | return |
---|
71 | % elseif F_struc(j,1)<0 & any(F_struc(j,2:end)<0) |
---|
72 | % % Trivially infeasible |
---|
73 | % problem = 1; |
---|
74 | % return |
---|
75 | else |
---|
76 | % Not posynomial at least |
---|
77 | problem = -4; |
---|
78 | return |
---|
79 | end |
---|
80 | end |
---|
81 | end |
---|
82 | |
---|
83 | % ********************************************************** |
---|
84 | % Fix equality constraints coming from fractional powers |
---|
85 | % of posynomials. |
---|
86 | % An equality constraint a(x) = t can be relaxed to a(x)<t |
---|
87 | % if only positive powers of t are used in the program. |
---|
88 | % NOTE : YALMIP defines these equalities as t-a(x)==0 |
---|
89 | % FIX : Is this check enough? |
---|
90 | % FIX : Speed things up... |
---|
91 | % ********************************************************** |
---|
92 | for j = 1:1:K.f |
---|
93 | k = find(F_struc(j,:)>0); |
---|
94 | if length(k)>1 |
---|
95 | error('Nonpositive terms in fractional expression in geometric program?') |
---|
96 | else |
---|
97 | if k==1 | ~ismember(k-1,extended_variables) |
---|
98 | % Monomial equality ok! |
---|
99 | else |
---|
100 | if all(A(:,find(ismember(linear_variables,k-1)))>=0) |
---|
101 | else |
---|
102 | error('Negative powers in fractional term in geometric program?') |
---|
103 | end |
---|
104 | end |
---|
105 | end |
---|
106 | end |
---|
107 | |
---|
108 | % ********************************************************** |
---|
109 | % Setup data related to inequalities derived from equalities |
---|
110 | % i.e. ax^b == 1 replaced with ax^b<1, (x^-b)/a < 1 |
---|
111 | % (except for extended variables according to above) |
---|
112 | % ********************************************************** |
---|
113 | for j = 1:1:K.f |
---|
114 | k = find(F_struc(j,:)>0); |
---|
115 | if length(k) == 1 |
---|
116 | vars_in_c = find(F_struc(j,:)); |
---|
117 | Atemp = mte(vars_in_c,linear_variables+1); |
---|
118 | Atemp = Atemp - repmat(mte(k,linear_variables+1),size(Atemp,1),1); |
---|
119 | btemp = reshape(-F_struc(j,vars_in_c)/F_struc(j,k),length(vars_in_c),1); |
---|
120 | removed = find(sum(abs(Atemp),2)==0); |
---|
121 | Atemp(removed,:) = []; |
---|
122 | btemp(removed) = []; |
---|
123 | |
---|
124 | if ~ismember(k-1,extended_variables) |
---|
125 | if length(btemp)==1 |
---|
126 | G = [G;Atemp]; |
---|
127 | h = [h;btemp]; |
---|
128 | else |
---|
129 | % a(x)+b(x) == c(x) not supported |
---|
130 | problem = -4; |
---|
131 | return |
---|
132 | end |
---|
133 | else |
---|
134 | % Just add upper inequalities |
---|
135 | A = [A;Atemp]; |
---|
136 | b = [b;btemp]; |
---|
137 | map_pos = map_pos + 1; |
---|
138 | this_map = repmat(map_pos,length(btemp),1); |
---|
139 | map = [map;this_map]; |
---|
140 | end |
---|
141 | else |
---|
142 | % a(x) < b(x) + c(x) not supported |
---|
143 | problem = -4; |
---|
144 | return |
---|
145 | end |
---|
146 | |
---|
147 | end |
---|
148 | |
---|
149 | % ********************************************************** |
---|
150 | % MOSEK does not like upper boud == lower bound |
---|
151 | % ********************************************************** |
---|
152 | if ~(isempty(lb) | isempty(ub)) |
---|
153 | fixed_variables = find(lb(linear_variables)==ub(linear_variables)); |
---|
154 | if ~isempty(fixed_variables) |
---|
155 | fixed_values = lb(linear_variables(fixed_variables)); |
---|
156 | if any(fixed_values==0) |
---|
157 | zeros_vars = find((lb(linear_variables)==ub(linear_variables)) & (lb(linear_variables)==0)); |
---|
158 | if any(A(:,zeros_vars)<0) |
---|
159 | problem = 1; |
---|
160 | return |
---|
161 | end |
---|
162 | end |
---|
163 | |
---|
164 | for i = 1:size(A,1) |
---|
165 | this_gain = 1; |
---|
166 | for j = 1:size(fixed_variables) |
---|
167 | this_gain = this_gain*fixed_values(j)^A(i,fixed_variables(j)); |
---|
168 | A(i,fixed_variables(j))=0; |
---|
169 | end |
---|
170 | b(i)=b(i)*this_gain; |
---|
171 | end |
---|
172 | end |
---|
173 | end |
---|
174 | if ~isempty(ub) |
---|
175 | for i = 1:length(ub(linear_variables)) |
---|
176 | if ~(isinf(ub(linear_variables(i))) | ub(linear_variables(i))==0) |
---|
177 | A(end+1,i) = 1; |
---|
178 | b(end+1) = 1/ub(linear_variables(i)); |
---|
179 | map_pos = map_pos + 1; |
---|
180 | map(end+1)=map_pos; |
---|
181 | end |
---|
182 | end |
---|
183 | end |
---|
184 | if ~isempty(lb) |
---|
185 | for i = 1:length(lb(linear_variables)) |
---|
186 | if ~(isinf(lb(linear_variables(i))) | lb(linear_variables(i))==0) |
---|
187 | A(end+1,i) = -1; |
---|
188 | b(end+1) = lb(linear_variables(i)); |
---|
189 | map_pos = map_pos + 1; |
---|
190 | map(end+1)=map_pos; |
---|
191 | end |
---|
192 | end |
---|
193 | end |
---|
194 | |
---|
195 | prob.b = b;prob.A = A;prob.map = map;prob.G = G;prob.h = h; |
---|