Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
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
* <http://shark-ml.org/>
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
{
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
struct
RotatedObjectiveFunction
:
public
SingleObjectiveFunction
{
46
RotatedObjectiveFunction
(
SingleObjectiveFunction
* objective)
47
:m_objective(objective){
48
if
(m_objective->
canProposeStartingPoint
())
49
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
50
if
(m_objective->
hasFirstDerivative
())
51
m_features
|=
HAS_FIRST_DERIVATIVE
;
52
}
53
54
/// \brief From INameable: return the class name.
55
std::string
name
()
const
56
{
return
"RotatedObjectiveFunction<"
+m_objective->
name
()+
">"
; }
57
58
std::size_t
numberOfVariables
()
const
{
59
return
m_objective->
numberOfVariables
();
60
}
61
62
void
init
(){
63
m_rotation =
blas::randomRotationMatrix
(*
mep_rng
,
numberOfVariables
());
64
m_objective->
setRng
(
mep_rng
);
65
m_objective->
init
();
66
}
67
68
bool
hasScalableDimensionality
()
const
{
69
return
m_objective->
hasScalableDimensionality
();
70
}
71
72
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
73
m_objective->
setNumberOfVariables
(numberOfVariables);
74
}
75
76
SearchPointType
proposeStartingPoint
()
const
{
77
RealVector y = m_objective->
proposeStartingPoint
();
78
79
return
prod(trans(m_rotation),y);
80
}
81
82
double
eval
(
SearchPointType
const
& p )
const
{
83
m_evaluationCounter
++;
84
RealVector x = prod(m_rotation,p);
85
return
m_objective->
eval
(x);
86
}
87
88
ResultType
evalDerivative
(
SearchPointType
const
& p,
FirstOrderDerivative
& derivative )
const
{
89
RealVector x = prod(m_rotation,p);
90
double
value = m_objective->
evalDerivative
(x,derivative);
91
derivative = prod(trans(m_rotation),derivative);
92
return
value;
93
}
94
private
:
95
SingleObjectiveFunction
* m_objective;
96
RealMatrix m_rotation;
97
};
98
99
}
100
101
#endif