IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
ScoreAccumulator.h
Go to the documentation of this file.
1 /**
2  * \file IMP/ScoreAccumulator.h \brief Class for adding scores from
3  * restraints to the model.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_SCORE_ACCUMULATOR_H
10 #define IMPKERNEL_SCORE_ACCUMULATOR_H
11 
12 #include <IMP/kernel_config.h>
13 #include "DerivativeAccumulator.h"
14 #include "constants.h"
15 #include <IMP/showable_macros.h>
16 #include <IMP/value_macros.h>
17 #include <IMP/thread_macros.h>
18 #include <IMP/log_macros.h>
19 #include <IMP/tuple_macros.h>
20 #include <IMP/math.h>
21 #include <IMP/exception.h>
22 #include <cereal/access.hpp>
23 
24 IMPKERNEL_BEGIN_NAMESPACE
25 
26 class Restraint;
27 
28 /** A class for storing evaluation state.*/
30  double score;
31  bool good;
32  EvaluationState(double oscore, bool ogood) : score(oscore), good(ogood) {}
33  EvaluationState() : score(BAD_SCORE), good(false) {}
34  IMP_SHOWABLE_INLINE(EvaluationState, out << score << " " << good;);
35 
36 private:
37  friend class cereal::access;
38 
39  template<class Archive> void serialize(Archive &ar) {
40  ar(score, good);
41  }
42 };
44 
45 //! Class for adding up scores during ScoringFunction evaluation.
46 /** This provides a place to accumulate scores from Restraint
47  evaluation. A new ScoreAccumulator is created for each Restraint
48  evaluation context (eg each Restraint::add_score_and_derivatives() call).
49  As a result, the ScoreAccumulator can automatically handle
50  restraint and derivative weights, keeping track of maximum scores
51  and other needed bookkeeping.
52 */
53 class IMPKERNELEXPORT ScoreAccumulator : public Value {
54  EvaluationState *score_;
55  DerivativeAccumulator weight_;
56  double global_max_;
57  double local_max_;
58  bool deriv_;
59  bool abort_on_bad_;
60  friend class ScoringFunction;
61  friend class Restraint;
62  ScoreAccumulator(EvaluationState *s, double weight, bool deriv,
63  double global_max, double local_max, bool abort_on_bad)
64  : score_(s),
65  weight_(weight),
66  global_max_(global_max),
67  local_max_(local_max),
68  deriv_(deriv),
69  abort_on_bad_(abort_on_bad) {}
70 
71  public:
72  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(ScoreAccumulator);
73  /** For swig, makes invalid (not null) state.*/
74  ScoreAccumulator() : score_(nullptr) {}
75  /** Compose outer accumulator with one for this restraint. */
77 
78  /** Compose outer accumulator with one for this restraint. */
79  ScoreAccumulator(ScoreAccumulator o, double weight, double local_max) {
80  operator=(o);
81  weight_ = DerivativeAccumulator(o.weight_, weight);
82  local_max_ = std::min(local_max, o.local_max_);
83  }
84 
85  /** Add to the total score. It will be weighted appropriately
86  internally. */
87  void add_score(double score) {
88  double wscore = weight_.get_weight() * score;
89  IMP_OMP_PRAGMA(atomic)
90  score_->score += wscore;
91  if (score > local_max_) {
92  IMP_OMP_PRAGMA(critical(imp_abort))
93  score_->good = false;
94  }
95  IMP_LOG_VERBOSE("Score is now " << score_->score << std::endl);
96  }
97 
98  //! Return if the score already exceeds the maximum
99  /** Expensive restraints can check this during evaluation to determine
100  if another restraint has aborted evaluation.
101  */
102  bool get_abort_evaluation() const {
103  if (global_max_ == NO_MAX && !abort_on_bad_) return false;
104  if (abort_on_bad_) {
105  bool good;
106  IMP_OMP_PRAGMA(critical(imp_abort))
107  good = score_->good;
108  return !good;
109  } else {
110  // be lazy for now
111  //#pragma omp flush (score_->score)
112  double score = score_->score;
113  return score > global_max_;
114  }
115  }
116 
117  /** Return true if the current evaluation being done is one where
118  scores are only considered if they are below some threshold
119  (get_maximum()). */
120  bool get_is_evaluate_if_below() const { return global_max_ != NO_MAX; }
121  /** Return true if the current evaluation should abort if any restraint
122  is above its maximum allowed good score. Restraints that take
123  advantage of this in evaluation should simply consult
124  get_maximum() as that will take into account both their maximum
125  and that on any RestraintSets that contain them and are being
126  evaluated.
127  */
128  bool get_is_evaluate_if_good() const { return abort_on_bad_; }
129  /** The maximum allowed score for the
130  Restraint::do_add_score_and_derivatives() call. */
131  double get_maximum() const { return std::min(global_max_, local_max_); }
132 
133  DerivativeAccumulator *get_derivative_accumulator() {
134  if (deriv_) {
135  return &weight_;
136  } else {
137  return nullptr;
138  }
139  }
140 
141  IMP_SHOWABLE_INLINE(ScoreAccumulator, out << *score_);
142 };
143 
145 
146 IMPKERNEL_END_NAMESPACE
147 
148 #endif /* IMPKERNEL_SCORE_ACCUMULATOR_H */
Helper functions to check for NaN or infinity.
const double NO_MAX
Use this value when you want to turn off maximum for restraint evaluation.
bool get_is_evaluate_if_below() const
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
bool get_is_evaluate_if_good() const
Various useful constants.
Class for adding derivatives from restraints to the model.
void add_score(double score)
const double BAD_SCORE
ScoreAccumulator(ScoreAccumulator o, double weight, double local_max)
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
Macros to help in defining tuple classes.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Base class for a simple primitive-like type.
Definition: Value.h:23
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Logging and error reporting support.
Class for adding up scores during ScoringFunction evaluation.
Represents a scoring function on the model.
double get_maximum() const
Macros to help in implementing Value objects.
Control for OpenMP.
#define IMP_OMP_PRAGMA(x)
Definition: thread_macros.h:35
bool get_abort_evaluation() const
Return if the score already exceeds the maximum.
Macros to help with objects that can be printed to a stream.
Class for adding derivatives from restraints to the model.
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:56