Commit 06bf18dc authored by Chuanren Wu's avatar Chuanren Wu

preparing for the refinement

parent 98c565cf
......@@ -16,7 +16,7 @@ template<class T> int atLeast1(T x)
return x > 1 ? static_cast<int>(x) : 1;
}
static void redist(
static bool redist(
int n,
const std::set<int> &s,
const std::vector<int> &minRes,
......@@ -25,6 +25,7 @@ static void redist(
)
{
assert(!s.empty());
bool changed = false;
const double factor = 1/std::accumulate(s.cbegin(), s.cend(), 0.0,
[&vl](double s, int i){ return s+vl[i]; } );
int rest = n;
......@@ -33,8 +34,10 @@ static void redist(
const double d = vl[i]*factor*n + rem;
const double x = std::floor(d);
if (x < minRes[i]) {
changed |= res[i] != minRes[i];
res[i] = minRes[i];
} else {
changed |= res[i] != x;
res[i] = x;
rem = d-x;
assert(rem < 1);
......@@ -42,7 +45,10 @@ static void redist(
rest -= res[i];
});
const int idFirst = *s.cbegin();
res[idFirst] = std::max(minRes[idFirst], atLeast1(rest));
const int first =std::max(minRes[idFirst], atLeast1(rest));
changed |= res[idFirst] != first;
res[idFirst] = first;
return changed;
}
static bool validate(
......@@ -67,17 +73,15 @@ static bool validate(
return false;
}
std::vector<int> discretizeImpl(
static bool balance(
const std::vector<Constraint> &vc,
const std::vector<int> &minRes,
const std::vector<double> &vl,
const std::vector<Constraint> &vc
std::vector<int> &res
)
{
const long maxIter = 100;
std::vector<int> minRes(vl.size());
std::transform(vl.begin(), vl.end(), minRes.begin(), atLeast1<double>);
// sort(vc) before the iterations?
long iterations = 0;
auto res = minRes;
bool trigger = true;
while (trigger && iterations < maxIter) {
trigger = false;
......@@ -92,11 +96,27 @@ std::vector<int> discretizeImpl(
#ifndef NDEBUG
std::cout << "max iteration number reached, failed to solve\n";
#endif
return std::vector<int>();
return false;
} else {
#ifndef NDEBUG
std::cout << iterations << " iterations used\n";
#endif
return true;
}
}
std::vector<int> discretizeImpl(
const std::vector<double> &vl,
const std::vector<Constraint> &vc
)
{
std::vector<int> minRes(vl.size());
std::transform(vl.begin(), vl.end(), minRes.begin(), atLeast1<double>);
// sort(vc) before the iterations?
auto res = minRes;
if (balance(vc, minRes, vl, res)) {
return res;
} else {
return std::vector<int>();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment