MOAB: Mesh Oriented datABase  (version 5.2.1)
MBMesquite::PMeanPTemplate Class Reference

\(\frac{1}{n}\sum_{i=1}^n\mu(s_i)^p\) More...

#include <PMeanPTemplate.hpp>

+ Inheritance diagram for MBMesquite::PMeanPTemplate:
+ Collaboration diagram for MBMesquite::PMeanPTemplate:

Public Member Functions

MESQUITE_EXPORT PMeanPTemplate (double power, QualityMetric *qm=0)
MESQUITE_EXPORT PMeanPTemplate (const PMeanPTemplate &copy)
 copy constructor
virtual MESQUITE_EXPORT ~PMeanPTemplate ()
MESQUITE_EXPORT double get_power () const
MESQUITE_EXPORT void set_power (double p)
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 bool evaluate_with_Hessian (EvalType type, PatchData &pd, double &value_out, std::vector< Vector3D > &grad_out, MsqHessian &Hessian_out, MsqError &err)
 Evaluate objective function and Hessian for specified patch.
virtual MESQUITE_EXPORT
ObjectiveFunction
clone () const
 Create copy with same state.
virtual MESQUITE_EXPORT void clear ()

Protected Member Functions

double get_value (double power_sum, size_t count, EvalType type, size_t &global_count)
 Handle EvalType for all eval functions, return OF value.

Protected Attributes

Exponent mPower
Exponent mPowerMinus1
Exponent mPowerMinus2
std::vector< size_t > qmHandles
std::vector< size_t > mIndices
std::vector< Vector3DmGradient
std::vector< SymMatrix3DmDiag
std::vector< Matrix3DmHessian

Private Attributes

size_t mCount
double mPowSum
size_t saveCount
double savePowSum

Detailed Description

\(\frac{1}{n}\sum_{i=1}^n\mu(s_i)^p\)

This class implements an objective function that is the power-mean of the quality metric evalutations raised to the power-mean power. That is, the sum of each quality metric value raised to a power, divided by the totoal number of quality metric values.

Definition at line 51 of file PMeanPTemplate.hpp.


Constructor & Destructor Documentation

Parameters:
powerThe exponent to use for the power-mean
qmThe quality metric.

Definition at line 59 of file PMeanPTemplate.hpp.

References clear(), and set_power().

Referenced by clone().

                                                          : ObjectiveFunctionTemplate( qm )
    {
        clear();
        set_power( power );
    }

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 72 of file PMeanPTemplate.hpp.

        : ObjectiveFunctionTemplate( copy ), mPower( copy.mPower ), mPowerMinus1( copy.mPowerMinus1 ),
          mPowerMinus2( copy.mPowerMinus2 ), mCount( copy.mCount ), mPowSum( copy.mPowSum ),
          saveCount( copy.saveCount ), savePowSum( copy.savePowSum )
    {
    }

Definition at line 80 of file PMeanPTemplate.hpp.

{}

Member Function Documentation

Clear any values accumulated for BCD-related eval calls

Implements MBMesquite::ObjectiveFunction.

Definition at line 47 of file PMeanPTemplate.cpp.

References mCount, mPowSum, saveCount, and savePowSum.

Referenced by MBMesquite::PatchPowerMeanP::initialize_block_coordinate_descent(), and PMeanPTemplate().

{
    mCount     = 0;
    mPowSum    = 0;
    saveCount  = 0;
    savePowSum = 0;
}

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::PatchPowerMeanP.

Definition at line 42 of file PMeanPTemplate.cpp.

References PMeanPTemplate().

{
    return new PMeanPTemplate( *this );
}
bool MBMesquite::PMeanPTemplate::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.

Parameters:
typeEvaluation type.
pdThe patch.
value_outThe passed-back value of the objective fuction.
freeIf true, incorporate the quality metric values only for those metric evaluations that depend on at least one free vertex
Returns:
false if any QualityMetric evaluation returned false, true otherwise.

Implements MBMesquite::ObjectiveFunction.

Reimplemented in MBMesquite::PatchPowerMeanP.

Definition at line 99 of file PMeanPTemplate.cpp.

References MBMesquite::ObjectiveFunction::ACCUMULATE, MBMesquite::QualityMetric::evaluate(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), MBMesquite::QualityMetric::get_single_pass(), get_value(), mPower, MSQ_CHKERR, MSQ_ERRFALSE, qmHandles, MBMesquite::Exponent::raise(), and value().

Referenced by PMeanPTemplateTest::test_evaluate().

{
    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, working_sum = 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;

        working_sum += mPower.raise( value );
    }

    // get overall OF value, update member data, etc.
    size_t global_count = 0;
    value_out           = qm->get_negate_flag() * get_value( working_sum, qmHandles.size(), type, global_count );
    return true;
}
bool MBMesquite::PMeanPTemplate::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.

