diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IFuncMinimizer.h b/Code/Mantid/Framework/API/inc/MantidAPI/IFuncMinimizer.h index ee45efe672c45976eaa764d737f3332899dae461..e3532a0f53c2ff1e6b002c112f4326dfb03186a8 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/IFuncMinimizer.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/IFuncMinimizer.h @@ -69,6 +69,10 @@ public: /// Get value of cost function virtual double costFunctionVal() = 0; + /// Finalize minimization, eg store additional outputs + virtual void finalize() {} + + protected: /// Error string. std::string m_errorString; diff --git a/Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FABADAMinimizer.h b/Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FABADAMinimizer.h index 9e73e1899086a021bc5519681ecf6f17585aece6..751e195e233676fd8a9f8be9af86c8a123189af1 100644 --- a/Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FABADAMinimizer.h +++ b/Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FABADAMinimizer.h @@ -49,7 +49,8 @@ public: virtual bool iterate(size_t iter); /// Return current value of the cost function virtual double costFunctionVal(); - void finalize(); + /// Finalize minimization, eg store additional outputs + virtual void finalize(); private: /// Pointer to the cost function. Must be the least squares. diff --git a/Code/Mantid/Framework/CurveFitting/src/FABADAMinimizer.cpp b/Code/Mantid/Framework/CurveFitting/src/FABADAMinimizer.cpp index 1d6ddced563056ec55945d5fca264fd322f36fdb..071a288ae706f03d36efc8a127194d8f4621a746 100644 --- a/Code/Mantid/Framework/CurveFitting/src/FABADAMinimizer.cpp +++ b/Code/Mantid/Framework/CurveFitting/src/FABADAMinimizer.cpp @@ -39,6 +39,12 @@ Kernel::Logger g_log("FABADAMinimizer"); const size_t convergenceMaxIterations = 50000; // number of iterations when convergence isn't expected const size_t lowerIterationLimit = 350; +// very large number +const double largeNumber = 1e100; +// jump checking rate +const size_t jumpCheckingRate = 200; +// low jump limit +const double lowJumpLimit = 1e-25; } DECLARE_FUNCMINIMIZER(FABADAMinimizer, FABADA) @@ -118,12 +124,12 @@ void FABADAMinimizer::initialize(API::ICostFunction_sptr function, if (bcon->hasLower()) { m_lower.push_back(bcon->lower()); } else { - m_lower.push_back(-10e100); + m_lower.push_back(-largeNumber); } if (bcon->hasUpper()) { m_upper.push_back(bcon->upper()); } else { - m_upper.push_back(10e100); + m_upper.push_back(largeNumber); } if (p < m_lower[i]) { p = m_lower[i]; @@ -135,8 +141,8 @@ void FABADAMinimizer::initialize(API::ICostFunction_sptr function, } } } else { - m_lower.push_back(-10e100); - m_upper.push_back(10e100); + m_lower.push_back(-largeNumber); + m_upper.push_back(largeNumber); } std::vector<double> v; v.push_back(p); @@ -161,7 +167,7 @@ void FABADAMinimizer::initialize(API::ICostFunction_sptr function, /// Do one iteration. Returns true if iterations to be continued, false if they /// must stop. -bool FABADAMinimizer::iterate(size_t iter) { +bool FABADAMinimizer::iterate(size_t) { if (!m_leastSquares) { throw std::runtime_error("Cost function isn't set up."); @@ -270,8 +276,6 @@ bool FABADAMinimizer::iterate(size_t iter) { } } - const size_t jumpCheckingRate = 200; - const double lowJumpLimit = 1e-25; const double jumpAR = getProperty("JumpAcceptanceRate"); // Update the jump once each jumpCheckingRate iterations @@ -314,8 +318,8 @@ bool FABADAMinimizer::iterate(size_t iter) { } } // for i - m_counter += - 1; // Update the counter, after finishing the iteration for each parameter + // Update the counter, after finishing the iteration for each parameter + m_counter += 1; // Check if Chi square has converged for all the parameters. if (m_counter > lowerIterationLimit && !m_converged) { @@ -362,9 +366,7 @@ bool FABADAMinimizer::iterate(size_t iter) { " iterations.\n Try to set better initial values for parameters: " + failed); } - } - - else { + } else { // If convergence has been reached, continue untill complete the chain // length. if (m_counter <= m_numberIterations) { @@ -376,22 +378,18 @@ bool FABADAMinimizer::iterate(size_t iter) { throw std::length_error("Convegence reached but Max Iterations parameter " "insufficient for creating the whole chain.\n " "Increase Max Iterations"); - return false; - } - - // When the all the iterations have been done, calculate and show all the - // results. - else { - finalize(); - return false; } + // nothing else to do, stop interations + return false; } - + // can we even get here? return true; } double FABADAMinimizer::costFunctionVal() { return m_chi2; } +/// When the all the iterations have been done, calculate and show all the +/// results. void FABADAMinimizer::finalize() { // Creating the reduced chain (considering only one each "Steps between // values" values) @@ -452,9 +450,9 @@ void FABADAMinimizer::finalize() { error_rigth[j] = *pos_right - *pos_par; } - const bool cond1 = !getPropertyValue("Parameters").empty(); + const bool outputParametersTable = !getPropertyValue("Parameters").empty(); - if (cond1) { + if (outputParametersTable) { // Create the workspace for the parameters' value and errors. API::ITableWorkspace_sptr wsPdfE = @@ -546,9 +544,9 @@ void FABADAMinimizer::finalize() { // Set and name the PDF workspace. setProperty("PDF", ws); - const bool cond3 = !getPropertyValue("Chains").empty(); + const bool outputChains = !getPropertyValue("Chains").empty(); - if (cond3) { + if (outputChains) { // Create the workspace for the complete parameters' chain (the last // histogram is for the Chi square). @@ -572,9 +570,9 @@ void FABADAMinimizer::finalize() { // Read if necessary to show the workspace for the converged part of the // chain. - const bool cond4 = !getPropertyValue("ConvergedChain").empty(); + const bool outputConvergedChains = !getPropertyValue("ConvergedChain").empty(); - if (cond4) { + if (outputConvergedChains) { // Create the workspace for the converged part of the chain. API::MatrixWorkspace_sptr wsConv = API::WorkspaceFactory::Instance().create( "Workspace2D", nParams + 1, conv_length, conv_length); @@ -598,9 +596,9 @@ void FABADAMinimizer::finalize() { } // Read if necessary to show the workspace for the Chi square values. - const bool cond5 = !getPropertyValue("CostFunctionTable").empty(); + const bool outputCostFunctionTable = !getPropertyValue("CostFunctionTable").empty(); - if (cond5) { + if (outputCostFunctionTable) { // Create the workspace for the Chi square values. API::ITableWorkspace_sptr wsChi2 = diff --git a/Code/Mantid/Framework/CurveFitting/src/Fit.cpp b/Code/Mantid/Framework/CurveFitting/src/Fit.cpp index b0e1c9b99b332dfb5a691440a0984ed531e29c7f..691c124409b3ae99a5e9cba7499b0da0d525bcd7 100644 --- a/Code/Mantid/Framework/CurveFitting/src/Fit.cpp +++ b/Code/Mantid/Framework/CurveFitting/src/Fit.cpp @@ -445,6 +445,8 @@ void Fit::exec() { } g_log.debug() << "Number of minimizer iterations=" << iter << "\n"; + minimizer->finalize(); + if (iter >= maxIterations) { if (!errorString.empty()) { errorString += '\n';