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