-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_and_bound.m
112 lines (96 loc) · 3.2 KB
/
branch_and_bound.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function [flag, bab_time] = branch_and_bound(W, b, XMIN, XMAX)
tic
%% ORDER
% set up output bounds for the whole domain (regular lpb)
%while:
% find the max of the upper bounds (just 1 at first),
% if it's negative we've proved the thing true
% if we can't prove it true this way we create new subdomains
% find the longest dimension with s function
% define new x input max and mins - two of them, for the two subdomains
% fill up the x1 and x2 vectors accoriding to rules in the notes
% find the bounds of the new subdomains using lpb
% see if the lower bounds are positive, if so we've got a false property
% update subdomain input bounds (XMIN and XMAX)
% update subdomain output bounds
% at the end
% flag for whether we have reached a definitive answer
COMPLETE = 0;
% initiating
flag = NaN;
% set up output bounds for the whole domain (regular lpb)
[YMIN, YMAX] = linear_programming_bound(W,b,XMIN,XMAX);
i = 0;
while COMPLETE == 0
i = i + 1;
disp(strcat('Iteration: ',num2str(i)))
% idx is the index of the subdomain with the largest output ub, xbar
[ymax, idx1] = max(YMAX);
if ymax < 0
COMPLETE = 1;
% proven true
flag = 1;
break
end
xbarmin = XMIN(idx1,:);
xbarmax = XMAX(idx1,:);
% if we can't prove it true this way we create new subdomains
% idx2 is index of dimension with longest relative length
S = (xbarmax-xbarmin)./(xmax-xmin);
[~, idx2] = max(S);
% x1bar and x2bar are xbar split up so the lower one x1bar shares its
% min with xbar and x2bar shares its max with xbar
x1barmin = xbarmin;
x2barmax = xbarmax;
% why does this look like an average
x1barmax = xbarmax;
x2barmin = xbarmin;
x1barmax(idx2) = (xbarmin(idx2) + xbarmax(idx2))/2;
x2barmin(idx2) = (xbarmin(idx2) + xbarmax(idx2))/2;
% find the bounds of the new subdomains using lpb
[y1min, y1max] = linear_programming_bound(W, b, x1barmin, x1barmax);
[y2min, y2max] = linear_programming_bound(W, b, x2barmin, x2barmax);
% see if we have found a counter-example
if (y1min > 0) || (y2min > 0)
flag = 0;
COMPLETE = 1;
break
end
% if not, we create a new partiion. split xbar into x1bar and x2bar
% and run again!
% update subdomain input bounds (XMIN and XMAX)
XMIN = [...
XMIN(1:idx1-1,:); ...
x1barmin;...
x2barmin;...
XMIN(idx1+1:size(XMIN,1),:)...
];
XMAX = [...
XMAX(1:idx1-1,:); ...
x1barmax;...
x2barmax;...
XMAX(idx1+1:size(XMAX,1),:)...
];
% update subdomain output bounds
YMIN = [...
YMIN(1:idx1-1,:); ...
y1min;...
y2min;...
YMIN(idx1+1:size(YMIN,1))...
];
YMAX = [...
YMAX(1:idx1-1,:); ...
y1max;...
y2max;...
YMAX(idx1+1:size(YMAX,1))...
];
end
if flag == 1
disp('Property is true, no counter-example exists')
elseif flag == 0
disp('Property is false, a counter-example exists')
else
disp('error')
end
bab_time = toc
end