IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/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>
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
24IMPKERNEL_BEGIN_NAMESPACE
25
26class Restraint;
27
28/** A class for storing evaluation state.*/
29struct EvaluationState {
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
36private:
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*/
53class IMPKERNELEXPORT ScoreAccumulator : public Value {
54 EvaluationState *score_;
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. */
76 ScoreAccumulator(ScoreAccumulator o, const Restraint *r);
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
146IMPKERNEL_END_NAMESPACE
147
148#endif /* IMPKERNEL_SCORE_ACCUMULATOR_H */
Class for adding derivatives from restraints to the model.
Class for adding derivatives from restraints to the model.
Class for adding up scores during ScoringFunction evaluation.
ScoreAccumulator(ScoreAccumulator o, const Restraint *r)
bool get_is_evaluate_if_below() const
bool get_is_evaluate_if_good() const
void add_score(double score)
ScoreAccumulator(ScoreAccumulator o, double weight, double local_max)
bool get_abort_evaluation() const
Return if the score already exceeds the maximum.
Various useful constants.
Exception definitions and assertions.
Logging and error reporting support.
#define IMP_LOG_VERBOSE(expr)
Definition log_macros.h:83
Helper functions to check for NaN or infinity.
const double BAD_SCORE
IMP::Vector< EvaluationState > EvaluationStates
const double NO_MAX
Use this value when you want to turn off maximum for restraint evaluation.
IMP::Vector< ScoreAccumulator > ScoreAccumulators
Macros to help with objects that can be printed to a stream.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Control for OpenMP.
#define IMP_OMP_PRAGMA(x)
Macros to help in defining tuple classes.
Macros to help in implementing Value objects.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.