-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_and_bound_2.m
161 lines (138 loc) · 4.15 KB
/
branch_and_bound_2.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function [flag, bab_time, i] = branch_and_bound_2(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
RESTART = 1;
RESTARTED = 0;
% flag for whether we have reached a definitive answer
COMPLETE = 0;
% how the bounds are determined in each partitioning
LOWER = 2;
UPPER = 1;
% 0: Unsound Method
% 1: Interval Bound Propagation
% 2: Linear Programming Bound
% initiating
flag = NaN;
while RESTART == 1
RESTART = 0;
% set up output bounds for the whole domain
if UPPER == 2
[ymin, ymax] = linear_programming_bound(W,b,xmin,xmax);
elseif UPPER == 1
[ymin, ymax] = interval_bound_propagation(W,b,xmin,xmax);
end
YMAX = ymax;
YMIN = ymin;
XMIN = xmin;
XMAX = xmax;
i = 0;
while COMPLETE == 0
if i == 500
%UPPER = 2;
RESTART = 1;
RESTARTED = 1;
disp('RESETTING')
flag = NaN;
bab_time = toc
COMPLETE = 1;
break
end
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
if UPPER == 2
[y2min, y2max] = ...
linear_programming_bound(W, b, x2barmin, x2barmax);
[y1min, y1max] = ...
linear_programming_bound(W, b, x1barmin, x1barmax);
elseif UPPER == 1
[y2min, y2max] = ...
interval_bound_propagation(W, b, x2barmin, x2barmax);
[y1min, y1max] = ...
interval_bound_propagation(W, b, x1barmin, x1barmax);
else
end
% 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
end
if RESTARTED == 1
disp('Restarted')
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