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.addClassDescription("Test object that minimizes a quadratic objective function computed "
20 "directly on the main application.");
21 params.addRequiredParam<Real>("objective", "Desired value of objective function.");
22 params.addRequiredParam<std::vector<Real>>("solution", "Desired solution to optimization.");
23 // This object computes the objective itself, so the reporter value declared by
24 // GeneralOptimization is never populated by a sub-application transfer.
25 params.set<ReporterValueName>("objective_name") = "objective_value";
26 params.suppressParameter<ReporterValueName>("objective_name");
27 return params;
28}
29
31 : GeneralOptimization(parameters),
32 _result(getParam<Real>("objective")),
33 _solution(getParam<std::vector<Real>>("solution"))
34{
36 if (_solution.size() != _ndof)
37 paramError("solution", "Size not equal to number of degrees of freedom (", _ndof, ").");
38}
39
40Real
42{
43 Real obj = _result;
44 unsigned int i = 0;
45 for (const auto & param : _parameters)
46 for (const auto & val : *param)
47 {
48 Real tmp = val - _solution[i++];
49 obj += tmp * tmp;
50 }
51
52 return obj;
53}
54
55void
57{
58 unsigned int i = 0;
59 for (const auto & param : _parameters)
60 for (const auto & val : *param)
61 {
62 gradient.set(i, 2.0 * (val - _solution[i]));
63 i++;
64 }
65 gradient.close();
66}
registerMooseObject("OptimizationTestApp", QuadraticMinimize)
Optimization reporter that interfaces with TAO.
virtual void setICsandBounds() override
Sets the initial conditions and bounds right before it is needed.
static InputParameters validParams()
void suppressParameter(const std::string &name)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
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.
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