IMP logo
IMP Reference Guide  2.15.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-2021 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  IMPISD_DEPRECATED_METHOD_DECL(2.12)
122  Int get_number_of_states() const;
123 
124  //! Get number of weights.
125  Int get_number_of_weights() const;
126 
127  //! Get unit simplex on which weight vector lies.
128  algebra::UnitSimplexKD get_unit_simplex() const;
129 
130  //! Set number of weights lazily
131  /** This should only be called during set-up and cannot be called
132  for an optimized Weight. New weights are initialized to 0.
133  This version delays enforcing the simplex constraint until
134  Model::update().
135  */
136  void set_number_of_weights_lazy(Int nweights);
137 
138  //! Set number of weights.
139  /** This should only be called during set-up and cannot be called
140  for an optimized Weight. New weights are initialized to 0.
141  */
142  void set_number_of_weights(Int nweights);
143 
144  static bool get_is_setup(Model *m, ParticleIndex pi);
145 };
146 
147 IMP_DECORATORS(Weight, Weights, Decorators);
148 
149 
150 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
151 class IMPISDEXPORT WeightSimplexConstraint : public IMP::Constraint {
152  private:
153  ParticleIndex pi_;
154 
155  private:
156  WeightSimplexConstraint(Particle *p)
157  : IMP::Constraint(p->get_model(), "WeightSimplexConstraint%1%")
158  , pi_(p->get_index()) {}
159 
160  public:
161  friend class Weight;
162  virtual void do_update_attributes() IMP_OVERRIDE;
163  virtual void do_update_derivatives(DerivativeAccumulator *da) IMP_OVERRIDE;
164  virtual ModelObjectsTemp do_get_inputs() const IMP_OVERRIDE;
165  virtual ModelObjectsTemp do_get_outputs() const IMP_OVERRIDE;
166  IMP_OBJECT_METHODS(WeightSimplexConstraint);
167 };
168 #endif
169 
170 IMPISD_END_NAMESPACE
171 
172 #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:72
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...
#define IMP_DECORATOR_METHODS(Name, Parent)
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
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:35
VectorD<-1 > VectorKD
Definition: VectorD.h:437
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for adding derivatives from restraints to the model.