IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
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  //! Get number of weights key
58  static IntKey get_number_of_weights_key();
59 
60  //! Get ith weight key
61  static FloatKey get_weight_key(int i);
62 
63  //! Get all weight keys
64  FloatKeys get_weight_keys() const;
65 
66  //! Get the ith weight
67  Float get_weight(int i) const;
68 
69  //! Get all weights
70  algebra::VectorKD get_weights() const;
71 
72  //! Set the ith weight lazily.
73  /** Delay enforcing the simplex constraint until Model::update(). */
74  void set_weight_lazy(int i, Float wi);
75 
76  //! Set all the weights
77  /** Delay enforcing the simplex constraint until Model::update(). */
78  void set_weights_lazy(const algebra::VectorKD& w);
79 
80  //! Set all weights, enforcing the simplex constraint
81  /** \see algebra::get_projected */
82  void set_weights(const algebra::VectorKD& w);
83 
84  //! Get weights are optimized
85  bool get_weights_are_optimized() const;
86 
87  //! Set weights are optimized
88  void set_weights_are_optimized(bool tf);
89 
90  //! Get derivative wrt ith weight.
91  Float get_weight_derivative(int i) const;
92 
93  //! Get derivatives wrt all weights.
94  algebra::VectorKD get_weights_derivatives() const;
95 
96  //! Add to derivative wrt ith weight.
97  void add_to_weight_derivative(int i, Float dwi,
98  const DerivativeAccumulator &da);
99 
100  //! Add to derivatives wrt all weights.
101  void add_to_weights_derivatives(const algebra::VectorKD& dw,
102  const DerivativeAccumulator &da);
103 
104  //! Extend the weight vector by one element lazily.
105  /** This should only be called during set-up and cannot be called
106  for an optimized Weight.
107  This version delays enforcing the simplex constraint until
108  Model::update().
109  */
110  void add_weight_lazy(Float wi = 0);
111 
112  //! Extend the weight vector by one element.
113  /** This should only be called during set-up and cannot be called
114  for an optimized Weight.
115  */
116  void add_weight(Float wi = 0);
117 
118  //! Get number of weights.
119  Int get_number_of_weights() const;
120 
121  //! Get unit simplex on which weight vector lies.
122  algebra::UnitSimplexKD get_unit_simplex() const;
123 
124  //! Set number of weights lazily
125  /** This should only be called during set-up and cannot be called
126  for an optimized Weight. New weights are initialized to 0.
127  This version delays enforcing the simplex constraint until
128  Model::update().
129  */
130  void set_number_of_weights_lazy(Int nweights);
131 
132  //! Set number of weights.
133  /** This should only be called during set-up and cannot be called
134  for an optimized Weight. New weights are initialized to 0.
135  */
136  void set_number_of_weights(Int nweights);
137 
138  static bool get_is_setup(Model *m, ParticleIndex pi);
139 };
140 
141 IMP_DECORATORS(Weight, Weights, Decorators);
142 
143 
144 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
145 class IMPISDEXPORT WeightSimplexConstraint : public IMP::Constraint {
146  private:
147  ParticleIndex pi_;
148 
149  private:
150  WeightSimplexConstraint(Particle *p)
151  : IMP::Constraint(p->get_model(), "WeightSimplexConstraint%1%")
152  , pi_(p->get_index()) {}
153 
154  public:
155  friend class Weight;
156  virtual void do_update_attributes() override;
157  virtual void do_update_derivatives(DerivativeAccumulator *da) override;
158  virtual ModelObjectsTemp do_get_inputs() const override;
159  virtual ModelObjectsTemp do_get_outputs() const override;
160  IMP_OBJECT_METHODS(WeightSimplexConstraint);
161 };
162 #endif
163 
164 IMPISD_END_NAMESPACE
165 
166 #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:86
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:119
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
virtual ModelObjectsTemp do_get_outputs() const =0
Represent a unit simplex embedded in d-dimensional real space.
Definition: UnitSimplexD.h:89
#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:43
#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:424
virtual ModelObjectsTemp do_get_inputs() const =0
Class for adding derivatives from restraints to the model.