IMP logo
IMP Reference Guide  develop.6701db5d00,2026/08/25
The Integrative Modeling Platform
ScoreState.h
Go to the documentation of this file.
1 /**
2  * \file IMP/ScoreState.h \brief Shared score state.
3  *
4  * Copyright 2007-2026 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_SCORE_STATE_H
9 #define IMPKERNEL_SCORE_STATE_H
10 
11 #include <IMP/kernel_config.h>
12 #include <IMP/WeakPointer.h>
13 #include "DerivativeAccumulator.h"
14 #include "utility.h"
15 #include "ModelObject.h"
16 #include "base_types.h"
17 #include <IMP/check_macros.h>
18 #include <IMP/deprecation_macros.h>
19 #include <IMP/ref_counted_macros.h>
20 #include <iostream>
21 #include <cereal/access.hpp>
22 #include <cereal/types/base_class.hpp>
23 
24 IMPKERNEL_BEGIN_NAMESPACE
25 
26 //! ScoreStates maintain invariants in the Model.
27 /** ScoreStates allow actions to be taken before and after the restraint
28  evaluation process. Such code can be used to, for example:
29  - log the optimization process
30  - maintain constraints (see Constraint)
31 
32  ScoreStates have two methods which are called during
33  the Model::evaluate() function
34  - before_evaluate() which is allowed to change the contents of
35  containers and the value of attributes of particles and
36  - after_evaluate() which can change particle derivatives
37 
38  The Model uses information that the ScoreState reports about
39  its input and output containers and particles to determine a safe
40  order in which all the ScoreState objects registered in the model
41  can be applied. That is, the Model will ensure that a ScoreState
42  that has Particle \c A in its output list is applied before a
43  ScoreState that has \c A in its input list.
44  For ScoreState::before_evaluate(), Input and output lists are
45  returned by the respective get_input_* and get_output_* calls.
46  For ScoreState::after_evaluate() they are reversed (see note below).
47 
48  \note The input and output sets for the ScoreState::after_evaluate()
49  functions are assumed to be the reverse of the ScoreState::before_evaluate()
50  functions. As a result, the ScoreStates are applied in opposite order
51  after evaluate. If you have a ScoreState for which this is not true,
52  consider splitting it into two parts.
53 
54  \see Model::add_score_state().
55  */
56 class IMPKERNELEXPORT ScoreState : public ModelObject {
57  int update_order_;
58  bool can_skip_;
59 
60  friend class cereal::access;
61 
62  template<class Archive> void serialize(Archive &ar) {
63  ar(cereal::base_class<ModelObject>(this), can_skip_);
64  if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
65  update_order_ = -1;
66  }
67  }
68 
69  protected:
70  // true iff this ScoreState explicitly has a null JAX implementation
71  // (usually because it makes no sense for JAX). This flag is always set
72  // in the constructor (and nowhere else) so is not serialized.
73  bool is_ignored_by_jax_;
74 
75  //! Set whether we can skip during model evaluation if appropriate
76  /** This should be set only once before the state is used (ideally in the
77  constructor), and can be used for ScoreStates that can safely be
78  skipped if their inputs or outputs didn't move. For example, a state
79  that expects to log every N frames should not be skipped.
80  By default, states are not skipped.
81  */
82  void set_can_skip(bool can_skip) { can_skip_ = can_skip; }
83 
84  public:
85  ScoreState(Model *m, std::string name);
86  ScoreState() : is_ignored_by_jax_(false) {}
87  //! Force update of the structure.
88  void before_evaluate();
89 
90  //! Do post evaluation work if needed
91  void after_evaluate(DerivativeAccumulator *accpt);
92 
93  //! Get whether this state can be skipped if its inputs/outputs didn't move
94  /** During Monte Carlo and other types of sampling that only move parts
95  of the system, ScoreStates where this returns true may be skipped at
96  model evaluation time if none of the inputs or outputs moved since
97  the last evaluation.
98  */
99  bool get_can_skip() const { return can_skip_; }
100 
101 #ifndef IMP_DOXYGEN
102  bool get_has_update_order() const { return update_order_ != -1; }
103  unsigned int get_update_order() const { return update_order_; }
104  virtual void handle_set_has_required_score_states(bool tf) override;
105  bool get_is_ignored_by_jax() const { return is_ignored_by_jax_; }
106 
107 #endif
108 
109  protected:
110  //! Update the state given the current state of the model.
111  /** This is also called prior to every calculation of the model score.
112  It should be implemented by ScoreStates in order to provide functionality.
113  */
114  virtual void do_before_evaluate() = 0;
115 
116  //! Do any necessary updates after the model score is calculated.
117  /** \param[in] accpt The object used to scale derivatives in the score
118  calculation, or nullptr if derivatives were not
119  requested.
120  */
121  virtual void do_after_evaluate(DerivativeAccumulator *accpt) = 0;
122 
123  IMP_REF_COUNTED_DESTRUCTOR(ScoreState);
124 };
125 
126 /** Return an appropriate (topologically sorted) order to update
127  the score states in. */
128 IMPKERNELEXPORT ScoreStatesTemp get_update_order(ScoreStatesTemp input);
129 
130 #ifndef SWIG
131 /** Return an appropriate (topologically sorted) order to update
132  the score states in. */
133 IMPKERNELEXPORT ScoreStatesTemp get_update_order(std::set<ScoreState*> input);
134 #endif
135 
136 IMPKERNEL_END_NAMESPACE
137 
138 #endif /* IMPKERNEL_SCORE_STATE_H */
Control display of deprecation information.
Macros to help with reference counting.
Basic types used by IMP.
void set_can_skip(bool can_skip)
Set whether we can skip during model evaluation if appropriate.
Definition: ScoreState.h:82
Class for adding derivatives from restraints to the model.
ScoreStatesTemp get_update_order(std::set< ScoreState * > input)
#define IMP_REF_COUNTED_DESTRUCTOR(Name)
Set up destructor for a ref counted object.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Base class for objects in a Model that depend on other objects.
Definition: ModelObject.h:28
bool get_can_skip() const
Get whether this state can be skipped if its inputs/outputs didn't move.
Definition: ScoreState.h:99
ScoreStates maintain invariants in the Model.
Definition: ScoreState.h:56
virtual void handle_set_has_required_score_states(bool)
Definition: ModelObject.h:93
A weak pointer to an Object or RefCountedObject.
Various general useful functions for IMP.
Base class for objects in a Model that depend on other objects.
Helper macros for throwing and handling exceptions.
IMP::Vector< IMP::WeakPointer< ScoreState > > ScoreStatesTemp
Definition: base_types.h:130
ScoreStatesTemp get_update_order(ScoreStatesTemp input)