-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
201 lines (183 loc) · 5.85 KB
/
main.cpp
File metadata and controls
201 lines (183 loc) · 5.85 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <random>
#include <fstream>
#include <iterator>
#include <memory>
#include <limits>
#include <tuple>
#include "ceres/ceres.h"
#include "glog/logging.h"
#include "opts.h"
#include "util.h"
#include "initial.h"
#include "l2reg.h"
#include "problem.h"
#include "discrete.h"
#include "cholmod.h"
#include "restrict/als.h"
#include "restrict/tensor.h"
using namespace ceres;
using namespace std;
int main(int argc, const char** argv) {
/* google::InitGoogleLogging(argv[0]); */
cout.precision(numeric_limits<double>::digits10);
random_device rd;
rng.seed(rd());
/* rng.seed(8); */
Problem::Options popts;
popts.enable_fast_removal = true;
MyProblem p(popts,N);
/* vector<ResidualBlockId> rids; */
/* for (int i=0; i<M; ++i) { */
/* rids.push_back(AddToProblem(p.p,p.x.data(),i)); */
/* } */
vector<tuple<int,int,int> > eqs;
for (int i=0; i<TA; ++i) {
for (int j=0; j<TB; ++j) {
for (int k=0; k<TC; ++k) {
if (
#ifdef SYM
i <= j && j <= k &&
#endif
TIGHTA[i] + TIGHTB[j] + TIGHTC[k] <= 0) {
eqs.push_back(make_tuple(i,j,k));
AddToProblem(p.p,p.x.data(),(i*TB+j)*TC+k);
}
}
}
}
fill_initial(p,argc>1 ? argv[1] : "");
// pure als followed by trust region refine
double costlast=1e7;
int bad = 0;
double sqalpha = 0.1;
for (int it=0; it<2000; ++it) {
#ifdef SYM
als_sym(p.x.data(),sqalpha);
/* als_sym_some(p.x.data(),eqs,sqalpha); */
#else
als(p.x.data(),it%3,sqalpha);
/* als_some(p.x.data(),eqs,it%3,sqalpha); */
#endif
double cost; p.p.Evaluate(Problem::EvaluateOptions(),&cost,0,0,0);
double ma = accumulate(p.x.begin(),p.x.end(),0.0,[](double a, double b)
{return max(a,std::abs(b));} );
double l2 = sqrt(accumulate(p.x.begin(),p.x.end(),0.0,[](double a, double b)
{return a+b*b;} ));
if ((costlast - cost) / costlast < 2e-4) {
bad++;
} else {
bad=0;
}
printf("%4d %20.15g %20.15g %20.15g %10.5g %d %5.1e\n",it,2*cost,ma,l2,sqalpha,bad,
(costlast-cost)/costlast );
if (cost < 1e-27)
break;
if (bad >= 3 || cost > costlast) {
if (sqalpha == 0.0 && bad >= 1000) {
break;
} else if (sqalpha > 0.0 && sqalpha < 1e-4) {
sqalpha = 0.0;
bad = 0;
} else if (sqalpha > 0.0) {
sqalpha *= 0.9;
bad = 0;
}
}
costlast = cost;
}
double cost; p.p.Evaluate(Problem::EvaluateOptions(),&cost,0,0,0);
printf("%20.15g\n",2*cost);
logsol(p.x,"out_dense.txt");
/* trust_region_f(p,[&](){ */
/* for (int it=0; it<3*4; ++it) { */
/* /1* als_some(p.x.data(),eqs,it%3,1e-3); *1/ */
/* als_sym(p.x.data()); */
/* /1* als_sym_some(p.x.data(),eqs); *1/ */
/* /1* als(p.x.data(),it%3); *1/ */
/* } */
/* },1e-7,300); */
/* logsol(p.x,"out_dense.txt"); */
/* // trust region refine/als with l2 regularization curretly trust region */
/* double costlast=1e7; */
/* int bad = 0; */
/* double sqalpha = 0.1; */
/* /1* for (int it=0; it<2000; ++it) { *1/ */
/* int it=0; */
/* trust_region_f(p,[&](){ */
/* for (int i=0; i<3*4; ++i) { */
/* als_some(p.x.data(),eqs,i%3); */
/* } */
/* double cost; p.p.Evaluate(Problem::EvaluateOptions(),&cost,0,0,0); */
/* double ma = accumulate(p.x.begin(),p.x.end(),0.0,[](double a, double b) */
/* {return max(a,std::abs(b));} ); */
/* double l2 = sqrt(accumulate(p.x.begin(),p.x.end(),0.0,[](double a, double b) */
/* {return a+b*b;} )); */
/* if ((costlast - cost) / costlast < 1e-3) { */
/* bad++; */
/* } else { */
/* bad=0; */
/* } */
/* printf("%4d %20.15g %20.15g %20.15g %10.5g %d\n",it,2*cost,ma,l2,sqalpha,bad); */
/* if (bad >= 3) { */
/* if (sqalpha < 1e-4) { */
/* sqalpha = 0.0; */
/* } else { */
/* sqalpha *= 0.7; */
/* } */
/* bad = 0; */
/* } */
/* costlast = cost; */
/* it++; */
/* },1e-7,1000); */
MyTerminationType term = NO_SOLUTION;
/* for (int bi=0; bi < BLOCKS; ++bi) { */
/* for (int i=0; i< MULT*(BBOUND[i+1] - BBOUND[i]); ++i) { */
/* p.p.SetParameterLowerBound(p.x.data()+MULT*BBOUND[bi],i,-2.0); */
/* p.p.SetParameterUpperBound(p.x.data()+MULT*BBOUND[bi],i,2.0); */
/* } */
/* } */
/* term = l2_reg_search(p, 1e-3, 1e-5, true, 3000, 0.1); */
/* term = l2_reg_search(p, 1e-3, 1e-3, true, 3000, 0.1); */
int cnt = 0;
for (int i = 0; i < N; ++i) {
if (p.x[i] == 0.0 || p.x[i] == 1.0 || p.x[i] == -1.0) {
set_value_constant(p,i);
cnt++;
}
}
cout << cnt << " set constant" << endl;
term = l2_reg_search(p, 5e-3, 5e-4, false, 10000, 0.1, false);
/* term = l2_reg_search(p, 5e-3, 1e-15, false, 50000, 0.0, true); */
switch (term) {
case CONTINUE: cout << "CONTINUE" << endl; break;
case CONTINUE_RESET: cout << "CONTINUE_RESET" << endl; break;
case SOLUTION: cout << "SOLUTION" << endl; break;
case BORDER: cout << "BORDER" << endl; break;
case BORDER_OR_NO_SOLUTION: cout << "BORDER_OR_NO_SOLUTION" << endl; break;
case NO_SOLUTION: cout << "NO_SOLUTION" << endl; break;
case UNKNOWN: cout << "UNKNOWN" << endl; break;
}
logsol(p.x,"out_dense.txt");
if (term == SOLUTION) {
/* if (term == SOLUTION || term == BORDER) { */
/* if (true){ */
minimize_max_abs(p, 0.01);
/* sparsify(p, 1.0, 1e-4); */
int successes = 0;
/* greedy_discrete_careful(p, successes, DA_ZERO); */
/* greedy_discrete_careful(p, successes, DA_E3); */
greedy_discrete(p, successes, DA_ZERO, N);
minimize_max_abs(p, 0.01);
if (MULT == 2) {
greedy_discrete(p, successes, DA_E3, N);
} else {
greedy_discrete(p, successes, DA_PM_ONE, N);
}
/* greedy_discrete_pairs(p, N*100); */
double ma = minimize_max_abs(p);
cout << "ma " << ma << endl;
logsol(p.x,"out.txt");
return 0;
}
return 1;
}