source: colin/trunk/colin/AppResponseAnalysis.h @ 5786

Revision 5786, 5.9 KB checked in by wehart, 5 years ago (diff)

Update of source to include Acro copyright statement

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*  _________________________________________________________________________
2 *
3 *  Acro: A Common Repository for Optimizers
4 *  Copyright (c) 2008 Sandia Corporation.
5 *  This software is distributed under the BSD License.
6 *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7 *  the U.S. Government retains certain rights in this software.
8 *  For more information, see the README.txt file in the top Acro directory.
9 *  _________________________________________________________________________
10 */
11
12/**
13 * \file AppResponseAnalysis.h
14 *
15 * Defines the colin::AppResponseAnalysis class.
16 */
17
18#ifndef colin_AppResponseAnalysis_h
19#define colin_AppResponseAnalysis_h
20
21#include <acro_config.h>
22#include <utilib/std_headers.h>
23#include <utilib/ParameterSet.h>
24#include <colin/real.h>
25
26namespace colin
27{
28
29/// Computes the constraint violation for an array of constraint
30/// values, given upper and lower bounds on those values.
31template <class ArrayT, class CArrayT>
32void constraint_violation(ArrayT& cvals,
33                          CArrayT& clower,
34                          CArrayT& cupper,
35                          real& constr_violation,
36                          double tolerance)
37{
38   if (cvals.size() != clower.size())
39      EXCEPTION_MNGR(std::runtime_error, "constraint_violation - cvals has length " << cvals.size() << " but clower has length " << clower.size());
40      if (cvals.size() != cupper.size())
41         EXCEPTION_MNGR(std::runtime_error, "constraint_violation - cvals has length " << cvals.size() << " but cupper has length " << cupper.size());
42         if (cvals.size() == 0)
43         {
44            constr_violation = 0.0;
45            return;
46         }
47
48   constr_violation = 0.0;
49   for (unsigned int k = 0; k < cvals.size(); k++)
50   {
51#ifdef _MSC_VER
52      if (_finite(clower[k]) &&
53#else
54      if (::finite(clower[k]) &&
55#endif
56            (cvals[k] < clower[k] - tolerance))
57         constr_violation += (cvals[k] - clower[k]) * (cvals[k] - clower[k]);
58#ifdef _MSC_VER
59      else if (_finite(cupper[k]) &&
60#else
61      else if (::finite(cupper[k]) &&
62#endif
63               (cvals[k] > cupper[k] + tolerance))
64         constr_violation += (cvals[k] - cupper[k]) * (cvals[k] - cupper[k]);
65   }
66}
67
68
69
70#if 0
71/// Defines mechanisms for analyzing an AppResponse, and in particular for
72/// computing an objective function value using various penalty
73/// functions.
74class AppResponseAnalysis : virtual public utilib::ParameterSet
75{
76public:
77
78   /// Constructor.
79   AppResponseAnalysis();
80
81   /// Compute the constraint violation (L2 norm).
82   template <class ArrayT, class CArrayT>
83   void constraint_violation(ArrayT& cvals, CArrayT& clower, CArrayT& cupper,
84                             real& constr_violation)
85   {
86      colin::constraint_violation(cvals, clower, cupper, constr_violation,
87                                  _constraint_tolerance);
88   }
89
90   /// Compute an augmented objective function with a penalty that is
91   /// weighted by a convergence factor.
92   real compute_penalty_function(real& fval, real& constr_violation,
93                                 double convergence_factor = 1.0)
94   {
95      if (ignore_convergence_factor)
96         return fval + constraint_penalty * constr_violation;
97
98      //
99      // If the convergence factor is effectively infinity but we're
100      // satisfying our constraints, then ignore them
101      //
102      real tmp = convergence_factor;
103      if ((tmp == real :: positive_infinity) && (constr_violation == 0.0))
104         return fval;
105      return fval + constraint_penalty * convergence_factor * constr_violation;
106   }
107
108   /// Compute an augmented objective function and constraint violation.
109   template <class ResponseT, class ArrayT>
110   void compute_response_info(ResponseT& response,
111                              ArrayT& clower, ArrayT& cupper,
112                              double convergence_factor)
113   {
114      constraint_violation(response.constraint_values(),
115                           clower, cupper,
116                           response.l2_constraint_violation());
117      response.augmented_function_value() = compute_penalty_function(
118                                               response.function_value(),
119                                               response.l2_constraint_violation(),
120                                               convergence_factor);
121   }
122
123   /// Compute an augmented objective function and constraint violation.
124   template <class ResponseT, class ArrayT>
125   void compute_response_info(ResponseT& response,
126                              ArrayT& clower, ArrayT& cupper,
127                              double convergence_factor,
128                              real& value,
129                              real& cvalue)
130   {
131      compute_response_info(response, clower, cupper, convergence_factor);
132      value = response.augmented_function_value();
133      cvalue = response.l2_constraint_violation();
134   }
135
136   /// Compute an augmented objective function and constraint violation.
137   template <class ResponseT, class ArrayT>
138   void compute_response_info(ResponseT& response,
139                              ArrayT& clower,
140                              ArrayT& cupper,
141                              real& value,
142                              real& constr_violation)
143   {
144      compute_response_info(response, clower, cupper, 1.0, value,
145                            constr_violation);
146   }
147
148   /// The constraint penalty coefficient.
149   double constraint_penalty;
150
151   /// If true, then ignore the convergence factor.
152   bool ignore_convergence_factor;
153
154   /// Initialize this object.
155   void initialize(unsigned int numNonlinearIneqConstraints_, double constraint_tolerance_)
156   {
157      _numNonlinearIneqConstraints = numNonlinearIneqConstraints_;
158      _constraint_tolerance = constraint_tolerance_;
159   }
160
161private:
162
163   /// The number of nonlinear inequality constraints.
164   unsigned int _numNonlinearIneqConstraints;
165
166   /// The tolerance for infeasibility for constraints.
167   double _constraint_tolerance;
168
169};
170#endif
171
172
173
174} // namespace colin
175
176#endif
177
Note: See TracBrowser for help on using the repository browser.