MOAB: Mesh Oriented datABase
(version 5.4.1)
|
(variance)^2 template More...
#include <VarianceTemplate.hpp>
Public Member Functions | |
MESQUITE_EXPORT | VarianceTemplate (QualityMetric *qm=0) |
MESQUITE_EXPORT | VarianceTemplate (const VarianceTemplate ©) |
copy constructor | |
virtual MESQUITE_EXPORT | ~VarianceTemplate () |
virtual MESQUITE_EXPORT bool | evaluate (EvalType type, PatchData &pd, double &value_out, bool free, MsqError &err) |
Evaluate objective function for specified patch. | |
virtual MESQUITE_EXPORT bool | evaluate_with_gradient (EvalType type, PatchData &pd, double &value_out, std::vector< Vector3D > &grad_out, MsqError &err) |
Evaluate objective function and gradient for specified patch. | |
virtual MESQUITE_EXPORT bool | evaluate_with_Hessian_diagonal (EvalType type, PatchData &pd, double &value_out, std::vector< Vector3D > &grad_out, std::vector< SymMatrix3D > &hess_diag_out, MsqError &err) |
Evaluate objective function and diagonal blocks of Hessian for specified patch. | |
virtual MESQUITE_EXPORT ObjectiveFunction * | clone () const |
Create copy with same state. | |
virtual MESQUITE_EXPORT void | clear () |
Private Member Functions | |
void | accumulate (double sum, double sqr_sum, size_t count, EvalType type, double &result_sum, double &result_sqr, size_t &global_count) |
Handle EvalType for all eval functions, return OF value. | |
Private Attributes | |
size_t | mCount |
double | mSum |
double | mSqrSum |
size_t | saveCount |
double | saveSum |
double | saveSqrSum |
std::vector< size_t > | qmHandles |
std::vector< size_t > | mIndices |
std::vector< Vector3D > | mGradient |
std::vector< Vector3D > | gradSum |
std::vector< SymMatrix3D > | mHessDiag |
std::vector< SymMatrix3D > | hessSum |
(variance)^2 template
This class implements an objective function that is the variance of the quality metric evalutations.
Definition at line 47 of file VarianceTemplate.hpp.
MESQUITE_EXPORT MBMesquite::VarianceTemplate::VarianceTemplate | ( | QualityMetric * | qm = 0 | ) | [inline] |
Definition at line 51 of file VarianceTemplate.hpp.
References clear().
Referenced by clone().
: ObjectiveFunctionTemplate( qm ) { clear(); }
MESQUITE_EXPORT MBMesquite::VarianceTemplate::VarianceTemplate | ( | const VarianceTemplate & | copy | ) | [inline] |
copy constructor
Define a copy constructor because the compiler-provided default one would also copy the temporary arrays, which would be a waste of time.
Definition at line 63 of file VarianceTemplate.hpp.
: ObjectiveFunctionTemplate( copy ), mCount( copy.mCount ), mSum( copy.mSum ), mSqrSum( copy.mSqrSum ), saveCount( copy.saveCount ), saveSum( copy.saveSum ), saveSqrSum( copy.saveSqrSum ) { }
virtual MESQUITE_EXPORT MBMesquite::VarianceTemplate::~VarianceTemplate | ( | ) | [inline, virtual] |
Definition at line 70 of file VarianceTemplate.hpp.
{}
void MBMesquite::VarianceTemplate::accumulate | ( | double | sum, |
double | sqr_sum, | ||
size_t | count, | ||
EvalType | type, | ||
double & | result_sum, | ||
double & | result_sqr, | ||
size_t & | global_count | ||
) | [private] |
Handle EvalType for all eval functions, return OF value.
This function implements the common handling of the EvalType argument for all forms of the 'evaluate' method.
NOTE: This function modifies accumulated values depenending on the value of EvalType.
sum | The sum over the current patch |
sqr_sum | The sum of squares over the current patch |
count | The number of qm evaluations for the current patch |
type | The evaluation type passed to 'evaluate' |
global_count | The total, accumulated number of QM evaluations |
result_sum | The sum term of the variance |
result_sqr | The sum of squares term of the variance |
Definition at line 55 of file VarianceTemplate.cpp.
References MBMesquite::ObjectiveFunction::ACCUMULATE, MBMesquite::ObjectiveFunction::CALCULATE, mCount, mSqrSum, mSum, MBMesquite::ObjectiveFunction::SAVE, saveCount, saveSqrSum, saveSum, moab::sum(), MBMesquite::ObjectiveFunction::TEMPORARY, and MBMesquite::ObjectiveFunction::UPDATE.
Referenced by evaluate(), evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().
{ switch( type ) { case CALCULATE: result_sum = sum; result_sqr = sqr_sum; global_count = count; break; case ACCUMULATE: result_sum = mSum += sum; result_sqr = mSqrSum += sqr_sum; global_count = mCount += count; break; case SAVE: saveSum = sum; saveSqrSum = sqr_sum; saveCount = count; result_sum = mSum; result_sqr = mSqrSum; global_count = mCount; break; case UPDATE: mSum -= saveSum; mSqrSum -= saveSqrSum; mCount -= saveCount; result_sum = mSum += saveSum = sum; result_sqr = mSqrSum += saveSqrSum = sqr_sum; global_count = mCount += saveCount = count; break; case TEMPORARY: result_sum = mSum - saveSum + sum; result_sqr = mSqrSum - saveSqrSum + sqr_sum; global_count = mCount + count - saveCount; break; } }
void MBMesquite::VarianceTemplate::clear | ( | ) | [virtual] |
Clear any values accumulated for BCD-related eval calls
Implements MBMesquite::ObjectiveFunction.
Definition at line 47 of file VarianceTemplate.cpp.
References mCount, mSqrSum, mSum, saveCount, saveSqrSum, and saveSum.
Referenced by VarianceTemplate().
ObjectiveFunction * MBMesquite::VarianceTemplate::clone | ( | ) | const [virtual] |
Create copy with same state.
Create a new instance of the objective function that is a copy of the callee with the same accumulated values, parameters, etc.
Implements MBMesquite::ObjectiveFunction.
Reimplemented in MBMesquite::StdDevTemplate.
Definition at line 42 of file VarianceTemplate.cpp.
References VarianceTemplate().
{ return new VarianceTemplate( *this ); }
bool MBMesquite::VarianceTemplate::evaluate | ( | EvalType | type, |
PatchData & | pd, | ||
double & | value_out, | ||
bool | free, | ||
MsqError & | err | ||
) | [virtual] |
Evaluate objective function for specified patch.
Either evaluate the objective function over the passed patch or update the accumulated, global objective function value for changes in the passed patch, depending on the value of the EvalType.
type | Evaluation type. |
pd | The patch. |
value_out | The passed-back value of the objective fuction. |
free | If true, incorporate the quality metric values only for those metric evaluations that depend on at least one free vertex |
Implements MBMesquite::ObjectiveFunction.
Reimplemented in MBMesquite::StdDevTemplate.
Definition at line 103 of file VarianceTemplate.cpp.
References MBMesquite::ObjectiveFunction::ACCUMULATE, accumulate(), MBMesquite::QualityMetric::evaluate(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), MBMesquite::QualityMetric::get_single_pass(), MSQ_CHKERR, MSQ_ERRFALSE, n, qmHandles, moab::sum(), and value().
{ QualityMetric* qm = get_quality_metric(); if( type == ObjectiveFunction::ACCUMULATE ) qm->get_single_pass( pd, qmHandles, free, err ); else qm->get_evaluations( pd, qmHandles, free, err ); MSQ_ERRFALSE( err ); // calculate OF value for just the patch std::vector< size_t >::const_iterator i; double value, sum = 0.0, sqr = 0.0; for( i = qmHandles.begin(); i != qmHandles.end(); ++i ) { bool result = qm->evaluate( pd, *i, value, err ); if( MSQ_CHKERR( err ) || !result ) return false; sum += value; sqr += value * value; } // get overall OF value, update member data, etc. size_t n; accumulate( sum, sqr, qmHandles.size(), type, sum, sqr, n ); // don't divide by zero if( n < 1 ) { value_out = 0.0; return true; } const double rms = sqr / n; const double avg = sum / n; value_out = qm->get_negate_flag() * ( rms - avg * avg ); return true; }
bool MBMesquite::VarianceTemplate::evaluate_with_gradient | ( | EvalType | eval_type, |
PatchData & | pd, | ||
double & | OF_val, | ||
std::vector< Vector3D > & | grad, | ||
MsqError & | err | ||
) | [virtual] |
Evaluate objective function and gradient for specified patch.
Either evaluate the objective function over the passed patch or update the accumulated, global objective function value for changes in the passed patch, depending on the value of the EvalType.
The default implementation of this function will use the value-only variation of the evaluate method and numerical approximation to calculate gradients. Whenever possible, objective function implementations should provide more efficient analyical gradient calculations.
type | Evaluation type. |
pd | The patch. |
value_out | The passed-back value of the objective fuction. |
grad_out | The gradient of the OF wrt the coordinates of each *free* vertex in the patch. |
Numerically Calculates the gradient of the ObjectiveFunction for the free vertices in the patch. Returns 'false' if the patch is outside of a required feasible region, returns 'ture' otherwise. The behavior of the function depends on the value of the boolean useLocalGradient. If useLocalGradient is set to 'true', compute_numerical_gradient creates a sub-patch around a free vertex, and then perturbs that vertex in one of the coordinate directions. Only the ObjectiveFunction value on the local sub-patch is used in the computation of the gradient. Therefore, useLocalGradient should only be set to 'true' for ObjectiveFunctions which can use this method. Unless the concrete ObjectiveFunction sets useLocalGradient to 'true' in its constructor, the value will be 'false'. In this case, the objective function value for the entire patch is used in the calculation of the gradient. This is computationally expensive, but it is numerically correct for all (C_1) functions.
pd | PatchData on which the gradient is taken. |
grad | Array of Vector3D of length the number of vertices used to store gradient. |
OF_val | will be set to the objective function value. |
Reimplemented from MBMesquite::ObjectiveFunction.
Reimplemented in MBMesquite::StdDevTemplate.
Definition at line 141 of file VarianceTemplate.cpp.
References accumulate(), MBMesquite::QualityMetric::evaluate_with_gradient(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), gradSum, mGradient, mIndices, MSQ_CHKERR, MSQ_ERRFALSE, n, MBMesquite::PatchData::num_free_vertices(), MBMesquite::OF_FREE_EVALS_ONLY, qmHandles, moab::sum(), and value().
{ QualityMetric* qm = get_quality_metric(); qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err ); MSQ_ERRFALSE( err ); // zero gradient data grad_out.clear(); // store sum of metric * gradient of metric, and later OF gradient grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); gradSum.clear(); // store sum of gradients of metrics gradSum.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); // calculate OF value and gradient for just the patch Matrix3D op; std::vector< size_t >::const_iterator i; double value, sum = 0.0, sqr = 0.0; for( i = qmHandles.begin(); i != qmHandles.end(); ++i ) { bool result = qm->evaluate_with_gradient( pd, *i, value, mIndices, mGradient, err ); if( MSQ_CHKERR( err ) || !result ) return false; if( fabs( value ) < DBL_EPSILON ) continue; sum += value; sqr += value * value; for( size_t j = 0; j < mIndices.size(); ++j ) { const size_t r = mIndices[j]; gradSum[r] += mGradient[j]; mGradient[j] *= value; grad_out[r] += mGradient[j]; } } // update accumulated values (if doing BCD) size_t n; accumulate( sum, sqr, qmHandles.size(), type, sum, sqr, n ); // don't divide by zero if( n < 1 ) { value_out = 0.0; grad_out.clear(); grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); return true; } // calculate OF value const double rms = sqr / n; const double avg = sum / n; value_out = qm->get_negate_flag() * ( rms - avg * avg ); // Finish calculation of gradient and Hessian const double f = qm->get_negate_flag() * 2.0 / n; for( size_t k = 0; k < pd.num_free_vertices(); ++k ) { gradSum[k] *= avg; grad_out[k] -= gradSum[k]; grad_out[k] *= f; } return true; }
bool MBMesquite::VarianceTemplate::evaluate_with_Hessian_diagonal | ( | EvalType | type, |
PatchData & | pd, | ||
double & | value_out, | ||
std::vector< Vector3D > & | grad_out, | ||
std::vector< SymMatrix3D > & | hess_diag_out, | ||
MsqError & | err | ||
) | [virtual] |
Evaluate objective function and diagonal blocks of Hessian for specified patch.
Either evaluate the objective function over the passed patch or update the accumulated, global objective function value for changes in the passed patch, depending on the value of the EvalType.
The default implementation of this function evaluate the entire Hessian and discard non-diagonal portions. Concrete objective functions should provide a more efficient implementation that evaluates and accumulates only the required terms.
type | Evaluation type. |
pd | The patch. |
value_out | The passed-back value of the objective fuction. |
grad_out | The gradient of the OF wrt the coordinates of each *free* vertex in the patch. |
hess_diag_out | The diagonal blocks of a Hessian. I.e. Decompose the Hessian into 3x3 submatrices and return only the submatrices (blocks) along the diagonal. |
Reimplemented from MBMesquite::ObjectiveFunction.
Reimplemented in MBMesquite::StdDevTemplate.
Definition at line 209 of file VarianceTemplate.cpp.
References accumulate(), MBMesquite::QualityMetric::evaluate_with_Hessian_diagonal(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), gradSum, hessSum, mGradient, mHessDiag, mIndices, MSQ_CHKERR, MSQ_ERRFALSE, n, MBMesquite::PatchData::num_free_vertices(), MBMesquite::OF_FREE_EVALS_ONLY, MBMesquite::outer(), qmHandles, moab::sum(), and value().
{ QualityMetric* qm = get_quality_metric(); qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err ); MSQ_ERRFALSE( err ); // zero gradient and Hessian data grad_out.clear(); // store sum of metric * gradient of metric, and later OF gradient grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); gradSum.clear(); // store sum of gradients of metrics gradSum.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); hessSum.clear(); // store sum of Hessians of metrics hessSum.resize( pd.num_free_vertices(), SymMatrix3D( 0.0 ) ); hess_diag_out.clear(); // store sum of metric * outer_product(metric gradient), and later OF Hessian hess_diag_out.resize( pd.num_free_vertices(), SymMatrix3D( 0.0 ) ); // calculate OF value and gradient for just the patch Matrix3D op; std::vector< size_t >::const_iterator i; double value, sum = 0.0, sqr = 0.0; for( i = qmHandles.begin(); i != qmHandles.end(); ++i ) { bool result = qm->evaluate_with_Hessian_diagonal( pd, *i, value, mIndices, mGradient, mHessDiag, err ); if( MSQ_CHKERR( err ) || !result ) return false; if( fabs( value ) < DBL_EPSILON ) continue; sum += value; sqr += value * value; for( size_t j = 0; j < mIndices.size(); ++j ) { const size_t r = mIndices[j]; hessSum[r] += mHessDiag[j]; hess_diag_out[r] += outer( mGradient[j] ); mHessDiag[j] *= value; hess_diag_out[r] += mHessDiag[j]; gradSum[r] += mGradient[j]; mGradient[j] *= value; grad_out[r] += mGradient[j]; } } // update accumulated values (if doing BCD) size_t n; accumulate( sum, sqr, qmHandles.size(), type, sum, sqr, n ); // don't divide by zero if( n < 1 ) { value_out = 0.0; grad_out.clear(); grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) ); hess_diag_out.clear(); hess_diag_out.resize( pd.num_free_vertices(), SymMatrix3D( 0.0 ) ); return true; } // calculate OF value const double rms = sqr / n; const double avg = sum / n; value_out = qm->get_negate_flag() * ( rms - avg * avg ); // Finish calculation of gradient and Hessian const double inv_n = 1.0 / n; const double f = qm->get_negate_flag() * 2.0 / n; for( size_t k = 0; k < pd.num_free_vertices(); ++k ) { hessSum[k] *= avg; hess_diag_out[k] -= hessSum[k]; hess_diag_out[k] -= inv_n * outer( gradSum[k] ); hess_diag_out[k] *= f; gradSum[k] *= avg; grad_out[k] -= gradSum[k]; grad_out[k] *= f; } return true; }
std::vector< Vector3D > MBMesquite::VarianceTemplate::gradSum [mutable, private] |
Definition at line 132 of file VarianceTemplate.hpp.
Referenced by evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().
std::vector< SymMatrix3D > MBMesquite::VarianceTemplate::hessSum [mutable, private] |
Definition at line 134 of file VarianceTemplate.hpp.
Referenced by evaluate_with_Hessian_diagonal().
size_t MBMesquite::VarianceTemplate::mCount [private] |
The number of accumulated entires
Definition at line 120 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().
std::vector< Vector3D > MBMesquite::VarianceTemplate::mGradient [mutable, private] |
Temporary storage for qm gradient
Definition at line 132 of file VarianceTemplate.hpp.
Referenced by evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().
std::vector< SymMatrix3D > MBMesquite::VarianceTemplate::mHessDiag [mutable, private] |
Temporary storage for qm Hessian diagonal data
Definition at line 134 of file VarianceTemplate.hpp.
Referenced by evaluate_with_Hessian_diagonal().
std::vector< size_t > MBMesquite::VarianceTemplate::mIndices [mutable, private] |
Temporary storage for qm vertex indices
Definition at line 130 of file VarianceTemplate.hpp.
Referenced by evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().
double MBMesquite::VarianceTemplate::mSqrSum [private] |
The running sum of the square of QM values
Definition at line 122 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().
double MBMesquite::VarianceTemplate::mSum [private] |
The runnnig sum of the qualtiy metric valuse
Definition at line 121 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().
std::vector< size_t > MBMesquite::VarianceTemplate::qmHandles [mutable, private] |
Temporary storage for qm sample handles
Definition at line 128 of file VarianceTemplate.hpp.
Referenced by evaluate(), evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().
size_t MBMesquite::VarianceTemplate::saveCount [private] |
Saved count from previous patch
Definition at line 123 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().
double MBMesquite::VarianceTemplate::saveSqrSum [private] |
Saved sum from previous patch
Definition at line 125 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().
double MBMesquite::VarianceTemplate::saveSum [private] |
Saved sum from previous patch
Definition at line 124 of file VarianceTemplate.hpp.
Referenced by accumulate(), and clear().