⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgradientfunction.cpp

📁 强化学习算法(R-Learning)难得的珍贵资料
💻 CPP
字号:
// Copyright (C) 2003
// Gerhard Neumann (gerhard@igi.tu-graz.ac.at)

//                
// This file is part of RL Toolbox.
// http://www.igi.tugraz.at/ril_toolbox
//
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "ril_debug.h"
#include "cgradientfunction.h"

#include <math.h>


CIndividualEtaCalculator::CIndividualEtaCalculator(int numWeights, rlt_real *l_etas)
{
	this->numWeights = numWeights;
	this->etas = new rlt_real[numWeights];

	if (l_etas != NULL)
	{
		memcpy(this->etas, l_etas, sizeof(rlt_real) * numWeights);
	}
	else
	{
		for (int i = 0; i < numWeights; i++)
		{
			etas[i] = 1.0;
		}
	}
}

CIndividualEtaCalculator::~CIndividualEtaCalculator()
{
	delete etas;
}


void CIndividualEtaCalculator::getWeightUpdates(CFeatureList *updates)
{
	CFeatureList::iterator it = updates->begin();
	for (; it != updates->end(); it ++)
	{
		(*it)->factor *= etas[(*it)->featureIndex];
	}
}

void CIndividualEtaCalculator::setEta(int index, rlt_real value)
{
	assert(index >= 0 && index < numWeights);
	etas[index] = value;
}

CVarioEta::CVarioEta(unsigned int numParams, rlt_real eta, rlt_real beta, rlt_real epsilon)
{
	/*this->beta = beta;
	this->epsilon = epsilon;
	this->eta = eta;*/
	addParameter("VarioEtaLearningRate", eta);
	addParameter("VarioEtaBeta", beta);
	addParameter("VarioEtaEpsilon",epsilon);

	this->numParams = numParams;

	eta_i = new rlt_real[numParams];
	v_i = new rlt_real[numParams];

	for (unsigned int i = 0; i < numParams; i++)
	{
		v_i[i] = 1;
		eta_i[i] = eta / (sqrt(v_i[i]) + epsilon);
	}
}

CVarioEta::~CVarioEta()
{
	delete eta_i;
	delete v_i;
}

void CVarioEta::getWeightUpdates(CFeatureList *Updates)
{
	rlt_real epsilon = getParameter("VarioEtaEpsilon");
	rlt_real beta = getParameter("VarioEtaBeta");
	rlt_real eta = getParameter("VarioEtaLearningRate");

	for (unsigned int i = 0; i < numParams; i ++)
	{
		eta_i[i] = eta / (sqrt(v_i[i]) + epsilon);
		v_i[i] *= (1 - beta); 
	}

	/*for (unsigned int i = 0; i < numParams; i ++)
	{
	v_i[i] = v_i[i] * (1 - beta);
	}*/

	CFeatureList::iterator it = Updates->begin();
	for (;it != Updates->end(); it ++)
	{
		v_i[(*it)->featureIndex] += beta * pow((*it)->factor / eta, 2);
		DebugPrint('v', "Vario Eta: Updating Feature %d with Eta %f (v_i: %f)\n", (*it)->featureIndex,eta_i[(*it)->featureIndex], v_i[(*it)->featureIndex]);
		(*it)->factor = (*it)->factor * eta_i[(*it)->featureIndex];
	}
}

CGradientUpdateFunction::CGradientUpdateFunction()
{
	localGradientFeatureBuffer = new CFeatureList();

	etaCalc = NULL;
}

CGradientUpdateFunction::~CGradientUpdateFunction()
{
	delete localGradientFeatureBuffer;
}


void CGradientUpdateFunction::updateGradient(CFeatureList *gradientFeatures, rlt_real factor)
{
	if (gradientFeatures != this->localGradientFeatureBuffer)
	{
		this->localGradientFeatureBuffer->clear();

		CFeatureList::iterator it1 = gradientFeatures->begin();

		for (; it1 != gradientFeatures->end(); it1 ++)
		{
			this->localGradientFeatureBuffer->update((*it1)->featureIndex, factor * (*it1)->factor);
		}
	}
	else
	{
		localGradientFeatureBuffer->multFactor(factor);
	}

	if (etaCalc)
	{
		etaCalc->getWeightUpdates(this->localGradientFeatureBuffer);
	}

	updateWeights(this->localGradientFeatureBuffer);
}

CAdaptiveEtaCalculator* CGradientUpdateFunction::getEtaCalculator()
{
	return getEtaCalculator();	
}

void CGradientUpdateFunction::setEtaCalculator(CAdaptiveEtaCalculator *etaCalc)
{
	addParameters(etaCalc);
	this->etaCalc = etaCalc;
}

