[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/febiosoftware/FEBio/develop/FEBioOpt/FEObjectiveFunction.cpp [Back]  [Original]

/*This file is part of the FEBio source code and is licensed under the MIT license
listed below.

See Copyright-FEBio.txt for details.

Copyright (c) 2021 University of Utah, The Trustees of Columbia University in
the City of New York, and others.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/


#include "stdafx.h"
#include "FEObjectiveFunction.h"
#include 
#include 

//=============================================================================

FEObjectiveFunction::FEObjectiveFunction(FEModel* fem) : m_fem(fem)
{
	m_verbose = false;
}

FEObjectiveFunction::~FEObjectiveFunction()
{
	
}

bool FEObjectiveFunction::Init()
{
	// make sure we have a model
	if (m_fem == 0) return false;

	return true;
}

void FEObjectiveFunction::Reset()
{
}

double FEObjectiveFunction::Evaluate()
{
	vector dummy(Measurements());
	return Evaluate(dummy);
}

double FEObjectiveFunction::Evaluate(vector& y)
{
	// get the number of measurements
	int ndata = Measurements();
	y.resize(ndata);

	// evaluate the functions
	EvaluateFunctions(y);

	// get the measurement vector
	vector y0(ndata);
	GetMeasurements(y0);

	vector x(ndata);
	for (int i = 0; i < ndata; ++i) x[i] = i + 1;
	GetXValues(x);

	// evaluate regression coefficient R^2
	double rsq = RegressionCoefficient(y0, y);

	double chisq = 0.0;
	if (m_verbose) feLog("                        CURRENT        REQUIRED      DIFFERENCE\n");
	for (int i = 0; iInit() == false) return false;

	return true;
}

//----------------------------------------------------------------------------
// set the data source
void FEDataFitObjective::SetDataSource(FEDataSource* src)
{
	if (m_src) delete m_src;
	m_src = src;
}

//----------------------------------------------------------------------------
// set the data measurements
void FEDataFitObjective::SetMeasurements(const vector& data)
{
	m_lc.Clear();
	int n = (int)data.size();
	for (int i=0; iReset();
}

//----------------------------------------------------------------------------
// return the number of measurements. I.e. the size of the measurement vector
int FEDataFitObjective::Measurements()
{
	return m_lc.Points();
}

//----------------------------------------------------------------------------
// Evaluate the measurement vector and return in y0
void FEDataFitObjective::GetMeasurements(vector& y0)
{
	int ndata = m_lc.Points();
	y0.resize(ndata);
	for (int i = 0; iGetParameterValue(ParamString(m_name.c_str()));
	if (val.isValid() == false) return false;
	if (val.type() != FE_PARAM_DOUBLE) return false;
	m_var = (double*)val.data_ptr();
	if (m_var == nullptr) return false;
	return true;
}

FEMinimizeObjective::FilterAvgFunction::FilterAvgFunction(FEModel* fem, FELogElemSource* pd, FEElementSet* elemSet, double trg) : Function(fem)
{
	m_pd = pd;
	m_elemSet = elemSet;
	m_y0 = trg;
}

bool FEMinimizeObjective::FilterAvgFunction::Init()
{
	if (!Function::Init()) return false;
	if (m_pd == nullptr) return false;
	if (m_elemSet == nullptr) return false;
	return true;
}

double FEMinimizeObjective::FilterAvgFunction::Value() const
{
	int NE = m_elemSet->Elements();
	double sum = 0.0;
	for (int i = 0; i < NE; ++i)
	{
		sum += m_pd->value(m_elemSet->Element(i));
	}
	sum /= (double)NE;

	return sum;
}

FEMinimizeObjective::FEMinimizeObjective(FEModel* fem) : FEObjectiveFunction(fem)
{
}

FEMinimizeObjective::~FEMinimizeObjective()
{
	for (Function* f : m_Func) delete f;
	m_Func.clear();
}

void FEMinimizeObjective::AddFunction(FEMinimizeObjective::Function* func)
{
	m_Func.push_back(func);
}

bool FEMinimizeObjective::Init()
{
	if (FEObjectiveFunction::Init() == false) return false;

	FEModel* fem = GetFEModel();
	if (fem == nullptr) return false;

	int N = (int) m_Func.size();
	for (int i=0; i 0);

	int N = (int)m_Data.size();
	f.resize(N);
	FEModel& fem = *GetFEModel();
	FEMesh& mesh = fem.GetMesh();
	for (int i = 0; i < N; ++i)
	{
		int n = m_Data[i].index;
		int v = m_Data[i].ivar;

		// calculate node value
		double val = m_var[v]->value(mesh.Node(n));

		// store result
		f[i] = val;
	}
}

// get the measurement vector (i.e. the y_i above)
void FENodeDataTable::GetMeasurements(vector& y)
{
	int N = (int)m_Data.size();
	y.resize(N);
	for (int i = 0; i < N; ++i)
	{
		y[i] = m_Data[i].target;
	}
}

Web Proxy Viewer  |  New URL  |  Original Page