Loading...
Searching...
No Matches
optimizer_ptr.h
Go to the documentation of this file.
1#ifndef _BBM_OPTIMIZER_PTR_H_
2#define _BBM_OPTIMIZER_PTR_H_
3
5#include "concepts/macro.h"
6
7#include "core/error.h"
8
9#include "bbm/config.h"
10#include "bbm/optimizer.h"
11
12/************************************************************************/
13/*! \file optimizer_ptr.h
14 \brief A shared_ptr wrapper for optimizers.
15
16 Implements: concepts::optimizer_ptr
17************************************************************************/
18
19namespace bbm {
20
21 /**********************************************************************/
22 /*! \brief optimizer_ptr: a wrapper around a shared_ptr to an optimizer
23
24 \tparam CONF = bbm configuration
25 ***********************************************************************/
26 template<typename CONF> requires concepts::config<CONF>
27 class optimizer_ptr : public virtual optimizer_base<CONF>
28 {
29 public:
31
32 //! \brief Default (empty) constructor; shared_ptr is set to nullptr
33 optimizer_ptr(void) = default;
34
35 //! \brief Construct from shared_ptr
36 template<typename OPT> requires concepts::optimizer<OPT>
37 optimizer_ptr(const std::shared_ptr<OPT>& ptr) : _opt(ptr) {}
38
39 //! \brief Assignment operator
41 {
42 _opt = src._opt;
43 return *this;
44 }
45
46 //! \brief Get the internal shared pointer
47 inline const std::shared_ptr<optimizer_base<Config>>& ptr(void) const { return _opt; }
48
49 //! @{ \name Override the -> operator
50 inline const std::shared_ptr<optimizer_base<Config>>& operator->(void)
51 {
52 if(!_opt) throw bbm_incomplete_init;
53 return _opt;
54 }
55
56 inline std::shared_ptr<optimizer_base<Config>> operator->(void) const
57 {
58 if(!_opt) throw bbm_incomplete_init;
59 return _opt;
60 }
61 //! @}
62
63 /********************************************************************/
64 /*! \brief Dereference passthrough of the 'step' method
65
66 \returns the current loss
67 *********************************************************************/
68 virtual Value step(void) override final
69 {
70 return (*this)->step();
71 }
72
73 /********************************************************************/
74 /*! \brief Dereference passthrough of the 'reset' method
75 ********************************************************************/
76 virtual void reset(void) override final
77 {
78 (*this)->reset();
79 }
80
81 /********************************************************************/
82 /*! \brief Dereference passthrough of the 'is_converged' method
83 *******************************************************************/
84 virtual Mask is_converged(void) override final
85 {
86 return (*this)->is_converged();
87 }
88
89 private:
90 /////////////////
91 // Data Members
92 /////////////////
93 std::shared_ptr<optimizer_base<Config>> _opt;
94 };
95
97
98
99 /**********************************************************************/
100 /*! \brief Helper method for making an optimizer_ptr from an optimizer (new construction)
101
102 \tparam OPTIMIZER = optimizer type that the pointer will point to.
103 \param args = constructor arguments for the OPTIMIZER
104
105 The method allocates a new OPTIMIZER and constructs it with the given
106 arguments. The optimizer_ptr owns the created object.
107 ***********************************************************************/
108 template<typename OPTIMIZER, typename... ARGS> requires concepts::optimizer<OPTIMIZER>
110 {
111 auto ptr = std::make_shared<OPTIMIZER>(std::forward<ARGS>(args)...);
113 }
114
115 /**********************************************************************/
116 /*! \brief Helper method for making an optimizer_ptr from a OPTIMIZATION_ALGORITHM
117
118 \tparam OPTIMIZATION_ALGORITHM = algorithm to point to
119 \param args = constructor arguments
120
121 Allocates a new optmizer<OPTIMIZATION_ALGORITHM> and constructs it with the
122 given args. The optimizer_ptr owns the OPTIMIZER object.
123 ***********************************************************************/
124 template<typename OPTIMIZATION_ALGORITHM, typename... ARGS> requires (concepts::optimization_algorithm<OPTIMIZATION_ALGORITHM> && !concepts::optimizer<OPTIMIZATION_ALGORITHM>)
126 {
127 auto ptr = std::make_shared<optimizer<OPTIMIZATION_ALGORITHM>>(std::forward<ARGS>(args)...);
129 }
130
131 /**********************************************************************/
132 /*! \brief Helper method for making an optimizer_ptr from a OPTIMZER (copy construction)
133
134 \param arg = optimizer to copy
135
136 A copy of the optimizer is allocated. The optimizer_ptr owns the copied object
137 ***********************************************************************/
138 template<typename OPTIMIZER> requires concepts::optimizer<OPTIMIZER>
140 {
141 auto ptr = std::make_shared<OPTIMIZER>(arg);
143 }
144
145 /**********************************************************************/
146 /*! \brief Helper method for making an optimizer_ptr from a OPTIMIZATION_ALGORITHM (copy construction)
147
148 \param arg = optimization algorithm to copy
149
150 A copy of the optimizer<OPTIMIZATION_ALGORITHM> is allocated (copied from
151 arg). The optimizer_ptr owns the copied object.
152 ************************************************************************/
153 template<typename OPTIMIZATION_ALGORITHM> requires (concepts::optimization_algorithm<OPTIMIZATION_ALGORITHM> && !concepts::optimizer<OPTIMIZATION_ALGORITHM>)
155 {
156 auto ptr = std::make_shared<optimizer<OPTIMIZATION_ALGORITHM>>(arg);
158 }
159
160 /**********************************************************************/
161 /*! \brief Helper method for making an optimizer_ptr (avoid optimizer_ptr of optimizer_ptr>)
162
163 \param arg = optimizer_ptr object we want to copy
164 **********************************************************************/
165 template<typename CONF> requires concepts::config<CONF>
167 {
168 return arg;
169 }
170
171} // end bbm namespace
172
173#endif /* _BBM_OPTIMIZER_PTR_H_ */
All BBM methods are defined to operate on a variety of value types and spectrum types....
Connects a optimization_algorithm to a optimizers.
optimizer_ptr: a wrapper around a shared_ptr to an optimizer
Definition: optimizer_ptr.h:28
const std::shared_ptr< optimizer_base< Config > > & operator->(void)
Definition: optimizer_ptr.h:50
virtual Value step(void) override final
Dereference passthrough of the 'step' method.
Definition: optimizer_ptr.h:68
optimizer_ptr(const std::shared_ptr< OPT > &ptr)
Construct from shared_ptr.
Definition: optimizer_ptr.h:37
optimizer_ptr(void)=default
Default (empty) constructor; shared_ptr is set to nullptr.
optimizer_ptr & operator=(const optimizer_ptr &src)
Assignment operator.
Definition: optimizer_ptr.h:40
std::shared_ptr< optimizer_base< Config > > _opt
Definition: optimizer_ptr.h:93
std::shared_ptr< optimizer_base< Config > > operator->(void) const
Definition: optimizer_ptr.h:56
virtual void reset(void) override final
Dereference passthrough of the 'reset' method.
Definition: optimizer_ptr.h:76
const std::shared_ptr< optimizer_base< Config > > & ptr(void) const
Get the internal shared pointer.
Definition: optimizer_ptr.h:47
virtual Mask is_converged(void) override final
Dereference passthrough of the 'is_converged' method.
Definition: optimizer_ptr.h:84
optimizer_ptr contract
Definition: optimizer_ptr.h:24
optimizer concept
Definition: optimizer.h:28
optimizer_ptr contract
Predefined exceptions for common errors.
#define bbm_incomplete_init
Definition: error.h:42
Macros for checking if a class meets a concept.
#define BBM_CHECK_CONCEPT(CONCEPTNAME, CLASSNAME,...)
Check a class for a concept with bbm::concepts::archetypes in the namespace.
Definition: macro.h:35
Definition: aggregatebsdf.h:29
typename std::decay_t< T >::Config get_config
get_config type trait
Definition: config.h:49
optimizer_ptr< get_config< OPTIMIZER > > make_optimizer_ptr(ARGS &&... args)
Helper method for making an optimizer_ptr from an optimizer (new construction)
Definition: optimizer_ptr.h:109
Forward declaration of bbm::arg.
Definition: arg.h:27
Forward declaration.
Definition: args.h:248
Abstract optimizer base class.
Definition: optimizer_base.h:23