void CGradientUpdateFunction::saveData(FILE *stream)
{
	rlt_real *parameters = new rlt_real[getNumWeights()];
	getWeights(parameters);

	fprintf(stream, "Gradient Function\n");
	fprintf(stream, "Parameters: %d\n", getNumWeights());

	for (int i = 0; i < getNumWeights(); i ++)
	{
		fprintf(stream, "%f ", parameters[i]);
	}
	fprintf(stream, "\n");
	delete parameters;
}

void CGradientUpdateFunction::loadData(FILE *stream)
{
	rlt_real *parameters = new rlt_real[getNumWeights()];
	int bufNumParam;

	fscanf(stream, "Gradient Function\n");
	fscanf(stream, "Parameters: %d\n", &bufNumParam);

	assert(bufNumParam == getNumWeights());

	for (int i = 0; i < getNumWeights(); i ++)
	{
		fscanf(stream, "%lf ", &parameters[i]);
	}
	fscanf(stream, "\n");
	setWeights(parameters);

	delete parameters;
}


CGradientDelayedUpdateFunction::CGradientDelayedUpdateFunction(CGradientUpdateFunction *gradientFunction)
{
	this->gradientFunction = gradientFunction;

	weightsUpdate = new rlt_real[gradientFunction->getNumWeights()];
}

CGradientDelayedUpdateFunction::~CGradientDelayedUpdateFunction()
{
	delete weightsUpdate;
}

void CGradientDelayedUpdateFunction::updateWeights(CFeatureList *dParams)
{
	CFeatureList::iterator it = dParams->begin();
	for (; it != dParams->end(); it ++)
	{
		weightsUpdate[(*it)->featureIndex] += (*it)->factor;
	}
}

int CGradientDelayedUpdateFunction::getNumWeights()
{
	return gradientFunction->getNumWeights();
}

void CGradientDelayedUpdateFunction::getWeights(rlt_real *parameters)
{
	return gradientFunction->getWeights(parameters);
}

void CGradientDelayedUpdateFunction::setWeights(rlt_real *parameters)
{
	gradientFunction->setWeights(parameters);
}

void CGradientDelayedUpdateFunction::resetData()
{
	memset(weightsUpdate, 0, sizeof(int) * getNumWeights());
}

void CGradientDelayedUpdateFunction::updateOriginalGradientFunction()
{
	gradientFunction->setWeights(weightsUpdate);
}


CDelayedFunctionUpdater::CDelayedFunctionUpdater(CGradientDelayedUpdateFunction * updateFunction, int nUpdateEpisodes, int nUpdateSteps)
{
	this->updateFunction = updateFunction;

	this->nUpdateEpisodes = nUpdateEpisodes;
	this->nUpdateSteps = nUpdateSteps;

	nEpisodes = 0;
	nSteps = 0;
}

CDelayedFunctionUpdater::~CDelayedFunctionUpdater()
{

}

void CDelayedFunctionUpdater::newEpisode()
{
	nEpisodes ++;
	if (nUpdateEpisodes > 0 && (nEpisodes % nUpdateEpisodes == 0))
	{
		updateFunction->updateOriginalGradientFunction();
	}
}

void CDelayedFunctionUpdater::nextStep(CStateCollection *oldState, CAction *action, CStateCollection *nextState)
{
	nSteps ++;
	if (nUpdateSteps > 0 && (nSteps % nUpdateSteps == 0))
	{
		updateFunction->updateOriginalGradientFunction();
	}
}


CGradientFunction::CGradientFunction()
{
}

CGradientFunction::~CGradientFunction()
{
}
/*
CComposedGradientFunction::CComposedGradientFunction(CGradientFunction *gradientFunction1, CGradientFunction *gradientFunction2)
{
	this->gradientFunction1 = gradientFunction1;
	this->gradientFunction2 = gradientFunction2;
}

CComposedGradientFunction::~CComposedGradientFunction();
{

}

void CComposedGradientFunction::getGradient(CMyVector *input, CMyVector *outputErrors, CFeatureList *gradientFeatures)
{

	localGradientFeatureBuffer->clear();

	gradientFunction1->getFunctionValue(input, )
	gradientFunction1->getGradient()
}

void CComposedGradientFunction::getFunctionValue(CMyVector *input, CMyVector *output)
{
}

void CComposedGradientFunction::getInputDerivation(CMyVector *input, CMyMatrix *targetVector)
{
}

int CComposedGradientFunction::getNumInputs()
{
}

int CComposedGradientFunction::getNumOutputs()
{
}

void CComposedGradientFunction::updateWeights(CFeatureList *dParams)
{
}

int CComposedGradientFunction::getNumWeights()
{
}

void CComposedGradientFunction::getWeights(rlt_real *parameters)
{
}

void CComposedGradientFunction::setWeights(rlt_real *parameters)
{
}

void CComposedGradientFunction::resetData()
{
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -