https://mooseframework.inl.gov
QuadraticMinimizeConstrained.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 #include "libmesh/petsc_vector.h"
12 
14 
17 {
19  params.addRequiredParam<Real>("solution_sum_equality",
20  "Desired sum of the solution for constrained optimization.");
21  return params;
22 }
23 
25  : QuadraticMinimize(parameters),
26  _result(getParam<Real>("objective")),
27  _solution(getParam<std::vector<Real>>("solution")),
28  _eq_constraint(getParam<Real>("solution_sum_equality"))
29 {
30 }
31 
32 Real
34 {
35  Real obj = _result;
36  unsigned int i = 0;
37  for (const auto & param : _parameters)
38  for (const auto & val : *param)
39  {
40  Real tmp = val - _solution[i++];
41  obj += tmp * tmp;
42  }
43 
44  return obj;
45 }
46 
47 void
49 {
50  unsigned int i = 0;
51  for (const auto & param : _parameters)
52  for (const auto & val : *param)
53  {
54  gradient.set(i, 2.0 * (val - _solution[i]));
55  i++;
56  }
57  gradient.close();
58 }
59 void
61  libMesh::PetscVector<Number> & eqs_constraints) const
62 {
63 
64  unsigned int i = 0;
65  for (const auto & param : _parameters)
66  {
67  const Real equality_constraint = std::accumulate(param->begin(), param->end(), 0.0);
68  eqs_constraints.set(i++, equality_constraint - _eq_constraint);
69  }
70  eqs_constraints.close();
71 }
72 void
74 {
75  gradient.zero();
76  for (const auto i : make_range(_n_eq_cons))
77  for (const auto j : index_range(*_parameters[0]))
78  gradient.set(i, j, 1);
79 
80  gradient.close();
81 }
const std::vector< Real > & _solution
Desired solution to optimize to.
virtual void computeEqualityConstraints(libMesh::PetscVector< Number > &eqs_constraints) const override
Function to compute the equality constraints.
virtual void zero() override
registerMooseObject("OptimizationTestApp", QuadraticMinimizeConstrained)
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
static InputParameters validParams()
QuadraticMinimizeConstrained(const InputParameters &parameters)
void addRequiredParam(const std::string &name, const std::string &doc_string)
This form function represents a constrained quadratic objective function: minimize f(x) = val + {i=1}...
virtual Real computeObjective() override
Function to compute objective.
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
virtual void close() override
virtual void computeEqualityGradient(libMesh::PetscMatrix< Number > &gradient) const override
Function to compute the gradient of the equality constraints/ This is the last call of the equality c...
const unsigned int _n_eq_cons
Number of equality constraint names.
virtual void set(const numeric_index_type i, const T value) override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Real & _result
Input objective function value.
IntRange< T > make_range(T beg, T end)
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
This form function simply represents a quadratic objective function: f(x) = val + {i=1}^N (x_i - a_i)...
auto index_range(const T &sizable)
virtual void close() override