Parameters:
typeEvaluation type.
pdThe patch.
value_outThe passed-back value of the objective fuction.
grad_outThe gradient of the OF wrt the coordinates of each *free* vertex in the patch.
Returns:
false if any QualityMetric evaluation returned false, true otherwise.

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.

Parameters:
pdPatchData on which the gradient is taken.
gradArray of Vector3D of length the number of vertices used to store gradient.
OF_valwill be set to the objective function value.

Reimplemented from MBMesquite::ObjectiveFunction.

Reimplemented in MBMesquite::PatchPowerMeanP.

Definition at line 125 of file PMeanPTemplate.cpp.

References MBMesquite::QualityMetric::evaluate_with_gradient(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), get_value(), mGradient, mIndices, mPower, mPowerMinus1, MSQ_CHKERR, MSQ_ERRFALSE, MBMesquite::PatchData::num_free_vertices(), MBMesquite::OF_FREE_EVALS_ONLY, qmHandles, MBMesquite::Exponent::raise(), MBMesquite::Exponent::value(), and value().

Referenced by PMeanPTemplateTest::test_gradient().

{
    QualityMetric* qm = get_quality_metric();
    qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
    MSQ_ERRFALSE( err );

    // zero gradient
    grad_out.clear();
    grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) );

    // calculate OF value and gradient for just the patch
    std::vector< size_t >::const_iterator i;
    double value, working_sum = 0.0;
    const double f = qm->get_negate_flag() * mPower.value();
    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;

        const double r1  = mPowerMinus1.raise( value );
        const double qmp = r1 * value;
        working_sum += qmp;
        value = f * r1;

        for( size_t j = 0; j < mIndices.size(); ++j )
        {
            mGradient[j] *= value;
            grad_out[mIndices[j]] += mGradient[j];
        }
    }

    // get overall OF value, update member data, etc.
    size_t global_count = 0;
    value_out           = qm->get_negate_flag() * get_value( working_sum, qmHandles.size(), type, global_count );
    if( global_count )
    {
        const double inv_n = 1.0 / global_count;
        std::vector< Vector3D >::iterator g;
        for( g = grad_out.begin(); g != grad_out.end(); ++g )
            *g *= inv_n;
    }
    return true;
}
bool MBMesquite::PMeanPTemplate::evaluate_with_Hessian ( EvalType  type,
PatchData pd,
double &  value_out,
std::vector< Vector3D > &  grad_out,
MsqHessian Hessian_out,
MsqError err 
) [virtual]

Evaluate objective function and 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 will fail.

Parameters:
typeEvaluation type.
pdThe patch.
value_outThe passed-back value of the objective fuction.
grad_outThe gradient of the OF wrt the coordinates of each *free* vertex in the patch.
Hessian_outThe Hessian of the OF wrt the coordinates of each *free* vertex in the patch.
Returns:
false if any QualityMetric evaluation returned false, true otherwise.

Reimplemented from MBMesquite::ObjectiveFunction.

Reimplemented in MBMesquite::PatchPowerMeanP.

Definition at line 245 of file PMeanPTemplate.cpp.

References MBMesquite::MsqHessian::add(), MBMesquite::QualityMetric::evaluate_with_Hessian(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), get_value(), mGradient, mHessian, mIndices, mPower, mPowerMinus2, MSQ_CHKERR, MSQ_ERRFALSE, n, MBMesquite::PatchData::num_free_vertices(), MBMesquite::OF_FREE_EVALS_ONLY, qmHandles, MBMesquite::Exponent::raise(), MBMesquite::MsqHessian::scale(), MBMesquite::Exponent::value(), value(), and MBMesquite::MsqHessian::zero_out().

Referenced by PMeanPTemplateTest::test_Hessian().

