123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //////////////////////////////////////////////////////////////////////
- //
- // DimWrapperCostFunction.cpp: implementation of the DimWrapperCostFunction class.
- //
- // Written By: Matthias Wacker
- //
- //////////////////////////////////////////////////////////////////////
- #include <iostream>
- #include "DimWrapperCostFunction.h"
- DimWrapperCostFunction::DimWrapperCostFunction() : SuperClass()
- {
- m_pOrigCostFunc=NULL;
- m_numOfParameters=0;
- }
- DimWrapperCostFunction::DimWrapperCostFunction(const DimWrapperCostFunction &func) : SuperClass(func)
- {
- m_pOrigCostFunc= func.m_pOrigCostFunc;
- m_selMatTransposed = func.m_selMatTransposed;
- m_fixedValues = func.m_fixedValues;
- }
- DimWrapperCostFunction::~DimWrapperCostFunction()
- {
- // nothing dynamically allocated..
- }
- DimWrapperCostFunction::DimWrapperCostFunction(CostFunction *orig, const matrix_type &selectionVector,
- const matrix_type &fixedValues)
- {
- if(orig == NULL)
- {
- std::cerr << "DimWrapperCostFunction::DimWrapperCostFunction NULL pointer error" << std::endl;
- exit(1);
- }
- m_pOrigCostFunc = orig;
- changeSelection(selectionVector,fixedValues);
- }
- void DimWrapperCostFunction::changeSelection(const matrix_type & selectionVector, const matrix_type &fixedValues)
- {
- // BUILD MATRIX
- //count nonzero values:
- m_fixedValues=fixedValues;
- int count=0;
- int dim = selectionVector.rows();
- std::vector<int> indices;
- for(int i=0; i < dim ; ++i)
- {
- if (selectionVector[i][0] != 0.0 )
- {
- count++;
- indices.push_back(i);
- m_fixedValues[i][0]= 0.0; // resets fixed values to zero for active params
- }
- }
- m_numOfParameters = count;
- m_selMatTransposed = matrix_type(dim,count);
- for(int i =0; i < m_selMatTransposed.rows(); ++i)
- {
- for(int j =0; j < m_selMatTransposed.cols(); ++j)
- {
- if( indices[j]==i )
- {
- m_selMatTransposed[i][j]=1.0;
- }
- else
- {
- m_selMatTransposed[i][j]=0.0;
- }
- }
- }
- }
- DimWrapperCostFunction &DimWrapperCostFunction::operator=(const DimWrapperCostFunction &func)
- {
-
- /*
- avoid self-copy
- */
- if(this != &func)
- {
-
- /*
- =operator of SuperClass
- */
- SuperClass::operator=(func);
- /*
- own values:
- */
- m_pOrigCostFunc= func.m_pOrigCostFunc;
- m_selMatTransposed = func.m_selMatTransposed;
- m_fixedValues = func.m_fixedValues;
-
- }
- return *this;
- }
- void DimWrapperCostFunction::init()
- {
- m_pOrigCostFunc->init();
- }
- double DimWrapperCostFunction::evaluate(const matrix_type &x)
- {
- return m_pOrigCostFunc->evaluate(m_selMatTransposed * x + m_fixedValues);
- }
- opt::matrix_type DimWrapperCostFunction::getFullParamsFromSubParams(const opt::matrix_type &x)
- {
- return m_selMatTransposed * x + m_fixedValues;
- }
- /*bool DimWrapperCostFunction::setX0(const opt::matrix_type &x0)
- {
- cout<<"dimWrapperCF - setX0"<<endl;
- if(x0.rows() == static_cast<int>(m_numOfParameters) && x0.cols() == 1)
- {
- cout<<"set m_x_0"<<endl;
- m_x_0 = x0;
- return true;
- }
- else
- return false;
- }
-
- bool DimWrapperCostFunction::setH0(const opt::matrix_type &H0)
- {
- cout<<"dimWrapperCF - setH0"<<endl;
- if(H0.rows() == static_cast<int>(m_numOfParameters) && H0.cols() == 1)
- {
- m_h_0= H0;
- return true;
- }
- else
- return false;
- }
-
- double DimWrapperCostFunction::evaluateSub(double lambda)
- {
- cout<<"dimWrapperCF - evalSub"<<endl;
- double eval= evaluate(m_x_0 + m_h_0 * lambda);
- return eval;
- }*/
|