source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/dev/dua_test.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.3 KB
Line 
1function 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
8if isempty(ops)
9    ops = sdpsettings;
10end
11ops.mp.algorithm = 1;
12ops.cachesolvers = 0;
13ops.mp.presolve=1;
14ops.solver = '';
15
16% Expand nonlinear operators only once
17F = expandmodel(F,obj);
18ops.expand = 0;
19
20% Gather all binary variables
21y = unique([depends(F) depends(obj)]);
22n = length(y)-length(x);
23y = intersect(y,[yalmip('binvariables') depends(F(find(is(F,'binary'))))]);
24y = recover(y);
25
26% Make sure binary relaxations satisfy 0-1 constraints
27F = F + set(0 <= y <= 1);
28
29% Bounded search-space
30Universe = unitbox(length(x),10);
31
32% recursive, starting in maximum universe
33sol = 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
37for 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
42end
43
44
45function sol = sub_dua(F,obj,ops,x,y,Universe)
46
47sol = {};
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
54y_feasible = 0;
55if 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
75else
76    ops.verbose = ops.verbose-1;
77    intsol = solvesdp(F,obj,ops);
78    ops.verbose = ops.verbose+1;
79    y_feasible = round(double(y));
80end
81
82%if intsol.problem == 0
83if ~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   
127end
128
129function sol = appendlists(sol,localsol,diggsol,flipsol)
130
131sol{end+1} = localsol;
132for 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
142end
143for 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
153end
154
Note: See TracBrowser for help on using the repository browser.