1 | function sol = dua_test(F,obj,ops,x) |
---|
2 | |
---|
3 | % F : All constraints |
---|
4 | % obj : Objective |
---|
5 | % x : parametric variables |
---|
6 | % y : all binary variables |
---|
7 | |
---|
8 | if isempty(ops) |
---|
9 | ops = sdpsettings; |
---|
10 | end |
---|
11 | ops.mp.algorithm = 1; |
---|
12 | ops.cachesolvers = 0; |
---|
13 | ops.mp.presolve=1; |
---|
14 | ops.solver = ''; |
---|
15 | |
---|
16 | % Expand nonlinear operators only once |
---|
17 | F = expandmodel(F,obj); |
---|
18 | ops.expand = 0; |
---|
19 | |
---|
20 | % Gather all binary variables |
---|
21 | y = unique([depends(F) depends(obj)]); |
---|
22 | n = length(y)-length(x); |
---|
23 | y = intersect(y,[yalmip('binvariables') depends(F(find(is(F,'binary'))))]); |
---|
24 | y = recover(y); |
---|
25 | |
---|
26 | % Make sure binary relaxations satisfy 0-1 constraints |
---|
27 | F = F + set(0 <= y <= 1); |
---|
28 | |
---|
29 | % Bounded search-space |
---|
30 | Universe = unitbox(length(x),10); |
---|
31 | |
---|
32 | % recursive, starting in maximum universe |
---|
33 | sol = sub_dua(F,obj,ops,x,y,Universe); |
---|
34 | |
---|
35 | % Nice, however, we have introduced variables along the process, so the |
---|
36 | % parametric solutions contain variables we don't care about |
---|
37 | for i = 1:length(sol) |
---|
38 | for j = 1:length(sol{i}.Fi) |
---|
39 | sol{i}.Fi{j} = sol{i}.Fi{j}(1:n,:); |
---|
40 | sol{i}.Gi{j} = sol{i}.Gi{j}(1:n,:); |
---|
41 | end |
---|
42 | end |
---|
43 | |
---|
44 | |
---|
45 | function sol = sub_dua(F,obj,ops,x,y,Universe) |
---|
46 | |
---|
47 | sol = {}; |
---|
48 | |
---|
49 | % Find a feasible point in this region |
---|
50 | % ops.verbose = ops.verbose-1; |
---|
51 | % intsol = solvesdp(F,obj,ops); |
---|
52 | % ops.verbose = ops.verbose+1; |
---|
53 | |
---|
54 | y_feasible = 0; |
---|
55 | if 1 |
---|
56 | localsol = {[]}; |
---|
57 | intsol.problem = 0; |
---|
58 | while isempty(localsol{1}) & (intsol.problem == 0) |
---|
59 | ops.verbose = ops.verbose-1; |
---|
60 | intsol = solvesdp(F,obj,ops); |
---|
61 | ops.verbose = ops.verbose+1; |
---|
62 | if intsol.problem == 0 |
---|
63 | if isequal(y_feasible , round(double(y))) |
---|
64 | 1; |
---|
65 | end |
---|
66 | y_feasible = round(double(y)) |
---|
67 | 'start' |
---|
68 | localsol = solvemp(F+set(y == y_feasible),obj,sdpsettings(ops,'relax',1),x); |
---|
69 | 'end' |
---|
70 | if isempty(localsol{1}) |
---|
71 | F = F + not_equal(y,y_feasible); |
---|
72 | end |
---|
73 | end |
---|
74 | end |
---|
75 | else |
---|
76 | ops.verbose = ops.verbose-1; |
---|
77 | intsol = solvesdp(F,obj,ops); |
---|
78 | ops.verbose = ops.verbose+1; |
---|
79 | y_feasible = round(double(y)); |
---|
80 | end |
---|
81 | |
---|
82 | %if intsol.problem == 0 |
---|
83 | if ~isempty(localsol{1})%intsol.problem == 0 |
---|
84 | |
---|
85 | % Compute feasible mpLP solution for this binary combination |
---|
86 | %y_feasible = round(double(y)); |
---|
87 | %localsol = solvemp(F+set(y == y_feasible),obj,sdpsettings(ops,'relax',1),x); |
---|
88 | |
---|
89 | % YALMIP syntax... |
---|
90 | if isa(localsol,'cell') |
---|
91 | localsol = localsol{1}; |
---|
92 | end |
---|
93 | |
---|
94 | % Now we want to find solutions with other binary combinations, in |
---|
95 | % order to find the best one. Cut away the current bionary using |
---|
96 | % overloaded not equal |
---|
97 | F = F + not_equal(y , y_feasible); |
---|
98 | |
---|
99 | % Could be that the binary was feasible, but the feasible space in the |
---|
100 | % other variables is empty/lower-dimensional |
---|
101 | if ~isempty(localsol) |
---|
102 | % Dig into this solution. Try to find another feasible binary |
---|
103 | % combination, with a better cost, in each of the regions |
---|
104 | for i = 1:length(localsol.Pn) |
---|
105 | G = F; |
---|
106 | % Better cost |
---|
107 | G = G + set(obj <= localsol.Bi{i}*x + localsol.Ci{i}); |
---|
108 | % In this region |
---|
109 | [H,K] = double(localsol.Pn(i)); |
---|
110 | G = G + set(H*x <= K); |
---|
111 | % Recurse |
---|
112 | diggsol{i} = sub_dua(G,obj,ops,x,y,localsol.Pn(i)); |
---|
113 | end |
---|
114 | |
---|
115 | % Create all neighbour regions, and compute solutions in them too |
---|
116 | flipped = regiondiff(union(Universe),union(localsol.Pn)); |
---|
117 | flipsol={}; |
---|
118 | for i = 1:length(flipped) |
---|
119 | [H,K] = double(flipped(i)); |
---|
120 | flipsol{i} = sub_dua(F+ set(H*x <= K),obj,ops,x,y,flipped(i)); |
---|
121 | end |
---|
122 | |
---|
123 | % Just place all solutions in one big cell. We should do some |
---|
124 | % intersect and compare already here, but I am lazy now. |
---|
125 | sol = appendlists(sol,localsol,diggsol,flipsol); |
---|
126 | end |
---|
127 | end |
---|
128 | |
---|
129 | function sol = appendlists(sol,localsol,diggsol,flipsol) |
---|
130 | |
---|
131 | sol{end+1} = localsol; |
---|
132 | for i = 1:length(diggsol) |
---|
133 | if ~isempty(diggsol{i}) |
---|
134 | if isa(diggsol{i},'cell') |
---|
135 | for j = 1:length(diggsol{i}) |
---|
136 | sol{end+1} = diggsol{i}{j}; |
---|
137 | end |
---|
138 | else |
---|
139 | sol{end+1} = diggsol{i}; |
---|
140 | end |
---|
141 | end |
---|
142 | end |
---|
143 | for i = 1:length(flipsol) |
---|
144 | if ~isempty(flipsol{i}) |
---|
145 | if isa(flipsol{i},'cell') |
---|
146 | for j = 1:length(flipsol{i}) |
---|
147 | sol{end+1} = flipsol{i}{j}; |
---|
148 | end |
---|
149 | else |
---|
150 | sol{end+1} = flipsol{i}; |
---|
151 | end |
---|
152 | end |
---|
153 | end |
---|
154 | |
---|