9#ifndef IMPDOMINO_SUBSET_SCORES_H
10#define IMPDOMINO_SUBSET_SCORES_H
22#include <boost/unordered_map.hpp>
24#ifdef IMP_DOMINO_USE_IMP_RMF
25# include <RMF/config.h>
26# if RMF_HAS_DEPRECATED_BACKENDS
27# include <RMF/HDF5/Group.h>
32IMPDOMINO_BEGIN_NAMESPACE
41class IMPDOMINOEXPORT RestraintCache :
public Object {
44 IMP_NAMED_TUPLE_3(RestraintData, RestraintDatas,
46 Subset, subset,
double, max, );
47 IMP_NAMED_TUPLE_2(RestraintSetData, RestraintSetDatas,
Slice, slice,
49 IMP_NAMED_TUPLE_2(SetData, SetDatas, RestraintSetDatas, members,
double,
54 typedef boost::unordered_map<Restraint *, RestraintData> RMap;
56 typedef boost::unordered_map<Restraint *, SetData> SMap;
62 typedef double result_type;
63 typedef Key argument_type;
64 template <
class Cache>
65 result_type operator()(
const argument_type &k,
const Cache &cache)
const {
66 RMap::const_iterator it = rmap_.find(k.get_restraint());
67 if (it != rmap_.end()) {
68 Subset s = rmap_.find(k.get_restraint())->second.get_subset();
73 e = it->second.get_scoring_function()->evaluate_if_below(
74 false, it->second.get_max());
77 <<
" evaluated to " << e <<
" on "
78 << k.get_assignment() <<
" vs "
79 << it->second.get_max() << std::endl);
81 if (e > it->second.get_max()) e = std::numeric_limits<double>::max();
84 SMap::const_iterator it = sets_.find(k.get_restraint());
89 for (
unsigned int i = 0; i < it->second.get_members().size(); ++i) {
90 Assignment cur = it->second.get_members()[i].get_slice().get_sliced(
92 double score = cache.get(
93 argument_type(it->second.get_members()[i].get_restraint(), cur));
94 total += score * k.get_restraint()->get_weight();
95 if (total >= it->second.get_max()) {
100 <<
" evaluated to " << total <<
" on "
101 << k.get_assignment() <<
" with max "
102 << it->second.get_max() << std::endl);
103 if (total >= it->second.get_max()) {
104 return std::numeric_limits<double>::max();
113 "don't pass restraint sets here as second arg");
114 sets_[rs].access_members().push_back(RestraintSetData(slice, r));
115 sets_[rs].set_max(max);
119 "don't pass restraint sets here");
120 if (rmap_.find(e) == rmap_.end()) {
121 rmap_[e] = RestraintData(e->create_scoring_function(1.0, max), s, max);
124 "Subsets don't match on restraint update");
125 rmap_[e].set_max(std::min(rmap_[e].get_max(), max));
133 struct ApproximatelyEqual {
134 bool operator()(
double a,
double b)
const {
135 return std::abs(a - b) < .1 * (a + b) + .1;
138 typedef boost::unordered_map<Particle *, ParticlesTemp>
140 void add_restraint_internal(
Restraint *r,
unsigned int index,
142 Subset parent_subset,
const DepMap &dependencies);
143 void add_restraint_set_child_internal(
Restraint *r,
148 void add_restraint_set_internal(
RestraintSet *rs,
unsigned int index,
149 const Subset &cur_subset,
double cur_max,
150 const DepMap &dependencies);
157 typedef boost::unordered_map<Pointer<Restraint>,
Subset>
159 KnownRestraints known_restraints_;
161 typedef boost::unordered_map<Pointer<Restraint>,
int>
163 RestraintIndex restraint_index_;
164 unsigned int next_index_;
168 unsigned int size = std::numeric_limits<unsigned int>::max());
182 double s = cache_.get(
Key(r, a));
200 const Subsets &exclusions)
const;
204#if RMF_HAS_DEPRECATED_BACKENDS || defined(IMP_DOXYGEN)
215 RMF::HDF5::Group group,
unsigned int max_entries);
216 void load_cache(
const ParticlesTemp &ps, RMF::HDF5::ConstGroup group);
230 double get_hit_rate()
const {
return cache_.get_hit_rate(); }
234IMPDOMINO_END_NAMESPACE
A Bayesian inference-based sampler.
A shared base class to help in debugging and things.
Abstract base class for all restraints.
A Bayesian inference-based sampler.
A Bayesian inference-based sampler.
Helper classes for various types of caching.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Key()
make a default key in a well-defined null state
Implement a simple least recently used cache.
Object(std::string name)
Construct an object with the given name.
void set_was_used(bool tf) const
Object used to hold a set of restraints.
A restraint is a term in an IMP ScoringFunction.
Provide a consistent interface for things that take Restraints as arguments.
A class to change and restore log state.
Helper class to aid in output of IMP classes to streams.
Store a configuration of a subset.
Slice get_slice(Restraint *r, const Subset &s) const
Return the slice for that restraint given the subset.
void show_restraint_information(std::ostream &out=std::cout) const
Print out information about the known restraints and restraint sets.
double get_score(Restraint *r, const Assignment &a) const
Get the score of a set or restraint.
void save_cache(const ParticlesTemp &particle_ordering, const RestraintsTemp &restraints, RMF::HDF5::Group group, unsigned int max_entries)
void validate() const
Check the entries in the cache.
RestraintsTemp get_restraints(const Subset &s, const Subsets &exclusions) const
double get_score(Restraint *r, const Subset &s, const Assignment &a) const
void load_last_score(Restraint *r, const Subset &s, const Assignment &a)
Make it so Restraint::get_last_score() returns the score.
void add_restraints(const RestraintsAdaptor &rs)
unsigned int get_number_of_entries() const
Return the number of entries currently in the cache.
Store a subset of a subset or assignment.
Represent a subset of the particles being optimized.
Functions for helping optimize and understand DOMINO-based sampling.
Logging and error reporting support.
#define IMP_LOG_TERSE(expr)
void load_particle_states(const Subset &s, const Assignment &ss, const ParticleStatesTable *pst)
Load the appropriate state for each particle in a Subset.
IMP::Vector< Subset > Subsets
IMP::Vector< IMP::WeakPointer< Restraint > > RestraintsTemp
@ SILENT
Do not output any text.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
A Bayesian inference-based sampler.
A smart pointer to a ref-counted Object that is a class member.
Smart pointer to Object-derived classes that does not refcount.