2020-12-07 09:49:58 -06:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2020 Equinor ASA
|
|
|
|
//
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RimCustomObjectiveFunction.h"
|
|
|
|
|
|
|
|
#include "RimCustomObjectiveFunctionWeight.h"
|
|
|
|
#include "RimEnsembleCurveSet.h"
|
2024-07-29 08:38:17 -05:00
|
|
|
#include "RimSummaryEnsemble.h"
|
2020-12-07 09:49:58 -06:00
|
|
|
|
|
|
|
#include <cafPdmUiLineEditor.h>
|
|
|
|
#include <cafPdmUiTreeOrdering.h>
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
CAF_PDM_SOURCE_INIT( RimCustomObjectiveFunction, "RimCustomObjectiveFunction" );
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimCustomObjectiveFunction::RimCustomObjectiveFunction()
|
|
|
|
{
|
2022-01-07 01:31:52 -06:00
|
|
|
CAF_PDM_InitObject( "Objective Function", ":/ObjectiveFunction.svg" );
|
2020-12-07 09:49:58 -06:00
|
|
|
|
2021-11-14 07:15:12 -06:00
|
|
|
CAF_PDM_InitFieldNoDefault( &m_functionTitle, "FunctionTitle", "Title" );
|
2020-12-07 09:49:58 -06:00
|
|
|
m_functionTitle.registerGetMethod( this, &RimCustomObjectiveFunction::title );
|
|
|
|
m_functionTitle.uiCapability()->setUiHidden( true );
|
|
|
|
|
2021-11-14 07:15:12 -06:00
|
|
|
CAF_PDM_InitFieldNoDefault( &m_customFunctionTitle, "CustomFunctionTitle", "Title" );
|
2020-12-07 09:49:58 -06:00
|
|
|
|
2021-11-14 07:15:12 -06:00
|
|
|
CAF_PDM_InitFieldNoDefault( &m_weights, "Weights", "" );
|
2020-12-07 09:49:58 -06:00
|
|
|
|
2021-11-14 07:15:12 -06:00
|
|
|
CAF_PDM_InitFieldNoDefault( &m_objectiveFunctions, "ObjectiveFunctions", "Objective Functions" );
|
2021-06-09 09:25:19 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
auto objFunc1 = new RimObjectiveFunction();
|
|
|
|
objFunc1->setFunctionType( RimObjectiveFunction::FunctionType::F1 );
|
|
|
|
m_objectiveFunctions.push_back( objFunc1 );
|
|
|
|
objFunc1->changed.connect( this, &RimCustomObjectiveFunction::onObjectiveFunctionChanged );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto objFunc1 = new RimObjectiveFunction();
|
|
|
|
objFunc1->setFunctionType( RimObjectiveFunction::FunctionType::F2 );
|
|
|
|
m_objectiveFunctions.push_back( objFunc1 );
|
|
|
|
objFunc1->changed.connect( this, &RimCustomObjectiveFunction::onObjectiveFunctionChanged );
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
m_isValid = true;
|
2020-12-11 07:17:33 -06:00
|
|
|
|
|
|
|
setDeletable( true );
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimCustomObjectiveFunctionWeight* RimCustomObjectiveFunction::addWeight()
|
|
|
|
{
|
|
|
|
auto newWeight = new RimCustomObjectiveFunctionWeight();
|
|
|
|
m_weights.push_back( newWeight );
|
|
|
|
return newWeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
std::vector<RimCustomObjectiveFunctionWeight*> RimCustomObjectiveFunction::weights() const
|
|
|
|
{
|
2023-05-12 14:41:34 -05:00
|
|
|
return m_weights.childrenByType();
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-06-09 09:25:19 -05:00
|
|
|
std::vector<double> RimCustomObjectiveFunction::functionValueForAllCases() const
|
2020-12-07 09:49:58 -06:00
|
|
|
{
|
2021-06-09 09:25:19 -05:00
|
|
|
if ( m_weights.empty() ) return {};
|
|
|
|
|
2024-07-29 08:38:17 -05:00
|
|
|
RimSummaryEnsemble* caseCollection = parentCurveSet()->summaryEnsemble();
|
2021-06-09 09:25:19 -05:00
|
|
|
if ( !caseCollection || caseCollection->allSummaryCases().empty() ) return {};
|
|
|
|
|
|
|
|
std::vector<double> values;
|
|
|
|
|
|
|
|
for ( auto sumCase : caseCollection->allSummaryCases() )
|
2020-12-07 09:49:58 -06:00
|
|
|
{
|
2021-06-09 09:25:19 -05:00
|
|
|
auto functionValue = value( sumCase );
|
|
|
|
values.push_back( functionValue );
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
2021-06-09 09:25:19 -05:00
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
double RimCustomObjectiveFunction::value( RimSummaryCase* summaryCase ) const
|
|
|
|
{
|
2021-06-09 09:25:19 -05:00
|
|
|
if ( m_functionValueForAllCases.count( summaryCase ) > 0 )
|
|
|
|
{
|
|
|
|
return m_functionValueForAllCases[summaryCase];
|
|
|
|
}
|
|
|
|
|
|
|
|
double value = 0.0;
|
2020-12-07 09:49:58 -06:00
|
|
|
for ( auto weight : m_weights )
|
|
|
|
{
|
2023-02-26 03:48:40 -06:00
|
|
|
double functionValue = objectiveFunction( weight->objectiveFunction() )
|
|
|
|
->value( summaryCase, weight->summaryAddresses(), parentCurveSet()->objectiveFunctionTimeConfig() );
|
2020-12-07 09:49:58 -06:00
|
|
|
|
|
|
|
value += weight->weightValue() * functionValue;
|
|
|
|
}
|
2021-06-09 09:25:19 -05:00
|
|
|
|
|
|
|
m_functionValueForAllCases[summaryCase] = value;
|
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
std::pair<double, double> RimCustomObjectiveFunction::minMaxValues() const
|
|
|
|
{
|
|
|
|
double minValue = std::numeric_limits<double>::infinity();
|
|
|
|
double maxValue = -std::numeric_limits<double>::infinity();
|
|
|
|
|
2021-06-09 09:25:19 -05:00
|
|
|
for ( auto value : functionValueForAllCases() )
|
2020-12-07 09:49:58 -06:00
|
|
|
{
|
|
|
|
if ( value != std::numeric_limits<double>::infinity() )
|
|
|
|
{
|
|
|
|
if ( value < minValue ) minValue = value;
|
|
|
|
if ( value > maxValue ) maxValue = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair( minValue, maxValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RimCustomObjectiveFunction::weightContainsFunctionType( RimObjectiveFunction::FunctionType functionType ) const
|
|
|
|
{
|
|
|
|
for ( auto weight : m_weights )
|
|
|
|
{
|
|
|
|
if ( weight->objectiveFunction() == functionType )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RimCustomObjectiveFunction::title() const
|
|
|
|
{
|
|
|
|
if ( m_customFunctionTitle().isEmpty() )
|
|
|
|
{
|
|
|
|
return autoGeneratedTitle();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_customFunctionTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RimCustomObjectiveFunction::autoGeneratedTitle() const
|
|
|
|
{
|
|
|
|
QStringList titles;
|
|
|
|
for ( auto weight : m_weights )
|
|
|
|
{
|
|
|
|
titles << weight->title();
|
|
|
|
}
|
|
|
|
if ( titles.count() == 0 )
|
|
|
|
{
|
|
|
|
titles << QString( "New Objective Function" );
|
|
|
|
}
|
|
|
|
return titles.join( " + " );
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RimCustomObjectiveFunction::isValid() const
|
|
|
|
{
|
2023-09-27 01:07:49 -05:00
|
|
|
return !m_weights.empty() && m_isValid;
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimCustomObjectiveFunction::onWeightChanged()
|
|
|
|
{
|
2021-06-09 09:25:19 -05:00
|
|
|
m_functionValueForAllCases.clear();
|
2020-12-07 09:49:58 -06:00
|
|
|
parentCollection()->onObjectiveFunctionChanged( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimCustomObjectiveFunction::invalidate()
|
|
|
|
{
|
|
|
|
m_isValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RimCustomObjectiveFunction::formulaString( std::vector<RifEclipseSummaryAddress> vectorSummaryAddresses ) const
|
|
|
|
{
|
|
|
|
QString formula = "Custom Objective Function = ";
|
|
|
|
if ( !m_customFunctionTitle().isEmpty() )
|
|
|
|
{
|
|
|
|
formula = m_customFunctionTitle();
|
|
|
|
}
|
|
|
|
QStringList weightFormulae;
|
|
|
|
for ( auto weight : weights() )
|
|
|
|
{
|
2023-02-26 03:48:40 -06:00
|
|
|
weightFormulae << QString( "%0 * %1" )
|
|
|
|
.arg( weight->weightValue() )
|
|
|
|
.arg( objectiveFunction( weight->objectiveFunction() )->formulaString( vectorSummaryAddresses ) );
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
|
|
|
formula += weightFormulae.join( " + " );
|
|
|
|
return formula;
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:25:19 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimCustomObjectiveFunction::clearCache()
|
|
|
|
{
|
|
|
|
m_functionValueForAllCases.clear();
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
caf::PdmFieldHandle* RimCustomObjectiveFunction::userDescriptionField()
|
|
|
|
{
|
|
|
|
return &m_functionTitle;
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:25:19 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimCustomObjectiveFunction::onObjectiveFunctionChanged( const caf::SignalEmitter* emitter )
|
|
|
|
{
|
|
|
|
onWeightChanged();
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2023-02-26 03:48:40 -06:00
|
|
|
void RimCustomObjectiveFunction::defineUiTreeOrdering( caf::PdmUiTreeOrdering& uiTreeOrdering, QString uiConfigName /* = "" */ )
|
2020-12-07 09:49:58 -06:00
|
|
|
{
|
|
|
|
for ( auto weight : weights() )
|
|
|
|
{
|
|
|
|
uiTreeOrdering.add( weight );
|
|
|
|
}
|
|
|
|
uiTreeOrdering.skipRemainingChildren( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2023-02-26 03:48:40 -06:00
|
|
|
void RimCustomObjectiveFunction::defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute )
|
2020-12-07 09:49:58 -06:00
|
|
|
{
|
|
|
|
if ( field == &m_customFunctionTitle )
|
|
|
|
{
|
|
|
|
caf::PdmUiLineEditorAttribute* attrib = dynamic_cast<caf::PdmUiLineEditorAttribute*>( attribute );
|
|
|
|
if ( attrib )
|
|
|
|
{
|
|
|
|
attrib->placeholderText = autoGeneratedTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:25:19 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimCustomObjectiveFunction::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
|
|
|
|
{
|
|
|
|
uiOrdering.add( &m_customFunctionTitle );
|
|
|
|
|
|
|
|
{
|
|
|
|
auto objFunc1 = objectiveFunction( RimObjectiveFunction::FunctionType::F1 );
|
|
|
|
if ( objFunc1 )
|
|
|
|
{
|
|
|
|
auto equationGroup = uiOrdering.addNewGroup( "Equation for Time Range" );
|
|
|
|
|
|
|
|
objFunc1->hideFunctionSelection();
|
|
|
|
objFunc1->uiOrdering( "", *equationGroup );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto objFunc2 = objectiveFunction( RimObjectiveFunction::FunctionType::F2 );
|
|
|
|
{
|
|
|
|
auto equationGroup = uiOrdering.addNewGroup( "Equation for Selected Time Range" );
|
|
|
|
|
|
|
|
objFunc2->hideFunctionSelection();
|
|
|
|
objFunc2->uiOrdering( "", *equationGroup );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:49:58 -06:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimEnsembleCurveSet* RimCustomObjectiveFunction::parentCurveSet() const
|
|
|
|
{
|
2023-05-12 14:41:34 -05:00
|
|
|
return firstAncestorOrThisOfType<RimEnsembleCurveSet>();
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimCustomObjectiveFunctionCollection* RimCustomObjectiveFunction::parentCollection() const
|
|
|
|
{
|
2023-05-12 14:41:34 -05:00
|
|
|
return firstAncestorOrThisOfType<RimCustomObjectiveFunctionCollection>();
|
2020-12-07 09:49:58 -06:00
|
|
|
}
|
2021-06-09 09:25:19 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimObjectiveFunction* RimCustomObjectiveFunction::objectiveFunction( RimObjectiveFunction::FunctionType functionType ) const
|
|
|
|
{
|
2023-05-12 14:41:34 -05:00
|
|
|
for ( auto objectiveFunc : m_objectiveFunctions.childrenByType() )
|
2021-06-09 09:25:19 -05:00
|
|
|
{
|
|
|
|
if ( objectiveFunc->functionType() == functionType )
|
|
|
|
{
|
|
|
|
return objectiveFunc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|