Skip to content

Commit

Permalink
Added a basic unit test to help with debugging the Conjugate Gradient
Browse files Browse the repository at this point in the history
optimizer.
  • Loading branch information
broxtronix committed Oct 30, 2006
1 parent dc5e797 commit 60fafdc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/workbook/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INCPATH += -I../../src
INCPATH += -I../../src -I/opt/local/include
LIBS = ../../src/vw/FileIO/libvwFileIO.la
CXXFLAGS = -O3 -g -Wall

Expand Down
3 changes: 2 additions & 1 deletion docs/workbook/workbook.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
\usepackage{float}
\usepackage{subfigure}
\usepackage{graphicx}
\usepackage{epstopdf}

% Define the \sourcelst command to create a floating listing of
% a (separate) source file.
Expand Down Expand Up @@ -40,7 +41,7 @@
\include{workingwithimages}
\include{imageprocessing}
\include{typesystem}

\include{camera_module}
\chapter{Advanced Topics}\label{ch:advanced-topics}

\section{Working with Shallow Views}\label{sec:advanced.shallow}
Expand Down
16 changes: 9 additions & 7 deletions src/vw/Math/ConjugateGradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
#ifndef __VW_MATH_CONJUGATEGRADIENT_H__
#define __VW_MATH_CONJUGATEGRADIENT_H__

#include <vw/Core/Debugging.h>

#define VW_CONJGRAD_MAX_ITERS_BETWEEN_SPACER_STEPS 20

namespace vw {
Expand Down Expand Up @@ -116,7 +118,7 @@ namespace math{
typename FuncT::result_type new_val = func(new_pos);
if( new_val - val <= thresh ) return new_pos;
if( ( stepsize < 1e-20 && val-new_val > 0 ) || stepsize < 1e-40 ) {
std::cout << "ArmijoStepSize punting! (slope=" << dot_prod(grad,dir) << ", delta=" << (new_val-val) << ", thresh=" << thresh << ")" << std::endl;
vw_out(DebugMessage) << "ArmijoStepSize punting! (slope=" << dot_prod(grad,dir) << ", delta=" << (new_val-val) << ", thresh=" << thresh << ")" << std::endl;
return new_pos;
}
stepsize *= beta;
Expand Down Expand Up @@ -204,13 +206,13 @@ namespace math{
int numiters ) {
typename FuncT::domain_type pos = seed;
typename FuncT::result_type val = func(pos);
std::cout << "Initial: " << val << std::endl;
vw_out(DebugMessage) << "Initial: " << val << std::endl;
for( int i=0; i<numiters; ++i ) {
typename FuncT::gradient_type grad = func.gradient( pos );
pos = step( func, pos, val, grad, -grad );
val = func(pos);
std::cout << "Step " << i << ": " << val << std::endl;
++stage;
vw_out(DebugMessage) << "Step " << i << ": " << val << std::endl;
// ++stage;
}
return pos;
}
Expand All @@ -223,7 +225,7 @@ namespace math{
int numiters ) {
typename FuncT::domain_type pos = seed;
typename FuncT::result_type val = func(pos);
std::cout << "Initial: " << val << std::endl;
vw_out(DebugMessage) << "Initial: " << val << std::endl;
double last_grad_norm2 = 0;
typename FuncT::gradient_type last_dir;
for( int i=0; i<numiters; ++i ) {
Expand All @@ -236,8 +238,8 @@ namespace math{
last_grad_norm2 = grad_norm2;
last_dir = dir;
val = func(pos);
std::cout << "Step " << i << ": " << val << std::endl;
++stage;
vw_out(DebugMessage) << "Step " << i << ": " << val << std::endl;
// ++stage;
}
return pos;
}
Expand Down
5 changes: 3 additions & 2 deletions src/vw/Math/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nodist_TestMatrix_SOURCES = TestMatrix.cxx
nodist_TestBBox_SOURCES = TestBBox.cxx
nodist_TestFunctions_SOURCES = TestFunctions.cxx
nodist_TestFunctors_SOURCES = TestFunctors.cxx
nodist_TestConjugateGradient_SOURCES = TestConjugateGradient.cxx

if HAVE_PKG_LAPACK
nodist_TestLinearAlgebra_SOURCES = TestLinearAlgebra.cxx
Expand All @@ -40,8 +41,8 @@ endif

#nodist_TestOptimization_SOURCES = TestOptimization.cxx

TESTS = TestVector TestMatrix TestBBox TestFunctions TestFunctors \
$(TestLinearAlgebra) #TestOptimization
TESTS = TestVector TestMatrix TestBBox TestFunctions TestFunctors \
TestConjugateGradient $(TestLinearAlgebra) #TestOptimization

endif

Expand Down
86 changes: 86 additions & 0 deletions src/vw/Math/tests/TestConjugateGradient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// __BEGIN_LICENSE__
//
// Copyright (C) 2006 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA). All Rights Reserved.
//
// Copyright 2006 Carnegie Mellon University. All rights reserved.
//
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
// Initiative. See the file COPYING at the top of the distribution
// directory tree for the complete NOSA document.
//
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
//
// __END_LICENSE__

// TestConjugateGradient.h
#include <cxxtest/TestSuite.h>
#include <vw/Math/Vector.h>
#include <vw/Math/ConjugateGradient.h>

using namespace vw;
using namespace vw::math;

// This quadratic function has a single minimum at [0.1962, 0.4846].
struct QuadraticFunction {
typedef double result_type;
typedef Vector2 domain_type;
typedef Vector2 gradient_type;

result_type operator()( domain_type const& x ) const {
return 1.2 * pow(x[0] - 0.6, 2) + 1.7 * pow(x[1] - 0.6, 2) + 2 * x[0] * x[1];
}
gradient_type gradient( domain_type const& x ) const {
return Vector2( 2.4*x[0]-1.44+2*x[1],
3.4*x[1]-2.04+2*x[0]);
}

unsigned dimension() const { return 2; }
};


class TestConjugateGradient : public CxxTest::TestSuite
{
public:

void test_steepest_descent()
{
// set_debug_level(VerboseDebugMessage);
Vector2 initial_guess(2,2);
int numiters = 100;
int max_stepsize = 1;
QuadraticFunction cost_functor;
QuadraticFunction::domain_type result = steepest_descent( cost_functor, initial_guess, ArmijoStepSize(max_stepsize), numiters);
// std::cout << "\n\n" << result << "\n";
// std::cout << cost_functor(result) << "\n";
// std::cout << cost_functor.gradient(result) << "\n";

TS_ASSERT_DELTA(result[0], 0.1962, 0.001);
TS_ASSERT_DELTA(result[1], 0.4846, 0.001);
}

void test_conjugate_gradient()
{
// set_debug_level(VerboseDebugMessage);
Vector2 initial_guess(2,2);
int numiters = 100;
int max_stepsize = 1;
QuadraticFunction cost_functor;
QuadraticFunction::domain_type result = conjugate_gradient( cost_functor, initial_guess, ArmijoStepSize(max_stepsize), numiters);
// std::cout << "\n\n" << result << "\n";
// std::cout << cost_functor(result) << "\n";
// std::cout << cost_functor.gradient(result) << "\n";

TS_ASSERT_DELTA(result[0], 0.1962, 0.001);
TS_ASSERT_DELTA(result[1], 0.4846, 0.001);
}

}; // class TestVector

0 comments on commit 60fafdc

Please sign in to comment.