Commit 9a67ee4c authored by Davis King's avatar Davis King

Added some more optimization tests.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403927
parent 1c6b473b
......@@ -61,6 +61,7 @@ set (tests
metaprogramming.cpp
multithreaded_object.cpp
optimization.cpp
optimization_test_functions.cpp
opt_qp_solver.cpp
pipe.cpp
pixel.cpp
......
......@@ -71,6 +71,7 @@ SRC += member_function_pointer.cpp
SRC += metaprogramming.cpp
SRC += multithreaded_object.cpp
SRC += optimization.cpp
SRC += optimization_test_functions.cpp
SRC += opt_qp_solver.cpp
SRC += pipe.cpp
SRC += pixel.cpp
......
This diff is collapsed.
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_OPTIMIZATION_TEST_FUNCTiONS_H___
#define DLIB_OPTIMIZATION_TEST_FUNCTiONS_H___
#include <dlib/matrix.h>
#include <sstream>
#include <cmath>
/*
Most of the code in this file is converted from the set of Fortran 90 routines
created by John Burkardt.
The original Fortran can be found here: http://orion.math.iastate.edu/burkardt/f_src/testopt/testopt.html
*/
namespace dlib
{
namespace test_functions
{
// ----------------------------------------------------------------------------------------
matrix<double,0,1> chebyquad_residuals(const matrix<double,0,1>& x);
double chebyquad_residual(int i, const matrix<double,0,1>& x);
int& chebyquad_calls();
double chebyquad(const matrix<double,0,1>& x );
matrix<double,0,1> chebyquad_derivative (const matrix<double,0,1>& x);
matrix<double,0,1> chebyquad_start (int n);
matrix<double,0,1> chebyquad_solution (int n);
matrix<double> chebyquad_hessian(const matrix<double,0,1>& x);
// ----------------------------------------------------------------------------------------
class chebyquad_function_model
{
public:
// Define the type used to represent column vectors
typedef matrix<double,0,1> column_vector;
// Define the type used to represent the hessian matrix
typedef matrix<double> general_matrix;
double operator() (
const column_vector& x
) const
{
return chebyquad(x);
}
void get_derivative_and_hessian (
const column_vector& x,
column_vector& d,
general_matrix& h
) const
{
d = chebyquad_derivative(x);
h = chebyquad_hessian(x);
}
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
double brown_residual (int i, const matrix<double,4,1>& x);
/*!
requires
- 1 <= i <= 20
ensures
- returns the ith brown residual
!*/
double brown ( const matrix<double,4,1>& x);
matrix<double,4,1> brown_derivative ( const matrix<double,4,1>& x);
matrix<double,4,4> brown_hessian ( const matrix<double,4,1>& x);
matrix<double,4,1> brown_start ();
matrix<double,4,1> brown_solution ();
class brown_function_model
{
public:
// Define the type used to represent column vectors
typedef matrix<double,4,1> column_vector;
// Define the type used to represent the hessian matrix
typedef matrix<double> general_matrix;
double operator() (
const column_vector& x
) const
{
return brown(x);
}
void get_derivative_and_hessian (
const column_vector& x,
column_vector& d,
general_matrix& h
) const
{
d = brown_derivative(x);
h = brown_hessian(x);
}
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename T>
matrix<T,2,1> rosen_big_start()
{
matrix<T,2,1> x;
x = -1.2, -1;
return x;
}
// This is a variation on the Rosenbrock test function but with large residuals. The
// minimum is at 1, 1 and the objective value is 1.
template <typename T>
T rosen_big_residual (int i, const matrix<T,2,1>& m)
{
using std::pow;
const T x = m(0);
const T y = m(1);
if (i == 1)
{
return 100*pow(y - x*x,2)+1.0;
}
else
{
return pow(1 - x,2) + 1.0;
}
}
template <typename T>
T rosen_big ( const matrix<T,2,1>& m)
{
using std::pow;
return 0.5*(pow(rosen_big_residual(1,m),2) + pow(rosen_big_residual(2,m)));
}
template <typename T>
matrix<T,2,1> rosen_big_solution ()
{
matrix<T,2,1> x;
// solution from original documentation.
x = 1,1;
return x;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename T>
matrix<T,2,1> rosen_start()
{
matrix<T,2,1> x;
x = -1.2, -1;
return x;
}
template <typename T>
T rosen ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
using std::pow;
// compute Rosenbrock's function and return the result
return 100.0*pow(y - x*x,2) + pow(1 - x,2);
}
template <typename T>
T rosen_residual (int i, const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
if (i == 1)
{
return 10*(y - x*x);
}
else
{
return 1 - x;
}
}
template <typename T>
const matrix<T,2,1> rosen_derivative ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
// make us a column vector of length 2
matrix<T,2,1> res(2);
// now compute the gradient vector
res(0) = -400*x*(y-x*x) - 2*(1-x); // derivative of rosen() with respect to x
res(1) = 200*(y-x*x); // derivative of rosen() with respect to y
return res;
}
template <typename T>
const matrix<T,2,2> rosen_hessian ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
// make us a column vector of length 2
matrix<T,2,2> res;
// now compute the gradient vector
res(0,0) = -400*y + 3*400*x*x + 2;
res(1,1) = 200;
res(0,1) = -400*x;
res(1,0) = -400*x;
return res;
}
template <typename T>
matrix<T,2,1> rosen_solution ()
{
matrix<T,2,1> x;
// solution from original documentation.
x = 1,1;
return x;
}
// ------------------------------------------------------------------------------------
template <typename T>
struct rosen_function_model
{
typedef matrix<T,2,1> column_vector;
typedef matrix<T,2,2> general_matrix;
T operator() ( column_vector x) const
{
return static_cast<T>(rosen(x));
}
void get_derivative_and_hessian (
const column_vector& x,
column_vector& d,
general_matrix& h
) const
{
d = rosen_derivative(x);
h = rosen_hessian(x);
}
};
// ----------------------------------------------------------------------------------------
}
}
#endif // DLIB_OPTIMIZATION_TEST_FUNCTiONS_H___
......@@ -3,6 +3,7 @@
#include <dlib/optimization.h>
#include "optimization_test_functions.h"
#include <sstream>
#include <string>
#include <cstdlib>
......@@ -19,79 +20,10 @@ namespace
using namespace test;
using namespace dlib;
using namespace std;
using namespace dlib::test_functions;
logger dlog("test.trust_region");
// ----------------------------------------------------------------------------------------
template <typename T>
T rosen ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
// compute Rosenbrock's function and return the result
return 100.0*pow(y - x*x,2) + pow(1 - x,2);
}
template <typename T>
const matrix<T,2,1> rosen_derivative ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
// make us a column vector of length 2
matrix<T,2,1> res(2);
// now compute the gradient vector
res(0) = -400*x*(y-x*x) - 2*(1-x); // derivative of rosen() with respect to x
res(1) = 200*(y-x*x); // derivative of rosen() with respect to y
return res;
}
template <typename T>
const matrix<T,2,2> rosen_hessian ( const matrix<T,2,1>& m)
{
const T x = m(0);
const T y = m(1);
// make us a column vector of length 2
matrix<T,2,2> res;
// now compute the gradient vector
res(0,0) = -400*y + 3*400*x*x + 2;
res(1,1) = 200;
res(0,1) = -400*x;
res(1,0) = -400*x;
return res;
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct rosen_model
{
typedef matrix<T,2,1> column_vector;
typedef matrix<T,2,2> general_matrix;
T operator() ( column_vector x) const
{
return static_cast<T>(rosen(x));
}
void get_derivative_and_hessian (
const column_vector& x,
column_vector& d,
general_matrix& h
) const
{
d = rosen_derivative(x);
h = rosen_hessian(x);
}
};
// ----------------------------------------------------------------------------------------
template <typename T>
......@@ -131,7 +63,7 @@ namespace
matrix<T,2,1> p = 100*matrix_cast<T>(randm(2,1,rnd)) - 50;
T obj = find_min_trust_region(objective_delta_stop_strategy(0, 100), rosen_model<T>(), p);
T obj = find_min_trust_region(objective_delta_stop_strategy(0, 100), rosen_function_model<T>(), p);
DLIB_TEST_MSG(obj == 0, "obj: " << obj);
DLIB_TEST_MSG(length(p-ans) == 0, "length(p): " << length(p-ans));
......@@ -258,6 +190,113 @@ namespace
// ----------------------------------------------------------------------------------------
void test_problems()
{
print_spinner();
{
matrix<double,4,1> ch;
ch = brown_start();
find_min_trust_region(objective_delta_stop_strategy(1e-7, 80),
brown_function_model(),
ch);
dlog << LINFO << "brown obj: " << brown(ch);
dlog << LINFO << "brown der: " << length(brown_derivative(ch));
dlog << LINFO << "brown error: " << length(ch - brown_solution());
DLIB_TEST(length(ch - brown_solution()) < 1e-5);
}
print_spinner();
{
matrix<double,2,1> ch;
ch = rosen_start<double>();
find_min_trust_region(objective_delta_stop_strategy(1e-7, 80),
rosen_function_model<double>(),
ch);
dlog << LINFO << "rosen obj: " << rosen(ch);
dlog << LINFO << "rosen der: " << length(rosen_derivative(ch));
dlog << LINFO << "rosen error: " << length(ch - rosen_solution<double>());
DLIB_TEST(length(ch - rosen_solution<double>()) < 1e-5);
}
print_spinner();
{
matrix<double,0,1> ch;
ch = chebyquad_start(2);
find_min_trust_region(objective_delta_stop_strategy(1e-7, 80),
chebyquad_function_model(),
ch);
dlog << LINFO << "chebyquad 2 obj: " << chebyquad(ch);
dlog << LINFO << "chebyquad 2 der: " << length(chebyquad_derivative(ch));
dlog << LINFO << "chebyquad 2 error: " << length(ch - chebyquad_solution(2));
DLIB_TEST(length(ch - chebyquad_solution(2)) < 1e-5);
}
print_spinner();
{
matrix<double,0,1> ch;
ch = chebyquad_start(4);
find_min_trust_region(objective_delta_stop_strategy(1e-7, 80),
chebyquad_function_model(),
ch);
dlog << LINFO << "chebyquad 4 obj: " << chebyquad(ch);
dlog << LINFO << "chebyquad 4 der: " << length(chebyquad_derivative(ch));
dlog << LINFO << "chebyquad 4 error: " << length(ch - chebyquad_solution(4));
DLIB_TEST(length(ch - chebyquad_solution(4)) < 1e-5);
}
print_spinner();
{
matrix<double,0,1> ch;
ch = chebyquad_start(6);
find_min_trust_region(objective_delta_stop_strategy(1e-12, 80),
chebyquad_function_model(),
ch);
dlog << LINFO << "chebyquad 6 obj: " << chebyquad(ch);
dlog << LINFO << "chebyquad 6 der: " << length(chebyquad_derivative(ch));
dlog << LINFO << "chebyquad 6 error: " << length(ch - chebyquad_solution(6));
DLIB_TEST(length(ch - chebyquad_solution(6)) < 1e-5);
}
print_spinner();
{
matrix<double,0,1> ch;
ch = chebyquad_start(8);
find_min_trust_region(objective_delta_stop_strategy(1e-10, 80),
chebyquad_function_model(),
ch);
dlog << LINFO << "chebyquad 8 obj: " << chebyquad(ch);
dlog << LINFO << "chebyquad 8 der: " << length(chebyquad_derivative(ch));
dlog << LINFO << "chebyquad 8 error: " << length(ch - chebyquad_solution(8));
DLIB_TEST(length(ch - chebyquad_solution(8)) < 1e-5);
}
}
class optimization_tester : public tester
{
public:
......@@ -280,6 +319,8 @@ namespace
test_trust_region_sub_problem();
test_problems();
}
} a;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment