A wrapper or manager for an RealMappedSPnode tree to be used for function estimation. More...
Public Member Functions | |||||||
FunctionEstimatorReal (const ivector &v, const MappedFobj &f, int lab=0) | |||||||
Initialised constructor. | |||||||
FunctionEstimatorReal (const SPnode &spn, const MappedFobj &f, int lab=0) | |||||||
Initialised constructor. | |||||||
FunctionEstimatorReal (const FunctionEstimatorReal &other) | |||||||
Copy constructor. | |||||||
~FunctionEstimatorReal () | |||||||
Destructor. | |||||||
const MappedFobj & | getFobjReference () const | ||||||
Get the reference to the function object used by this. | |||||||
int | getLabel () const | ||||||
Get the label. | |||||||
void | setLabel (int lab) | ||||||
Set the label. | |||||||
bool | hasSubPaving () const | ||||||
Get whether this has a subpaving to manage. | |||||||
bool | hasNegativeFunctionEstimates () const | ||||||
Get whether the subpaving managed by this has negative range values. | |||||||
cxsc::ivector | getRootBox () const | ||||||
Get the box of the subpaving managed by this. | |||||||
int | getDimensions () const | ||||||
get the dimensions of the subpaving this manages. | |||||||
cxsc::real | getDomainVolume () const | ||||||
get volume of the root box of the subpaving this manages. | |||||||
size_t | getRootLeaves () const | ||||||
Gets number of leaf nodes in the root paving. | |||||||
IntVec | getLeafLevels () const | ||||||
std::string | getLeafLevelsString () const | ||||||
subpavings::PiecewiseConstantFunction | makePiecewiseConstantFunction () const | ||||||
Make a PiecewiseConstantFunction out of this estimator. | |||||||
void | bruteForceEstimate (cxsc::real tolerance) | ||||||
Create an estimate of the function described by fobj using a depth-first brute force approach. | |||||||
bool | splitToShape (std::string instruction) | ||||||
Split an estimator to a specified shape. | |||||||
cxsc::real | getTotalIntegralOfRealEstimate () const | ||||||
Get the total integral of the function as estimated by this. | |||||||
std::ostream & | outputToStreamTabs (std::ostream &os, int prec=5) const | ||||||
Output the subpaving managed by this to a given stream. | |||||||
Output the subpaving managed by this to a txt file. | |||||||
Format is a tab-delimited file of numeric data starting with nodeName, then the node box volume, then the node counter, then the description of the node box as a tab-delimited list of interval upper and lower bounds.
| |||||||
void | outputToTxtTabs (const std::string &s, int prec=5) const | ||||||
void | outputToTxtTabs (const std::string &s, int prec, bool confirm) const | ||||||
Output details of full sample (from root) to txt file. | |||||||
Format is a mixture of alpha and numeric data.
| |||||||
void | outputRootToTxt (const std::string &s, int prec=5) const | ||||||
void | outputRootToTxt (const std::string &s, int prec, bool confirm) const | ||||||
Output all nodes of the subpaving managed by this to | |||||||
Format is a tab-delimited data giving details of all nodes.
| |||||||
std::ostream & | outputRootToStreamTabs (std::ostream &os, int prec=5) const | ||||||
void | outputLog (const std::string &s, int i, int prec=5) const | ||||||
Append current state of estimator to a txt log file. | |||||||
std::string | stringSummary () const | ||||||
Get a string summary of this estimator's properties. |
A wrapper or manager for an RealMappedSPnode tree to be used for function estimation.
FunctionEstimatorReal::FunctionEstimatorReal | ( | const ivector & | v, |
const MappedFobj & | f, | ||
int | lab = 0 |
||
) |
Initialised constructor.
Initialised with domain box.
Throws a MalconstructedBox_Error if the box is not suitable as the basis of a subpaving (eg, box has no dimensions, or the box has a thin interval on at least one dimension).
Ideal constructor when the support domain of the function is set a priori.
v | The box to use for the subpaving to be managed. |
f | The function to be estimated by this. |
lab | The label for this (defaults to 0). |
: rootPaving(NULL), fobj(f), label(lab) { try { // check the box here if (!checkBox(v)) { throw subpavings::MalconstructedBox_Error( "FunctionEstimatorReal::FunctionEstimatorReal(const ivector&, const MappedFobj&, int lab)"); } rootPaving = new RealMappedSPnode(v); RealEstimator estimator(fobj); rootPaving->acceptSPValueVisitor(estimator); } catch (exception const& e) { constructor_error_handler(); } }
FunctionEstimatorReal::FunctionEstimatorReal | ( | const SPnode & | spn, |
const MappedFobj & | f, | ||
int | lab = 0 |
||
) |
Initialised constructor.
Initialised with a subpaving.
spn | A subpaving to copy as the subpaving to be managed. |
f | The function to be estimated by this. |
lab | The label for this (defaults to 0). |
: rootPaving(NULL), fobj(f), label(lab) { try { // check spn has box if (spn.isEmpty()) { throw subpavings::NoBox_Error( "FunctionEstimatorReal::FunctionEstimatorReal(const SPnode&, MappedFobj&, int lab"); } rootPaving = new RealMappedSPnode(spn); RealEstimator estimator(fobj); rootPaving->acceptSPValueVisitor(estimator); } catch (exception const& e) { constructor_error_handler(); } }
void FunctionEstimatorReal::bruteForceEstimate | ( | cxsc::real | tolerance | ) |
Create an estimate of the function described by fobj using a depth-first brute force approach.
The over all aim to create an estimate of the described by fobj such that each of the leaf nodes of the subpaving managed by this has a box associated with it that meets the interval image tolerance requirement specified by tolerance.
The interval image tolerance requirement is that the diameter of the interval image of the box associated with any leaf node of of the subpaving managed by this should be less than or equal to the tolerance.
But in some cases this can result in boxes being split beyond the limits of the real number screen. If a split of a leaf node that does not meet the interval image tolerance requirement would result in the child nodes' volumes being < the value of the cxsc::MinReal (the smallest representable real number) then that leaf will not be split.
This means that after the operation, the subpaving managed by this may have leaf nodes where the interval image tolerance requirement is not met.
This operation works depth first, ie working from a leaf root it will successively split the root then the root's left child and that child's, left child, etc. It will only start splitting the right child of any node already split when all the descendents of the left child meet the interval image tolerance requirement (or they cannot be split further). This may cause the machine to run out of memory before the process is completed.
tolerance | describes the tolerance to be used in making the estimate. |
{ string errorMsg( "FunctionEstimatorReal::bruteForceEstimate(cxsc::real)"); if (!hasSubPaving()) { throw NullSubpavingPointer_Error(errorMsg); } RealExpanderEstimator estimator(fobj, tolerance); getSubPaving()->acceptSPExpandVisitor(estimator); }
int FunctionEstimatorReal::getDimensions | ( | ) | const |
get the dimensions of the subpaving this manages.
{ int retValue = 0; if (hasSubPaving()) { retValue = getSubPaving()->getDimension(); } return retValue; }
cxsc::real FunctionEstimatorReal::getDomainVolume | ( | ) | const |
get volume of the root box of the subpaving this manages.
{ real retValue(0.0); if (hasSubPaving()) { retValue = getSubPaving()->nodeRealVolume(); } return retValue; }
const MappedFobj & FunctionEstimatorReal::getFobjReference | ( | ) | const |
Get the reference to the function object used by this.
{return fobj;}
int FunctionEstimatorReal::getLabel | ( | ) | const |
Get the label.
{return label;}
IntVec FunctionEstimatorReal::getLeafLevels | ( | ) | const |
Get a vector of the leaf node levels.
Root is level 0, next level down is 1, etc.
{ IntVec levels; // empty container if (hasSubPaving()) { getSubPaving()->getLeafNodeLevels(levels, 0); //levels has now been filled in } return levels; }
std::string FunctionEstimatorReal::getLeafLevelsString | ( | ) | const |
Get a string of the leaf node levels.
Root is level 0, next level down is 1, etc. Example return string "3,3,2,1"
{ string retValue = ""; if (hasSubPaving()) retValue = getSubPaving()->getLeafNodeLevelsString(); return retValue; }
cxsc::ivector FunctionEstimatorReal::getRootBox | ( | ) | const |
Get the box of the subpaving managed by this.
{ if (!hasSubPaving()) { throw NullSubpavingPointer_Error( "FunctionEstimatorReal::getRootBox()"); } return getSubPaving()->getBox(); }
size_t FunctionEstimatorReal::getRootLeaves | ( | ) | const |
Gets number of leaf nodes in the root paving.
Throws NullSubpavingPointer_Error is the subpaving that this manages is a NULL pointer.
{ if (!hasSubPaving()) { throw NullSubpavingPointer_Error("FunctionEstimatorReal::getRootLeaves()"); } return getSubPaving()->getNumberLeaves(); }
cxsc::real FunctionEstimatorReal::getTotalIntegralOfRealEstimate | ( | ) | const |
Get the total integral of the function as estimated by this.
The integral is calculated as the sum over all the leaves of the subpaving managed by this of the absolute value of the real range on the leaf multiplied by the volume of the box represented by the leaf.
{ if (!hasSubPaving()) { throw NullSubpavingPointer_Error( "FunctionEstimatorReal::getTotalIntegralOfRealEstimate)"); } return getSubPaving()->getTotalAbsLeafAreaRangeWithBox(); }
bool FunctionEstimatorReal::hasNegativeFunctionEstimates | ( | ) | const |
Get whether the subpaving managed by this has negative range values.
{ if (!hasSubPaving()) { throw NullSubpavingPointer_Error( "FunctionEstimatorReal::hasNegativeFunctionEstimates()"); } return getSubPaving()->hasNegativeRangeInTree(); }
bool FunctionEstimatorReal::hasSubPaving | ( | ) | const |
Get whether this has a subpaving to manage.
{
return ( getSubPaving() != NULL );
}
subpavings::PiecewiseConstantFunction FunctionEstimatorReal::makePiecewiseConstantFunction | ( | ) | const |
Make a PiecewiseConstantFunction out of this estimator.
{ return PiecewiseConstantFunction(*getSubPaving(), getLabel()); }
void FunctionEstimatorReal::outputLog | ( | const std::string & | s, |
int | i, | ||
int | prec = 5 |
||
) | const |
Append current state of estimator to a txt log file.
Format is a tab-delimited file of numeric data. Output includes node contributions to unscaled EMP under COPERR and AIC and the changes in EMP that would result from splitting the node.
s | the name of the txt file to send output to. |
i | the number of pass (ie, 0, 1, 2, 3 etc) in process. |
prec | the precision for output formatting. ie, number of decimal places. |
{ // To add output of the FunctionEstimatorReal object to file ofstream os(s.c_str(), ios::app); // append if (os.is_open()) { os << std::endl; os << "Pass " << i << std::endl; // numbering getSubPaving()->leavesOutputTabs(os, prec); // the output os.close(); } else { std::cerr << "Error: could not open file named " << s << std::endl << std::endl; } }
std::ostream & FunctionEstimatorReal::outputToStreamTabs | ( | std::ostream & | os, |
int | prec = 5 |
||
) | const |
Output the subpaving managed by this to a given stream.
Format is a tab-delimited data giving details of leaf nodes.
os | is a reference to the stream to output the estimator to. |
prec | the precision for output formatting. ie, number of decimal places. |
{ if (hasSubPaving()) { // have to use cxsc io manipulators os << cxsc::SaveOpt; os << cxsc::Variable << cxsc::SetPrecision(prec+2,prec); getSubPaving()->leavesOutputTabs(os); // the output os << cxsc::RestoreOpt; } return os; }
bool FunctionEstimatorReal::splitToShape | ( | std::string | instruction | ) |
Split an estimator to a specified shape.
Throws a NullSubpavings_Error if the subpaving that this manages is a NULL pointer.
Throws a NoBox_Error if the subpaving box is empty.
Prints a message to the standard error output if the instruction could not be carried out.
instruction | specifies the required shape, eg "3, 3, 2, 1" |
{ // checks: is there a root paving, is the string properly formed? if (!hasSubPaving()) { throw NullSubpavingPointer_Error( "FunctionEstimatorReal::splitToShape()"); } bool success = false; RealMappedSPnode temp(*getSubPaving()); // copy to temp try { if (instruction.length() == 0) { throw std::invalid_argument( "FunctionEstimatorReal::splitToShape() : No instruction"); } std::string legal(", 0123456789"); if (instruction.find_first_not_of(legal) != std::string::npos) { throw std::invalid_argument( "FunctionEstimatorReal::splitToShape() : Illegal character"); } // all seems to be okay, we can start splitting the root paving success = getSubPaving()->splitRootToShape(instruction); /* ALSO NEED to set the interval ranges using fobj */ RealEstimator estimator(fobj); rootPaving->acceptSPValueVisitor(estimator); if (!success) { handleSplitToShapeError(temp); } } catch (std::invalid_argument const& ia) { cerr << ia.what() << endl; handleSplitToShapeError(temp); success = false; } catch (std::logic_error const& le) { cerr << le.what() << endl; handleSplitToShapeError(temp); success = false; } return success; // any other exceptions are unhandled }
std::string FunctionEstimatorReal::stringSummary | ( | ) | const |
Get a string summary of this estimator's properties.
A string description of this. Includes the address of the subpaving managed but not details of that subpaving.
{ std::ostringstream oss; oss << "This address = " << (this) << endl; oss << "Reference to function object is = " << (&fobj) << endl; if (hasSubPaving()) oss << "Address of subpaving is " << getSubPaving() << endl; else oss << "Subpaving is NULL" << endl; return oss.str(); }