Skip to content

Commit

Permalink
Re #5851. Adding minimizer properties to fit browser
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Sep 14, 2012
1 parent 0522bd2 commit d105eab
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 102 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ set ( TEST_FILES
FileFinderTest.h
FilePropertyTest.h
FrameworkManagerTest.h
FuncMinimizerFactoryTest.h
FunctionDomainTest.h
FunctionFactoryTest.h
FunctionPropertyTest.h
Expand Down
22 changes: 14 additions & 8 deletions Code/Mantid/Framework/API/inc/MantidAPI/FuncMinimizerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ namespace API
class MANTID_API_DLL FuncMinimizerFactoryImpl : public Kernel::DynamicFactory<IFuncMinimizer>
{
public:
/**Creates an instance of a function
* @param type :: The function's type
* @return A pointer to the created function
*/
IFuncMinimizer* createFunction(const std::string& type) const;

/// Creates an instance of a minimizer
boost::shared_ptr<IFuncMinimizer> createMinimizer(const std::string& type) const;

private:
friend struct Mantid::Kernel::CreateUsingNew<FuncMinimizerFactoryImpl>;
Expand All @@ -71,11 +67,21 @@ namespace API
///Forward declaration of a specialisation of SingletonHolder for AlgorithmFactoryImpl (needed for dllexport/dllimport) and a typedef for it.
#ifdef _WIN32
// this breaks new namespace declaraion rules; need to find a better fix
template class Mantid::Kernel::SingletonHolder<FuncMinimizerFactoryImpl>;
template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<FuncMinimizerFactoryImpl>;
#endif /* _WIN32 */
typedef Mantid::Kernel::SingletonHolder<FuncMinimizerFactoryImpl> FuncMinimizerFactory;
typedef MANTID_API_DLL Mantid::Kernel::SingletonHolder<FuncMinimizerFactoryImpl> FuncMinimizerFactory;

} // namespace API
} // namespace Mantid

/**
* Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory
*/
#define DECLARE_FUNCMINIMIZER(classname,username) \
namespace { \
Mantid::Kernel::RegistrationHelper register_funcminimizer_##classname( \
((Mantid::API::FuncMinimizerFactory::Instance().subscribe<classname>(#username)) \
, 0)); \
}

#endif /*MANTID_API_FUNCMINIMIZERFACTORY_H_*/
11 changes: 0 additions & 11 deletions Code/Mantid/Framework/API/inc/MantidAPI/IFuncMinimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidAPI/ICostFunction.h"
#include "MantidAPI/FuncMinimizerFactory.h"

namespace Mantid
{
Expand Down Expand Up @@ -76,14 +75,4 @@ typedef boost::shared_ptr<IFuncMinimizer> IFuncMinimizer_sptr;
} // namespace API
} // namespace Mantid

/**
* Macro for declaring a new type of minimizers to be used with the FuncMinimizerFactory
*/
#define DECLARE_FUNCMINIMIZER(classname,username) \
namespace { \
Mantid::Kernel::RegistrationHelper register_funcminimizer_##classname( \
((Mantid::API::FuncMinimizerFactory::Instance().subscribe<classname>(#username)) \
, 0)); \
}

#endif /*MANTID_API_IFUNCMINIMIZER_H_*/
60 changes: 52 additions & 8 deletions Code/Mantid/Framework/API/src/FuncMinimizerFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IFuncMinimizer.h"
#include "MantidAPI/Expression.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/LibraryManager.h"

#include <stdexcept>

namespace Mantid
{
namespace API
namespace API
{

FuncMinimizerFactoryImpl::FuncMinimizerFactoryImpl() : Kernel::DynamicFactory<IFuncMinimizer>(), g_log(Kernel::Logger::get("FuncMinimizerFactory"))
{
// we need to make sure the library manager has been loaded before we
// are constructed so that it is destroyed after us and thus does
// not close any loaded DLLs with loaded algorithms in them
Mantid::Kernel::LibraryManager::Instance();
g_log.debug() << "FuncMinimizerFactory created." << std::endl;
}

/**
* Creates an instance of a minimizer
* @param str :: The minimizer initialization string which includes its type
* and optionally properties: "type,prop1=value1,prop2=value2"
* @return A pointer to the created minimizer
*/
boost::shared_ptr<IFuncMinimizer> FuncMinimizerFactoryImpl::createMinimizer(const std::string& str) const
{
Expression parser;
parser.parse( str );
parser.toList(); // make it a list even if it has 1 item
const size_t n = parser.size();
if ( n == 0 )
{
std::string mess = "Found empty initialization string";
g_log.error(mess);
throw std::invalid_argument(mess);
}

// create the minimizer from the type which is
const std::string type = parser[0].str();
auto minimizer = create( type );

FuncMinimizerFactoryImpl::FuncMinimizerFactoryImpl() : Kernel::DynamicFactory<IFuncMinimizer>(), g_log(Kernel::Logger::get("FuncMinimizerFactory"))
// set the properties if there are any
for(size_t i = 1; i < n; ++i)
{
auto& param = parser[i];
if ( param.size() == 2 && param.name() == "=" )
{
// we need to make sure the library manager has been loaded before we
// are constructed so that it is destroyed after us and thus does
// not close any loaded DLLs with loaded algorithms in them
Mantid::Kernel::LibraryManager::Instance();
g_log.debug() << "FuncMinimizerFactory created." << std::endl;
const std::string parName = param[0].str();
if ( minimizer->existsProperty( parName ) )
{
minimizer->setPropertyValue( parName, param[1].str() );
}
}
}

return minimizer;
}


} // namespace API
} // namespace API
} // namespace Mantid
100 changes: 100 additions & 0 deletions Code/Mantid/Framework/API/test/FuncMinimizerFactoryTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#ifndef FUNCMINIMIZERFACTORYTEST_H_
#define FUNCMINIMIZERFACTORYTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IFuncMinimizer.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IFunction.h"
#include "MantidKernel/System.h"

