MOAB: Mesh Oriented datABase  (version 5.4.1)
MBMesquite::CompositeOFAdd Class Reference

Adds two ObjectiveFunction values together. More...

#include <CompositeOFAdd.hpp>

+ Inheritance diagram for MBMesquite::CompositeOFAdd:
+ Collaboration diagram for MBMesquite::CompositeOFAdd:

Public Member Functions

MESQUITE_EXPORT CompositeOFAdd (ObjectiveFunction *, ObjectiveFunction *, bool delete_OFs=false)
virtual MESQUITE_EXPORT ~CompositeOFAdd ()
virtual MESQUITE_EXPORT void initialize_queue (MeshDomainAssoc *mesh_and_domain, const Settings *settings, MsqError &err)
 Called at start of instruction queue processing.
virtual MESQUITE_EXPORT bool initialize_block_coordinate_descent (MeshDomainAssoc *mesh_and_domain, const Settings *settings, PatchSet *user_set, MsqError &err)
 Initial accumulated value for block coordinate descent algorithms.
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 ()
virtual MESQUITE_EXPORT int min_patch_layers () const

Private Attributes

std::vector< Vector3DmGradient
std::vector< SymMatrix3DmDiagonal
MsqHessian mHessian
ObjectiveFunctionobjFunc1
ObjectiveFunctionobjFunc2
bool deleteObjFuncs

Detailed Description

Adds two ObjectiveFunction values together.

Definition at line 55 of file CompositeOFAdd.hpp.


Constructor & Destructor Documentation

MBMesquite::CompositeOFAdd::CompositeOFAdd ( ObjectiveFunction Obj1,
ObjectiveFunction Obj2,
bool  delete_OFs = false 
)

Sets the QualityMetric pointer to the metric associated with Obj1 and Obj2 if Obj1 and Obj2 are associated with the same metric. Otherwise, it sets the QualityMetric pointer to NULL. The new ObjectiveFunction's negateFlag is always set to one, because the values produced by obj1 and obj2 have already been multiplied by negative one if it was needed. Defaults to the analytical gradient.

Parameters:
Obj1(ObjectiveFunction*)
Obj2(ObjectiveFunction*)

Definition at line 54 of file CompositeOFAdd.cpp.

References objFunc1, and objFunc2.

Referenced by clone().

    : deleteObjFuncs( delete_OFs )
{
    objFunc1 = Obj1;
    objFunc2 = Obj2;
}

Definition at line 73 of file CompositeOFAdd.cpp.

References deleteObjFuncs, objFunc1, and objFunc2.

{
    if( deleteObjFuncs )
    {
        delete objFunc1;
        delete objFunc2;
    }
}

Member Function Documentation

Clear any values accumulated for BCD-related eval calls

Implements MBMesquite::ObjectiveFunction.

Definition at line 66 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::clear(), objFunc1, and objFunc2.

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.

Definition at line 61 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::clone(), CompositeOFAdd(), objFunc1, and objFunc2.

{
    return new CompositeOFAdd( objFunc1->clone(), objFunc2->clone(), true );
}
bool MBMesquite::CompositeOFAdd::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.

Definition at line 100 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::evaluate(), MSQ_CHKERR, objFunc1, and objFunc2.