{
    QualityMetric* qm = get_quality_metric();
    qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
    MSQ_ERRFALSE( err );

    // zero gradient and hessian
    grad_out.clear();
    grad_out.resize( pd.num_free_vertices(), 0.0 );
    Hessian_out.zero_out();

    // calculate OF value and gradient for just the patch
    std::vector< size_t >::const_iterator i;
    size_t j, k, n;
    double value, working_sum = 0.0;
    const double f1 = qm->get_negate_flag() * mPower.value();
    const double f2 = f1 * ( mPower.value() - 1 );
    Matrix3D m;
    for( i = qmHandles.begin(); i != qmHandles.end(); ++i )
    {
        bool result = qm->evaluate_with_Hessian( pd, *i, value, mIndices, mGradient, mHessian, err );
        if( MSQ_CHKERR( err ) || !result ) return false;
        if( fabs( value ) < DBL_EPSILON ) continue;

        const size_t nfree = mIndices.size();
        n                  = 0;
        if( mPower.value() == 1.0 )
        {
            working_sum += mPower.raise( value );
            for( j = 0; j < nfree; ++j )
            {
                mGradient[j] *= f1;
                grad_out[mIndices[j]] += mGradient[j];
                for( k = j; k < nfree; ++k )
                {
                    mHessian[n] *= f1;
                    Hessian_out.add( mIndices[j], mIndices[k], mHessian[n], err );
                    MSQ_ERRFALSE( err );
                    ++n;
                }
            }
        }
        else
        {
            const double r2 = mPowerMinus2.raise( value );
            const double r1 = r2 * value;
            working_sum += r1 * value;
            const double hf = f2 * r2;
            const double gf = f1 * r1;
            for( j = 0; j < nfree; ++j )
            {
                for( k = j; k < nfree; ++k )
                {
                    m.outer_product( mGradient[j], mGradient[k] );
                    m *= hf;
                    mHessian[n] *= gf;
                    m += mHessian[n];
                    Hessian_out.add( mIndices[j], mIndices[k], m, err );
                    MSQ_ERRFALSE( err );
                    ++n;
                }
            }
            for( j = 0; j < nfree; ++j )
            {
                mGradient[j] *= gf;
                grad_out[mIndices[j]] += mGradient[j];
            }
        }
    }

    // get overall OF value, update member data, etc.
    size_t global_count = 0;
    value_out           = qm->get_negate_flag() * get_value( working_sum, qmHandles.size(), type, global_count );
    if( global_count )
    {
        const double inv_n = 1.0 / global_count;
        std::vector< Vector3D >::iterator g;
        for( g = grad_out.begin(); g != grad_out.end(); ++g )
            *g *= inv_n;
        Hessian_out.scale( inv_n );
    }
    return true;
}
bool MBMesquite::PMeanPTemplate::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.

Parameters:
typeEvaluation type.
pdThe patch.
value_outThe passed-back value of the objective fuction.
grad_outThe gradient of the OF wrt the coordinates of each *free* vertex in the patch.
hess_diag_outThe diagonal blocks of a Hessian. I.e. Decompose the Hessian into 3x3 submatrices and return only the submatrices (blocks) along the diagonal.
Returns:
false if any QualityMetric evaluation returned false, true otherwise.

Reimplemented from MBMesquite::ObjectiveFunction.

Definition at line 171 of file PMeanPTemplate.cpp.

References MBMesquite::QualityMetric::evaluate_with_Hessian_diagonal(), MBMesquite::QualityMetric::get_evaluations(), MBMesquite::QualityMetric::get_negate_flag(), MBMesquite::ObjectiveFunctionTemplate::get_quality_metric(), get_value(), mDiag, mGradient, mIndices, mPower, mPowerMinus2, MSQ_CHKERR, MSQ_ERRFALSE, MBMesquite::PatchData::num_free_vertices(), MBMesquite::OF_FREE_EVALS_ONLY, MBMesquite::outer(), qmHandles, MBMesquite::Exponent::raise(), MBMesquite::Exponent::value(), and value().

Referenced by PMeanPTemplateTest::test_diagonal().

{
    QualityMetric* qm = get_quality_metric();
    qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
    MSQ_ERRFALSE( err );

    // zero gradient and hessian
    const size_t s = pd.num_free_vertices();
    grad_out.clear();
    grad_out.resize( s, 0.0 );
    hess_diag_out.clear();
    hess_diag_out.resize( s, 0.0 );

    // calculate OF value and gradient for just the patch
    std::vector< size_t >::const_iterator i;
    size_t j;
    double value, working_sum = 0.0;
    const double f1 = qm->get_negate_flag() * mPower.value();
    const double f2 = f1 * ( mPower.value() - 1 );
    for( i = qmHandles.begin(); i != qmHandles.end(); ++i )
    {
        bool result = qm->evaluate_with_Hessian_diagonal( pd, *i, value, mIndices, mGradient, mDiag, err );
        if( MSQ_CHKERR( err ) || !result ) return false;
        if( fabs( value ) < DBL_EPSILON ) continue;

        const size_t nfree = mIndices.size();
        if( mPower.value() == 1.0 )
        {
            working_sum += mPower.raise( value );
            for( j = 0; j < nfree; ++j )
            {
                const size_t idx = mIndices[j];
                hess_diag_out[idx] += f1 * mDiag[j];
                mGradient[j] *= f1;
                grad_out[idx] += mGradient[j];
            }
        }
        else
        {
            const double r2 = mPowerMinus2.raise( value );
            const double r1 = r2 * value;
            working_sum += r1 * value;
            const double hf = f2 * r2;
            const double gf = f1 * r1;
            for( j = 0; j < nfree; ++j )
            {
                const size_t idx = mIndices[j];

                hess_diag_out[idx] += hf * outer( mGradient[j] );
                hess_diag_out[idx] += gf * mDiag[j];

                mGradient[j] *= gf;
                grad_out[idx] += mGradient[j];
            }
        }
    }

    // get overall OF value, update member data, etc.
    size_t global_count = 0;
    value_out           = qm->get_negate_flag() * get_value( working_sum, qmHandles.size(), type, global_count );
    if( global_count )
    {
        const double inv_n = 1.0 / global_count;
        for( j = 0; j < s; ++j )
        {
            grad_out[j] *= inv_n;
            hess_diag_out[j] *= inv_n;
        }
    }
    return true;
}

