Filter a set of particles based on the maximum difference in state indexes between them (a completely silly criteria). The thing to note is how the filter table determines when it should be applied and how it sets up the filters.
#ifndef IMPEXAMPLE_EXAMPLE_SUBSET_FILTER_TABLE_H
#define IMPEXAMPLE_EXAMPLE_SUBSET_FILTER_TABLE_H
#include "example_config.h"
IMPEXAMPLE_BEGIN_NAMESPACE
class IMPEXAMPLEEXPORT ExampleSubsetFilterTable:
public domino::SubsetFilterTable
{
int max_diff_;
const Particles ps_;
Ints get_indexes(const domino::Subset &s,
const domino::Subsets& prior_subsets) const;
public:
ExampleSubsetFilterTable(unsigned int max_diff, const ParticlesTemp &pt);
};
IMPEXAMPLE_END_NAMESPACE
#endif
#include <iterator>
IMPEXAMPLE_BEGIN_NAMESPACE
namespace {
class ExampleSubsetFilter: public domino::SubsetFilter {
Ints indices_;
int max_;
public:
ExampleSubsetFilter(const Ints &indices, unsigned int max):
domino::SubsetFilter("ExampleSubsetFilter%1%"), indices_(indices),
max_(max){}
};
bool ExampleSubsetFilter::get_is_ok(const domino::Assignment &a) const {
for (unsigned int i=0; i< indices_.size(); ++i) {
if (indices_[i]==-1) continue;
for (unsigned int j=0; j < i; ++j) {
if (indices_[j]==-1) continue;
if (std::abs(a[indices_[i-1]]- a[indices_[i]]) > max_) return false;
}
}
return true;
}
void ExampleSubsetFilter::do_show(std::ostream &) const {
}
}
ExampleSubsetFilterTable::ExampleSubsetFilterTable(unsigned int max_diff,
const ParticlesTemp &ps):
domino::SubsetFilterTable("ExampleSubsetFilterTable%1%"),
max_diff_(max_diff), ps_(ps.begin(), ps.end()){}
Ints ExampleSubsetFilterTable
::get_indexes(const domino::Subset &s,
const domino::Subsets& prior_subsets) const {
Ints ret(ps_.size(), -1);
for (unsigned int i=0; i< s.size(); ++i) {
for (unsigned int j=0; j< ps_.size(); ++j) {
if (s[i]== ps_[j]) {
ret[j]=i;
break;
}
}
}
for (unsigned int i=0; i< ret.size(); ++i) {
if (ret[i]==-1) return Ints();
}
for (unsigned int i=0; i< prior_subsets.size(); ++i) {
unsigned int count=0;
for (unsigned j=0; j< prior_subsets[i].size(); ++j) {
for (unsigned int k=0; k< ps_.size(); ++k) {
if (prior_subsets[i][j]== ps_[k]) {
++count;
break;
}
}
}
if (count == ps_.size()) {
return Ints();
}
}
return ret;
}
double ExampleSubsetFilterTable::
get_strength(const domino::Subset& cur_subset,
const domino::Subsets& prior_subsets) const {
if (get_indexes(cur_subset, prior_subsets).size()!= ps_.size()) {
return 0;
} else {
return .5;
}
}
domino::SubsetFilter* ExampleSubsetFilterTable::
get_subset_filter(const domino::Subset& cur_subset,
const domino::Subsets& prior_subsets) const {
Ints its= get_indexes(cur_subset, prior_subsets);
if (its.size() != ps_.size()) {
return nullptr;
} else {
IMP_NEW(ExampleSubsetFilter, ret, (its, max_diff_));
return ret.release();
}
}
void ExampleSubsetFilterTable::do_show(std::ostream &) const {}
IMPEXAMPLE_END_NAMESPACE