MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
mma.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_MMA
13#define MFEM_MMA
14
15#include "../config/config.hpp"
16
17#include <memory>
18
19#ifdef MFEM_USE_MPI
20#include <mpi.h>
21#endif
22
23namespace mfem
24{
25// forward declaration
26class Vector;
27
28/** \brief MMA (Method of Moving Asymptotes) solves an optimization problem
29 * of the form:
30 *
31 * Find x that minimizes the objective function F(x),
32 * subject to C(x)_i <= 0, for all i = 1, ... m
33 * x_lo <= x <= x_hi.
34 *
35 * The objective functions are replaced by convex functions
36 * chosen based on gradient information, and solved using a dual method.
37 * The unique optimal solution of this subproblem is returned as the next
38 * iteration point. Optimality is determined by the KKT conditions.
39 *
40 * The "Update" function in MMA advances the optimization and must be called
41 * in every optimization iteration. Current and previous iteration points
42 * construct the "moving asymptotes". The design variables, objective function,
43 * constraints are passed to an approximating subproblem. The design variables
44 * are updated and returned. Its implementation closely follows the original
45 * formulation of 'Svanberg, K. (2007). MMA and GCMMA-two methods
46 * for nonlinear optimization. vol, 1, 1-15.'
47 *
48 * When used in parallel, all Vectors are assumed to be true dof vectors,
49 * and the operators are expected to be defined for tdof vectors.
50 * */
51
52class MMA
53{
54public:
55 /// Serial constructor:
56 /// nVar - number of design parameters;
57 /// nCon - number of constraints;
58 /// xval[nVar] - initial parameter values
59 MMA(int nVar, int nCon, real_t *xval, int iterationNumber = 0);
60 MMA(const int nVar, int nCon, Vector & xval, int iterationNumber = 0);
61
62#ifdef MFEM_USE_MPI
63 /// Parallel constructor:
64 /// comm_ - communicator
65 MMA(MPI_Comm comm_, int nVar, int nCon, real_t *xval,
66 int iterationNumber = 0);
67 MMA(MPI_Comm comm_, const int & nVar, const int & nCon, const Vector & xval,
68 int iterationNumber = 0);
69#endif
70
71 /// Destructor
72 ~MMA();
73
74 /// Update the optimization parameters
75 /// dfdx[nVar] - gradients of the objective
76 /// gx[nCon] - values of the constraints
77 /// dgdx[nCon*nVar] - gradients of the constraints ordered
78 /// constraint by constraint, e.g. {dg0dx0, dg0dx1, ... ,}
79 /// {dg1dx0, dg1dx1, ... ,}
80 /// xmin[nVar] - lower bounds
81 /// xmax[nVar] - upper bounds
82 /// xval[nVar] - input/output for optimization parameters
83 void Update(const Vector& dfdx,
84 const Vector& gx, const Vector& dgdx,
85 const Vector& xmin, const Vector& xmax,
86 Vector& xval);
87 /// Unconstrained
88 void Update( const Vector& dfdx,
89 const Vector& xmin, const Vector& xmax,
90 Vector& xval);
91
92 void SetIteration( int iterationNumber ) { iter = iterationNumber; };
93 int GetIteration() { return iter; };
94
95 void SetPrintLevel(int print_lvl) { print_level = print_lvl; }
96
97protected:
98 // Local vectors
99 ::std::unique_ptr<real_t[]> a, b, c, d;
102 int nCon, nVar;
103
104 // counter for Update() calls
105 int iter = 0;
106
107 // print level: 1 = none, 2 = warnings
108 int print_level = 1;
109
110 // Global: Asymptotes, bounds, objective approx., constraint approx.
111 ::std::unique_ptr<real_t[]> low, upp;
112 ::std::unique_ptr<real_t[]> x, y, xsi, eta, lam, mu, s;
113
114private:
115 // MMA-specific
116 real_t asyinit, asyincr, asydecr;
117 real_t xmamieps, lowmin, lowmax, uppmin, uppmax, zz;
118 ::std::unique_ptr<real_t[]> factor;
119
120 /// values from the previous two iterations
121 ::std::unique_ptr<real_t[]> xo1, xo2;
122
123 /// KKT norm
124 real_t kktnorm;
125
126 /// intialization state
127 bool isInitialized = false;
128
129#ifdef MFEM_USE_MPI
130 MPI_Comm comm;
131#endif
132
133 /// Allocate the memory for MMA
134 void AllocData(int nVar, int nCon);
135
136 /// Initialize data
137 void InitData(real_t *xval);
138
139 /// Update the optimization parameters
140 /// dfdx[nVar] - gradients of the objective
141 /// gx[nCon] - values of the constraints
142 /// dgdx[nCon*nVar] - gradients of the constraints
143 /// xxmin[nVar] - lower bounds
144 /// xxmax[nVar] - upper bounds
145 /// xval[nVar] - (input: current optimization parameters)
146 /// xval[nVar] - (output: updated optimization parameters)
147 void Update(const real_t* dfdx, const real_t* gx,
148 const real_t* dgdx,
149 const real_t* xxmin,const real_t* xxmax,
150 real_t* xval);
151
152 /// Subproblem base class
153 class MMASubBase
154 {
155 public:
156 /// Constructor
157 MMASubBase(MMA& mma) :mma_ref(mma) {}
158
159 /// Destructor
160 virtual ~MMASubBase() {}
161
162 /// Update the optimization parameters
163 virtual
164 void Update(const real_t* dfdx,
165 const real_t* gx,
166 const real_t* dgdx,
167 const real_t* xmin,
168 const real_t* xmax,
169 const real_t* xval)=0;
170
171 protected:
172 MMA& mma_ref;
173 };
174
175 ::std::unique_ptr<MMASubBase> mSubProblem;
176
177 friend class MMASubSvanberg;
178
179 class MMASubSvanberg:public MMASubBase
180 {
181 public:
182 /// Constructor
183 MMASubSvanberg(MMA& mma, int nVar, int nCon):MMASubBase(mma)
184 {
185 AllocSubData(nVar,nCon);
186
187 nVar_global = nVar;
188#ifdef MFEM_USE_MPI
189 MPI_Allreduce(&nVar, &nVar_global, 1, MPI_INT, MPI_SUM, mma.comm);
190#endif
191 }
192
193 /// Destructor
194 virtual
195 ~MMASubSvanberg() = default;
196
197 /// Update the optimization parameters
198 virtual
199 void Update(const real_t* dfdx,
200 const real_t* gx,
201 const real_t* dgdx,
202 const real_t* xmin,
203 const real_t* xmax,
204 const real_t* xval);
205
206 private:
207 int ittt, itto, itera, nVar_global;
208
209 real_t epsi, delz, dz, dzet, stmxx, stmalfa, stmbeta,
210 sum, stminv, steg, zold, zetold,
211 residunorm, residumax, resinew, raa0, albefa, move, xmamieps;
212
213 ::std::unique_ptr<real_t[]> sum1, ux1, xl1, plam, qlam, gvec, residu, GG, delx,
214 dely,
215 dellam,
216 dellamyi, diagx, diagy, diaglamyi, bb, bb1, Alam, AA, AA1,
217 dlam, dx, dy, dxsi, deta, dmu, Axx, axz, ds, xx, dxx,
218 stepxx, stepalfa, stepbeta, xold, yold,
219 lamold, xsiold, etaold, muold, sold, p0, q0, P, Q, alfa,
220 beta, xmami, b;
221
222 // parallel helper variables
223 real_t global_max = 0.0;
224 real_t global_norm = 0.0;
225 real_t stmxx_global = 0.0;
226 real_t stmalfa_global = 0.0;
227 real_t stmbeta_global = 0.0;
228
229 ::std::unique_ptr<real_t[]> b_local, gvec_local, Alam_local, sum_local,
230 sum_global;
231
232 /// Allocate the memory for the subproblem
233 void AllocSubData(int nVar, int nCon);
234
235 };
236};
237
238} // mfem namespace
239
240#endif // MFEM_MMA
MMA (Method of Moving Asymptotes) solves an optimization problem of the form:
Definition mma.hpp:53
::std::unique_ptr< real_t[]> mu
Definition mma.hpp:112
::std::unique_ptr< real_t[]> b
Definition mma.hpp:99
::std::unique_ptr< real_t[]> eta
Definition mma.hpp:112
int print_level
Definition mma.hpp:108
::std::unique_ptr< real_t[]> d
Definition mma.hpp:99
real_t a0
Definition mma.hpp:100
~MMA()
Destructor.
Definition mma.cpp:1028
real_t machineEpsilon
Definition mma.hpp:100
real_t zet
Definition mma.hpp:101
friend class MMASubSvanberg
Definition mma.hpp:177
::std::unique_ptr< real_t[]> c
Definition mma.hpp:99
void Update(const Vector &dfdx, const Vector &gx, const Vector &dgdx, const Vector &xmin, const Vector &xmax, Vector &xval)
Definition mma.cpp:1072
int GetIteration()
Definition mma.hpp:93
MMA(int nVar, int nCon, real_t *xval, int iterationNumber=0)
Serial MMA.
Definition mma.cpp:975
void SetIteration(int iterationNumber)
Definition mma.hpp:92
::std::unique_ptr< real_t[]> upp
Definition mma.hpp:111
::std::unique_ptr< real_t[]> a
Definition mma.hpp:99
::std::unique_ptr< real_t[]> x
Definition mma.hpp:112
::std::unique_ptr< real_t[]> xsi
Definition mma.hpp:112
::std::unique_ptr< real_t[]> lam
Definition mma.hpp:112
::std::unique_ptr< real_t[]> low
Definition mma.hpp:111
real_t z
Definition mma.hpp:101
::std::unique_ptr< real_t[]> y
Definition mma.hpp:112
int nCon
Definition mma.hpp:102
void SetPrintLevel(int print_lvl)
Definition mma.hpp:95
::std::unique_ptr< real_t[]> s
Definition mma.hpp:112
int nVar
Definition mma.hpp:102
int iter
Definition mma.hpp:105
real_t epsimin
Definition mma.hpp:100
Vector data type.
Definition vector.hpp:82
real_t b
Definition lissajous.cpp:42
float real_t
Definition config.hpp:43
void Update(Vector &x, int k, DenseMatrix &h, Vector &s, Array< Vector * > &v)
Definition solvers.cpp:995