https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
32Real
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
47void
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}
59void
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}
72void
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}
registerMooseObject("OptimizationTestApp", QuadraticMinimizeConstrained)
void addRequiredParam(const std::string &name, const std::string &doc_string)
const unsigned int _n_eq_cons
Number of equality constraint names.
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
This form function represents a constrained quadratic objective function: minimize f(x) = val + \sum_...
const Real & _result
Input objective function value.
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...
virtual Real computeObjective() override
Function to compute objective.
const std::vector< Real > & _solution
Desired solution to optimize to.
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
virtual void computeEqualityConstraints(libMesh::PetscVector< Number > &eqs_constraints) const override
Function to compute the equality constraints.
QuadraticMinimizeConstrained(const InputParameters &parameters)
This form function simply represents a quadratic objective function: f(x) = val + \sum_{i=1}^N (x_i -...
static InputParameters validParams()
virtual void close() override
virtual void zero() override
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
virtual void set(const numeric_index_type i, const T value) override
virtual void close() override