Skip to content

Commit

Permalink
Re #5851. Added StartsWithValidator for Minimizer property
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Sep 14, 2012
1 parent d105eab commit 1b42ddc
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 15 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ set ( INC_FILES
inc/MantidCurveFitting/SimplexMinimizer.h
inc/MantidCurveFitting/SpecialFunctionSupport.h
inc/MantidCurveFitting/SplineBackground.h
inc/MantidCurveFitting/StartsWithValidator.h
inc/MantidCurveFitting/StaticKuboToyabe.h
inc/MantidCurveFitting/SteepestDescentMinimizer.h
inc/MantidCurveFitting/StretchExp.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DLLExport LevenbergMarquardtMinimizer : public API::IFuncMinimizer
public:
/// constructor and destructor
~LevenbergMarquardtMinimizer();
LevenbergMarquardtMinimizer(){}
LevenbergMarquardtMinimizer():m_data(NULL),m_gslSolver(NULL){}

/// Overloading base class methods
/// Name of the minimizer.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef MANTID_KERNEL_STARTSWITHVALIDATOR_H_
#define MANTID_KERNEL_STARTSWITHVALIDATOR_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/DllConfig.h"
#include "MantidKernel/ListValidator.h"
#include <boost/lexical_cast.hpp>
#include <vector>

namespace Mantid
{
namespace CurveFitting
{
/** StartsWithValidator is a validator that requires the value of a property to start with of one
of the strings in a defined list of possibilities.
Copyright &copy; 2008-9 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class StartsWithValidator : public Kernel::StringListValidator
{
public:
/**
* Constructor.
* @param values :: A vector with the allowed values.
*/
StartsWithValidator(const std::vector<std::string>& values):Kernel::StringListValidator(values){}
/**
* Constructor.
* @param values :: A set with the allowed values.
*/
StartsWithValidator(const std::set<std::string>& values):Kernel::StringListValidator(values){}
protected:
/** Checks if the string passed is in the list
* @param value :: The value to test
* @return "" if the value is on the list, or "The value does not start with any of the allowed values"
*/
std::string checkValidity(const std::string & value) const
{
for(auto it = m_allowedValues.begin(); it != m_allowedValues.end(); ++it )
{
if ( value.substr(0, it->size()) == *it )
{
return "";
}
}
if ( isEmpty(value) ) return "Select a value";
std::ostringstream os;
os << "The value \"" << value << "\" does not start with any of the allowed values";
return os.str();
}

};

} // namespace CurveFitting
} // namespace Mantid

#endif /*MANTID_KERNEL_STARTSWITHVALIDATOR_H_*/
7 changes: 4 additions & 3 deletions Code/Mantid/Framework/CurveFitting/src/Fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "MantidCurveFitting/CostFuncFitting.h"
#include "MantidCurveFitting/FitMW.h"
#include "MantidCurveFitting/MultiDomainCreator.h"
#include "MantidCurveFitting/StartsWithValidator.h"

#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/IFuncMinimizer.h"
Expand Down Expand Up @@ -109,7 +110,7 @@ namespace CurveFitting
auto it = std::find(minimizerOptions.begin(), minimizerOptions.end(), "Levenberg-Marquardt");
minimizerOptions.erase( it );
}
minimizerProperty->replaceValidator( Kernel::IValidator_sptr(new Kernel::ListValidator<std::string>(minimizerOptions)) );
minimizerProperty->replaceValidator( Kernel::IValidator_sptr(new StartsWithValidator(minimizerOptions)) );
}

void Fit::setFunction()
Expand Down Expand Up @@ -296,8 +297,8 @@ namespace CurveFitting
std::vector<std::string> minimizerOptions = API::FuncMinimizerFactory::Instance().getKeys();

declareProperty("Minimizer","Levenberg-Marquardt",
Kernel::IValidator_sptr(new Kernel::ListValidator<std::string>(minimizerOptions)),
"The minimizer method applied to do the fit, default is Levenberg-Marquardt", Kernel::Direction::InOut);
Kernel::IValidator_sptr(new StartsWithValidator(minimizerOptions)),
"The minimizer method applied to do the fit, default is Levenberg-Marquardt");

