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 AppResponseValues.h
12 : *
13 : * Defines the colin::AppResponseValues class.
14 : */
15 :
16 : #ifndef colin_AppResponseValues_h
17 : #define colin_AppResponseValues_h
18 :
19 : #include <acro_config.h>
20 : #include <utilib/traits.h>
21 : #include <colin/real.h>
22 : #include <colin/AppResponseInfo.h>
23 :
24 : namespace colin {
25 :
26 : template <class TypesT>
27 : class AppResponseValues;
28 :
29 : }
30 :
31 :
32 : ///
33 : /// Setting is_cache_allocated traits for AppResponseValues
34 : /// This needs to be setup _outside_ of the colin namespace
35 : ///
36 : namespace utilib {
37 :
38 : /// A trait indicating if caching is allowed.
39 : template <class TypesT>
40 : struct is_cache_allocated < typename colin::AppResponseValues<TypesT> > {
41 : public:
42 : /// The value of this trait.
43 : static const bool value = true;
44 : };
45 :
46 : }
47 :
48 : namespace colin {
49 :
50 : /** \class AppResponseValues
51 : *
52 : * This class includes the results of an application evaluation, which may
53 : * include the values of one or more functions and the values of
54 : * constraints. Additionally, this class includes values for gradients and
55 : * Hessians for the functions and constraints. Although these are not used
56 : * in all contexts, it convenient to define optimization problems using a
57 : * container class for the optimization response values.
58 : */
59 : template <class TypesT>
60 : class AppResponseValues :
61 : public utilib::CachedAllocatorObject< AppResponseValues<TypesT> >
62 : {
63 : public:
64 :
65 : /// The constructor.
66 : AppResponseValues()
67 1444 : : num_real_params(0), num_functions(0), num_constraints(0)
68 4332 : {
69 1444 : l2_cviolationPtr = &l2_cviolation;
70 1444 : functionValuePtr = 0;
71 1444 : aug_functionValuePtr = 0;
72 1444 : functionGradientPtr = 0;
73 1444 : functionHessianPtr = 0;
74 1444 : functionValuesPtr = &functionValues;
75 1444 : aug_functionValuesPtr = &aug_functionValues;
76 1444 : functionGradientsPtr = &functionGradients;
77 1444 : functionHessiansPtr = &functionHessians;
78 1444 : constraintValuesPtr = &constraintValues;
79 1444 : constraintGradientsPtr = &constraintGradients;
80 1444 : constraintHessiansPtr = &constraintHessians;
81 : }
82 :
83 : /// Destructor.
84 0 : virtual ~AppResponseValues() {}
85 :
86 : /// A method to resize the buffers in a AppResponseValues object.
87 : void resize(unsigned int num_functions, unsigned int num_constraints,
88 : unsigned int num_real_params,
89 : bool using_gradients=false, bool using_hessians=false);
90 :
91 : /// Resize the gradient buffers
92 : void resize_gradients();
93 :
94 : /// Resize the Hessian buffers
95 : void resize_hessians();
96 :
97 : /// Copy information from another AppResponseValues object.
98 : void copy(const AppResponseValues<TypesT>& info)
99 26933 : {
100 26933 : l2_cviolation = info.l2_cviolation;
101 26933 : functionValues << info.functionValues;
102 26933 : aug_functionValues << info.aug_functionValues;
103 26933 : functionGradients << info.functionGradients;
104 26933 : functionHessians << info.functionHessians;
105 26933 : constraintValues << info.constraintValues;
106 26933 : constraintGradients << info.constraintGradients;
107 26933 : constraintHessians << info.constraintHessians;
108 26933 : num_real_params = info.num_real_params;
109 26933 : num_functions = info.num_functions;
110 26933 : num_constraints = info.num_constraints;
111 : //aug_functionValuePtr = info.aug_functionValuePtr;
112 : //functionGradientPtr = info.functionGradientPtr;
113 : //functionHessianPtr = info.functionHessianPtr;
114 : }
115 :
116 : /// The L2 constraint violation
117 : real l2_cviolation;
118 :
119 : /// The set of function values
120 : typename TypesT::realarray_t functionValues;
121 :
122 : /// The set of augmented function values
123 : typename TypesT::realarray_t aug_functionValues;
124 :
125 : /// The set of function gradients
126 : typename TypesT::vectorarray_t functionGradients;
127 :
128 : /// The set of function Hessians
129 : typename TypesT::matrixarray_t functionHessians;
130 :
131 : /// The set of constraint values
132 : typename TypesT::vector_t constraintValues;
133 :
134 : /// The set of constraint gradients
135 : typename TypesT::vectorarray_t constraintGradients;
136 :
137 : /// The set of constraint Hessians
138 : typename TypesT::matrixarray_t constraintHessians;
139 :
140 : /// A pointer to l2 constraint violation info
141 : real* l2_cviolationPtr;
142 :
143 : /// A pointer to function value info
144 : real* functionValuePtr;
145 :
146 : /// A pointer to augmented function value info
147 : real* aug_functionValuePtr;
148 :
149 : /// A pointer to gradient value info
150 : typename TypesT::vector_t* functionGradientPtr;
151 :
152 : /// A pointer to Hessian value info
153 : typename TypesT::matrix_t* functionHessianPtr;
154 :
155 : /// A pointer to an array of function values
156 : typename TypesT::realarray_t* functionValuesPtr;
157 :
158 : /// A pointer to an array of augmented function values
159 : typename TypesT::realarray_t* aug_functionValuesPtr;
160 :
161 : /// A pointer to an array of gradient values
162 : typename TypesT::vectorarray_t* functionGradientsPtr;
163 :
164 : /// A pointer to an array of Hessian values
165 : typename TypesT::matrixarray_t* functionHessiansPtr;
166 :
167 : /// A pointer to an array of constraint values
168 : typename TypesT::vector_t* constraintValuesPtr;
169 :
170 : /// A pointer to an array of constraint gradient values
171 : typename TypesT::vectorarray_t* constraintGradientsPtr;
172 :
173 : /// A pointer to an array of constraint Hessian values
174 : typename TypesT::matrixarray_t* constraintHessiansPtr;
175 :
176 : /// The number of real-valued parameters
177 : unsigned int num_real_params;
178 :
179 : /// The number of functions
180 : unsigned int num_functions;
181 :
182 : /// The number of constraints
183 : unsigned int num_constraints;
184 :
185 : /// Initialize with colin::AppResponseInfo data.
186 127420 : virtual void init(AppResponseInfo* ) {}
187 :
188 : /// Deallocate with cached information.
189 : void deallocate_derived()
190 257508 : { utilib::CachedAllocator< AppResponseValues<TypesT> > :: deallocate_derived(this); }
191 :
192 : };
193 :
194 : template <class TypesT>
195 : void AppResponseValues<TypesT>::resize(
196 : unsigned int num_functions_,
197 : unsigned int num_constraints_,
198 : unsigned int num_real_params_,
199 : bool using_gradients,
200 : bool using_hessians)
201 126003 : {
202 126003 : if (num_functions != num_functions_) {
203 1444 : num_functions = num_functions_;
204 1444 : functionValues.resize(num_functions);
205 1444 : aug_functionValues.resize(num_functions);
206 : }
207 126003 : if (num_constraints != num_constraints_) {
208 258 : num_constraints = num_constraints_;
209 258 : constraintValues.resize(num_constraints);
210 : }
211 126003 : if (num_real_params != num_real_params_)
212 953 : num_real_params = num_real_params_;
213 :
214 126003 : if (using_gradients)
215 715 : resize_gradients();
216 126003 : if (using_hessians)
217 715 : resize_hessians();
218 : }
219 :
220 :
221 : template <class TypesT>
222 : void AppResponseValues<TypesT>::resize_gradients()
223 715 : {
224 715 : functionGradients.resize(num_functions);
225 1430 : for (unsigned int i=0; i<num_functions; i++)
226 715 : functionGradients[i].resize(num_real_params);
227 :
228 715 : constraintGradients.resize(num_constraints);
229 1135 : for (unsigned int i=0; i<num_constraints; i++)
230 420 : constraintGradients[i].resize(num_real_params);
231 : }
232 :
233 :
234 :
235 : template <class TypesT>
236 : void AppResponseValues<TypesT>::resize_hessians()
237 715 : {
238 : #if 0
239 : //// TODO
240 : functionHessians.resize(num_functions);
241 : for (unsigned int i=0; i<num_functions; i++)
242 : functionHessians[i].resize(num_constraints,num_real_params);
243 :
244 : constraintHessians.resize(num_constraints);
245 : for (unsigned int i=0; i<num_constraints; i++)
246 : constraintHessians[i].resize(num_constraints,num_real_params);
247 : #endif
248 : }
249 :
250 :
251 :
252 :
253 : /// Copy AppResponseValues information from one objec to another
254 : /// using an AppResponseInfo object to guide the copy process.
255 : template <class TypesT, class LTypesT>
256 : void map_AppResponseValues(AppResponseValues<TypesT>* to,
257 : AppResponseValues<LTypesT>* from,
258 : AppResponseInfo* info)
259 124051 : {
260 124051 : if (info->mode & mode_f) {
261 124051 : if (from->functionValuePtr && to->functionValuePtr) {
262 0 : if (info->responseASV[0] | mode_f)
263 0 : *(to->functionValuePtr) = *(from->functionValuePtr);
264 : }
265 :
266 124051 : else if (from->functionValuePtr) {
267 0 : if (info->responseASV[0] | mode_f)
268 0 : (*(to->functionValuesPtr))[0] = *(from->functionValuePtr);
269 : }
270 :
271 124051 : else if (to->functionValuePtr) {
272 0 : if (info->responseASV[0] | mode_f)
273 0 : (*(to->functionValuePtr)) = (*(from->functionValuesPtr))[0];
274 : }
275 :
276 : else {
277 248102 : for (unsigned int i=0; i<info->num_functions; i++)
278 124051 : if (info->responseASV[i] | mode_f)
279 124051 : (*(to->functionValuesPtr))[i] = (*(from->functionValuesPtr))[i];
280 : }
281 : }
282 :
283 124051 : if (info->mode & mode_f) {
284 124051 : if (from->aug_functionValuePtr && to->aug_functionValuePtr) {
285 0 : if (info->responseASV[0] | mode_f)
286 0 : *(to->aug_functionValuePtr) = *(from->aug_functionValuePtr);
287 : }
288 :
289 124051 : else if (from->aug_functionValuePtr) {
290 0 : if (info->responseASV[0] | mode_f)
291 0 : (*(to->aug_functionValuesPtr))[0] = *(from->aug_functionValuePtr);
292 : }
293 :
294 124051 : else if (to->aug_functionValuePtr) {
295 0 : if (info->responseASV[0] | mode_f)
296 0 : (*(to->aug_functionValuePtr)) = (*(from->aug_functionValuesPtr))[0];
297 : }
298 :
299 : else {
300 248102 : for (unsigned int i=0; i<info->num_functions; i++)
301 124051 : if (info->responseASV[i] | mode_f)
302 124051 : (*(to->aug_functionValuesPtr))[i] = (*(from->aug_functionValuesPtr))[i];
303 : }
304 : }
305 :
306 124051 : if (info->mode & mode_g) {
307 0 : if (from->functionGradientPtr && to->functionGradientPtr) {
308 0 : if (info->responseASV[0] | mode_g)
309 0 : *(to->functionGradientPtr) << *(from->functionGradientPtr);
310 : }
311 :
312 0 : if (from->functionGradientPtr) {
313 0 : if (info->responseASV[0] | mode_g)
314 0 : (*(to->functionGradientsPtr))[0] << *(from->functionGradientPtr);
315 : }
316 :
317 0 : if (to->functionGradientPtr) {
318 0 : if (info->responseASV[0] | mode_g)
319 0 : (*(to->functionGradientPtr)) << (*(from->functionGradientsPtr))[0];
320 : }
321 :
322 : else
323 0 : map_jacobian(*(to->functionGradientsPtr),*(from->functionGradientsPtr));
324 : }
325 :
326 124051 : if (info->mode & mode_h) {
327 0 : if (from->functionHessianPtr && to->functionHessianPtr) {
328 0 : if (info->responseASV[0] | mode_h)
329 0 : map_matrix(*(to->functionHessianPtr),*(from->functionHessianPtr));
330 : }
331 :
332 0 : if (from->functionHessianPtr) {
333 0 : if (info->responseASV[0] | mode_h)
334 0 : map_matrix((*(to->functionHessianPtr))[0],*(from->functionHessianPtr));
335 : }
336 :
337 0 : if (to->functionHessianPtr) {
338 0 : if (info->responseASV[0] | mode_h)
339 0 : map_matrix(*(to->functionHessianPtr),(*(from->functionHessianPtr))[0]);
340 : }
341 :
342 : else {
343 0 : for (unsigned int i=0; i<info->num_functions; i++) {
344 0 : if (info->responseASV[i] | mode_h)
345 0 : map_matrix((*(to->functionHessiansPtr))[i],(*(from->functionHessiansPtr))[i]);
346 : }
347 : }
348 : }
349 :
350 124051 : if (info->mode & mode_cf) {
351 91813 : if (from->constraintValuesPtr && to->constraintValuesPtr) {
352 91813 : *(to->constraintValuesPtr) << *(from->constraintValuesPtr);
353 91813 : *(to->l2_cviolationPtr) = *(from->l2_cviolationPtr);
354 : }
355 : }
356 :
357 124051 : if (info->mode & mode_cg) {
358 0 : if (from->constraintGradientsPtr && to->constraintGradientsPtr)
359 0 : map_jacobian(*(to->constraintGradientsPtr),*(from->constraintGradientsPtr));
360 : }
361 :
362 124051 : if (info->mode & mode_ch) {
363 0 : if (from->constraintHessiansPtr && to->constraintHessiansPtr) {
364 0 : for (unsigned int i=0; i<info->num_constraints; i++)
365 0 : map_matrix((*(to->constraintHessiansPtr))[i],(*(from->constraintHessiansPtr))[i]);
366 : }
367 : }
368 : }
369 :
370 : } // namespace colin
371 :
372 :
373 : /// Copy operator.
374 : template <class TypesT>
375 : inline colin::AppResponseValues<TypesT>& operator<<( colin::AppResponseValues<TypesT>& x, const colin::AppResponseValues<TypesT>& info)
376 53866 : { x.copy(info); return x; }
377 :
378 : #endif
|