MOAB: Mesh Oriented datABase  (version 5.3.0)
MBMesquite::CompositeOFMultiply Class Reference

Multiplies two ObjectiveFunction values together. More...

#include <CompositeOFMultiply.hpp>

+ Inheritance diagram for MBMesquite::CompositeOFMultiply:
+ Collaboration diagram for MBMesquite::CompositeOFMultiply:

Public Member Functions

MESQUITE_EXPORT CompositeOFMultiply (ObjectiveFunction *, ObjectiveFunction *, bool delete_OFs=false)
virtual MESQUITE_EXPORT ~CompositeOFMultiply ()
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
ObjectiveFunctionobjFunc1
ObjectiveFunctionobjFunc2
bool deleteObjFuncs

Detailed Description

Multiplies two ObjectiveFunction values together.

Definition at line 52 of file CompositeOFMultiply.hpp.


Constructor & Destructor Documentation

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 set to negative one only if both Obj1 and Obj2's negateFlag are negative one (because obj1 and obj2's evaluate function multiply their return values by negative one if their respective function needs to be maximized. If both of these functions needed to be maximized, then the negative ones will have cancelled out). Otherwise, the negateFlag is set to one. Defaults to the analytical gradient.

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

Definition at line 56 of file CompositeOFMultiply.cpp.

References objFunc1, and objFunc2.

Referenced by clone().

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

Definition at line 64 of file CompositeOFMultiply.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 78 of file CompositeOFMultiply.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 73 of file CompositeOFMultiply.cpp.

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

{
    return new CompositeOFMultiply( objFunc1->clone(), objFunc2->clone(), true );
}
bool MBMesquite::CompositeOFMultiply::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 101 of file CompositeOFMultiply.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::CompositeOFMultiply::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 115 of file CompositeOFMultiply.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 *= value_2;
        *j *= value_out;
        *i += *j;
        ++i;
        ++j;
    }
    value_out *= value_2;
    return true;
}
bool MBMesquite::CompositeOFMultiply::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 170 of file CompositeOFMultiply.cpp.

References MBMesquite::MsqError::INVALID_STATE, and MSQ_SETERR.

Referenced by CompositeOFTest::test_multiply_hessian().

{
    MSQ_SETERR( err )
    ( "Mesquite is not capable of representing the dense "
      "Hessian of the product of two objective fuctions. "
      "Either choose a solver that does not require the "
      "Hessian of the objective function or do not use the "
      "CompositeOFMultiple objective function .",
      MsqError::INVALID_STATE );
    return false;
}
bool MBMesquite::CompositeOFMultiply::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 CompositeOFMultiply.cpp.

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

Referenced by CompositeOFTest::test_multiply_hess_diagonal().

{
    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 )
    {
        diag_out[i] *= value_2;
        mDiagonal[i] *= value_out;
        diag_out[i] += mDiagonal[i];
        diag_out[i] += outer_plus_transpose( grad_out[i], mGradient[i] );

        grad_out[i] *= value_2;
        mGradient[i] *= value_out;
        grad_out[i] += mGradient[i];
    }

    value_out *= value_2;
    return true;
}
bool MBMesquite::CompositeOFMultiply::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 90 of file CompositeOFMultiply.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::CompositeOFMultiply::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 84 of file CompositeOFMultiply.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 183 of file CompositeOFMultiply.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

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

Temporary storage for hessian diagonal

Definition at line 100 of file CompositeOFMultiply.hpp.

Referenced by evaluate_with_Hessian_diagonal().

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

Temporary storage for gradient

Definition at line 98 of file CompositeOFMultiply.hpp.

Referenced by evaluate_with_gradient(), and evaluate_with_Hessian_diagonal().

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