IMP logo
IMP Reference Guide  2.10.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-2018 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/nullptr.h>
22 #include <IMP/exception.h>
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 };
37 
38 //! Class for adding up scores during ScoringFunction evaluation.
39 /** This provides a place to accumulate scores from Restraint
40  evaluation. A new ScoreAccumulator is created for each Restraint
41  evaluation context (eg each Restraint::add_score_and_derivatives() call).
42  As a result, the ScoreAccumulator can automatically handle
43  restraint and derivative weights, keeping track of maximum scores
44  and other needed bookkeeping.
45 */
46 class IMPKERNELEXPORT ScoreAccumulator : public Value {
47  EvaluationState *score_;
48  DerivativeAccumulator weight_;
49  double global_max_;
50  double local_max_;
51  bool deriv_;
52  bool abort_on_bad_;
53  friend class ScoringFunction;
54  friend class Restraint;
55  ScoreAccumulator(EvaluationState *s, double weight, bool deriv,
56  double global_max, double local_max, bool abort_on_bad)
57  : score_(s),
58  weight_(weight),
59  global_max_(global_max),
60  local_max_(local_max),
61  deriv_(deriv),
62  abort_on_bad_(abort_on_bad) {}
63 
64  public:
65  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(ScoreAccumulator);
66  /** For swig, makes invalid (not null) state.*/
67  ScoreAccumulator() : score_(nullptr) {}
68  /** Compose outer accumulator with one for this restraint. */
70 
71  /** Compose outer accumulator with one for this restraint. */
72  ScoreAccumulator(ScoreAccumulator o, double weight, double local_max) {
73  operator=(o);
74  weight_ = DerivativeAccumulator(o.weight_, weight);
75  local_max_ = std::min(local_max, o.local_max_);
76  }
77 
78  /** Add to the total score. It will be weighted appropriately
79  internally. */
80  void add_score(double score) {
81  double wscore = weight_.get_weight() * score;
82  IMP_OMP_PRAGMA(atomic)
83  score_->score += wscore;
84  if (score > local_max_) {
85  IMP_OMP_PRAGMA(critical(imp_abort))
86  score_->good = false;
87  }
88  IMP_LOG_VERBOSE("Score is now " << score_->score << std::endl);
89  }
90 
91  //! Return if the score already exceeds the maximum
92  /** Expensive restraints can check this during evaluation to determine
93  if another restraint has aborted evaluation.
94  */
95  bool get_abort_evaluation() const {
96  if (global_max_ == NO_MAX && !abort_on_bad_) return false;
97  if (abort_on_bad_) {
98  bool good;
99  IMP_OMP_PRAGMA(critical(imp_abort))
100  good = score_->good;
101  return !good;
102  } else {
103  // be lazy for now
104  //#pragma omp flush (score_->score)
105  double score = score_->score;
106  return score > global_max_;
107  }
108  }
109 
110  /** Return true if the current evaluation being done is one where
111  scores are only considered if they are below some threshold
112  (get_maximum()). */
113  bool get_is_evaluate_if_below() const { return global_max_ != NO_MAX; }
114  /** Return true if the current evaluation should abort if any restraint
115  is above its maximum allowed good score. Restraints that take
116  advantage of this in evaluation should simply consult
117  get_maximum() as that will take into account both their maximum
118  and that on any RestraintSets that contain them and are being
119  evaluated.
120  */
121  bool get_is_evaluate_if_good() const { return abort_on_bad_; }
122  /** The maximum allowed score for the
123  Restraint::do_add_score_and_derivatives() call. */
124  double get_maximum() const { return std::min(global_max_, local_max_); }
125 
126  DerivativeAccumulator *get_derivative_accumulator() {
127  if (deriv_) {
128  return &weight_;
129  } else {
130  return nullptr;
131  }
132  }
133 
134  IMP_SHOWABLE_INLINE(ScoreAccumulator, out << *score_);
135 };
136 
138 
139 IMPKERNEL_END_NAMESPACE
140 
141 #endif /* IMPKERNEL_SCORE_ACCUMULATOR_H */
Declare an efficient stl-compatible map.
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:94
Various general useful macros for IMP.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:39
Base for a simple primitive-like type.
Definition: Value.h:21
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Provide a nullptr keyword analog.
Logging and error reporting support.
Class for adding up scores during ScoringFunction evaluation.
Represents a scoring function on the model.
double get_maximum() const
Various general useful macros for IMP.
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.
const std::nullptr_t nullptr
Definition: nullptr.h:27
Various general useful macros for IMP.
Class for adding derivatives from restraints to the model.
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:54