-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualSys.hpp
193 lines (153 loc) · 5.5 KB
/
dualSys.hpp
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
#ifndef DUALSYS_HPP
#define DUALSYS_HPP
#include <vector>
#include <set>
#include <iostream>
#include <cmath>
#include <Eigen/Dense>
#include <Eigen/IterativeLinearSolvers>
#include "myUtils.hpp"
#include "node.hpp"
#include "clique.hpp"
#include "subProblem.hpp"
class dualSys {
std::vector<double> uEnergy_;
std::vector<double> uEnergyCliqShare_;
std::vector<double> uEnergyUnshared_;
std::vector<int> unaryOffset_;
std::size_t nNode_;
std::size_t numLabTot_;
std::vector<short> nLabel_;
std::size_t nCliq_;
std::size_t nSubProb_;
std::size_t maxCliqSiz_;
std::vector<int> cliqSizes_;
std::size_t nRow_; //for grids
std::size_t nCol_;
Eigen::VectorXd dualVar_;
Eigen::VectorXd momentum_;
Eigen::VectorXd gradient_;
Eigen::VectorXd gradientDual_;
Eigen::VectorXd newtonStep_;
std::vector<node> node_;
std::vector<clique> clique_;
std::vector<subProblem> subProb_;
std::vector<std::vector<int> > cliqPerNode_;
std::vector<std::vector<int> > subProbPerNode_;
std::vector<double> primalFeas_;
std::vector<double> primalFrac_;
std::vector<int> primalMax_;
std::vector<int> bestPrimalMax_;
double bestIntPrimalEnergy_;
double bestNonSmoothPrimalEnergy_;
double bestSmoothPrimalEnergy_;
double gradNorm_;
double gradMax_;
bool bestPrimalFlag_;
std::string interPrimFile_;
double tau_, tauStep_;
bool solnQual_;
double curEnergy_;
double dualEnergy_;
double finalEnergy_;
bool sparseFlag_;
double pdGap_;
bool pdInitFlag_;
int smallGapIterCnt_;
int popGradEnergyFistaSP();
int nColCliq_, nRowCliq_;
int performLineSearch(double);
int distributeDualVars();
int distributeMomentum();
void recoverFracPrimal();
int recoverFeasPrimal();
void recoverMaxPrimal(std::vector<double>);
void assignPrimalVars(std::string);
int setFracAsFeas();
double compNonSmoothEnergySP();
double compIntPrimalEnergyFolded();
double compSmoothPrimalEnergyOld();
int solveCG(const int, const Eigen::VectorXd &, const Eigen::MatrixXd &, Eigen::VectorXd &, std::vector<Eigen::VectorXd> &, int);
int solveSR1Sub(const int, const Eigen::VectorXd &, const Eigen::MatrixXd &, Eigen::VectorXd &, std::vector<Eigen::VectorXd> &, int);
int solveSR1SubDampBased(const int, const Eigen::VectorXd &, const Eigen::MatrixXd &, Eigen::VectorXd &, std::vector<Eigen::VectorXd> &, int);
int precondFlag_; //indicate which preconditioner is used
//Jacobi preconditioner
Eigen::VectorXd diagPrecond(const Eigen::VectorXd &,const Eigen::VectorXd &);
//Quasi-Newton
Eigen::VectorXd dualDiff_;
Eigen::VectorXd gradDiff_;
double b0Scale_;
std::vector<Eigen::VectorXd> sVecs_;
std::vector<Eigen::VectorXd> yVecs_;
int qnMem_, qnRingOffset_;
bool qnReadyFlag_;
int qnTypeFlag_;
Eigen::VectorXd getQNHessVecProd(const Eigen::VectorXd &);
Eigen::VectorXd getQNHessInvVecProd(const Eigen::VectorXd &);
Eigen::MatrixXd Nmat_;
Eigen::MatrixXd Mmat_;
Eigen::MatrixXd S_blk_;
Eigen::MatrixXd Y_blk_;
Eigen::MatrixXd S_blk_scale;
Eigen::MatrixXd L_k;
Eigen::MatrixXd D_k;
std::vector<double> qnRho_;
Eigen::VectorXd getSROneHessVecProd(const Eigen::VectorXd &);
double trustLambda_;
double trustDelta_;
static constexpr double mcTol_ = 1e-16;
static constexpr double gradTol_ = 0.001;
static constexpr double dampTol_ = 0.001;
int cntIter_;
int maxIter_;
static constexpr double lsTol_ = 1e-6;
static constexpr double exitTol_ = 1e-8;
static constexpr double lsC_ = 1e-4;
static constexpr double lsRho_ = 0.8;
static constexpr double dampScale_ = 1./3; // earlier, it was 1/6, 1/3
static constexpr double trustLambdaReset_ = 1;
static constexpr double tauScale_ = 2;
static constexpr double tauMax_ = 1024;
int annealIval_;
static constexpr int annealType_ = 1; //1: gradient based; 2: PD gap based
static constexpr int exitType_ = 1;
static constexpr int cntExitCond_ = 10;
public:
friend int matVecMult(const dualSys &, const std::vector<double> &, std::vector<double> &);
dualSys(int, std::vector<short>, double, int, int, int, int, std::string);
int addNode(int, std::vector<double>);
int addCliq(const std::vector<int> &, std::vector<double> *);
int addCliq(const std::vector<int> &, double *, std::map<int,double> *);
int prepareDualSys(int, int, std::vector<std::vector<int> >, std::vector<uint_fast8_t>);
int setNumRowCol(int, int);
int solveQuasiNewton();
int solveSROne();
int solveFista();
int popGradEnergySP();
double computeEnergy(std::vector<double>);
double computeEnergySP(const Eigen::VectorXd &);
double compSmoothPrimalEnergy(); //compSmoothPrimalEnergy();
double compIntPrimalEnergy();
double compNonSmoothPrimalEnergy();
double compNonSmoothDualEnergy();
int compIllExit(const double &, const double &, const Eigen::VectorXd &, const Eigen::VectorXd &, const Eigen::VectorXd &, bool &);
double getBestSmoothPrimalEnergy() {return bestSmoothPrimalEnergy_;}
double getBestIntPrimalEnergy() {return bestIntPrimalEnergy_;}
double getBestNonSmoothPrimalEnergy() {return bestNonSmoothPrimalEnergy_;}
std::vector<int> getPrimalMax() {return bestPrimalMax_;}
int getNumNodes() {return nNode_;}
std::vector<short> getNodeLabels() {return nLabel_;}
int getNumDualVar() {return nDualVar_;}
Eigen::VectorXd getGradientVec() {return gradient_;}
Eigen::VectorXd getDualVec() {return dualVar_;}
Eigen::VectorXd getStepVec() {return newtonStep_;}
double getGradNorm() {return gradNorm_;}
double getGradMax() {return gradMax_;}
double getSmoothDualEnergy() {return curEnergy_;}
int setStepVec(Eigen::VectorXd newtonStep) {
newtonStep_ = newtonStep;
return 0;
}
std::size_t nDualVar_;
};
#endif //DUALSYSGEN_HPP