{
    double value_2;
    bool ok;

    ok = objFunc1->evaluate( type, pd, value_out, free, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;
    ok = objFunc2->evaluate( type, pd, value_2, free, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;

    value_out += value_2;
    return true;
}
bool MBMesquite::CompositeOFAdd::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.

Definition at line 114 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::evaluate_with_gradient(), mGradient, MSQ_CHKERR, MBMesquite::PatchData::num_free_vertices(), objFunc1, and objFunc2.

{
    double value_2;
    bool ok;

    ok = objFunc1->evaluate_with_gradient( type, pd, value_out, grad_out, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;
    ok = objFunc2->evaluate_with_gradient( type, pd, value_2, mGradient, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;

    assert( grad_out.size() == pd.num_free_vertices() );
    assert( mGradient.size() == pd.num_free_vertices() );

    std::vector< Vector3D >::iterator i = grad_out.begin(), j = mGradient.begin();
    while( i != grad_out.end() )
    {
        *i += *j;
        ++i;
        ++j;
    }
    value_out += value_2;
    return true;
}
bool MBMesquite::CompositeOFAdd::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.

Definition at line 167 of file CompositeOFAdd.cpp.

References MBMesquite::MsqHessian::add(), MBMesquite::ObjectiveFunction::evaluate_with_Hessian(), MBMesquite::MsqHessian::initialize(), mGradient, mHessian, MSQ_CHKERR, MBMesquite::PatchData::num_free_vertices(), objFunc1, and objFunc2.

{
    double value_2;
    bool ok;

    mHessian.initialize( Hessian_out );

    ok = objFunc1->evaluate_with_Hessian( type, pd, value_out, grad_out, Hessian_out, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;
    ok = objFunc2->evaluate_with_Hessian( type, pd, value_2, mGradient, mHessian, err );
    if( MSQ_CHKERR( err ) || !ok ) return false;

    value_out += value_2;

    assert( grad_out.size() == pd.num_free_vertices() );
    assert( mGradient.size() == pd.num_free_vertices() );

    for( size_t i = 0; i < pd.num_free_vertices(); ++i )
        grad_out[i] += mGradient[i];
    Hessian_out.add( mHessian );
    return true;
}
bool MBMesquite::CompositeOFAdd::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 142 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::evaluate_with_Hessian_diagonal(), mDiagonal, mGradient, MSQ_CHKERR, MBMesquite::PatchData::num_free_vertices(), objFunc1, and objFunc2.

{
    double value_2;
    bool valid;

    valid = objFunc1->evaluate_with_Hessian_diagonal( type, pd, value_out, grad_out, diag_out, err );
    if( MSQ_CHKERR( err ) || !valid ) return false;
    valid = objFunc2->evaluate_with_Hessian_diagonal( type, pd, value_2, mGradient, mDiagonal, err );
    if( MSQ_CHKERR( err ) || !valid ) return false;

    for( size_t i = 0; i < pd.num_free_vertices(); ++i )
    {
        grad_out[i] += mGradient[i];
        diag_out[i] += mDiagonal[i];
    }

    value_out += value_2;
    return true;
}
bool MBMesquite::CompositeOFAdd::initialize_block_coordinate_descent ( MeshDomainAssoc mesh_and_domain,
const Settings settings,
PatchSet user_set,
MsqError err 
) [virtual]

Initial accumulated value for block coordinate descent algorithms.

Set accumulated value of objective function to the value for the entire, unmodified mesh. This is the initial state for a block coordinate descent algorithm. The ObjectiveFunction will asked to add or remove values for a specific patch of the mesh during the optimization.

Parameters:
meshThe Mesh
domainThe MeshDomain
user_setUser-defined patch set - not relevant for most OF templates.

Implements MBMesquite::ObjectiveFunction.

Definition at line 88 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::initialize_block_coordinate_descent(), MSQ_CHKERR, MSQ_ERRZERO, objFunc1, and objFunc2.

{
    bool rval1, rval2;
    rval1 = objFunc1->initialize_block_coordinate_descent( mesh_and_domain, settings, user_set, err );
    MSQ_ERRZERO( err );
    rval2 = objFunc2->initialize_block_coordinate_descent( mesh_and_domain, settings, user_set, err );
    return !MSQ_CHKERR( err ) && rval1 && rval2;
}
void MBMesquite::CompositeOFAdd::initialize_queue ( MeshDomainAssoc mesh_and_domain,
const Settings settings,
MsqError err 
) [virtual]

Called at start of instruction queue processing.

Do any preliminary global initialization, consistency checking, etc.

Implements MBMesquite::ObjectiveFunction.

Definition at line 82 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::initialize_queue(), MSQ_ERRRTN, objFunc1, and objFunc2.

{
    objFunc1->initialize_queue( mesh_and_domain, settings, err );MSQ_ERRRTN( err );
    objFunc2->initialize_queue( mesh_and_domain, settings, err );MSQ_ERRRTN( err );
}

Get the minimum number of layers of adjacent elements required in a patch to evaluate the objective function for a single free vertex.

Implements MBMesquite::ObjectiveFunction.

Definition at line 195 of file CompositeOFAdd.cpp.

References MBMesquite::ObjectiveFunction::min_patch_layers(), objFunc1, and objFunc2.

{
    const int v1 = objFunc1->min_patch_layers();
    const int v2 = objFunc2->min_patch_layers();
    return v1 > v2 ? v1 : v2;
}

Member Data Documentation

Definition at line 122 of file CompositeOFAdd.hpp.

Referenced by ~CompositeOFAdd().

std::vector< SymMatrix3D > MBMesquite::CompositeOFAdd::mDiagonal [mutable, private]

Temporary storage for hessian diagonal

Definition at line 116 of file CompositeOFAdd.hpp.

Referenced by evaluate_with_Hessian_diagonal().

std::vector< Vector3D > MBMesquite::CompositeOFAdd::mGradient [mutable, private]

Temporary storage for gradient

Definition at line 114 of file CompositeOFAdd.hpp.

Referenced by evaluate_with_gradient(), evaluate_with_Hessian(), and evaluate_with_Hessian_diagonal().

Temporary storage for Hessian

Definition at line 118 of file CompositeOFAdd.hpp.

Referenced by evaluate_with_Hessian().

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