Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
ObjectiveFunctions
Benchmarks
RotatedErrorFunction.h
Go to the documentation of this file.
1
/*!
2
*
3
*
4
* \brief Implements a wrapper over an m_objective function which just rotates its inputs
5
*
6
*
7
* \author O.Voss
8
* \date 2010-2014
9
*
10
*
11
* \par Copyright 1995-2017 Shark Development Team
12
*
13
* <BR><HR>
14
* This file is part of Shark.
15
* <https://shark-ml.github.io/Shark/>
16
*
17
* Shark is free software: you can redistribute it and/or modify
18
* it under the terms of the GNU Lesser General Public License as published
19
* by the Free Software Foundation, either version 3 of the License, or
20
* (at your option) any later version.
21
*
22
* Shark is distributed in the hope that it will be useful,
23
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
* GNU Lesser General Public License for more details.
26
*
27
* You should have received a copy of the GNU Lesser General Public License
28
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
29
*
30
*/
31
#ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ROTATEDOBJECTIVEFUNCTION_H
32
#define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ROTATEDOBJECTIVEFUNCTION_H
33
34
#include <
shark/ObjectiveFunctions/AbstractObjectiveFunction.h
>
35
#include <
shark/LinAlg/rotations.h
>
36
37
namespace
shark
{
namespace
benchmarks{
38
/// \brief Rotates an objective function using a randomly initialized rotation.
39
///
40
/// Most benchmark functions are axis aligned because it is assumed that the algorithm
41
/// is rotation invariant. However this does not mean that all its aspects are the same.
42
/// Especially linear algebra routines might take longer when the problem is not
43
/// axis aligned. This function creates a random rotation function and
44
/// applies it to the given input points to make it no longer axis aligned.
45
/// \ingroup benchmarks
46
struct
RotatedObjectiveFunction
:
public
SingleObjectiveFunction
{
47
RotatedObjectiveFunction
(
SingleObjectiveFunction
* objective)
48
:m_objective(objective){
49
if
(m_objective->
canProposeStartingPoint
())
50
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
51
if
(m_objective->
hasFirstDerivative
())
52
m_features
|=
HAS_FIRST_DERIVATIVE
;
53
}
54
55
/// \brief From INameable: return the class name.
56
std::string
name
()
const
57
{
return
"RotatedObjectiveFunction<"
+m_objective->
name
()+
">"
; }
58
59
std::size_t
numberOfVariables
()
const
{
60
return
m_objective->
numberOfVariables
();
61
}
62
63
void
init
(){
64
m_rotation =
blas::randomRotationMatrix
(*
mep_rng
,
numberOfVariables
());
65
m_objective->
setRng
(
mep_rng
);
66
m_objective->
init
();
67
}
68
69
bool
hasScalableDimensionality
()
const
{
70
return
m_objective->
hasScalableDimensionality
();
71
}
72
73
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
74
m_objective->
setNumberOfVariables
(
numberOfVariables
);
75
}
76
77
SearchPointType
proposeStartingPoint
()
const
{
78
RealVector y = m_objective->
proposeStartingPoint
();
79
80
return
prod(trans(m_rotation),y);
81
}
82
83
double
eval
(
SearchPointType
const
& p )
const
{
84
m_evaluationCounter
++;
85
RealVector x = prod(m_rotation,p);
86
return
m_objective->
eval
(x);
87
}
88
89
ResultType
evalDerivative
(
SearchPointType
const
& p,
FirstOrderDerivative
& derivative )
const
{
90
RealVector x = prod(m_rotation,p);
91
double
value = m_objective->
evalDerivative
(x,derivative);
92
derivative = prod(trans(m_rotation),derivative);
93
return
value;
94
}
95
private
:
96
SingleObjectiveFunction
* m_objective;
97
RealMatrix m_rotation;
98
};
99
100
}}
101
102
#endif