std::vector<std::string> costFuncOptions = API::CostFunctionFactory::Instance().getKeys();
// select only CostFuncFitting variety
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ void LevenbergMarquardtMinimizer::initialize(API::ICostFunction_sptr costFunctio

LevenbergMarquardtMinimizer::~LevenbergMarquardtMinimizer()
{
delete m_data;
gsl_multifit_fdfsolver_free(m_gslSolver);
if ( m_data )
{
delete m_data;
}
if ( m_gslSolver )
{
gsl_multifit_fdfsolver_free(m_gslSolver);
}
}

bool LevenbergMarquardtMinimizer::iterate()
Expand Down
17 changes: 14 additions & 3 deletions Code/Mantid/Framework/CurveFitting/src/SimplexMinimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ double SimplexMinimizer::fun(const gsl_vector * x, void *params)

SimplexMinimizer::SimplexMinimizer():
m_size(1.0),
m_simplexStepSize(NULL),
m_startGuess(NULL),
m_gslSolver(NULL)
{
}
Expand Down Expand Up @@ -116,9 +118,18 @@ SimplexMinimizer::~SimplexMinimizer()
/// clear memory
void SimplexMinimizer::clearMemory()
{
gsl_vector_free(m_simplexStepSize);
gsl_vector_free(m_startGuess);
gsl_multimin_fminimizer_free(m_gslSolver);
if ( m_simplexStepSize )
{
gsl_vector_free(m_simplexStepSize);
}
if ( m_startGuess )
{
gsl_vector_free(m_startGuess);
}
if ( m_gslSolver )
{
gsl_multimin_fminimizer_free(m_gslSolver);
}
}

double SimplexMinimizer::costFunctionVal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ListValidator : public TypedValidator<TYPE>
{
m_allowedValues.insert(value);
}
private:
protected:
/** Checks if the string passed is in the list
* @param value :: The value to test
* @return "" if the value is on the list, or "The value is not in the list of allowed values"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS FitPropertyBrowser: public QDockWidget,
/// Set the output name
void setOutputName(const std::string&);
/// Get the minimizer
std::string minimizer()const;
std::string minimizer(bool withProperties = false)const;
/// Get the cost function
std::string costFunction()const;

Expand Down
16 changes: 12 additions & 4 deletions Code/Mantid/MantidQt/MantidWidgets/src/FitPropertyBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,19 @@ void FitPropertyBrowser::setOutputName(const std::string& name)
}

/// Get the minimizer
std::string FitPropertyBrowser::minimizer()const
std::string FitPropertyBrowser::minimizer(bool withProperties)const
{
int i = m_enumManager->value(m_minimizer);
return m_minimizers[i].toStdString();
QString minimStr = m_minimizers[i];
// append minimizer properties as name=value pairs
if ( withProperties )
{
foreach(QtProperty* prop,m_minimizerProperties)
{
minimStr += "," + prop->propertyName() + "=" + QString::number( m_doubleManager->value(prop) );
}
}
return minimStr.toStdString();
}

/// Get the cost function
Expand Down Expand Up @@ -1443,7 +1452,7 @@ void FitPropertyBrowser::fit()
alg->setProperty("StartX",startX());
alg->setProperty("EndX",endX());
alg->setPropertyValue("Output",outputName());
alg->setPropertyValue("Minimizer",minimizer());
alg->setPropertyValue("Minimizer",minimizer(true));
alg->setPropertyValue("CostFunction",costFunction());
observeFinish(alg);
alg->executeAsync();
Expand Down Expand Up @@ -2931,7 +2940,6 @@ void FitPropertyBrowser::columnChanged(QtProperty* prop)
*/
void FitPropertyBrowser::minimizerChanged()
{
std::cerr << typeid(Mantid::API::FuncMinimizerFactory::Instance()).name() << std::endl;
// delete old minimizer properties
foreach(QtProperty* prop,m_minimizerProperties)
{
Expand Down

0 comments on commit 1b42ddc

Please sign in to comment.