bpp-phyl3  3.0.0
Parameter.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LIKELIHOOD_DATAFLOW_PARAMETER_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_PARAMETER_H
7 
8 #include <Bpp/Exceptions.h>
12 #include <functional>
13 #include <unordered_map>
14 
15 
16 namespace bpp
17 {
26  public Value<Parameter*>
27 {
28 private:
29  const Context& context_;
30 
31 public:
33  using Target = Parameter;
34 
36  static std::shared_ptr<Self> create (Context& c, NodeRefVec&& deps, const Parameter& param)
37  {
38  checkDependenciesNotNull (typeid (Self), deps);
39  checkDependencyVectorSize (typeid (Self), deps, 1);
40  checkNthDependencyIsValue<double>(typeid (Self), deps, 0);
41  return cachedAs<Self>(c, std::make_shared<Self>(c, std::move(deps), param));
42  }
43 
44  static std::shared_ptr<Self> create (Context& c, const Parameter& param)
45  {
47 
48  return cachedAs<Self>(c, std::make_shared<Self>(c, std::move(deps), param));
49  }
50 
51  static std::shared_ptr<Self> resetDependencies(Context& c, std::shared_ptr<Self> self, NodeRefVec&& deps)
52  {
53  checkDependenciesNotNull (typeid (Self), deps);
54  checkDependencyVectorSize (typeid (Self), deps, 1);
55  checkNthDependencyIsValue<double>(typeid (Self), deps, 0);
56 
57  self->resetDependencies_(std::move(deps));
58 
59  return cachedAs<Self>(c, self);
60  }
61 
62 
64  : Parameter (param), Value<Parameter*>({std::shared_ptr<NumericMutable<double>>(new NumericMutable<double>(param.getValue()))}, this), context_(param.context_)
65  {}
66 
67  ConfiguredParameter (const Context& context, NodeRefVec&& deps, const Parameter& parameter);
68 
69  ConfiguredParameter (const Context& context, NodeRefVec&& deps, Parameter&& parameter);
70 
71  ConfiguredParameter* clone() const override
72  {
73  return new ConfiguredParameter(*this);
74  }
75 
77 
78  std::string description () const override;
79  std::string color () const override;
80  std::string debugInfo () const override;
81 
82  /*
83  * @brief setValue is transferred to the double dependency,
84  * through the parameter constraints test.
85  *
86  */
87  void setValue(double v) override
88  {
89  Parameter::setValue (v); // Will apply possible constraints
91  }
92 
93  /*
94  * @brief computation of double dependency is done and
95  * transferred to parameter value, with constraint.
96  *
97  */
98  double getValue() const override
99  {
100  double x = static_cast<NumericMutable<double>&>(*dependency(0)).targetValue();
101  accessValueConst()->Parameter::setValue(x);
102  return Parameter::getValue();
103  }
104 
105  bool compareAdditionalArguments (const Node_DF& other) const override;
106 
107  std::size_t hashAdditionalArguments () const override;
108 
109  NodeRef derive (Context& c, const Node_DF& node) override;
110 
111  NodeRef recreate (Context& c, NodeRefVec&& deps) override;
112 
113  NodeRef recreate (Context& c);
114 
115 private:
116  void compute () override;
117 };
118 
119 
131 {
132 public:
134 
136  static std::shared_ptr<ConfiguredParameter> create(Context& c, NodeRefVec&& deps, Parameter& param, const int n)
137  {
138  // Check dependencies
139  checkDependenciesNotNull (typeid (Self), deps);
140  checkDependencyVectorSize (typeid (Self), deps, 2);
141  checkDependencyRangeIsValue<double>(typeid (Self), deps, 0, 2);
142  // Detect if we have a chain of ShiftParameter with the same delta.
143 
144  auto& delta = deps[1];
145  auto* paramAsShiftParameter = dynamic_cast<ShiftParameter*>(&param);
146 
147  if (paramAsShiftParameter != nullptr && paramAsShiftParameter->dependency (1) == delta)
148  {
149  // Merge with ShiftParameter dependency by summing the n.
150  Parameter p2(param);
151  return Self::create (c, std::move(deps), p2, n + paramAsShiftParameter->getN ());
152  }
153  // Not a merge, select node implementation.
154  if (n == 0 || delta->hasNumericalProperty (NumericalProperty::ConstantZero))
155  {
156  auto* paramAsConf = dynamic_cast<ConfiguredParameter*>(&param);
157  if (paramAsConf)
158  return std::dynamic_pointer_cast<ConfiguredParameter>(paramAsConf->shared_from_this());
159  else
160  return ConfiguredParameter::create(c, {deps[0]}, param);
161  }
162  else
163  {
164  return cachedAs<ConfiguredParameter>(c, std::make_shared<Self>(c, std::move(deps), param, n));
165  }
166  }
167 
168  ShiftParameter (const Context& context, NodeRefVec&& deps, const Parameter& parameter, int n)
169  : ConfiguredParameter (context, std::move (deps), parameter), n_ (n)
170  {}
171 
172  ShiftParameter (const Context& context, NodeRefVec&& deps, Parameter&& parameter, int n)
173  : ConfiguredParameter (context, std::move (deps), std::move(parameter)), n_ (n)
174  {}
175 
176  std::string description () const override { return "Shift" + ConfiguredParameter::description();}
177 
178  std::string color () const override
179  {
180  auto& name = getName();
181  if (name.substr(0, 5) == "BrLen")
182  return "#aa99aa";
183  else
184  return "#ffcc44";
185  }
186 
187 
188  std::string debugInfo () const override
189  {
191  " n=" + std::to_string (n_);
192  }
193 
194  // ShiftDelta additional arguments = (n_).
195  bool compareAdditionalArguments (const Node_DF& other) const final
196  {
197  const auto* derived = dynamic_cast<const Self*>(&other);
198  return derived != nullptr && n_ == derived->n_;
199  }
200 
201  /*
202  * @brief setValue is not possible here, since computation is
203  * done only through ompute method.
204  *
205  */
206  void setValue(double v) override
207  {
208  throw Exception("ShiftParameter setValue should not be called");
209  }
210 
211 
216  double getValue() const override
217  {
218  return Parameter::getValue();
219  }
220 
221  std::size_t hashAdditionalArguments () const final
222  {
223  std::size_t seed = 0;
224  combineHash (seed, n_);
225  return seed;
226  }
227 
228  NodeRef derive (Context& c, const Node_DF& node) final
229  {
230  if (&node == this)
231  {
233  }
234  auto dx = this->dependency (0)->derive (c, node);
235  auto ddelta = this->dependency (1)->derive (c, node);
236  if (dx->hasNumericalProperty (NumericalProperty::ConstantZero))
238 
239  if (dx->hasNumericalProperty (NumericalProperty::ConstantOne))
241 
242  return Self::create (c, {dx, ddelta}, *this, n_);
243  }
244 
245  NodeRef recreate (Context& c, NodeRefVec&& deps) final
246  {
247  return Self::create (c, std::move (deps), *this, n_);
248  }
249 
250  int getN () const { return n_; }
251 
252 private:
253  void compute () final
254  {
255  const auto& x = accessValueConstCast<double>(*this->dependency (0));
256  const auto& delta = accessValueConstCast<double>(*this->dependency (1));
257  double r = n_ * delta + x;
258  // Boundary mgmt not so clean!
259  this->accessValueMutable()->Parameter::setValue(hasConstraint() ? (getConstraint()->isCorrect(r) ? r : getConstraint()->getAcceptedLimit(r)) : r);
260  }
261 
262  int n_;
263 };
264 
272 class ValueFromConfiguredParameter : public Value<double>
273 {
274 public:
277  using T = double;
278 
280 
281  std::string debugInfo () const final;
282 
283  bool compareAdditionalArguments (const Node_DF& other) const final;
284 
285  NodeRef derive (Context& c, const Node_DF& node) final;
286  NodeRef recreate (Context& c, NodeRefVec&& deps) final;
287 
288  std::string description () const
289  {
290  return "ValueFrom(" + dependency(0)->description() + ")";
291  }
292 
293  std::string color () const;
294 
295 private:
296  void compute () final;
297 
298 public:
299  static std::shared_ptr<Self> create (Context& c, NodeRefVec&& deps);
300 };
301 } // namespace bpp
302 #endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_PARAMETER_H
Data flow node representing a parameter.
Definition: Parameter.h:27
void compute() override
Computation implementation.
Definition: Parameter.cpp:88
NodeRef derive(Context &c, const Node_DF &node) override
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Parameter.cpp:68
void setValue(double v) override
Definition: Parameter.h:87
std::size_t hashAdditionalArguments() const override
Return the hash of node-specific configuration.
Definition: Parameter.cpp:62
std::string description() const override
Node pretty name (default = type name).
Definition: Parameter.cpp:27
static std::shared_ptr< Self > create(Context &c, const Parameter &param)
Definition: Parameter.h:44
const Context & context_
Definition: Parameter.h:29
std::string color() const override
Definition: Parameter.cpp:37
double getValue() const override
Definition: Parameter.h:98
std::string debugInfo() const override
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Parameter.cpp:32
NodeRef recreate(Context &c, NodeRefVec &&deps) override
Recreate the node with different dependencies.
Definition: Parameter.cpp:83
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Parameter &param)
Build a new ConfiguredParameter node.
Definition: Parameter.h:36
ConfiguredParameter * clone() const override
Definition: Parameter.h:71
static std::shared_ptr< Self > resetDependencies(Context &c, std::shared_ptr< Self > self, NodeRefVec &&deps)
Definition: Parameter.h:51
bool compareAdditionalArguments(const Node_DF &other) const override
Compare node-specific configuration to another.
Definition: Parameter.cpp:49
ConfiguredParameter(const ConfiguredParameter &param)
Definition: Parameter.h:63
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantZero node of the given dimension.
Context for dataflow node construction.
Definition: DataFlow.h:527
Base dataflow Node class.
Definition: DataFlow.h:152
const NodeRef & dependency(std::size_t i) const noexcept
Definition: DataFlow.h:185
friend class Context
Definition: DataFlow.h:316
r = variable_value.
static std::shared_ptr< Self > create(Context &, Args &&... args)
Build a new NumericMutable node with T(args...) value.
virtual void setValue(double value)
virtual double getValue() const
virtual std::shared_ptr< const ConstraintInterface > getConstraint() const
virtual const std::string & getName() const
virtual bool hasConstraint() const
shift param value = n * delta + x.
Definition: Parameter.h:131
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Parameter.h:228
static std::shared_ptr< ConfiguredParameter > create(Context &c, NodeRefVec &&deps, Parameter &param, const int n)
Build a new ShiftDelta node with the given output dimensions and shift number.
Definition: Parameter.h:136
std::string debugInfo() const override
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Parameter.h:188
std::string description() const override
Node pretty name (default = type name).
Definition: Parameter.h:176
void setValue(double v) override
Definition: Parameter.h:206
double getValue() const override
Raw value access (const).
Definition: Parameter.h:216
ShiftParameter(const Context &context, NodeRefVec &&deps, Parameter &&parameter, int n)
Definition: Parameter.h:172
std::size_t hashAdditionalArguments() const final
Return the hash of node-specific configuration.
Definition: Parameter.h:221
ShiftParameter(const Context &context, NodeRefVec &&deps, const Parameter &parameter, int n)
Definition: Parameter.h:168
void compute() final
Computation implementation.
Definition: Parameter.h:253
int getN() const
Definition: Parameter.h:250
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
Definition: Parameter.h:195
std::string color() const override
Definition: Parameter.h:178
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Parameter.h:245
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps)
Definition: Parameter.cpp:117
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Parameter.cpp:144
ValueFromConfiguredParameter(NodeRefVec &&deps)
Definition: Parameter.cpp:101
ValueFromConfiguredParameter Self
Definition: Parameter.h:275
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Parameter.cpp:105
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Parameter.cpp:125
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
Definition: Parameter.cpp:111
void compute() final
Computation implementation.
Definition: Parameter.cpp:149
std::string description() const
Node pretty name (default = type name).
Definition: Parameter.h:288
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
const Parameter * & accessValueConst() const noexcept
Raw value access (const).
Definition: DataFlow.h:385
const Parameter * & targetValue()
Access value, recompute if needed.
Definition: DataFlow.h:374
Parameter * & accessValueMutable() noexcept
Definition: DataFlow.h:416
Defines the basic types of data flow nodes.
void checkDependenciesNotNull(const std::type_info &contextNodeType, const NodeRefVec &deps)
Checks that all dependencies are not null, throws if not.
Definition: DataFlow.cpp:103
std::string to_string(const NoDimension &)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
void checkDependencyVectorSize(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t expectedSize)
Definition: DataFlow.cpp:83
void combineHash(std::size_t &seed, const T &t)
Combine hashable value to a hash, from Boost library.
Definition: DataFlow.h:33
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78