#include <sstream>

using namespace Mantid;
using namespace Mantid::API;

class FuncMinimizerFactoryTest_A: public IFuncMinimizer
{
int m_attr;
public:
FuncMinimizerFactoryTest_A()
{
declareProperty("paramA",0.0);
declareProperty("paramB",0.0);
}

/// Overloading base class methods
std::string name()const {return "Boevs";}
bool iterate() {return true;}
int hasConverged() {return 101;}
double costFunctionVal() {return 5.0;}
void initialize(API::ICostFunction_sptr)
{
}
};

DECLARE_FUNCMINIMIZER(FuncMinimizerFactoryTest_A, nedtur);


class FuncMinimizerFactoryTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FuncMinimizerFactoryTest *createSuite() { return new FuncMinimizerFactoryTest(); }
static void destroySuite( FuncMinimizerFactoryTest *suite ) { delete suite; }

FuncMinimizerFactoryTest()
{
Mantid::API::FrameworkManager::Instance();
}

void testCreateFunction()
{
IFuncMinimizer* minimizerA = FuncMinimizerFactory::Instance().createUnwrapped("nedtur");
TS_ASSERT(minimizerA);
TS_ASSERT(minimizerA->name().compare("Boevs") == 0);

delete minimizerA;
}

void test_createMinimizer_setAllProperties()
{
auto minimizer = FuncMinimizerFactory::Instance().createMinimizer("nedtur, paramA= 3.14, paramB = 2.73");
TS_ASSERT( minimizer );
TS_ASSERT( minimizer->existsProperty("paramA") );
TS_ASSERT( minimizer->existsProperty("paramB") );
double a = minimizer->getProperty("paramA");
double b = minimizer->getProperty("paramB");
TS_ASSERT_EQUALS( a, 3.14 );
TS_ASSERT_EQUALS( b, 2.73 );
}

void test_createMinimizer_defaultPropeties()
{
auto minimizer = FuncMinimizerFactory::Instance().createMinimizer("nedtur");
TS_ASSERT( minimizer );
TS_ASSERT( minimizer->existsProperty("paramA") );
TS_ASSERT( minimizer->existsProperty("paramB") );
double a = minimizer->getProperty("paramA");
double b = minimizer->getProperty("paramB");
TS_ASSERT_EQUALS( a, 0.0 );
TS_ASSERT_EQUALS( b, 0.0 );
}

void test_createMinimizer_setOneProperty()
{
auto minimizer = FuncMinimizerFactory::Instance().createMinimizer("nedtur, paramB = 2.73");
TS_ASSERT( minimizer );
TS_ASSERT( minimizer->existsProperty("paramA") );
TS_ASSERT( minimizer->existsProperty("paramB") );
double a = minimizer->getProperty("paramA");
double b = minimizer->getProperty("paramB");
TS_ASSERT_EQUALS( a, 0.0 );
TS_ASSERT_EQUALS( b, 2.73 );
}

};

#endif /*FUNCMINIMIZERFACTORYTEST_H_*/
1 change: 0 additions & 1 deletion Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ set ( TEST_FILES
FRConjugateGradientTest.h
FitMWTest.h
FlatBackgroundTest.h
FuncMinimizerFactoryTest.h
FunctionFactoryConstraintTest.h
GSLMatrixTest.h
GausDecayTest.h
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/BFGS_Minimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/BFGS_Minimizer.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Logger.h"

namespace Mantid
Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/CurveFitting/src/DampingMinimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/DampingMinimizer.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidCurveFitting/CostFuncLeastSquares.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IFunction.h"

#include "MantidKernel/Logger.h"

#include <boost/lexical_cast.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/FRConjugateGradientMinimizer.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Logger.h"

namespace Mantid
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ namespace CurveFitting

// get the minimizer
std::string minimizerName = getPropertyValue("Minimizer");
API::IFuncMinimizer_sptr minimizer = API::FuncMinimizerFactory::Instance().create(minimizerName);
API::IFuncMinimizer_sptr minimizer = API::FuncMinimizerFactory::Instance().createMinimizer(minimizerName);

// Try to retrieve optional properties
const int maxIterations = getProperty("MaxIterations");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/LevenbergMarquardtMDMinimizer.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidCurveFitting/CostFuncLeastSquares.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IFunction.h"

#include "MantidKernel/Logger.h"

//#include "MantidAPI/FunctionDomain1D.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/LevenbergMarquardtMinimizer.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidCurveFitting/CostFuncLeastSquares.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/System.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/PRConjugateGradientMinimizer.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Logger.h"

namespace Mantid
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/SimplexMinimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//----------------------------------------------------------------------
#include "MantidCurveFitting/SimplexMinimizer.h"
#include "MantidCurveFitting/CostFuncFitting.h"

#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Logger.h"

namespace Mantid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/SteepestDescentMinimizer.h"

#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"

#include "MantidKernel/Logger.h"

namespace Mantid
Expand Down

0 comments on commit d105eab

Please sign in to comment.