IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
Weight.h
Go to the documentation of this file.
1 /**
2  * \file IMP/isd/Weight.h
3  * \brief Add weights constrained to the unit simplex to a particle.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPISD_WEIGHT_H
10 #define IMPISD_WEIGHT_H
11 
12 #include <IMP/isd/isd_config.h>
14 #include <IMP/Particle.h>
15 #include <IMP/decorator_macros.h>
16 #include <IMP/Model.h>
17 #include <IMP/Decorator.h>
18 
19 IMPISD_BEGIN_NAMESPACE
20 
21 static const int IMPISD_MAX_WEIGHTS = 1000;
22 
23 //! Add weights to a particle.
24 /** Weights are constrained to the unit simplex.
25 
26  \see algebra::UnitSimplexD
27  \ingroup decorators
28 */
29 class IMPISDEXPORT Weight : public Decorator {
30 
31  static void do_setup_particle(Model *m, ParticleIndex pi);
32 
33  static void do_setup_particle(Model *m, ParticleIndex pi, Int nweights);
34 
35  static void do_setup_particle(Model *m, ParticleIndex pi,
36  const algebra::VectorKD& w);
37 
38  //! Add unit simplex constraint.
39  static void add_constraint(Model *m, ParticleIndex pi);
40 
41  static ObjectKey get_constraint_key();
42 
43  public:
45 
46  //! Set up an empty Weight.
47  /** Weights must be added with add_weight() before use. */
49 
50  //! Set up Weight with a fixed number of weights.
51  /** All weights are initialized with the same value. */
53 
54  //! Set up Weight from the provided weight vector.
56 
57  IMPISD_DEPRECATED_METHOD_DECL(2.12)
58  static IntKey get_nstates_key();
59 
60  //! Get number of weights key
61  static IntKey get_number_of_weights_key();
62 
63  //! Get ith weight key
64  static FloatKey get_weight_key(int i);
65 
66  //! Get all weight keys
67  FloatKeys get_weight_keys() const;
68 
69  //! Get the ith weight
70  Float get_weight(int i) const;
71 
72  //! Get all weights
73  algebra::VectorKD get_weights() const;
74 
75  //! Set the ith weight lazily.
76  /** Delay enforcing the simplex constraint until Model::update(). */
77  void set_weight_lazy(int i, Float wi);
78 
79  //! Set all the weights
80  /** Delay enforcing the simplex constraint until Model::update(). */
81  void set_weights_lazy(const algebra::VectorKD& w);
82 
83  //! Set all weights, enforcing the simplex constraint
84  /** \see algebra::get_projected */
85  void set_weights(const algebra::VectorKD& w);
86 
87  //! Get weights are optimized
88  bool get_weights_are_optimized() const;
89 
90  //! Set weights are optimized
91  void set_weights_are_optimized(bool tf);
92 
93  //! Get derivative wrt ith weight.
94  Float get_weight_derivative(int i) const;
95 
96  //! Get derivatives wrt all weights.
97  algebra::VectorKD get_weights_derivatives() const;
98 
99  //! Add to derivative wrt ith weight.
100  void add_to_weight_derivative(int i, Float dwi,
101  const DerivativeAccumulator &da);
102 
103  //! Add to derivatives wrt all weights.
104  void add_to_weights_derivatives(const algebra::VectorKD& dw,
105  const DerivativeAccumulator &da);
106 
107  //! Extend the weight vector by one element lazily.
108  /** This should only be called during set-up and cannot be called
109  for an optimized Weight.
110  This version delays enforcing the simplex constraint until
111  Model::update().
112  */
113  void add_weight_lazy(Float wi = 0);
114 
115  //! Extend the weight vector by one element.
116  /** This should only be called during set-up and cannot be called
117  for an optimized Weight.
118  */
119  void add_weight(Float wi = 0);
120 
121  //! Get number of weights.
122  Int get_number_of_weights() const;
123 
124  //! Get unit simplex on which weight vector lies.
125  algebra::UnitSimplexKD get_unit_simplex() const;
126 
127  //! Set number of weights lazily
128  /** This should only be called during set-up and cannot be called
129  for an optimized Weight. New weights are initialized to 0.
130  This version delays enforcing the simplex constraint until
131  Model::update().
132  */
133  void set_number_of_weights_lazy(Int nweights);
134 
135  //! Set number of weights.
136  /** This should only be called during set-up and cannot be called
137  for an optimized Weight. New weights are initialized to 0.
138  */
139  void set_number_of_weights(Int nweights);
140 
141  static bool get_is_setup(Model *m, ParticleIndex pi);
142 };
143 
144 IMP_DECORATORS(Weight, Weights, Decorators);
145 
146 
147 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
148 class IMPISDEXPORT WeightSimplexConstraint : public IMP::Constraint {
149  private:
150  ParticleIndex pi_;
151 
152  private:
153  WeightSimplexConstraint(Particle *p)
154  : IMP::Constraint(p->get_model(), "WeightSimplexConstraint%1%")
155  , pi_(p->get_index()) {}
156 
157  public:
158  friend class Weight;
159  virtual void do_update_attributes() override;
160  virtual void do_update_derivatives(DerivativeAccumulator *da) override;
161  virtual ModelObjectsTemp do_get_inputs() const override;
162  virtual ModelObjectsTemp do_get_outputs() const override;
163  IMP_OBJECT_METHODS(WeightSimplexConstraint);
164 };
165 #endif
166 
167 IMPISD_END_NAMESPACE
168 
169 #endif /* IMPISD_WEIGHT_H */
The base class for decorators.
Add weights to a particle.
Definition: Weight.h:29
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Storage of a model, its restraints, constraints and particles.
Simple unit simplex class.
Implement a constraint on the Model.
Definition: Constraint.h:49
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:73
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
Helper macros for implementing Decorators.
#define IMP_DECORATOR_SETUP_0(Name)
Interface to specialized Particle types (e.g. atoms)
Definition: Decorator.h:118
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
virtual ModelObjectsTemp do_get_outputs() const =0
#define IMP_DECORATOR_METHODS(Name, Parent)
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Class to handle individual particles of a Model object.
Definition: Particle.h:41
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
int Int
Basic integer value.
Definition: types.h:34
VectorD<-1 > VectorKD
Definition: VectorD.h:437
virtual ModelObjectsTemp do_get_inputs() const =0
Class for adding derivatives from restraints to the model.