MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
discrete_aggregation.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast_optimization_discrete_aggregation_h__
21 #define __mast_optimization_discrete_aggregation_h__
22 
23 // MAST includes
26 
27 // libMesh includes
28 #include <libmesh/parallel.h>
29 
30 namespace MAST {
31 namespace Optimization {
32 namespace Aggregation {
33 
42 template <typename ScalarType>
43 ScalarType
44 aggregate_minimum(const libMesh::Parallel::Communicator *comm,
45  const std::vector<ScalarType> &vec,
46  const real_t p) {
47 
48  ScalarType
49  v = 0.,
50  v_min = 0.;
51 
53 
54  if (comm) MAST::Numerics::Utility::comm_min(*comm, v_min);
55 
56  for (uint_t i=0; i<vec.size(); i++) {
57 
58  v += exp(-p * (vec[i] - v_min));
59  }
60 
61  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
62 
63  v = v_min - log(v) / p;
64 
65  return v;
66 }
67 
68 
78 template <typename ScalarType>
79 ScalarType
80 aggregate_minimum_sensitivity(const libMesh::Parallel::Communicator *comm,
81  const std::vector<ScalarType> &vec,
82  const uint_t i,
83  const real_t p) {
84 
85  ScalarType
86  v = 0.,
87  v_min = 0.;
88 
90 
91  if (comm) MAST::Numerics::Utility::comm_min(*comm, v_min);
92 
93  for (uint_t i=0; i<vec.size(); i++) {
94 
95  v += exp(-p * (vec[i] - v_min));
96  }
97 
98  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
99 
100  v = exp(-p * (vec[i] - v_min)) / v;
101 
102  return v;
103 }
104 
105 
106 
117 template <typename ScalarType>
118 ScalarType
119 aggregate_minimum_sensitivity(const libMesh::Parallel::Communicator *comm,
120  const std::vector<ScalarType> &vec,
121  const std::vector<ScalarType> &dvec,
122  const real_t p) {
123 
124  ScalarType
125  dv = 0.,
126  v = 0.,
127  v_min = 0.;
128 
130 
131  if (comm) MAST::Numerics::Utility::comm_min(*comm, v_min);
132 
133  for (uint_t i=0; i<vec.size(); i++) {
134 
135  dv += exp(-p * (vec[i] - v_min)) * dvec[i];
136  v += exp(-p * (vec[i] - v_min));
137  }
138 
139  if (comm) MAST::Numerics::Utility::comm_sum(*comm, dv);
140  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
141 
142  v = dv / v;
143 
144  return v;
145 }
146 
147 
148 
157 template <typename ScalarType>
158 void
159 aggregate_minimum_denominator(const libMesh::Parallel::Communicator *comm,
160  const std::vector<ScalarType> &vec,
161  const real_t p,
162  ScalarType &denom,
163  ScalarType &v_min) {
164 
165  denom = 0.;
166 
168 
169  if (comm) MAST::Numerics::Utility::comm_min(*comm, v_min);
170 
171  for (uint_t i=0; i<vec.size(); i++) {
172 
173  denom += exp(-p * (vec[i] - v_min));
174  }
175 
176  if (comm) MAST::Numerics::Utility::comm_sum(*comm, denom);
177 }
178 
179 
180 
190 template <typename ScalarType>
191 ScalarType
192 aggregate_minimum_sensitivity(const std::vector<ScalarType> &vec,
193  const uint_t i,
194  const real_t p,
195  const ScalarType &denom,
196  const ScalarType &v_min) {
197 
198  return exp(-p * (vec[i] - v_min)) / denom;
199 }
200 
201 
215 template <typename ScalarType>
216 ScalarType
217 aggregate_minimum_sensitivity(const libMesh::Parallel::Communicator *comm,
218  const std::vector<ScalarType> &vec,
219  const std::vector<ScalarType> &dvec,
220  const real_t p,
221  const ScalarType &denom,
222  const ScalarType &v_min) {
223 
224  ScalarType
225  dv = 0.;
226 
227  for (uint_t i=0; i<vec.size(); i++) {
228 
229  dv += exp(-p * (vec[i] - v_min)) * dvec[i];
230  }
231 
232  if (comm) MAST::Numerics::Utility::comm_sum(*comm, dv);
233 
234  return dv / denom;
235 }
236 
237 
246 template <typename ScalarType>
247 ScalarType
248 aggregate_maximum(const libMesh::Parallel::Communicator *comm,
249  const std::vector<ScalarType> &vec,
250  const real_t p) {
251 
252  ScalarType
253  v = 0.,
254  v_max = 0.;
255 
257 
258  if (comm) MAST::Numerics::Utility::comm_max(*comm, v_max);
259 
260  for (uint_t i=0; i<vec.size(); i++) {
261 
262  v += exp(p * (vec[i] - v_max));
263  }
264 
265  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
266 
267  v = v_max + log(v) / p;
268 
269  return v;
270 }
271 
272 
282 template <typename ScalarType>
283 ScalarType
284 aggregate_maximum_sensitivity(const libMesh::Parallel::Communicator *comm,
285  const std::vector<ScalarType> &vec,
286  const uint_t i,
287  const real_t p) {
288 
289  ScalarType
290  v = 0.,
291  v_max = 0.;
292 
294 
295  if (comm) MAST::Numerics::Utility::comm_max(*comm, v_max);
296 
297  for (uint_t i=0; i<vec.size(); i++) {
298 
299  v += exp(p * (vec[i] - v_max));
300  }
301 
302  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
303 
304  v = exp(p * (vec[i] - v_max)) / v;
305 
306  return v;
307 }
308 
309 
310 
321 template <typename ScalarType>
322 ScalarType
323 aggregate_maximum_sensitivity(const libMesh::Parallel::Communicator *comm,
324  const std::vector<ScalarType> &vec,
325  const std::vector<ScalarType> &dvec,
326  const real_t p) {
327 
328  ScalarType
329  dv = 0.,
330  v = 0.,
331  v_max = 0.;
332 
334 
335  if (comm) MAST::Numerics::Utility::comm_max(*comm, v_max);
336 
337  for (uint_t i=0; i<vec.size(); i++) {
338 
339  dv += exp(p * (vec[i] - v_max)) * dvec[i];
340  v += exp(p * (vec[i] - v_max));
341  }
342 
343  if (comm) MAST::Numerics::Utility::comm_sum(*comm, dv);
344  if (comm) MAST::Numerics::Utility::comm_sum(*comm, v);
345 
346  v = dv / v;
347 
348  return v;
349 }
350 
351 
360 template <typename ScalarType>
361 void
362 aggregate_maximum_denominator(const libMesh::Parallel::Communicator *comm,
363  const std::vector<ScalarType> &vec,
364  const real_t p,
365  ScalarType &denom,
366  ScalarType &v_max) {
367 
368  denom = 0.;
369 
371 
372  if (comm) MAST::Numerics::Utility::comm_max(*comm, v_max);
373 
374  for (uint_t i=0; i<vec.size(); i++) {
375 
376  denom += exp(p * (vec[i] - v_max));
377  }
378 
379  if (comm) MAST::Numerics::Utility::comm_sum(*comm, denom);
380 }
381 
382 
383 
393 template <typename ScalarType>
394 ScalarType
395 aggregate_maximum_sensitivity(const std::vector<ScalarType> &vec,
396  const uint_t i,
397  const real_t p,
398  const ScalarType &denom,
399  const ScalarType &v_max) {
400 
401  return exp(p * (vec[i] - v_max)) / denom;
402 }
403 
404 
418 template <typename ScalarType>
419 ScalarType
420 aggregate_maximum_sensitivity(const libMesh::Parallel::Communicator *comm,
421  const std::vector<ScalarType> &vec,
422  const std::vector<ScalarType> &dvec,
423  const real_t p,
424  const ScalarType &denom,
425  const ScalarType &v_max) {
426 
427  ScalarType
428  dv = 0.;
429 
430  for (uint_t i=0; i<vec.size(); i++) {
431 
432  dv += exp(p * (vec[i] - v_max)) * dvec[i];
433  }
434 
435  if (comm) MAST::Numerics::Utility::comm_sum(*comm, dv);
436 
437  return dv / denom;
438 }
439 
440 
441 } // Aggregation
442 } // Optimization
443 } // MAST
444 
445 #endif // __mast_optimization_discrete_aggregation_h__
real_t comm_max(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
Definition: utility.hpp:422
void aggregate_minimum_denominator(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const real_t p, ScalarType &denom, ScalarType &v_min)
Computes the denominator of the sensitivity of aggregated minimum function for use in later sensitivi...
ScalarType aggregate_minimum(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const real_t p)
computes aggregated minimum of values specified in vector vec.
void aggregate_maximum_denominator(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const real_t p, ScalarType &denom, ScalarType &v_max)
Computes the denominator of the sensitivity of aggregated maximum function for use in later sensitivi...
real_t real_minimum(const std::vector< real_t > &vec)
computes minimum of vector
Definition: utility.hpp:92
ScalarType aggregate_minimum_sensitivity(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const uint_t i, const real_t p)
computes sensitivity of aggregated minimum of values specified in vector vec with respect to i th val...
ScalarType aggregate_maximum_sensitivity(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const uint_t i, const real_t p)
computes sensitivity of aggregated maximum of values specified in vector vec with respect to i th val...
ScalarType aggregate_maximum(const libMesh::Parallel::Communicator *comm, const std::vector< ScalarType > &vec, const real_t p)
computes aggregated maximum of values specified in vector vec.
real_t real_maximum(const std::vector< real_t > &vec)
computes maximum of vector
Definition: utility.hpp:141
real_t comm_min(const libMesh::Parallel::Communicator &comm, const std::vector< real_t > &v)
Definition: utility.hpp:382
unsigned int uint_t
void comm_sum(const libMesh::Parallel::Communicator &comm, real_t &v)
Definition: utility.hpp:328
double real_t