Some cleanup, renaming
parent
490a558fe1
commit
42284355f4
|
@ -78,16 +78,19 @@ static vector<size_t> iidSampler(const vector<double> &weight, const size_t n) {
|
||||||
|
|
||||||
/* compute the sum of the weights */
|
/* compute the sum of the weights */
|
||||||
const double sum = std::accumulate(weight.begin(), weight.end(), 0.0);
|
const double sum = std::accumulate(weight.begin(), weight.end(), 0.0);
|
||||||
|
if (sum==0.0) {
|
||||||
|
throw std::runtime_error("Weight vector has no non-zero weights");
|
||||||
|
}
|
||||||
|
|
||||||
/* make a normalized and accumulated version of the weight vector */
|
/* make a normalized and accumulated version of the weight vector */
|
||||||
const size_t m = weight.size();
|
const size_t m = weight.size();
|
||||||
vector<double> w; w.reserve(m);
|
vector<double> cdf; cdf.reserve(m);
|
||||||
for ( size_t i = 0 ; i < m ; ++i ) {
|
for ( size_t i = 0 ; i < m ; ++i ) {
|
||||||
w.push_back(weight[i]/sum);
|
cdf.push_back(weight[i]/sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<double> acc(m);
|
vector<double> acc(m);
|
||||||
std::partial_sum(w.begin(),w.end(),acc.begin());
|
std::partial_sum(cdf.begin(),cdf.end(),acc.begin());
|
||||||
|
|
||||||
/* iid sample n times */
|
/* iid sample n times */
|
||||||
vector<size_t> result; result.reserve(n);
|
vector<size_t> result; result.reserve(n);
|
||||||
|
@ -95,18 +98,18 @@ static vector<size_t> iidSampler(const vector<double> &weight, const size_t n) {
|
||||||
for ( size_t i = 0 ; i < n ; ++i ) {
|
for ( size_t i = 0 ; i < n ; ++i ) {
|
||||||
const double value = rand() / denominator;
|
const double value = rand() / denominator;
|
||||||
/* binary search the interval containing "value" */
|
/* binary search the interval containing "value" */
|
||||||
vector<double>::iterator it = std::lower_bound(acc.begin(), acc.end(), value);
|
const auto it = std::lower_bound(acc.begin(), acc.end(), value);
|
||||||
size_t idx = it - acc.begin();
|
const size_t idx = it - acc.begin();
|
||||||
result.push_back(idx);
|
result.push_back(idx);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
vector<size_t> uniqueSampler(const vector<double> &weight, const size_t n) {
|
static vector<size_t> UniqueSampler(const vector<double> &weight, const size_t n) {
|
||||||
|
|
||||||
const size_t m = weight.size();
|
const size_t m = weight.size();
|
||||||
if ( n > m ) throw std::invalid_argument("uniqueSampler: invalid input size");
|
if ( n > m ) throw std::invalid_argument("UniqueSampler: invalid input size");
|
||||||
|
|
||||||
vector<size_t> result;
|
vector<size_t> result;
|
||||||
|
|
||||||
|
@ -221,11 +224,11 @@ SubgraphBuilderParameters::Skeleton SubgraphBuilderParameters::skeletonTranslato
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
std::string SubgraphBuilderParameters::skeletonTranslator(Skeleton w) {
|
std::string SubgraphBuilderParameters::skeletonTranslator(Skeleton s) {
|
||||||
if ( w == NATURALCHAIN )return "NATURALCHAIN";
|
if ( s == NATURALCHAIN ) return "NATURALCHAIN";
|
||||||
else if ( w == BFS ) return "BFS";
|
else if ( s == BFS ) return "BFS";
|
||||||
else if ( w == KRUSKAL )return "KRUSKAL";
|
else if ( s == KRUSKAL ) return "KRUSKAL";
|
||||||
else return "UNKNOWN";
|
else return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
|
@ -271,7 +274,7 @@ std::string SubgraphBuilderParameters::augmentationWeightTranslator(Augmentation
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
vector<size_t> SubgraphBuilder::buildTree(const GaussianFactorGraph &gfg,
|
vector<size_t> SubgraphBuilder::buildTree(const GaussianFactorGraph &gfg,
|
||||||
const FastMap<Key, size_t> &ordering,
|
const FastMap<Key, size_t> &ordering,
|
||||||
const vector<double> &w) const {
|
const vector<double> &weights) const {
|
||||||
const SubgraphBuilderParameters &p = parameters_;
|
const SubgraphBuilderParameters &p = parameters_;
|
||||||
switch (p.skeleton_) {
|
switch (p.skeleton_) {
|
||||||
case SubgraphBuilderParameters::NATURALCHAIN:
|
case SubgraphBuilderParameters::NATURALCHAIN:
|
||||||
|
@ -281,7 +284,7 @@ vector<size_t> SubgraphBuilder::buildTree(const GaussianFactorGraph &gfg,
|
||||||
return bfs(gfg);
|
return bfs(gfg);
|
||||||
break;
|
break;
|
||||||
case SubgraphBuilderParameters::KRUSKAL:
|
case SubgraphBuilderParameters::KRUSKAL:
|
||||||
return kruskal(gfg, ordering, w);
|
return kruskal(gfg, ordering, weights);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "SubgraphBuilder::buildTree undefined skeleton type" << endl;
|
std::cerr << "SubgraphBuilder::buildTree undefined skeleton type" << endl;
|
||||||
|
@ -357,10 +360,10 @@ vector<size_t> SubgraphBuilder::bfs(const GaussianFactorGraph &gfg) const {
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
vector<size_t> SubgraphBuilder::kruskal(const GaussianFactorGraph &gfg,
|
vector<size_t> SubgraphBuilder::kruskal(const GaussianFactorGraph &gfg,
|
||||||
const FastMap<Key, size_t> &ordering,
|
const FastMap<Key, size_t> &ordering,
|
||||||
const vector<double> &w) const {
|
const vector<double> &weights) const {
|
||||||
const VariableIndex variableIndex(gfg);
|
const VariableIndex variableIndex(gfg);
|
||||||
const size_t n = variableIndex.size();
|
const size_t n = variableIndex.size();
|
||||||
const vector<size_t> idx = sort_idx(w) ;
|
const vector<size_t> idx = sort_idx(weights) ;
|
||||||
|
|
||||||
/* initialize buffer */
|
/* initialize buffer */
|
||||||
vector<size_t> result;
|
vector<size_t> result;
|
||||||
|
@ -379,7 +382,7 @@ vector<size_t> SubgraphBuilder::kruskal(const GaussianFactorGraph &gfg,
|
||||||
if ( dsf.find(u) != dsf.find(v) ) {
|
if ( dsf.find(u) != dsf.find(v) ) {
|
||||||
dsf.merge(u, v) ;
|
dsf.merge(u, v) ;
|
||||||
result.push_back(id) ;
|
result.push_back(id) ;
|
||||||
sum += w[id] ;
|
sum += weights[id] ;
|
||||||
if ( ++count == n-1 ) break ;
|
if ( ++count == n-1 ) break ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,14 +391,14 @@ vector<size_t> SubgraphBuilder::kruskal(const GaussianFactorGraph &gfg,
|
||||||
|
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
vector<size_t> SubgraphBuilder::sample(const vector<double> &weights, const size_t t) const {
|
vector<size_t> SubgraphBuilder::sample(const vector<double> &weights, const size_t t) const {
|
||||||
return uniqueSampler(weights, t);
|
return UniqueSampler(weights, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************/
|
/****************************************************************/
|
||||||
Subgraph::shared_ptr SubgraphBuilder::operator() (const GaussianFactorGraph &gfg) const {
|
Subgraph::shared_ptr SubgraphBuilder::operator() (const GaussianFactorGraph &gfg) const {
|
||||||
|
|
||||||
const SubgraphBuilderParameters &p = parameters_;
|
const auto &p = parameters_;
|
||||||
const Ordering inverse_ordering = Ordering::Natural(gfg);
|
const auto inverse_ordering = Ordering::Natural(gfg);
|
||||||
const FastMap<Key, size_t> forward_ordering = inverse_ordering.invert();
|
const FastMap<Key, size_t> forward_ordering = inverse_ordering.invert();
|
||||||
const size_t n = inverse_ordering.size(), m = gfg.size();
|
const size_t n = inverse_ordering.size(), m = gfg.size();
|
||||||
|
|
||||||
|
@ -411,7 +414,7 @@ Subgraph::shared_ptr SubgraphBuilder::operator() (const GaussianFactorGraph &gfg
|
||||||
// Build spanning tree.
|
// Build spanning tree.
|
||||||
const vector<size_t> tree = buildTree(gfg, forward_ordering, weights);
|
const vector<size_t> tree = buildTree(gfg, forward_ordering, weights);
|
||||||
if ( tree.size() != n-1 ) {
|
if ( tree.size() != n-1 ) {
|
||||||
throw std::runtime_error("SubgraphBuilder::operator() tree.size() != n-1 failed ");
|
throw std::runtime_error("SubgraphBuilder::operator() failure: tree.size() != n-1");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downweight the tree edges to zero.
|
// Downweight the tree edges to zero.
|
||||||
|
|
|
@ -129,11 +129,11 @@ namespace gtsam {
|
||||||
|
|
||||||
enum AugmentationWeight { /* how to weigh the graph edges */
|
enum AugmentationWeight { /* how to weigh the graph edges */
|
||||||
SKELETON = 0, /* use the same weights in building the skeleton */
|
SKELETON = 0, /* use the same weights in building the skeleton */
|
||||||
// STRETCH, /* stretch in the laplacian sense */
|
// STRETCH, /* stretch in the laplacian sense */
|
||||||
// GENERALIZED_STRETCH /* the generalized stretch defined in jian2013iros */
|
// GENERALIZED_STRETCH /* the generalized stretch defined in jian2013iros */
|
||||||
} augmentationWeight_ ;
|
} augmentationWeight_ ;
|
||||||
|
|
||||||
double complexity_;
|
double complexity_; /* factor multiplied with n, yields number of extra edges. */
|
||||||
|
|
||||||
SubgraphBuilderParameters()
|
SubgraphBuilderParameters()
|
||||||
: skeleton_(KRUSKAL), skeletonWeight_(RANDOM), augmentationWeight_(SKELETON), complexity_(1.0) {}
|
: skeleton_(KRUSKAL), skeletonWeight_(RANDOM), augmentationWeight_(SKELETON), complexity_(1.0) {}
|
||||||
|
@ -145,7 +145,7 @@ namespace gtsam {
|
||||||
friend std::ostream& operator<<(std::ostream &os, const PreconditionerParameters &p);
|
friend std::ostream& operator<<(std::ostream &os, const PreconditionerParameters &p);
|
||||||
|
|
||||||
static Skeleton skeletonTranslator(const std::string &s);
|
static Skeleton skeletonTranslator(const std::string &s);
|
||||||
static std::string skeletonTranslator(Skeleton w);
|
static std::string skeletonTranslator(Skeleton s);
|
||||||
static SkeletonWeight skeletonWeightTranslator(const std::string &s);
|
static SkeletonWeight skeletonWeightTranslator(const std::string &s);
|
||||||
static std::string skeletonWeightTranslator(SkeletonWeight w);
|
static std::string skeletonWeightTranslator(SkeletonWeight w);
|
||||||
static AugmentationWeight augmentationWeightTranslator(const std::string &s);
|
static AugmentationWeight augmentationWeightTranslator(const std::string &s);
|
||||||
|
@ -170,7 +170,7 @@ namespace gtsam {
|
||||||
std::vector<size_t> unary(const GaussianFactorGraph &gfg) const ;
|
std::vector<size_t> unary(const GaussianFactorGraph &gfg) const ;
|
||||||
std::vector<size_t> natural_chain(const GaussianFactorGraph &gfg) const ;
|
std::vector<size_t> natural_chain(const GaussianFactorGraph &gfg) const ;
|
||||||
std::vector<size_t> bfs(const GaussianFactorGraph &gfg) const ;
|
std::vector<size_t> bfs(const GaussianFactorGraph &gfg) const ;
|
||||||
std::vector<size_t> kruskal(const GaussianFactorGraph &gfg, const FastMap<Key, size_t> &ordering, const std::vector<double> &w) const ;
|
std::vector<size_t> kruskal(const GaussianFactorGraph &gfg, const FastMap<Key, size_t> &ordering, const std::vector<double> &weights) const ;
|
||||||
std::vector<size_t> sample(const std::vector<double> &weights, const size_t t) const ;
|
std::vector<size_t> sample(const std::vector<double> &weights, const size_t t) const ;
|
||||||
Weights weights(const GaussianFactorGraph &gfg) const;
|
Weights weights(const GaussianFactorGraph &gfg) const;
|
||||||
SubgraphBuilderParameters parameters_;
|
SubgraphBuilderParameters parameters_;
|
||||||
|
|
Loading…
Reference in New Issue