1 | function model = bb_mpmilp(Matrices,OriginalModel,model,options,ExploreSpace) |
---|
2 | |
---|
3 | nu = Matrices.nu; |
---|
4 | nx = Matrices.nx; |
---|
5 | |
---|
6 | if nargin<5 |
---|
7 | % Derive a bounding box |
---|
8 | [global_lower,global_upper] = detect_and_improve_bounds(Matrices,Matrices.lb,Matrices.ub,Matrices.binary_var_index,options); |
---|
9 | Matrices.lb = global_lower; |
---|
10 | Matrices.ub = global_upper; |
---|
11 | A = [eye(nx);-eye(nx)]; |
---|
12 | b = [global_upper(end-nx+1:end);-global_lower(end-nx+1:end)]; |
---|
13 | ExploreSpace = polytope(A,b); |
---|
14 | model = bb_mpmilp(Matrices,OriginalModel,model,options,ExploreSpace); |
---|
15 | return |
---|
16 | end |
---|
17 | |
---|
18 | % Find a feasible integer |
---|
19 | vartype = repmat('C',nx+nu,1); |
---|
20 | vartype(Matrices.binary_var_index) = 'B'; |
---|
21 | [xmin,fmin,how,exitflag]=mpt_solveMILP([Matrices.H';zeros(nx,1)],[Matrices.G -Matrices.E],Matrices.W,[Matrices.Aeq Matrices.Beq],Matrices.beq,Matrices.lb,Matrices.ub,vartype,[],[],options.mpt.milpsolver); |
---|
22 | |
---|
23 | while isequal(how,'ok') |
---|
24 | |
---|
25 | feasible_binary = round(xmin(Matrices.binary_var_index)); |
---|
26 | |
---|
27 | % Solve mpLP for this binary |
---|
28 | lower = Matrices.lb; |
---|
29 | upper = Matrices.ub; |
---|
30 | lower(Matrices.binary_var_index) = feasible_binary; |
---|
31 | upper(Matrices.binary_var_index) = feasible_binary; |
---|
32 | nodeModel = solveNode(Matrices,lower,upper,OriginalModel,[],options); |
---|
33 | |
---|
34 | % Cut away this binary solution |
---|
35 | cut = zeros(1,nu); |
---|
36 | zv = find(feasible_binary==0); |
---|
37 | ov = find(feasible_binary==1); |
---|
38 | cut(Matrices.binary_var_index(ov)) = 1; |
---|
39 | cut(Matrices.binary_var_index(zv)) = -1; |
---|
40 | Matrices.G = [Matrices.G;cut]; |
---|
41 | Matrices.E = [Matrices.E;zeros(1,nx)]; |
---|
42 | Matrices.W = [Matrices.W;length(ov)-1]; |
---|
43 | |
---|
44 | if ~isempty(nodeModel) |
---|
45 | disp('Real case') |
---|
46 | % Dig into this set |
---|
47 | plot(nodeModel{1}.Pn); |
---|
48 | for i = 1:length(nodeModel{1}.Pn) |
---|
49 | newMatrices = Matrices; |
---|
50 | % Add constraint that we explore current region |
---|
51 | [H,K] = double(nodeModel{1}.Pn(i)); |
---|
52 | newMatrices.G = [newMatrices.G;zeros(size(H,1),nu)]; |
---|
53 | newMatrices.E = [newMatrices.E;-H]; |
---|
54 | newMatrices.W = [newMatrices.W;K]; |
---|
55 | % Add upper bound constraint HU < |
---|
56 | newMatrices.G = [newMatrices.G;newMatrices.H]; |
---|
57 | newMatrices.E = [newMatrices.E;nodeModel{1}.Bi{i}]; |
---|
58 | newMatrices.W = [newMatrices.W;nodeModel{1}.Ci{i}]; |
---|
59 | |
---|
60 | % Solve recusively in this new sub region |
---|
61 | plot(nodeModel{1}.Pn(i),'g') |
---|
62 | newmodel{i} = bb_mpmilp(newMatrices,OriginalModel,[],options,nodeModel{1}.Pn(i)); |
---|
63 | end |
---|
64 | |
---|
65 | % Now we have explored all feasible regions. |
---|
66 | % As a second step, we must explore infeasible regions |
---|
67 | InfeasibleRegions = regiondiff(ExploreSpace,nodeModel{1}.Pfinal); |
---|
68 | for i=1:length(InfeasibleRegions) |
---|
69 | |
---|
70 | newMatrices = Matrices; |
---|
71 | [H,K] = double(InfeasibleRegions(i)); |
---|
72 | newMatrices.G = [newMatrices.G;zeros(size(H,1),nu)]; |
---|
73 | newMatrices.E = [newMatrices.E;-H]; |
---|
74 | newMatrices.W = [newMatrices.W;K]; |
---|
75 | |
---|
76 | % Solve recusively in this new sub region |
---|
77 | plot(InfeasibleRegions(i),'g') |
---|
78 | newmodel{end+1} = bb_mpmilp(newMatrices,OriginalModel,model,options,InfeasibleRegions(i)); |
---|
79 | end |
---|
80 | |
---|
81 | % Now, we have the upper bound models nodeModel{1} for the investigated |
---|
82 | % binary varible, and candidate models in newmodel |
---|
83 | |
---|
84 | % Assume nodeModel is best |
---|
85 | model = nodeModel; |
---|
86 | for i = 1:length(newmodel) |
---|
87 | if ~isempty(newmodel{i}) |
---|
88 | model = {rmovlps({nodeModel{1},newmodel{i}{1}},struct('verbose',0))}; |
---|
89 | end |
---|
90 | end |
---|
91 | end |
---|
92 | |
---|
93 | % Pick a new integer solution |
---|
94 | [xmin,fmin,how,exitflag]=mpt_solveMILP([Matrices.H';zeros(nx,1)],[Matrices.G -Matrices.E],Matrices.W,[Matrices.Aeq Matrices.Beq],Matrices.beq,Matrices.lb,Matrices.ub,vartype,[],[],options.mpt.milpsolver); |
---|
95 | end |
---|
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 | function model = duabb_mpmilp(Matrices,OriginalModel,model,options) |
---|
121 | |
---|
122 | nu = size(Matrices.G,2); |
---|
123 | nx = size(Matrices.E,2); |
---|
124 | vartype = repmat('C',nx+nu,1); |
---|
125 | vartype(Matrices.binary_var_index) = 'B'; |
---|
126 | |
---|
127 | % Derive a bounding box |
---|
128 | [global_lower,global_upper] = detect_and_improve_bounds(Matrices,Matrices.lb,Matrices.ub,Matrices.binary_var_index,options); |
---|
129 | Matrices.lb = global_lower; |
---|
130 | Matrices.ub = global_upper; |
---|
131 | |
---|
132 | % Find a feasible integer |
---|
133 | [xmin,fmin,how,exitflag]=mpt_solveMILP([Matrices.H';zeros(nx,1)],[Matrices.G -Matrices.E],Matrices.W,[Matrices.Aeq Matrices.Beq],Matrices.beq,Matrices.lb,Matrices.ub,vartype,[],[],options.mpt.lpsolver); |
---|
134 | |
---|
135 | while isequal(how,'ok') |
---|
136 | |
---|
137 | feasible_binary = xmin(Matrices.binary_var_index); |
---|
138 | |
---|
139 | % Solve mpLP for this binary |
---|
140 | lower = Matrices.lb; |
---|
141 | upper = Matrices.ub; |
---|
142 | lower(Matrices.binary_var_index) = feasible_binary; |
---|
143 | upper(Matrices.binary_var_index) = feasible_binary; |
---|
144 | nodeModel = solveNode(Matrices,lower,upper,OriginalModel,model,options); |
---|
145 | |
---|
146 | if ~isempty(nodeModel) |
---|
147 | % Dig into this set |
---|
148 | plot(nodeModel{1}.Pn); |
---|
149 | if ~isempty(model) |
---|
150 | model = rmovlps({model{1},nodeModel{1}}); |
---|
151 | end |
---|
152 | for i = 1:length(nodeModel{1}.Pn) |
---|
153 | newMatrices = Matrices; |
---|
154 | % Add constraint that we explore current region |
---|
155 | [H,K] = double(nodeModel{1}.Pn(i)); |
---|
156 | newMatrices.G = [newMatrices.G;zeros(size(H,1),nu)]; |
---|
157 | newMatrices.E = [newMatrices.E;-H]; |
---|
158 | newMatrices.W = [newMatrices.W;K]; |
---|
159 | % Add upper bound constraint HU < |
---|
160 | newMatrices.G = [newMatrices.G;newMatrices.H]; |
---|
161 | newMatrices.E = [newMatrices.E;nodeModel{1}.Bi{i}]; |
---|
162 | newMatrices.W = [newMatrices.W;nodeModel{1}.Ci{i}]; |
---|
163 | % Cut away this binary solution |
---|
164 | cut = zeros(1,nu); |
---|
165 | zv = find(feasible_binary==0); |
---|
166 | ov = find(feasible_binary==1); |
---|
167 | cut(Matrices.binary_var_index(ov)) = 1; |
---|
168 | cut(Matrices.binary_var_index(zv)) = -1; |
---|
169 | newMatrices.G = [newMatrices.G;cut]; |
---|
170 | newMatrices.E = [newMatrices.E;zeros(1,nx)]; |
---|
171 | newMatrices.W = [newMatrices.W;length(ov)-1]; |
---|
172 | |
---|
173 | % Solve recusively in this new sub region |
---|
174 | plot(nodeModel{1}.Pn(i),'g') |
---|
175 | newmodel = solveDua(newMatrices,OriginalModel,nodeModel,options); |
---|
176 | if ~isempty(newmodel) |
---|
177 | if isempty(model) |
---|
178 | model = newmodel; |
---|
179 | else |
---|
180 | model = {rmovlps({model{1},newmodel{1}})}; |
---|
181 | end |
---|
182 | end |
---|
183 | end |
---|
184 | end |
---|
185 | % Cut away this binary solution |
---|
186 | cut = zeros(1,nu); |
---|
187 | zv = find(feasible_binary==0); |
---|
188 | ov = find(feasible_binary==1); |
---|
189 | cut(Matrices.binary_var_index(ov)) = 1; |
---|
190 | cut(Matrices.binary_var_index(zv)) = -1; |
---|
191 | Matrices.G = [Matrices.G;cut]; |
---|
192 | Matrices.E = [Matrices.E;zeros(1,nx)]; |
---|
193 | Matrices.W = [Matrices.W;length(ov)-1]; |
---|
194 | % Pick a new integer solution |
---|
195 | [xmin,fmin,how,exitflag]=mpt_solveMILP([Matrices.H';zeros(nx,1)],[Matrices.G -Matrices.E],Matrices.W,[Matrices.Aeq Matrices.Beq],Matrices.beq,Matrices.lb,Matrices.ub,vartype,[],[],options.mpt.lpsolver); |
---|
196 | end |
---|
197 | |
---|
198 | % now solve in the complement regions (where this feasible solution is |
---|
199 | % infeasible) |
---|
200 | % OtherRegions = regiondiff(polytope([eye(nx);-eye(nx)],[global_upper(end-nx+1:end);-global_lower(end-nx+1:end)]),model{1}.Pfinal); |
---|
201 | % for i = 1:length(OtherRegions) |
---|
202 | % |
---|
203 | % end |
---|