-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #5851. Adding minimizer properties to fit browser
- Loading branch information
1 parent
0522bd2
commit d105eab
Showing
17 changed files
with
220 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
Code/Mantid/Framework/API/test/FuncMinimizerFactoryTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.