Skip to content

Commit e2b67b2

Browse files
committed
Refactor problems to avoid adding extra template parameters.
1 parent be1d8fa commit e2b67b2

38 files changed

Lines changed: 394 additions & 574 deletions

include/ensmallen_bits/problems/aug_lagrangian_test_functions.hpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,40 @@ class AugLagrangianTestFunction
5656
* The minimum that satisfies the two constraints is given as
5757
* x = [0.12288, -1.1078, 0.015100], with an objective value of about 29.634.
5858
*/
59-
template<typename MatType = arma::mat>
60-
class GockenbachFunctionType
59+
class GockenbachFunction
6160
{
6261
public:
63-
GockenbachFunctionType();
64-
GockenbachFunctionType(const MatType& initialPoint);
62+
GockenbachFunction();
63+
GockenbachFunction(const arma::mat& initialPoint);
6564

66-
template<typename InputMatType>
67-
typename InputMatType::elem_type Evaluate(const InputMatType& coordinates);
65+
template<typename MatType>
66+
typename MatType::elem_type Evaluate(const MatType& coordinates);
6867

69-
template<typename InputMatType, typename InputGradType>
70-
void Gradient(const InputMatType& coordinates, InputGradType& gradient);
68+
template<typename MatType, typename GradType>
69+
void Gradient(const MatType& coordinates, GradType& gradient);
7170

7271
size_t NumConstraints() const { return 2; }
7372

74-
template<typename InputMatType>
75-
typename InputMatType::elem_type EvaluateConstraint(
73+
template<typename MatType>
74+
typename MatType::elem_type EvaluateConstraint(
7675
const size_t index,
77-
const InputMatType& coordinates);
76+
const MatType& coordinates);
7877

79-
template<typename InputMatType, typename InputGradType>
78+
template<typename MatType, typename GradType>
8079
void GradientConstraint(const size_t index,
81-
const InputMatType& coordinates,
82-
InputGradType& gradient);
80+
const MatType& coordinates,
81+
GradType& gradient);
8382

84-
template<typename InputMatType>
85-
InputMatType GetInitialPoint() const
83+
template<typename MatType>
84+
MatType GetInitialPoint() const
8685
{
87-
return conv_to<InputMatType>::from(initialPoint);
86+
return arma::conv_to<MatType>::from(initialPoint);
8887
}
8988

9089
private:
91-
MatType initialPoint;
90+
arma::mat initialPoint;
9291
};
9392

