ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
zoo
testproblems
ROL_LeastSquares.hpp
Go to the documentation of this file.
1
// @HEADER
2
// *****************************************************************************
3
// Rapid Optimization Library (ROL) Package
4
//
5
// Copyright 2014 NTESS and the ROL contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
14
15
#ifndef USE_HESSVEC
16
#define USE_HESSVEC 1
17
#endif
18
19
#ifndef ROL_LEASTSQUARES_HPP
20
#define ROL_LEASTSQUARES_HPP
21
22
#include "
ROL_StdVector.hpp
"
23
#include "
ROL_TestProblem.hpp
"
24
25
namespace
ROL
{
26
namespace
ZOO {
27
30
template
<
class
Real>
31
class
Objective_LeastSquares
:
public
Objective
<Real> {
32
typedef
typename
std::vector<Real>::size_type
uint
;
33
34
public
:
35
36
Real
value
(
const
Vector<Real>
&x, Real &tol ) {
37
ROL::Ptr<const std::vector<Real> > ex
38
=
dynamic_cast<
const
StdVector<Real>
&
>
(x).getVector();
39
40
uint
n = ex->size();
41
Real h = 1.0/((Real)n+1.0);
42
Real val = 0.0;
43
Real res = 0.0;
44
for
(
uint
i=0; i<n; i++) {
45
if
( i == 0 ) {
46
res = 2.0*h*(5.0/6.0) + 1.0/h*((*ex)[i+1]-2.0*(*ex)[i]);
47
}
48
else
if
( i == n-1 ) {
49
res = 2.0*h*(5.0/6.0) + 1.0/h*((*ex)[i-1]-2.0*(*ex)[i]);
50
}
51
else
{
52
res = 2.0*h + 1.0/h*((*ex)[i-1]-2.0*(*ex)[i]+(*ex)[i+1]);
53
}
54
val += 0.5*res*res;
55
}
56
return
val;
57
}
58
59
void
gradient
(
Vector<Real>
&g,
const
Vector<Real>
&x, Real &tol ) {
60
ROL::Ptr<std::vector<Real> > eg
61
=
dynamic_cast<
StdVector<Real>
&
>
(g).getVector();
62
ROL::Ptr<const std::vector<Real> > ex
63
=
dynamic_cast<
const
StdVector<Real>
&
>
(x).getVector();
64
65
uint
n = ex->size();
66
Real h = 1.0/((Real)n+1.0);
67
std::vector<Real> res(n,0.0);
68
for
(
uint
i=0; i<n; i++) {
69
if
( i == 0 ) {
70
res[i] = 2.0*h*(5.0/6.0) + 1.0/h*((*ex)[i+1]-2.0*(*ex)[i]);
71
}
72
else
if
( i == n-1 ) {
73
res[i] = 2.0*h*(5.0/6.0) + 1.0/h*((*ex)[i-1]-2.0*(*ex)[i]);
74
}
75
else
{
76
res[i] = 2.0*h + 1.0/h*((*ex)[i-1]-2.0*(*ex)[i]+(*ex)[i+1]);
77
}
78
}
79
80
for
(
uint
i=0; i<n; i++) {
81
if
( i == 0 ) {
82
(*eg)[i] = 1.0/h*(res[i+1]-2.0*res[i]);
83
}
84
else
if
( i == n-1 ) {
85
(*eg)[i] = 1.0/h*(res[i-1]-2.0*res[i]);
86
}
87
else
{
88
(*eg)[i] = 1.0/h*(res[i-1]-2.0*res[i]+res[i+1]);
89
}
90
}
91
}
92
#if USE_HESSVEC
93
void
hessVec
(
Vector<Real>
&hv,
const
Vector<Real>
&v,
const
Vector<Real>
&x, Real &tol ) {
94
ROL::Ptr<std::vector<Real> > ehv
95
=
dynamic_cast<
StdVector<Real>
&
>
(hv).getVector();
96
ROL::Ptr<const std::vector<Real> > ev
97
=
dynamic_cast<
const
StdVector<Real>
&
>
(v).getVector();
98
ROL::Ptr<const std::vector<Real> > ex
99
=
dynamic_cast<
const
StdVector<Real>
&
>
(x).getVector();
100
101
uint
n = ex->size();
102
Real h = 1.0/((Real)n+1.0);
103
std::vector<Real> res(n,0.0);
104
for
(
uint
i=0; i<n; i++) {
105
if
( i == 0 ) {
106
res[i] = 1.0/h*((*ev)[i+1]-2.0*(*ev)[i]);
107
}
108
else
if
( i == n-1 ) {
109
res[i] = 1.0/h*((*ev)[i-1]-2.0*(*ev)[i]);
110
}
111
else
{
112
res[i] = 1.0/h*((*ev)[i-1]-2.0*(*ev)[i]+(*ev)[i+1]);
113
}
114
}
115
116
for
(
uint
i=0; i<n; i++) {
117
if
( i == 0 ) {
118
(*ehv)[i] = 1.0/h*(res[i+1]-2.0*res[i]);
119
}
120
else
if
( i == n-1 ) {
121
(*ehv)[i] = 1.0/h*(res[i-1]-2.0*res[i]);
122
}
123
else
{
124
(*ehv)[i] = 1.0/h*(res[i-1]-2.0*res[i]+res[i+1]);
125
}
126
}
127
}
128
#endif
129
};
130
131
template
<
class
Real>
132
class
getLeastSquares
:
public
TestProblem
<Real> {
133
public
:
134
getLeastSquares
(
void
){}
135
136
Ptr<Objective<Real>>
getObjective
(
void
)
const
{
137
// Instantiate Objective Function
138
return
ROL::makePtr<Objective_LeastSquares<Real>>();
139
}
140
141
Ptr<Vector<Real>>
getInitialGuess
(
void
)
const
{
142
// Problem dimension
143
int
n = 32;
144
// Get Initial Guess
145
ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
146
for
(
int
i = 0; i < n; i++ ) {
147
(*x0p)[i] = 0.0;
148
}
149
return
ROL::makePtr<StdVector<Real>>(x0p);
150
}
151
152
Ptr<Vector<Real>>
getSolution
(
const
int
i = 0)
const
{
153
// Problem dimension
154
int
n = 32;
155
// Get Solution
156
ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
157
Real h = 1.0/((Real)n+1.0), pt = 0.0;
158
for
(
int
li = 0; li < n; li++ ) {
159
pt = (Real)(li+1)*h;
160
(*xp)[li] = pt*(1.0-pt);
161
}
162
return
ROL::makePtr<StdVector<Real>>(xp);
163
}
164
};
165
166
}
// End ZOO Namespace
167
}
// End ROL Namespace
168
169
#endif
ROL_StdVector.hpp
ROL_TestProblem.hpp
Contains definitions of test objective functions.
ROL::ROL::Objective::hessVec
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Definition
ROL_ObjectiveDef.hpp:61
ROL::ROL::Objective::Objective
Objective()
Definition
ROL_Constraint_SerialSimOpt.hpp:53
ROL::StdVector
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Definition
ROL_StdVector.hpp:27
ROL::TestProblem::TestProblem
TestProblem(void)
Definition
ROL_TestProblem.hpp:28
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL::ZOO::Objective_LeastSquares
Least squares function.
Definition
ROL_LeastSquares.hpp:31
ROL::ZOO::Objective_LeastSquares::value
Real value(const Vector< Real > &x, Real &tol)
Definition
ROL_LeastSquares.hpp:36
ROL::ZOO::Objective_LeastSquares::uint
std::vector< Real >::size_type uint
Definition
ROL_LeastSquares.hpp:32
ROL::ZOO::Objective_LeastSquares::gradient
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Definition
ROL_LeastSquares.hpp:59
ROL::ZOO::getLeastSquares::getInitialGuess
Ptr< Vector< Real > > getInitialGuess(void) const
Definition
ROL_LeastSquares.hpp:141
ROL::ZOO::getLeastSquares::getLeastSquares
getLeastSquares(void)
Definition
ROL_LeastSquares.hpp:134
ROL::ZOO::getLeastSquares::getSolution
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition
ROL_LeastSquares.hpp:152
ROL::ZOO::getLeastSquares::getObjective
Ptr< Objective< Real > > getObjective(void) const
Definition
ROL_LeastSquares.hpp:136
ROL
Definition
ROL_ElementwiseVector.hpp:27
Generated by
1.17.0