-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathopt_runs_restart.m
175 lines (132 loc) · 6.15 KB
/
opt_runs_restart.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
%
% Bayesian Optimization of Combinatorial Structures
%
% Copyright (C) 2018 R. Baptista & M. Poloczek
%
% BOCS is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% BOCS is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with BOCS. If not, see <http://www.gnu.org/licenses/>.
%
% Copyright (C) 2018 MIT & University of Arizona
% Authors: Ricardo Baptista & Matthias Poloczek
% E-mails: rsb@mit.edu & poloczek@email.arizona.edu
%
% Script restarts the optimization runs for the test case
% specified in test_name if the runs were interrupted. The
% script reloads the data files saved in the results folder
% and runs the case if data is missing.
clear; close all; clc
addpath(genpath('../algorithms'))
addpath(genpath('../stat_model'))
addpath(genpath('../test_problems'))
addpath(genpath('../tools'))
%% Re-load cases and parameters
test_name = 'ising';
load(['../results/' test_name '/all_tests'])
%% Parallel Problem setup
% Find number of tests
n_test = length(inputs_all);
parpool(n_proc);
parfor t=1:n_test
% Load current results if results file already exists
file_name = sprintf(['../results/' test_name '/test%d.mat'], t);
if exist(file_name, 'file')
[rnd, sa, bo, ols, smc, smac, bayes, mle, hs, inputs_t] = iLoad(file_name);
% If file exists, then load it
else
% Set test inputs struct
inputs_t = inputs_all{t};
% Declare cells to store results
rnd = cell(length(lambda_vals));
sa = cell(length(lambda_vals));
bo = cell(length(lambda_vals));
ols = cell(length(lambda_vals));
smc = cell(length(lambda_vals));
smac = cell(length(lambda_vals));
bayes = struct;
bayes.stSA1 = cell(length(lambda_vals));
bayes.stSA2 = cell(length(lambda_vals));
bayes.stSA3 = cell(length(lambda_vals));
bayes.sdp = cell(length(lambda_vals));
mle = struct;
mle.stSA1 = cell(length(lambda_vals));
mle.stSA2 = cell(length(lambda_vals));
mle.stSA3 = cell(length(lambda_vals));
mle.sdp = cell(length(lambda_vals));
hs = struct;
hs.stSA1 = cell(length(lambda_vals));
hs.stSA2 = cell(length(lambda_vals));
hs.stSA3 = cell(length(lambda_vals));
hs.sdp = cell(length(lambda_vals));
end
%% Run optimization
for l=1:length(lambda_vals)
% Continue onto next lambda if results already exist for last test
if ~isempty(hs.sdp{l})
continue
end
% Define objective function with penalty term
inputs_t.lambda = lambda_vals(l);
penalty = @(x) inputs_t.lambda*inputs_t.reg_term(x);
objective = @(x) inputs_t.model(x) + penalty(x);
fprintf('--------------------------------------------\n')
fprintf('Test = %d/%d, Lambda = %f\n\n', t, n_test, lambda_vals(l));
% Run different ML optimization algorithms
rnd{l} = random_samp(objective, inputs_t);
fprintf('Random - Runtime: %f\n', sum(rnd{l}.runTime));
sa{l} = simulated_annealing(objective, inputs_t);
fprintf('SA - Runtime = %f\n', sum(sa{l}.runTime));
bo{l} = bayes_opt(objective, inputs_t);
fprintf('BO - Runtime = %f\n', sum(bo{l}.runTime));
ols{l} = local_search(objective, inputs_t);
fprintf('OLS - Runtime = %f\n', sum(ols{l}.runTime));
smc{l} = binary_smc(objective, inputs_t);
fprintf('SMC - Runtime = %f\n', sum(smc{l}.runTime));
smac{l} = run_smac(objective, inputs_t);
fprintf('SMAC - Runtime = %f\n', sum(smac{l}.runTime));
% Run BOCS with Bayesian model
inputs_t.estimator = 'bayes';
bayes.stSA1{l} = BOCS(inputs_t.model, penalty, inputs_t, 1, 'SA');
fprintf('Bayes.SA1 - Runtime = %f\n', sum(bayes.stSA1{l}.runTime));
bayes.stSA2{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'SA');
fprintf('Bayes.SA2 - Runtime = %f\n', sum(bayes.stSA2{l}.runTime));
bayes.stSA3{l} = BOCS(inputs_t.model, penalty, inputs_t, 3, 'SA');
fprintf('Bayes.SA3 - Runtime = %f\n', sum(bayes.stSA3{l}.runTime));
bayes.sdp{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'sdp');
fprintf('Bayes.SDP - Runtime = %f\n', sum(bayes.sdp{l}.runTime));
% Run BOCS with MLE model
inputs_t.estimator = 'mle';
mle.stSA1{l} = BOCS(inputs_t.model, penalty, inputs_t, 1, 'SA');
fprintf('MLE.SA1 - Runtime = %f\n', sum(mle.stSA1{l}.runTime));
mle.stSA2{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'SA');
fprintf('MLE.SA2 - Runtime = %f\n', sum(mle.stSA2{l}.runTime));
mle.stSA3{l} = BOCS(inputs_t.model, penalty, inputs_t, 3, 'SA');
fprintf('MLE.SA3 - Runtime = %f\n', sum(mle.stSA3{l}.runTime));
mle.sdp{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'sdp');
fprintf('MLE.SDP - Runtime = %f\n', sum(mle.sdp{l}.runTime));
% Run BOCS with Horseshoe model
inputs_t.estimator = 'horseshoe';
hs.stSA1{l} = BOCS(inputs_t.model, penalty, inputs_t, 1, 'SA');
fprintf('HS.SA1 - Runtime = %f\n', sum(hs.stSA1{l}.runTime));
hs.stSA2{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'SA');
fprintf('HS.SA2 - Runtime = %f\n', sum(hs.stSA2{l}.runTime));
hs.stSA3{l} = BOCS(inputs_t.model, penalty, inputs_t, 3, 'SA');
fprintf('HS.SA3 - Runtime = %f\n', sum(hs.stSA3{l}.runTime));
hs.sdp{l} = BOCS(inputs_t.model, penalty, inputs_t, 2, 'sdp');
fprintf('HS.SDP - Runtime = %f\n', sum(hs.sdp{l}.runTime));
% Save results
iSave(sprintf(['../results/' test_name '/test%d.mat'], t), ...
rnd, sa, bo, ols, smc, smac, bayes, mle, hs, inputs_t);
end
end
delete(gcp('nocreate'))
% -- END OF FILE --