22 numStates(numStates) {
38 return (h - compressedEntry.
h) * numStates;
59 for (
size_t h = 0; h < histogram.size(); ++h) {
60 uint64_t numStates = histogram[h];
61 assert(numStates >= 0);
64 entries.push_back(entry);
74 return entries.size();
78 const auto &compressedEntry = entries[startPos];
80 for (
size_t pos = startPos; pos < endPos; ++pos)
81 cost += entries[pos].GetPenalty(compressedEntry);
93 return firstEntry->
GetH();
98 return subsolution->GetFirstH();
106 : firstEntry(&onlyEntry),
107 subsolution(nullptr),
108 cost(hTable.ComputeCostBetween(onlyEntry.GetPos(),
114 const shared_ptr<const Solution> &subsolution)
115 : firstEntry(&firstEntry),
116 subsolution(subsolution),
117 cost(hTable.ComputeCostBetween(firstEntry.GetPos(),
118 subsolution->firstEntry->GetPos()) +
128 const Solution *restSolution =
this;
129 while (restSolution) {
130 result.push_back(restSolution->
GetFirstH());
138 double weightedH = 0.0;
139 double totalStates = 0.0;
140 const Solution *currentSolution =
this;
141 for (
size_t h = 0; h < histogram.size(); ++h) {
142 uint64_t numStates = histogram[h];
143 while (currentSolution->
GetNextH() <=
static_cast<int>(h))
144 currentSolution = currentSolution->
subsolution.get();
145 weightedH += currentSolution->
GetFirstH() * numStates;
146 totalStates += numStates;
148 return weightedH / totalStates;
154 const vector<uint64_t> &histogram) {
157 for (
size_t i = 0; i < hValues.size(); ++i) {
162 cout <<
"] with cost " << solution.
GetCost()
163 <<
" and average h value " << solution.
GetAverageH(histogram)
168 static vector<int>
Optimize(
const vector<uint64_t> &histogram,
int numValues,
169 bool dumpResult =
false) {
170 assert(numValues >= 1);
174 vector<shared_ptr<const Solution>> subsolutions;
175 for (
int iteration = 0; iteration < numValues; ++iteration) {
182 vector<shared_ptr<const Solution>> solutions;
183 solutions.reserve(hTable.
GetSize());
185 for (
int pos = 0; pos < hTable.
GetSize(); ++pos) {
186 const auto &entry = hTable[pos];
187 Solution bestSolution(hTable, entry);
189 if (!subsolutions.empty()) {
190 for (
int boundaryPos = pos + 1;
191 boundaryPos < hTable.
GetSize();
193 auto subsolution = subsolutions[boundaryPos];
194 Solution solution(hTable, entry, subsolution);
196 bestSolution = solution;
200 solutions.push_back(make_shared<Solution>(bestSolution));
202 subsolutions = move(solutions);
205 auto solution = *subsolutions[0];
210 return solution.GetHValues();
215 int numValues, vector<int> &result) {
216 result =
Optimize(distribution, numValues,
false);
221 Optimize(distribution, numValues,
true);