94-
using GockenbachFunction = GockenbachFunctionType<arma::mat>;
95-
9693
/**
9794
* This function is the Lovasz-Theta semidefinite program, as implemented in the
9895
* following paper:

include/ensmallen_bits/problems/aug_lagrangian_test_functions_impl.hpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,32 @@ inline void AugLagrangianTestFunction::GradientConstraint(const size_t index,
8282
//
8383
// GockenbachFunction
8484
//
85-
template<typename MatType>
86-
GockenbachFunctionType<MatType>::GockenbachFunctionType()
85+
inline GockenbachFunction::GockenbachFunction()
8786
{
8887
// Set the initial point to (0, 0, 1).
8988
initialPoint.zeros(3, 1);
9089
initialPoint[2] = 1;
9190
}
9291

93-
template<typename MatType>
94-
GockenbachFunctionType<MatType>::GockenbachFunctionType(const MatType& initialPoint) :
92+
inline GockenbachFunction::GockenbachFunction(const arma::mat& initialPoint) :
9593
initialPoint(initialPoint)
9694
{
9795
// Nothing to do.
9896
}
9997

10098
template<typename MatType>
101-
template<typename InputMatType>
102-
typename InputMatType::elem_type GockenbachFunctionType<MatType>::Evaluate(
103-
const InputMatType& coordinates)
99+
inline typename MatType::elem_type GockenbachFunction::Evaluate(
100+
const MatType& coordinates)
104101
{
105102
// f(x) = (x_1 - 1)^2 + 2 (x_2 + 2)^2 + 3(x_3 + 3)^2
106103
return ((std::pow(coordinates[0] - 1, 2)) +
107104
(2 * std::pow(coordinates[1] + 2, 2)) +
108105
(3 * std::pow(coordinates[2] + 3, 2)));
109106
}
110107

111-
template<typename MatType>
112-
template<typename InputMatType, typename InputGradType>
113-
void GockenbachFunctionType<MatType>::Gradient(
114-
const InputMatType& coordinates, InputGradType& gradient)
108+
template<typename MatType, typename GradType>
109+
inline void GockenbachFunction::Gradient(const MatType& coordinates,
110+
GradType& gradient)
115111
{
116112
// f'_x1(x) = 2 (x_1 - 1)
117113
// f'_x2(x) = 4 (x_2 + 2)
@@ -124,35 +120,35 @@ void GockenbachFunctionType<MatType>::Gradient(
124120
}
125121

126122
template<typename MatType>
127-
template<typename InputMatType>
128-
typename InputMatType::elem_type GockenbachFunctionType<MatType>::EvaluateConstraint(
129-
const size_t index, const InputMatType& coordinates)
123+
inline typename MatType::elem_type GockenbachFunction::EvaluateConstraint(
124+
const size_t index,
125+
const MatType& coordinates)
130126
{
131-
typename InputMatType::elem_type constraint = 0;
127+
typename MatType::elem_type constraint = 0;
132128

133129
switch (index)
134130
{
135131
case 0: // g(x) = (x_3 - x_2 - x_1 - 1) = 0
136132
constraint = (coordinates[2] - coordinates[1] - coordinates[0] -
137-
typename InputMatType::elem_type(1));
133+
typename MatType::elem_type(1));
138134
break;
139135

140136
case 1: // h(x) = (x_3 - x_1^2) >= 0
141137
// To deal with the inequality, the constraint will simply evaluate to 0
142138
// when h(x) >= 0.
143-
constraint = std::min(typename InputMatType::elem_type(0), (coordinates[2] -
144-
std::pow(coordinates[0], typename InputMatType::elem_type(2))));
139+
constraint = std::min(typename MatType::elem_type(0), (coordinates[2] -
140+
std::pow(coordinates[0], typename MatType::elem_type(2))));
145141
break;
146142
}
147143

148144
// 0 will be returned for an invalid index (but this is okay).
149145
return constraint;
150146
}
151147

152-
template<typename MatType>
153-
template<typename InputMatType, typename InputGradType>
154-
void GockenbachFunctionType<MatType>::GradientConstraint(
155-
const size_t index, const InputMatType& coordinates, InputGradType& gradient)
148+
template<typename MatType, typename GradType>
149+
inline void GockenbachFunction::GradientConstraint(const size_t index,
150+
const MatType& coordinates,
151+
GradType& gradient)
156152
{
157153
gradient.zeros(3, 1);
158154

@@ -339,7 +335,8 @@ inline const arma::mat& LovaszThetaSDP::GetInitialPoint()
339335
for (size_t j = 0; j < (size_t) vertices; j++)
340336
{
341337
if (i == j)
342-
initialPoint(i, j) = std::sqrt(1.0 / r) + std::sqrt(1.0 / (vertices * m));
338+
initialPoint(i, j) = std::sqrt(1.0 / r) +
339+
std::sqrt(1.0 / (vertices * m));
343340
else
344341
initialPoint(i, j) = std::sqrt(1.0 / (vertices * m));
345342
}

include/ensmallen_bits/problems/generalized_rosenbrock_function.hpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,15 @@ namespace test {
4242
* }
4343
* @endcode
4444
*/
45-
template<
46-
typename MatType = arma::mat,
47-
typename LabelsType = typename ForwardType<MatType>::urowvec>
48-
class GeneralizedRosenbrockFunctionType
45+
class GeneralizedRosenbrockFunction
4946
{
5047
public:
5148
/*
5249
* Initialize the GeneralizedRosenbrockFunction.
5350
*
5451
* @param n Number of dimensions for the function.
5552
*/
56-
GeneralizedRosenbrockFunctionType(const size_t n);
53+
GeneralizedRosenbrockFunction(const size_t n);
5754

5855
/**
5956
* Shuffle the order of function visitation. This may be called by the
@@ -71,6 +68,7 @@ class GeneralizedRosenbrockFunctionType
7168
* @param begin The first function.
7269
* @param batchSize Number of points to process.
7370
*/
71+
template<typename MatType>
7472
typename MatType::elem_type Evaluate(const MatType& coordinates,
7573
const size_t begin,
7674
const size_t batchSize = 1) const;
@@ -80,6 +78,7 @@ class GeneralizedRosenbrockFunctionType
8078
*
8179
* @param coordinates The function coordinates.
8280
*/
81+
template<typename MatType>
8382
typename MatType::elem_type Evaluate(const MatType& coordinates) const;
8483

8584
/**
@@ -90,7 +89,7 @@ class GeneralizedRosenbrockFunctionType
9089
* @param gradient The function gradient.
9190
* @param batchSize Number of points to process.
9291
*/
93-
template<typename GradType>
92+
template<typename MatType, typename GradType>
9493
void Gradient(const MatType& coordinates,
9594
const size_t begin,
9695
GradType& gradient,
@@ -102,7 +101,7 @@ class GeneralizedRosenbrockFunctionType
102101
* @param coordinates The function coordinates.
103102
* @param gradient The function gradient.
104103
*/
105-
template<typename GradType>
104+
template<typename MatType, typename GradType>
106105
void Gradient(const MatType& coordinates, GradType& gradient) const;
107106

108107
// Note: GetInitialPoint(), GetFinalPoint(), and GetFinalObjective() are not
@@ -111,38 +110,33 @@ class GeneralizedRosenbrockFunctionType
111110
// infrastructure.
112111

113112
//! Get the starting point.
114-
template<typename InputMatType = MatType>
115-
const InputMatType GetInitialPoint() const
113+
template<typename MatType = arma::mat>
114+
const MatType GetInitialPoint() const
116115
{
117-
return conv_to<InputMatType>::from(initialPoint);
116+
return arma::conv_to<MatType>::from(initialPoint);
118117
}
119118

120119
//! Get the final point.
121-
template<typename InputMatType = MatType>
122-
const InputMatType GetFinalPoint() const
120+
template<typename MatType = arma::mat>
121+
const MatType GetFinalPoint() const
123122
{
124-
InputMatType finalPoint(initialPoint.n_rows, initialPoint.n_cols);
125-
finalPoint.ones();
126-
return finalPoint;
123+
return arma::ones<MatType>(initialPoint.n_rows, initialPoint.n_cols);
127124
}
128125

129126
//! Get the final objective.
130127
double GetFinalObjective() const { return 0.0; }
131128

132129
private:
133130
//! Locally-stored Initial point.
134-
MatType initialPoint;
131+
arma::mat initialPoint;
135132

136-
//! Number of dimensions for the function.
133+
//! //! Number of dimensions for the function.
137134
size_t n;
138135

139136
//! For shuffling.
140-
LabelsType visitationOrder;
137+
arma::Row<size_t> visitationOrder;
141138
};
142139

143-
using GeneralizedRosenbrockFunction = GeneralizedRosenbrockFunctionType<
144-
arma::mat>;
145-
146140
} // namespace test
147141
} // namespace ens
148142

