IMP logo
IMP Reference Guide  develop.4e0325f730,2022/07/05
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 
23 IMPKERNEL_BEGIN_NAMESPACE
24 
25 class Restraint;
26 
27 /** A class for storing evaluation state.*/
29  double score;
30  bool good;
31  EvaluationState(double oscore, bool ogood) : score(oscore), good(ogood) {}
32  EvaluationState() : score(BAD_SCORE), good(false) {}
33  IMP_SHOWABLE_INLINE(EvaluationState, out << score << " " << good;);
34 };
36 
37 //! Class for adding up scores during ScoringFunction evaluation.
38 /** This provides a place to accumulate scores from Restraint
39  evaluation. A new ScoreAccumulator is created for each Restraint
40  evaluation context (eg each Restraint::add_score_and_derivatives() call).
41  As a result, the ScoreAccumulator can automatically handle
42  restraint and derivative weights, keeping track of maximum scores
43  and other needed bookkeeping.
44 */
45 class IMPKERNELEXPORT ScoreAccumulator : public Value {
46  EvaluationState *score_;
47  DerivativeAccumulator weight_;
48  double global_max_;
49  double local_max_;
50  bool deriv_;
51  bool abort_on_bad_;
52  friend class ScoringFunction;
53  friend class Restraint;
54  ScoreAccumulator(EvaluationState *s, double weight, bool deriv,
55  double global_max, double local_max, bool abort_on_bad)
56  : score_(s),
57  weight_(weight),
58  global_max_(global_max),
59  local_max_(local_max),
60  deriv_(deriv),
61  abort_on_bad_(abort_on_bad) {}
62 
63  public:
64  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(ScoreAccumulator);
65  /** For swig, makes invalid (not null) state.*/
66  ScoreAccumulator() : score_(nullptr) {}
67  /** Compose outer accumulator with one for this restraint. */
69 
70  /** Compose outer accumulator with one for this restraint. */
71  ScoreAccumulator(ScoreAccumulator o, double weight, double local_max) {
72  operator=(o);
73  weight_ = DerivativeAccumulator(o.weight_, weight);
74  local_max_ = std::min(local_max, o.local_max_);
75  }
76 
77  /** Add to the total score. It will be weighted appropriately
78  internally. */
79  void add_score(double score) {
80  double wscore = weight_.get_weight() * score;
81  IMP_OMP_PRAGMA(atomic)
82  score_->score += wscore;
83  if (score > local_max_) {
84  IMP_OMP_PRAGMA(critical(imp_abort))
85  score_->good = false;
86  }
87  IMP_LOG_VERBOSE("Score is now " << score_->score << std::endl);
88  }
89 
90  //! Return if the score already exceeds the maximum
91  /** Expensive restraints can check this during evaluation to determine
92  if another restraint has aborted evaluation.
93  */
94  bool get_abort_evaluation() const {
95  if (global_max_ == NO_MAX && !abort_on_bad_) return false;
96  if (abort_on_bad_) {
97  bool good;
98  IMP_OMP_PRAGMA(critical(imp_abort))
99  good = score_->good;
100  return !good;
101  } else {
102  // be lazy for now
103  //#pragma omp flush (score_->score)
104  double score = score_->score;
105  return score > global_max_;
106  }
107  }
108 
109  /** Return true if the current evaluation being done is one where
110  scores are only considered if they are below some threshold
111  (get_maximum()). */
112  bool get_is_evaluate_if_below() const { return global_max_ != NO_MAX; }
113  /** Return true if the current evaluation should abort if any restraint
114  is above its maximum allowed good score. Restraints that take
115  advantage of this in evaluation should simply consult
116  get_maximum() as that will take into account both their maximum
117  and that on any RestraintSets that contain them and are being
118  evaluated.
119  */
120  bool get_is_evaluate_if_good() const { return abort_on_bad_; }
121  /** The maximum allowed score for the
122  Restraint::do_add_score_and_derivatives() call. */
123  double get_maximum() const { return std::min(global_max_, local_max_); }
124 
125  DerivativeAccumulator *get_derivative_accumulator() {
126  if (deriv_) {
127  return &weight_;
128  } else {
129  return nullptr;
130  }
131  }
132 
133  IMP_SHOWABLE_INLINE(ScoreAccumulator, out << *score_);
134 };
135 
137 
138 IMPKERNEL_END_NAMESPACE
139 
140 #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:40
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:53