-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSubProblem.h
More file actions
215 lines (183 loc) · 5.49 KB
/
SubProblem.h
File metadata and controls
215 lines (183 loc) · 5.49 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef SUBPROBLEM_H_
#define SUBPROBLEM_H_
#include <vector>
#include <list>
#include <utility>
#include <limits>
#include <stack>
#include "overload.h"
typedef std::pair<unsigned int, unsigned int> Pair;
typedef std::pair<long int, long int> LPair;
class Subproblem
{
public:
unsigned int num; // the number of subproblems
//unsigned int counter; // if (counter == 0) then the D array of this subproblem is fully filled out.
unsigned int now;
long int last;
std::vector<long int> Di, Ei;
std::vector<float> Dv, Ev;
std::vector<unsigned int> Dp; // store the index of the point which gives the maximum value
std::vector<unsigned int> Ep; // store the Point which gives the maximum value
std::vector<long int> Eb; // Ei[j] gives the index of point in D array such that Ei[j] is the first element in Ei array that is >= Di[Ei[j]]
std::vector<long int> Db;
std::vector<unsigned int> D; // storing the index [0,1,2,...., n]
std::vector<unsigned int> E;
std::vector<std::pair<long int, long int>> Block; // store the block for the maximization structure
std::stack<LPair> S_1; // S_1 will be used in the middle of computing maximization structure
Subproblem(); // default constructor
Subproblem(unsigned int num1);
//Subproblem(unsigned int & num1, std::vector<long int> & Di1, std::vector<long int> & Ei1); // for leaf case
//Subproblem(unsigned int & num1, std::vector<long int> & Di1, std::vector<long int> & Ei1, std::vector<long int> & Eb1, std::vector<long int> & Db1); // for non-leaf case
~Subproblem() {};
friend std::ostream & operator<<(std::ostream & os, const Subproblem & M);
};
Subproblem::Subproblem() {}
//initialization
Subproblem::Subproblem(unsigned int num1) {
num = num1;
last = -1;
now = 0;
}
// std::ostream & operator<<(std::ostream & os, const Subproblem & M) {
// os << "{num: " << M.num << "\n";
// os << "Di: " << M.Di << ", Dp: " << M.Dp << ", Dv: " << M.Dv << ", Db: " << M.Db << ", Ei: " << M.Ei << ", Ep: " << M.Ep << ", Ev: " << M.Ev << ", Eb: " << M.Eb << ", last: " << M.last << ", now: "
// << M.now << ", Block: " << M.Block << ", S_1: " << M.S_1 << "\n";
// return os;
// }
///////////////////////////////////////////////////////////////////////////////
/*
//initialization for leaf case
Subproblem::Subproblem(unsigned int & num1, std::vector<long int> & Di1, std::vector<long int> & Ei1) {
unsigned int h = Ei1.size();
num = num1;
Ei = Ei1;
E = Ei;
std::iota(E.begin(), E.end(), 0);
std::vector<float> p(h, 10);
std::vector<long> z(h, 0);
Ev = p;
Ep = z;
last = -1;
now = 0;
}
// initializationfor non-leaf case
Subproblem::Subproblem(unsigned int & num1, std::vector<long int> & Di1, std::vector<long int> & Ei1, std::vector<long int> & Eb1, std::vector<long int> & Db1) {
unsigned int h = Ei1.size();
unsigned int l = Di1.size();
now = 0;
last = -1;
Di = Di1;
std::vector<float> v(l, 0);
std::vector<unsigned int> w(l, 0);
Db = Db1;
Dv = v;
Dp = w;
num = num1;
Ei = Ei1;
D = Di;
E = Ei;
std::iota(D.begin(), D.end(), 0);
std::iota(E.begin(), E.end(), 0);
std::vector<float> p(h, 10);
std::vector<long> z(h, -1);
Ev = p;
Ep = z;
Eb = Eb1;
std::pair<long int, long int> dummy_pair = std::make_pair(-1, h+1);
S_1.push(dummy_pair);
}
*/
class StackOfSubProblems
{
public:
vector<Subproblem> StackSub;
StackOfSubProblems () {};
~StackOfSubProblems () {};
int size() {
return StackSub.size();
}
void Clear(int & ee);
void clear();
void Push_Back(int & ee, Subproblem & s); // ee is current position in StackSub
void ClearSingle (int & ee);
void pop_back();
Subproblem & operator[](int i) {
return StackSub[i];
}
};
void
StackOfSubProblems::clear() {
// clear vectors of each element in StackSub;
StackSub.clear();
}
void
StackOfSubProblems::pop_back() {
StackSub.pop_back();
}
void
StackOfSubProblems::Clear (int & ee) {
// clear vectors of each element in StackSub;
for (int i = 0; i <= ee - 1; ++i) {
StackSub[i].num = 0;
StackSub[i].now = 0;
StackSub[i].last = -1;
StackSub[i].Di.clear();
StackSub[i].Ei.clear();
StackSub[i].Dv.clear();
StackSub[i].Ev.clear();
StackSub[i].Dp.clear();
StackSub[i].Ep.clear();
StackSub[i].Db.clear();
StackSub[i].Eb.clear();
StackSub[i].D.clear();
StackSub[i].E.clear();
StackSub[i].Block.clear();
while (!StackSub[i].S_1.empty()) {
StackSub[i].S_1.pop();
}
}
}
void
StackOfSubProblems::Push_Back (int & ee, Subproblem & s) { // ee is the number of positions which are already filled out in StackSub
if (ee < size()) { // StackSub[0] -- StackSub[ee - 1] are filled out
StackSub[ee].num = s.num;
StackSub[ee].now = s.now;
StackSub[ee].last = s.last;
StackSub[ee].Di = s.Di;
StackSub[ee].Ei = s.Ei;
StackSub[ee].Dv = s.Dv;
StackSub[ee].Ev = s.Ev;
StackSub[ee].Dp = s.Dp;
StackSub[ee].Ep = s.Ep;
StackSub[ee].Db = s.Db;
StackSub[ee].Eb = s.Eb;
StackSub[ee].D = s.D;
StackSub[ee].E = s.E;
// TODO(Jingwen): No Block and S_1 here??
}
else { // ee >= size
StackSub.push_back(s);
}
}
void
StackOfSubProblems::ClearSingle (int & ee) {
StackSub[ee - 1].num = 0;
StackSub[ee - 1].now = 0;
StackSub[ee - 1].last = -1;
StackSub[ee - 1].Di.clear();
StackSub[ee - 1].Ei.clear();
StackSub[ee - 1].Dv.clear();
StackSub[ee - 1].Ev.clear();
StackSub[ee - 1].Dp.clear();
StackSub[ee - 1].Ep.clear();
StackSub[ee - 1].Db.clear();
StackSub[ee - 1].Eb.clear();
StackSub[ee - 1].D.clear();
StackSub[ee - 1].E.clear();
StackSub[ee - 1].Block.clear();
while (!StackSub[ee - 1].S_1.empty()) {
StackSub[ee - 1].S_1.pop();
}
}
#endif