Commit ae0668a1 authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #18312. Use unique_ptr to store ties and constraints.

parent 9ae43918
......@@ -150,10 +150,10 @@ public:
/// Get the tie of i-th parameter
ParameterTie *getTie(size_t i) const override;
/// Add a new tie
void addTie(ParameterTie *tie) override;
void addTie(std::unique_ptr<ParameterTie>tie) override;
/// Overwrite IFunction methods
void addConstraint(IConstraint *ic) override;
void addConstraint(std::unique_ptr<IConstraint> ic) override;
/// Get constraint of i-th parameter
IConstraint *getConstraint(size_t i) const override;
/// Prepare function for a fit
......
......@@ -117,7 +117,7 @@ public:
bool hasAttribute(const std::string &attName) const override;
/// Tie a parameter of decorated function to other parameters (or a constant).
ParameterTie *tie(const std::string &parName, const std::string &expr,
void tie(const std::string &parName, const std::string &expr,
bool isDefault = false) override;
/// Apply the ties in decorated function.
void applyTies() override;
......@@ -130,7 +130,7 @@ public:
ParameterTie *getTie(size_t i) const override;
/// Add a constraint to decorated function.
void addConstraint(IConstraint *ic) override;
void addConstraint(std::unique_ptr<IConstraint> ic) override;
/// Get constraint of i-th parameter of decorated function.
IConstraint *getConstraint(size_t i) const override;
/// Remove a constraint of decorated function.
......@@ -147,7 +147,7 @@ protected:
void declareParameter(const std::string &name, double initValue,
const std::string &description) override;
void addTie(ParameterTie *tie) override;
void addTie(std::unique_ptr<ParameterTie>) override;
virtual void beforeDecoratedFunctionSet(const IFunction_sptr &fn);
void setDecoratedFunctionPrivate(const IFunction_sptr &fn);
......
......@@ -7,7 +7,6 @@
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/FunctionDomain.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidAPI/Jacobian.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/Unit.h"
......@@ -450,7 +449,7 @@ public:
/** @name Ties */
//@{
/// Tie a parameter to other parameters (or a constant)
virtual ParameterTie *tie(const std::string &parName, const std::string &expr,
virtual void tie(const std::string &parName, const std::string &expr,
bool isDefault = false);
/// Add several ties
virtual void addTies(const std::string &ties, bool isDefault = false);
......@@ -465,7 +464,7 @@ public:
/// Get the tie of i-th parameter
virtual ParameterTie *getTie(size_t i) const = 0;
/// Add a new tie. Derived classes must provide storage for ties
virtual void addTie(ParameterTie *tie) = 0;
virtual void addTie(std::unique_ptr<ParameterTie> tie) = 0;
//@}
/** @name Constraints */
......@@ -473,7 +472,7 @@ public:
/// Add a list of conatraints from a string
virtual void addConstraints(const std::string &str, bool isDefault = false);
/// Add a constraint to function
virtual void addConstraint(IConstraint *ic) = 0;
virtual void addConstraint(std::unique_ptr<IConstraint> ic) = 0;
/// Get constraint of i-th parameter
virtual IConstraint *getConstraint(size_t i) const = 0;
/// Remove a constraint
......@@ -583,6 +582,10 @@ protected:
Kernel::ProgressBase *m_progReporter;
private:
/// No copying
IFunction(const IFunction&) = delete;
/// No copying
IFunction& operator=(const IFunction&) = delete;
/// The declared attributes
std::map<std::string, API::IFunction::Attribute> m_attrs;
/// The covariance matrix of the fitting parameters
......
......@@ -5,8 +5,9 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/Unit.h"
#include "MantidAPI/IConstraint.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/ParameterTie.h"
#include <string>
#include <vector>
......@@ -16,8 +17,6 @@ namespace API {
// Forward declaration
//----------------------------------------------------------------------
class Jacobian;
class ParameterTie;
class IConstraint;
/** Implements the part of IFunction interface dealing with parameters. This
function has parameters of its own
as opposed to a CompositeFunction which list of parameters consists only of
......@@ -111,10 +110,10 @@ public:
/// Get the tie of i-th parameter
ParameterTie *getTie(size_t i) const override;
/// Add a new tie
void addTie(ParameterTie *tie) override;
void addTie(std::unique_ptr<ParameterTie> tie) override;
/// Add a constraint to function
void addConstraint(IConstraint *ic) override;
void addConstraint(std::unique_ptr<IConstraint> ic) override;
/// Get constraint of i-th parameter
IConstraint *getConstraint(size_t i) const override;
/// Remove a constraint
......@@ -143,9 +142,9 @@ private:
/// Keeps parameter errors
std::vector<double> m_errors;
/// Holds parameter ties as <parameter index,tie pointer>
std::vector<ParameterTie *> m_ties;
std::vector<std::unique_ptr<ParameterTie>> m_ties;
/// Holds the constraints added to function
std::vector<IConstraint *> m_constraints;
std::vector<std::unique_ptr<IConstraint>> m_constraints;
/// Flags of explicitly set parameters
std::vector<bool> m_explicitlySet;
/// parameter descriptions
......
......@@ -44,6 +44,8 @@ namespace API {
class MANTID_API_DLL ParameterReference {
public:
ParameterReference();
ParameterReference(const ParameterReference&){}
// ParameterReference& operator=(const ParameterReference&) {}
ParameterReference(IFunction *fun, std::size_t index, bool isDefault = false);
std::size_t getIndex() const;
void reset(IFunction *fun, std::size_t index, bool isDefault = false);
......
......@@ -634,10 +634,10 @@ ParameterTie *CompositeFunction::getTie(size_t i) const {
* Attaches a tie to this function. The attached tie is owned by the function.
* @param tie :: A pointer to a new tie
*/
void CompositeFunction::addTie(ParameterTie *tie) {
void CompositeFunction::addTie(std::unique_ptr<ParameterTie> tie) {
size_t i = getParameterIndex(*tie);
size_t iFun = functionIndex(i);
m_functions[iFun]->addTie(tie);
m_functions[iFun]->addTie(std::move(tie));
}
/**
......@@ -659,10 +659,10 @@ void CompositeFunction::declareParameter(const std::string &name,
/** Add a constraint
* @param ic :: Pointer to a constraint.
*/
void CompositeFunction::addConstraint(IConstraint *ic) {
void CompositeFunction::addConstraint(std::unique_ptr<IConstraint> ic) {
size_t i = getParameterIndex(*ic);
size_t iFun = functionIndex(i);
getFunction(iFun)->addConstraint(ic);
getFunction(iFun)->addConstraint(std::move(ic));
}
/**
......
......@@ -278,9 +278,9 @@ void FunctionFactoryImpl::addConstraints(IFunction_sptr fun,
*/
void FunctionFactoryImpl::addConstraint(IFunction_sptr fun,
const Expression &expr) const {
IConstraint *c =
ConstraintFactory::Instance().createInitialized(fun.get(), expr);
fun->addConstraint(c);
auto c = std::unique_ptr<IConstraint>(
ConstraintFactory::Instance().createInitialized(fun.get(), expr));
fun->addConstraint(std::move(c));
}
/**
......
#include "MantidAPI/FunctionParameterDecorator.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/IConstraint.h"
#include "MantidAPI/ParameterReference.h"
#include "MantidAPI/ParameterTie.h"
namespace Mantid {
namespace API {
......@@ -220,12 +222,12 @@ bool FunctionParameterDecorator::hasAttribute(
return m_wrappedFunction->hasAttribute(attName);
}
ParameterTie *FunctionParameterDecorator::tie(const std::string &parName,
void FunctionParameterDecorator::tie(const std::string &parName,
const std::string &expr,
bool isDefault) {
throwIfNoFunctionSet();
return m_wrappedFunction->tie(parName, expr, isDefault);
m_wrappedFunction->tie(parName, expr, isDefault);
}
void FunctionParameterDecorator::applyTies() {
......@@ -258,10 +260,10 @@ ParameterTie *FunctionParameterDecorator::getTie(size_t i) const {
return m_wrappedFunction->getTie(i);
}
void FunctionParameterDecorator::addConstraint(IConstraint *ic) {
void FunctionParameterDecorator::addConstraint(std::unique_ptr<IConstraint> ic) {
throwIfNoFunctionSet();
m_wrappedFunction->addConstraint(ic);
m_wrappedFunction->addConstraint(std::move(ic));
}
IConstraint *FunctionParameterDecorator::getConstraint(size_t i) const {
......@@ -298,10 +300,10 @@ void FunctionParameterDecorator::declareParameter(
}
/// Forwads addTie-call to the decorated function.
void FunctionParameterDecorator::addTie(ParameterTie *tie) {
void FunctionParameterDecorator::addTie(std::unique_ptr<ParameterTie> tie) {
throwIfNoFunctionSet();
m_wrappedFunction->addTie(tie);
m_wrappedFunction->addTie(std::move(tie));
}
/**
......
......@@ -109,18 +109,16 @@ void IFunction::functionDeriv(const FunctionDomain &domain,
* with this reference: a tie or a constraint.
* @return newly ties parameters
*/
ParameterTie *IFunction::tie(const std::string &parName,
void IFunction::tie(const std::string &parName,
const std::string &expr, bool isDefault) {
auto ti = new ParameterTie(this, parName, expr, isDefault);
auto ti = std::make_unique<ParameterTie>(this, parName, expr, isDefault);
this->fix(getParameterIndex(*ti));
if (!isDefault && ti->isConstant()) {
setParameter(parName, ti->eval());
delete ti;
ti = nullptr;
} else {
addTie(ti);
addTie(std::move(ti));
}
return ti;
// return ti.get();
}
/**
......@@ -253,9 +251,9 @@ void IFunction::addConstraints(const std::string &str, bool isDefault) {
list.parse(str);
list.toList();
for (const auto &expr : list) {
IConstraint *c =
ConstraintFactory::Instance().createInitialized(this, expr, isDefault);
this->addConstraint(c);
auto c = std::unique_ptr<IConstraint>(
ConstraintFactory::Instance().createInitialized(this, expr, isDefault));
this->addConstraint(std::move(c));
}
}
......@@ -904,7 +902,7 @@ void IFunction::setMatrixWorkspace(
<< "Can't set penalty factor for constraint\n";
}
}
addConstraint(constraint);
addConstraint(std::unique_ptr<IConstraint>(constraint));
}
}
}
......
......@@ -4,8 +4,6 @@
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include "MantidAPI/ParamFunction.h"
#include "MantidAPI/IConstraint.h"
#include "MantidAPI/ParameterTie.h"
#include <cmath>
#include <limits>
......@@ -19,13 +17,7 @@ Kernel::Logger g_log("ParamFunction");
/// Destructor
ParamFunction::~ParamFunction() {
for (auto &tie : m_ties) {
delete tie;
}
m_ties.clear();
for (auto &constraint : m_constraints) {
delete constraint;
}
m_constraints.clear();
}
......@@ -276,19 +268,18 @@ void ParamFunction::unfix(size_t i) {
* ParamFunction.
* @param tie :: A pointer to a new tie
*/
void ParamFunction::addTie(ParameterTie *tie) {
void ParamFunction::addTie(std::unique_ptr<ParameterTie> tie) {
size_t iPar = tie->getIndex();
bool found = false;
for (auto &m_tie : m_ties) {
if (m_tie->getIndex() == iPar) {
found = true;
delete m_tie;
m_tie = tie;
m_tie = std::move(tie);
break;
}
}
if (!found) {
m_ties.push_back(tie);
m_ties.push_back(std::move(tie));
}
}
......@@ -305,16 +296,16 @@ void ParamFunction::applyTies() {
* Used to find ParameterTie for a parameter i
*/
class ReferenceEqual {
const size_t m_i; ///< index to find
/// index to find
const size_t m_i;
public:
/** Constructor
*/
/// Constructor
explicit ReferenceEqual(size_t i) : m_i(i) {}
/**Bracket operator
* @param p :: the parameter you are looking for
* @return True if found
*/
bool operator()(ParameterReference *p) { return p->getIndex() == m_i; }
/// Bracket operator
/// @param p :: the element you are looking for
/// @return True if found
template<class T>
bool operator()(const std::unique_ptr<T> &p) { return p->getIndex() == m_i; }
};
/** Removes i-th parameter's tie if it is tied or does nothing.
......@@ -327,7 +318,6 @@ bool ParamFunction::removeTie(size_t i) {
}
auto it = std::find_if(m_ties.begin(), m_ties.end(), ReferenceEqual(i));
if (it != m_ties.end()) {
delete *it;
m_ties.erase(it);
unfix(i);
return true;
......@@ -345,7 +335,7 @@ ParameterTie *ParamFunction::getTie(size_t i) const {
}
auto it = std::find_if(m_ties.cbegin(), m_ties.cend(), ReferenceEqual(i));
if (it != m_ties.cend()) {
return *it;
return it->get();
}
return nullptr;
}
......@@ -356,7 +346,6 @@ void ParamFunction::clearTies() {
for (auto &tie : m_ties) {
size_t i = getParameterIndex(*tie);
unfix(i);
delete tie;
}
m_ties.clear();
}
......@@ -364,19 +353,18 @@ void ParamFunction::clearTies() {
/** Add a constraint
* @param ic :: Pointer to a constraint.
*/
void ParamFunction::addConstraint(IConstraint *ic) {
void ParamFunction::addConstraint(std::unique_ptr<IConstraint> ic) {
size_t iPar = ic->getIndex();
bool found = false;
for (auto &constraint : m_constraints) {
if (constraint->getIndex() == iPar) {
found = true;
delete constraint;
constraint = ic;
constraint = std::move(ic);
break;
}
}
if (!found) {
m_constraints.push_back(ic);
m_constraints.push_back(std::move(ic));
}
}
......@@ -391,7 +379,7 @@ IConstraint *ParamFunction::getConstraint(size_t i) const {
auto it = std::find_if(m_constraints.cbegin(), m_constraints.cend(),
ReferenceEqual(i));
if (it != m_constraints.cend()) {
return *it;
return it->get();
}
return nullptr;
}
......@@ -403,7 +391,6 @@ void ParamFunction::removeConstraint(const std::string &parName) {
size_t iPar = parameterIndex(parName);
for (auto it = m_constraints.begin(); it != m_constraints.end(); ++it) {
if (iPar == (**it).getIndex()) {
delete *it;
m_constraints.erase(it);
break;
}
......@@ -418,15 +405,8 @@ void ParamFunction::setUpForFit() {
/// Nonvirtual member which removes all declared parameters
void ParamFunction::clearAllParameters() {
for (auto &tie : m_ties) {
delete tie;
}
m_ties.clear();
for (auto &constraint : m_constraints) {
delete constraint;
}
m_constraints.clear();
m_parameters.clear();
m_parameterNames.clear();
m_parameterDescriptions.clear();
......
......@@ -280,7 +280,8 @@ public:
getFunctionParameterDecoratorGaussian();
IFunction_sptr decoratedFunction = fn->getDecoratedFunction();
ParameterTie *tie = fn->tie("Height", "Height=2.0*Sigma");
fn->tie("Height", "Height=2.0*Sigma");
ParameterTie *tie = fn->getTie(fn->parameterIndex("Height"));
TS_ASSERT(tie);
TS_ASSERT_EQUALS(decoratedFunction->getTie(0), tie);
......
......@@ -333,9 +333,9 @@ bool ConvertEmptyToTof::doFitGaussianPeak(int workspaceindex, double &center,
double centerrightend = center + sigma * 0.5;
std::ostringstream os;
os << centerleftend << " < PeakCentre < " << centerrightend;
auto *centerbound = API::ConstraintFactory::Instance().createInitialized(
gaussianpeak.get(), os.str(), false);
gaussianpeak->addConstraint(centerbound);
auto centerbound = std::unique_ptr<API::IConstraint>(API::ConstraintFactory::Instance().createInitialized(
gaussianpeak.get(), os.str(), false));
gaussianpeak->addConstraint(std::move(centerbound));
g_log.debug("Calling createChildAlgorithm : Fit...");
// 4. Fit
......
......@@ -93,7 +93,7 @@ public:
/// Return parameter index from a parameter reference.
size_t getParameterIndex(const API::ParameterReference &ref) const override;
/// Tie a parameter to other parameters (or a constant)
API::ParameterTie *tie(const std::string &parName, const std::string &expr,
void tie(const std::string &parName, const std::string &expr,
bool isDefault = false) override;
/// Apply the ties
void applyTies() override;
......@@ -107,7 +107,7 @@ public:
API::ParameterTie *getTie(size_t i) const override;
/// Add a constraint to function
void addConstraint(API::IConstraint *ic) override;
void addConstraint(std::unique_ptr<API::IConstraint> ic) override;
/// Get constraint of i-th parameter
API::IConstraint *getConstraint(size_t i) const override;
/// Remove a constraint
......@@ -125,7 +125,7 @@ protected:
const std::string &description = "") override;
/// Add a new tie. Derived classes must provide storage for ties
void addTie(API::ParameterTie *tie) override;
void addTie(std::unique_ptr<API::ParameterTie> tie) override;
//@}
public:
......
......@@ -371,7 +371,7 @@ void FitPowderDiffPeaks::fitPeaksRobust() {
// II. Create local background function.
Polynomial_sptr backgroundfunction =
boost::make_shared<Polynomial>(Polynomial());
boost::make_shared<Polynomial>();
backgroundfunction->setAttributeValue("n", 1);
backgroundfunction->initialize();
......@@ -1106,7 +1106,7 @@ bool FitPowderDiffPeaks::fitSinglePeakSimulatedAnnealing(
void FitPowderDiffPeaks::fitPeaksWithGoodStartingValues() {
// 1. Initialize (local) background function
Polynomial_sptr backgroundfunction =
boost::make_shared<Polynomial>(Polynomial());
boost::make_shared<Polynomial>();
backgroundfunction->setAttributeValue("n", 1);
backgroundfunction->initialize();
......@@ -1256,19 +1256,17 @@ bool FitPowderDiffPeaks::fitSinglePeakConfident(
// a) Peak centre
double peakcentreleftbound = peak->centre() - peak->fwhm();
double peakcentrerightbound = peak->centre() + peak->fwhm();
BoundaryConstraint *x0bc = new BoundaryConstraint(
auto x0bc = std::make_unique<BoundaryConstraint>(
peak.get(), "X0", peakcentreleftbound, peakcentrerightbound);
peak->addConstraint(x0bc);
peak->addConstraint(std::move(x0bc));
// b) A
BoundaryConstraint *abc =
new BoundaryConstraint(peak.get(), "A", 1.0E-10, false);
peak->addConstraint(abc);
auto abc = std::make_unique<BoundaryConstraint>(peak.get(), "A", 1.0E-10, false);
peak->addConstraint(std::move(abc));
// c) B
BoundaryConstraint *bbc =
new BoundaryConstraint(peak.get(), "B", 1.0E-10, false);
peak->addConstraint(bbc);
auto bbc = std::make_unique<BoundaryConstraint>(peak.get(), "B", 1.0E-10, false);
peak->addConstraint(std::move(bbc));
// d) Guessed height
peak->setHeight(maxheight);
......@@ -1524,26 +1522,25 @@ FitPowderDiffPeaks::doFitPeak(Workspace2D_sptr dataws,
double tof_h = peakfunction->centre();
double centerleftend = tof_h - guessedfwhm * 3.0;
double centerrightend = tof_h + guessedfwhm * 3.0;
BoundaryConstraint *centerbound = new BoundaryConstraint(
auto centerbound = std::make_unique<BoundaryConstraint>(
peakfunction.get(), "X0", centerleftend, centerrightend, false);
peakfunction->addConstraint(centerbound);
peakfunction->addConstraint(std::move(centerbound));
g_log.debug() << "[DoFitPeak] Peak Center Boundary = " << centerleftend
<< ", " << centerrightend << '\n';
}
// A > 0, B > 0, S > 0
BoundaryConstraint *abound = new BoundaryConstraint(
auto abound = std::make_unique<BoundaryConstraint>(
peakfunction.get(), "A", 0.0000001, DBL_MAX, false);
peakfunction->addConstraint(abound);
peakfunction->addConstraint(std::move(abound));
BoundaryConstraint *bbound = new BoundaryConstraint(
auto bbound = std::make_unique<BoundaryConstraint>(
peakfunction.get(), "B", 0.0000001, DBL_MAX, false);
peakfunction->addConstraint(bbound);
peakfunction->addConstraint(std::move(bbound));
BoundaryConstraint *sbound =
new BoundaryConstraint(peakfunction.get(), "S", 0.0001, DBL_MAX, false);
peakfunction->addConstraint(sbound);
auto sbound = std::make_unique<BoundaryConstraint>(peakfunction.get(), "S", 0.0001, DBL_MAX, false);
peakfunction->addConstraint(std::move(sbound));
// 2. Unfix all parameters
vector<string> paramnames = peakfunction->getParameterNames();
......@@ -1812,10 +1809,9 @@ bool FitPowderDiffPeaks::doFitGaussianPeak(DataObjects::Workspace2D_sptr dataws,
// b) Constraint
double centerleftend = in_center - leftfwhm * 0.5;
double centerrightend = in_center + rightfwhm * 0.5;
Constraints::BoundaryConstraint *centerbound =
new Constraints::BoundaryConstraint(gaussianpeak.get(), "PeakCentre",
auto centerbound = std::make_unique<BoundaryConstraint>(gaussianpeak.get(), "PeakCentre",
centerleftend, centerrightend, false);
gaussianpeak->addConstraint(centerbound);
gaussianpeak->addConstraint(std::move(centerbound));
// 3. Fit
API::IAlgorithm_sptr fitalg = createChildAlgorithm("Fit", -1, -1, true);
......@@ -2119,9 +2115,9 @@ void FitPowderDiffPeaks::setOverlappedPeaksConstraints(
double leftcentrebound = centre - 0.5 * fwhm;
double rightcentrebound = centre + 0.5 * fwhm;
BoundaryConstraint *bc = new BoundaryConstraint(
auto bc = std::make_unique<BoundaryConstraint>(
thispeak.get(), "X0", leftcentrebound, rightcentrebound, false);
thispeak->addConstraint(bc);
thispeak->addConstraint(std::move(bc));
}
}
......@@ -2727,10 +2723,9 @@ FitPowderDiffPeaks::genPeak(map<string, int> hklmap,
map<string, string> bk2bk2braggmap, bool &good,
vector<int> &hkl, double &d_h) {
// Generate a peak function
BackToBackExponential newpeak;
newpeak.initialize();
BackToBackExponential_sptr newpeakptr =
boost::make_shared<BackToBackExponential>(newpeak);
boost::make_shared<BackToBackExponential>();
newpeakptr->initialize();
// Check miller index (HKL) is a valid value in a miller indexes pool (hklmap)
good = getHKLFromMap(hklmap, hkl);
......