15#include <boost/lexical_cast.hpp>
21Kernel::Logger
g_log(
"BoundaryConstraint");
30 :
API::
IConstraint(), m_penaltyFactor(getDefaultPenaltyFactor()), m_hasLowerBound(false), m_hasUpperBound(false),
31 m_lowerBound(DBL_MAX), m_upperBound(-DBL_MAX) {}
36 :
API::
IConstraint(), m_penaltyFactor(getDefaultPenaltyFactor()), m_hasLowerBound(false), m_hasUpperBound(false) {
50 const double upperBound,
bool isDefault)
51 : m_penaltyFactor(getDefaultPenaltyFactor()), m_hasLowerBound(true), m_hasUpperBound(true),
52 m_lowerBound(lowerBound), m_upperBound(upperBound) {
58 : m_penaltyFactor(getDefaultPenaltyFactor()), m_hasLowerBound(true), m_hasUpperBound(false),
59 m_lowerBound(lowerBound), m_upperBound(-DBL_MAX) {
73 if (expr.
size() < 2 || expr.
name() !=
"==") {
74 g_log.
error(
"Wrong initialization expression");
75 throw std::invalid_argument(
"Wrong initialization expression");
80 std::vector<double> values(3);
84 for (
size_t i = 0; i < terms.
size(); i++) {
85 std::string name = terms[i].
str();
87 auto d = boost::lexical_cast<double>(name);
93 ilow =
static_cast<int>(i);
94 }
else if (op[0] ==
'>') {
95 ihi =
static_cast<int>(i);
97 g_log.
error(
"Unknown operator in initialization expression");
98 throw std::invalid_argument(
"Unknown operator in initialization expression");
103 ihi =
static_cast<int>(i);
104 }
else if (op[0] ==
'>') {
105 ilow =
static_cast<int>(i);
107 g_log.
error(
"Unknown operator in initialization expression");
108 throw std::invalid_argument(
"Unknown operator in initialization expression");
111 }
catch (boost::bad_lexical_cast &) {
112 if (!parName.empty()) {
114 throw std::invalid_argument(
"Non-numeric value for a bound");
124 g_log.
error() <<
"Parameter " << parName <<
" not found in function " << fun->
name() <<
'\n';
142 g_log.
warning() <<
"Penalty factor <= 0 selected for boundary constraint."
143 <<
" Only positive penalty factor allowed. Penalty factor set to 1";
154 <<
" this constraint serves no purpose!";
170 <<
" this constraint serves no purpose!";
176 double penalty = 0.0;
193 double penalty = 0.0;
219 double penalty = 0.0;
241 std::ostringstream ostr;
#define DECLARE_CONSTRAINT(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
This class represents an expression made up of names, binary operators and brackets.
std::string name() const
Returns the name of the expression which is a function or variable name.
std::string operator_name() const
Returns the the expression's binary operator on its left.
std::string str() const
Returns this expression as a string.
size_t size() const
Returns the number of argumens.
An interface to a constraint.
static double getDefaultPenaltyFactor()
Return the value for default fitting penalties.
This is an interface to a fitting function - a semi-abstarct class.
virtual std::string name() const =0
Returns the function's name.
virtual size_t parameterIndex(const std::string &name) const =0
Returns the index of parameter name.
bool isDefault() const
Returns the default value flag.
double getParameter() const
Get the value of the parameter.
void reset(IFunction *fun, std::size_t index, bool isDefault=false)
Reset the reference.
void setParameter(const double &value, bool isExplicitlySet=true)
Set the parameter.
std::string parameterName() const
Return parameter name in the owning function.
bool m_hasLowerBound
name of parameter you want to constraint
void setLower(const double &value)
Set lower bound value.
double m_lowerBound
the lower bound
bool m_hasUpperBound
has a upper bound set true/false
double m_penaltyFactor
Penalty factor for the given boundary constraint.
void initialize(API::IFunction *fun, const API::Expression &expr, bool isDefault) override
Initialize the constraint from an expression.
BoundaryConstraint()
Default constructor.
std::string asString() const override
Return the string that can be used in this->initialize() to recreate this constraint.
double check() override
Get parameter name.
double checkDeriv2() override
Returns the derivative of the penalty for each active parameter.
void setPenaltyFactor(const double &c) override
implement IConstraint virtual functions
void setParamToSatisfyConstraint() override
Set the parameters of IFitFunction to satisfy constraint.
void clearBounds()
Clear both bounds (lower and upper) at the same time.
void setUpper(const double &value)
Set upper bound value.
double checkDeriv() override
Returns the derivative of the penalty for each active parameter.
double m_upperBound
the upper bound
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
Kernel::Logger g_log("ExperimentInfo")
static logger object