https://mooseframework.inl.gov
Loading...
Searching...
No Matches
QuadraticMinimize.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
10#include "QuadraticMinimize.h"
11#include "libmesh/petsc_vector.h"
12
13registerMooseObject("OptimizationTestApp", QuadraticMinimize);
14
17{
19 params.addRequiredParam<Real>("objective", "Desired value of objective function.");
20 params.addRequiredParam<std::vector<Real>>("solution", "Desired solution to optimization.");
21 return params;
22}
23
25 : OptimizationReporter(parameters),
26 _result(getParam<Real>("objective")),
27 _solution(getParam<std::vector<Real>>("solution"))
28{
30 if (_solution.size() != _ndof)
31 paramError("solution", "Size not equal to number of degrees of freedom (", _ndof, ").");
32}
33
34Real
36{
37 Real obj = _result;
38 unsigned int i = 0;
39 for (const auto & param : _parameters)
40 for (const auto & val : *param)
41 {
42 Real tmp = val - _solution[i++];
43 obj += tmp * tmp;
44 }
45
46 return obj;
47}
48
49void
51{
52 unsigned int i = 0;
53 for (const auto & param : _parameters)
54 for (const auto & val : *param)
55 {
56 gradient.set(i, 2.0 * (val - _solution[i]));
57 i++;
58 }
59 gradient.close();
60}
registerMooseObject("OptimizationTestApp", QuadraticMinimize)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void paramError(const std::string &param, Args... args) const
dof_id_type _ndof
Total number of parameters.
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
Computes gradient and contains reporters for communicating between optimizeSolve and subapps.
static InputParameters validParams()
virtual void setICsandBounds() override
Sets the initial conditions and bounds right before it is needed.
This form function simply represents a quadratic objective function: f(x) = val + \sum_{i=1}^N (x_i -...
static InputParameters validParams()
virtual Real computeObjective() override
Function to compute objective.
QuadraticMinimize(const InputParameters &parameters)
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
const std::vector< Real > & _solution
Desired solution to optimization.
const Real & _result
Input objective function value.
virtual void set(const numeric_index_type i, const T value) override
virtual void close() override