include/ensmallen_bits/problems/generalized_rosenbrock_function_impl.hpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
namespace ens {
2020
namespace test {
2121

22-
template<typename MatType, typename LabelsType>
23-
GeneralizedRosenbrockFunctionType<
24-
MatType, LabelsType>::GeneralizedRosenbrockFunctionType(
22+
inline GeneralizedRosenbrockFunction::GeneralizedRosenbrockFunction(
2523
const size_t n) :
2624
n(n),
27-
visitationOrder(linspace<LabelsType>(0, n - 1, n))
25+
visitationOrder(arma::linspace<arma::Row<size_t> >(0, n - 1, n))
2826

2927
{
3028
initialPoint.set_size(n, 1);
@@ -41,15 +39,14 @@ GeneralizedRosenbrockFunctionType<
4139
}
4240
}
4341

44-
template<typename MatType, typename LabelsType>
45-
void GeneralizedRosenbrockFunctionType<MatType, LabelsType>::Shuffle()
42+
inline void GeneralizedRosenbrockFunction::Shuffle()
4643
{
47-
visitationOrder = shuffle(linspace<LabelsType>(0, n - 2, n - 1));
44+
visitationOrder = arma::shuffle(arma::linspace<arma::Row<size_t>>(0, n - 2,
45+
n - 1));
4846
}
4947

50-
template<typename MatType, typename LabelsType>
51-
typename MatType::elem_type GeneralizedRosenbrockFunctionType<
52-
MatType, LabelsType>::Evaluate(
48+
template<typename MatType>
49+
typename MatType::elem_type GeneralizedRosenbrockFunction::Evaluate(
5350
const MatType& coordinates,
5451
const size_t begin,
5552
const size_t batchSize) const
@@ -65,9 +62,8 @@ typename MatType::elem_type GeneralizedRosenbrockFunctionType<
6562
return objective;
6663
}
6764

68-
template<typename MatType, typename LabelsType>
69-
typename MatType::elem_type GeneralizedRosenbrockFunctionType<
70-
MatType, LabelsType>::Evaluate(
65+
template<typename MatType>
66+
typename MatType::elem_type GeneralizedRosenbrockFunction::Evaluate(
7167
const MatType& coordinates) const
7268
{
7369
typename MatType::elem_type fval = 0;
@@ -80,9 +76,8 @@ typename MatType::elem_type GeneralizedRosenbrockFunctionType<
8076
return fval;
8177
}
8278

83-
template<typename MatType, typename LabelsType>
84-
template<typename GradType>
85-
void GeneralizedRosenbrockFunctionType<MatType, LabelsType>::Gradient(
79+
template<typename MatType, typename GradType>
80+
inline void GeneralizedRosenbrockFunction::Gradient(
8681
const MatType& coordinates,
8782
const size_t begin,
8883
GradType& gradient,
@@ -98,9 +93,8 @@ void GeneralizedRosenbrockFunctionType<MatType, LabelsType>::Gradient(
9893
}
9994
}
10095

101-
template<typename MatType, typename LabelsType>
102-
template<typename GradType>
103-
void GeneralizedRosenbrockFunctionType<MatType, LabelsType>::Gradient(
96+
template<typename MatType, typename GradType>
97+
inline void GeneralizedRosenbrockFunction::Gradient(
10498
const MatType& coordinates,
10599
GradType& gradient) const
106100
{

0 commit comments

Comments
 (0)