Definition at line 83 of file PMeanPTemplate.hpp.

References mPower, and MBMesquite::Exponent::value().

    {
        return mPower.value();
    }
double MBMesquite::PMeanPTemplate::get_value ( double  power_sum,
size_t  count,
EvalType  type,
size_t &  global_count 
) [protected]

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.

Parameters:
power_sumThe sum over the current patch
countThe number of qm evaluations for the current patch
typeThe evaluation type passed to 'evaluate'
global_countThe total, accumulated number of QM evaluations
Returns:
The objective function value to return from 'evaluate'

Definition at line 55 of file PMeanPTemplate.cpp.

References MBMesquite::ObjectiveFunction::ACCUMULATE, MBMesquite::ObjectiveFunction::CALCULATE, mCount, mPowSum, MBMesquite::ObjectiveFunction::SAVE, saveCount, savePowSum, MBMesquite::ObjectiveFunction::TEMPORARY, and MBMesquite::ObjectiveFunction::UPDATE.

Referenced by MBMesquite::PatchPowerMeanP::evaluate(), evaluate(), MBMesquite::PatchPowerMeanP::evaluate_with_gradient(), evaluate_with_gradient(), MBMesquite::PatchPowerMeanP::evaluate_with_Hessian(), evaluate_with_Hessian(), and evaluate_with_Hessian_diagonal().

{
    double result = 0;
    switch( type )
    {
        case CALCULATE:
            result       = power_sum;
            global_count = count;
            break;

        case ACCUMULATE:
            mPowSum += power_sum;
            mCount += count;
            result       = mPowSum;
            global_count = mCount;
            break;

        case SAVE:
            savePowSum   = power_sum;
            saveCount    = count;
            result       = mPowSum;
            global_count = mCount;
            break;

        case UPDATE:
            mPowSum -= savePowSum;
            mCount -= saveCount;
            savePowSum = power_sum;
            saveCount  = count;
            mPowSum += savePowSum;
            mCount += saveCount;
            result       = mPowSum;
            global_count = mCount;
            break;

        case TEMPORARY:
            result       = mPowSum - savePowSum + power_sum;
            global_count = mCount + count - saveCount;
            break;
    }

    return global_count ? result / global_count : 0.0;
}

Definition at line 89 of file PMeanPTemplate.hpp.

References mPower, mPowerMinus1, and mPowerMinus2.

Referenced by PMeanPTemplate().

    {
        mPower       = p;
        mPowerMinus1 = p - 1;
        mPowerMinus2 = p - 2;
    }

Member Data Documentation

The number of accumulated entires

Definition at line 139 of file PMeanPTemplate.hpp.

Referenced by clear(), and get_value().

std::vector< SymMatrix3D > MBMesquite::PMeanPTemplate::mDiag [mutable, protected]

Temporary storage for qm hessian diagonal

Definition at line 152 of file PMeanPTemplate.hpp.

Referenced by evaluate_with_Hessian_diagonal().

std::vector< Matrix3D > MBMesquite::PMeanPTemplate::mHessian [mutable, protected]

Temporary storage for qm Hessian

Definition at line 154 of file PMeanPTemplate.hpp.

Referenced by MBMesquite::PatchPowerMeanP::evaluate_with_Hessian(), and evaluate_with_Hessian().

mPower - 1.0

Definition at line 135 of file PMeanPTemplate.hpp.

Referenced by evaluate_with_gradient(), and set_power().

The accumulated sum of values to the mPower

Definition at line 140 of file PMeanPTemplate.hpp.

Referenced by clear(), and get_value().

Saved count from previous patch

Definition at line 141 of file PMeanPTemplate.hpp.

Referenced by clear(), and get_value().

Saved sum from previous patch

Definition at line 142 of file PMeanPTemplate.hpp.

Referenced by clear(), and get_value().

List of all members.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines