source: colin/trunk/colin/OptProblemFrag_RealParams.h @ 1487

Revision 1487, 10.4 KB checked in by wehart, 10 years ago (diff)

Generalizing the template parameterization used for setting and getting
bound constraints. This will hopefully resolve issues at the DAKOTA-Acro
interface.

( Links to Acro-related mail archives at  http://software.sandia.gov/Acro/ )

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*  _________________________________________________________________________
2 *
3 *  COLIN: A Common Optimization Library INterface
4 *  Copyright (c) 2003, Sandia National Laboratories.
5 *  This software is distributed under the GNU Lesser General Public License.
6 *  For more information, see the README.html file in the top COLIN directory.
7 *  _________________________________________________________________________
8 */
9
10/**
11 * \file OptProblemFrag_RealParams.h
12 *
13 * Defines the colin::OptProblemFragment_RealParams class.
14 */
15
16#ifndef colin_OptProblemFrag_RealParams_h
17#define colin_OptProblemFrag_RealParams_h
18
19#include <acro_config.h>
20#include <colin/OptProblemFrag_Bounds.h>
21
22namespace colin {
23
24//============================================================================
25//============================================================================
26// Class OptProblemFragment_RealParams
27//============================================================================
28//============================================================================
29
30/**
31 *  Defines the elements of an OptProblem that pertain to real parameters.
32 */
33template <class DomainT, class ResponseT>
34class OptProblemFragment_RealParams : virtual public OptProblemFragment_Bounds<DomainT,ResponseT>
35{
36public:
37
38  /// Constructor
39  OptProblemFragment_RealParams() {}
40
41  /// Virtual destructor
42  virtual ~OptProblemFragment_RealParams() {}
43
44  /// Returns the number of real parameters in the domain
45  unsigned int num_real_params()
46                { return (this->state ? this->state->num_real_params : 0);}
47
48  /// Initializes the number of real params in the domain
49  void init_real_params(unsigned int num)
50                {
51                if (!(this->state))
52                   EXCEPTION_MNGR(std::runtime_error, "OptProblemFragment_RealParams::init_real_params - no application!")
53                if (this->state->num_real_params != num) {
54                   this->state->num_real_params = num;
55                   if(this->state->int_lower_bounds.size()==0)
56                     this->state->enforcing_bound_constraints = false;
57                   }
58                }
59
60  /// Returns true if the problem supports the calculation of gradients
61  bool& using_gradients()
62                {
63                if (!(this->state))
64                   EXCEPTION_MNGR(std::runtime_error, "OptProblemFragment_RealParams::using_gradients - no application!")
65                return this->state->using_gradients;
66                }
67
68  /// Returns true if the problem supports the calculation of Hessians
69  bool& using_hessians()
70                {
71                if (!(this->state))
72                   EXCEPTION_MNGR(std::runtime_error, "OptProblemFragment_RealParams::using_hessians - no application!")
73                return this->state->using_hessians;
74                }
75
76  /// Compute the gradient of the point
77  void EvalG(const DomainT& point, typename ResponseT::vector_t& value)
78                {
79                this->handle->set_point(point);
80                this->handle->EvalG(value);
81                }
82
83  /// Asynchronously compute the gradient of the point
84  void AsyncEvalG(const DomainT& point, typename ResponseT::vector_t* value, int& priority, int tag=-1)
85                {
86                this->handle->set_point(point);
87                this->handle->AsyncEvalG(value,priority,tag);
88                }
89
90  /// Asynchronously compute the gradient of the point with a default priority
91  void AsyncEvalG(const DomainT& point, typename ResponseT::vector_t* value, int tag=-1)
92                {
93                AsyncEvalG(point,value,this->default_priority,tag);
94                }
95
96  /// Compute the function value and gradient of the point
97  void EvalFG(const DomainT& point, real& fvalue, typename ResponseT::vector_t& value)
98                {
99                this->handle->set_point(point);
100                this->handle->EvalFG(value);
101                }
102
103  /// Asynchronously compute the function value and gradient of the point
104  void AsyncEvalFG(const DomainT& point, real* fvalue, typename ResponseT::vector_t* value,
105                                int& priority, int tag=-1)
106                {
107                this->handle->set_point(point);
108                this->handle->AsyncEvalFG(value,priority,tag);
109                }
110
111  /// Asynchronously compute the function value and gradient of the point
112  ///   with a default priority
113  void AsyncEvalFG(const DomainT& point, real* fvalue, typename ResponseT::vector_t* value,
114                                int tag=-1)
115                {
116                AsyncEvalFG(point,fvalue,value,this->default_priority,tag);
117                }
118
119  /// Evaluate the Jacobian (constraint gradients) at the given point
120  void EvalCG(const DomainT& point, typename ResponseT::matrix_t& value)
121                {
122                if (this->state->numConstraints == 0)
123                   return;
124                this->handle->set_point(point);
125                this->handle->EvalCG(value);
126                }
127
128  /// Asynchronously evaluate the Jacobian (constraint gradients) at
129  /// the given point
130  void AsyncEvalCG(const DomainT& point, typename ResponseT::matrix_t* value, int& priority,
131                                int tag=-1)
132                {
133                if (this->state->numConstraints == 0)
134                   return;
135                this->handle->set_point(point);
136                this->handle->AsyncEvalCG(value,priority,tag);
137                }
138
139  /// Asynchronously evaluate the Jacobian (constraint gradients) at
140  /// the given point with a default priority
141  void AsyncEvalCG(const DomainT& point, typename ResponseT::matrix_t* value, int tag=-1)
142                {
143                AsyncEvalCG(point,value,this->default_priority,tag);
144                }
145
146  /// Sets the type of an real lower bound
147  void real_lower_bound_type(int i, bound_type_enum type)
148                {
149                if (this->state->enforcing_bound_constraints == false)
150                   EXCEPTION_MNGR(std::runtime_error, "Trying to set a bound type when real bounds are not used.");
151                this->state->real_lower_bound_type[i] = type;
152                }
153
154  /// Sets the type of an real upper bound
155  void real_upper_bound_type(int i, bound_type_enum type)
156                {
157                if (this->state->enforcing_bound_constraints == false)
158                   EXCEPTION_MNGR(std::runtime_error, "Trying to set a bound type when real bounds are not used.");
159                this->state->real_upper_bound_type[i] = type;
160                }
161
162  /// Returns the type of the real lower bound
163  bound_type_enum real_lower_bound_type(int i) const
164                {
165                if (this->state->enforcing_bound_constraints == false)
166                   return no_bound;
167                return this->state->real_lower_bound_type[i];
168                }
169
170  /// Returns the type of the real upper bound
171  bound_type_enum real_upper_bound_type(int i) const
172                {
173                if (this->state->enforcing_bound_constraints == false)
174                   return no_bound;
175                return this->state->real_upper_bound_type[i];
176                }
177
178  /// Returns true if this problem has a lower bound on the $i$-th parameter
179  bool has_real_lower_bound(int i) const
180                {return real_lower_bound_type(i) == no_bound;}
181
182  /// Returns true if this problem has an upper bound on the $i$-th parameter
183  bool has_real_upper_bound(int i) const
184                {return real_upper_bound_type(i) == no_bound;}
185
186  /// Set real boundary constraints - dense format
187#ifdef ACRO_HAVE_TEMPLATES_AS_TEMPLATE_ARGUMENTS
188  template <template <class TYPE> class LArrayT>
189  void set_real_bounds(const LArrayT<real>& lower, const LArrayT<real>& upper)
190#else
191  template <class ArrayT>
192  void set_real_bounds(const ArrayT& lower, const ArrayT& upper)
193#endif
194                {
195                std::vector<real> lower_,upper_;
196                lower_ << lower;
197                upper_ << upper;
198                this->state->set_real_bounds(lower_,upper_);
199                }
200
201#ifdef ACRO_HAVE_TEMPLATES_AS_TEMPLATE_ARGUMENTS
202  template <template <class TYPE> class LArrayT>
203  void set_real_bounds(const LArrayT<double>& lower, const LArrayT<double>& upper)
204                {
205                std::vector<real> lower_(lower.size()),upper_(lower.size());
206                for (size_type i=0; i<lower.size(); i++) {
207                  lower_[i]=lower[i]; upper_[i]=upper[i]; }
208                this->state->set_real_bounds(lower_,upper_);
209                }
210#endif
211
212  /// Get real boundary constraints - dense format
213#ifdef ACRO_HAVE_TEMPLATES_AS_TEMPLATE_ARGUMENTS
214  template <template <class TYPE> class LArrayT>
215  void get_real_bounds(LArrayT<real>& lower, LArrayT<real>& upper)
216#else
217  template <class ArrayT>
218  void get_real_bounds(ArrayT& lower, ArrayT& upper)
219#endif
220                {
221                if (!(this->state->enforcing_bound_constraints))
222                   EXCEPTION_MNGR(std::runtime_error,"Requested bounds for a problem without enforced bounds.")
223                std::vector<real> lower_,upper_;
224                this->state->get_real_bounds(lower_,upper_);
225                lower.resize(lower_.size());
226                upper.resize(lower_.size());
227                for (size_type i=0; i<lower.size(); i++) {
228                  lower[i]=lower_[i]; upper[i]=upper_[i]; }
229                }
230
231#ifdef ACRO_HAVE_TEMPLATES_AS_TEMPLATE_ARGUMENTS
232  template <template <class TYPE> class LArrayT>
233  void get_real_bounds(LArrayT<double>& lower, LArrayT<double>& upper)
234                {
235                if (!(this->state->enforcing_bound_constraints))
236                   EXCEPTION_MNGR(std::runtime_error,"Requested bounds for a problem without enforced bounds.")
237                std::vector<real> lower_,upper_;
238                this->state->get_real_bounds(lower_,upper_);
239                lower.resize(lower_.size());
240                upper.resize(lower_.size());
241                for (size_type i=0; i<lower.size(); i++) {
242                  lower[i]=lower_[i]; upper[i]=upper_[i]; }
243                }
244#endif
245
246  /// Returns true if the bounds on the $i$-th real variable are periodic
247  bool periodic_real_bound(int i) const
248                {return (this->state->enforcing_bound_constraints) &&
249                        (this->state->real_lower_bound_type[i] == periodic_bound);}
250
251  /// Indicates that the bounds on the $i$-th real variable are periodic
252  void set_periodic_real_bound(int i)
253                {
254                if (this->state->enforcing_bound_constraints) {
255                   this->state->real_lower_bound_type[i] = periodic_bound;
256                   this->state->real_upper_bound_type[i] = periodic_bound;
257                   }
258                }
259
260  /// Returns true if the point is feasible with respect to bound constraints
261#ifdef ACRO_HAVE_TEMPLATES_AS_TEMPLATE_ARGUMENTS
262  template <template <class TYPE> class LArrayT>
263  bool test_feasibility(const LArrayT<double>& vals)
264#else
265  template <class ArrayT>
266  bool test_feasibility(const ArrayT& vals)
267#endif
268                {
269                if (!(this->state->enforcing_bound_constraints)) return true;
270                unsigned int nreals = num_real_params();
271                for (unsigned int i=0; i<nreals; i++) {
272                  if ((this->state->real_lower_bound_type[i] == hard_bound) &&
273                      (this->state->real_lower_bounds[i] > vals[i]) )
274                        return false;
275                  if ((this->state->real_upper_bound_type[i] == hard_bound) &&
276                      (this->state->real_upper_bounds[i] < vals[i]))
277                        return false;
278                  }
279                return true;
280                }
281};
282
283template <class DomainT, class ResponseT, bool IsUsed>
284class OptProblemInterface_RealParams : public OptProblemFragment_RealParams<DomainT,ResponseT>
285{
286public:
287
288  OptProblemFragment_RealParams<DomainT,ResponseT>& domain_real()
289                {return *this;}
290
291};
292
293template <class DomainT, class ResponseT>
294class OptProblemInterface_RealParams<DomainT,ResponseT,false> : virtual public OptProblemFragment_Base<DomainT,ResponseT>
295{
296public:
297
298  OptProblemFragment_RealParams<DomainT,ResponseT>& domain_real()
299                {
300                EXCEPTION_MNGR(std::runtime_error,"Error attempting to access the RealParams domain information.");
301                return *static_cast<OptProblemFragment_RealParams<DomainT,ResponseT>*>(0);
302                }
303
304};
305
306}
307
308#endif
Note: See TracBrowser for help on using the repository browser.