gsearch.cpp

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 147 行

CPP
147
字号
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#include "GSearch.h"#include <string.h>GRealVectorCritic::GRealVectorCritic(int nVectorSize){	m_nVectorSize = nVectorSize;	m_dBestError = 1e100;	m_pBestYet = new double[nVectorSize];	int i;	for(i = 0; i < nVectorSize; i++)		m_pBestYet[i] = 0;}GRealVectorCritic::~GRealVectorCritic(){	delete(m_pBestYet);}double GRealVectorCritic::Critique(double* pVector){	double dError = ComputeError(pVector);	if(dError < m_dBestError)	{		m_dBestError = dError;		memcpy(m_pBestYet, pVector, sizeof(double) * m_nVectorSize);	}	return dError;}// -------------------------------------------------------GRealVectorSearch::GRealVectorSearch(GRealVectorCritic* pCritic){	m_pCritic = pCritic;}/*virtual*/ GRealVectorSearch::~GRealVectorSearch(){}// -------------------------------------------------------class GAction{protected:	int m_nAction;	GAction* m_pPrev;	unsigned int m_nRefs;	~GAction()	{		if(m_pPrev)			m_pPrev->Release(); // todo: this could overflow the stack with recursion	}public:	GAction(int nAction, GAction* pPrev)	 : m_nAction(nAction), m_nRefs(0)	{		m_pPrev = pPrev;		if(pPrev)			pPrev->AddRef();	}	void AddRef()	{		m_nRefs++;	}	void Release()	{		if(--m_nRefs == 0)			delete(this);	}	GAction* GetPrev() { return m_pPrev; }	int GetAction() { return m_nAction; }};// -------------------------------------------------------GActionPath::GActionPath(GActionPathState* pState) : m_pLastAction(NULL), m_nPathLen(0){	m_pHeadState = pState;}GActionPath::~GActionPath(){	if(m_pLastAction)		m_pLastAction->Release();	delete(m_pHeadState);}void GActionPath::DoAction(int nAction){	GAction* pPrevAction = m_pLastAction;	m_pLastAction = new GAction(nAction, pPrevAction);	m_pLastAction->AddRef(); // referenced by m_pLastAction	if(pPrevAction)		pPrevAction->Release(); // no longer referenced by m_pLastAction	m_nPathLen++;	m_pHeadState->PerformAction(nAction);}GActionPath* GActionPath::Fork(){	GActionPath* pNewPath = new GActionPath(m_pHeadState->Copy());	pNewPath->m_nPathLen = m_nPathLen;	pNewPath->m_pLastAction = m_pLastAction;	if(m_pLastAction)		m_pLastAction->AddRef();	return pNewPath;}void GActionPath::GetPath(int nCount, int* pOutBuf){	while(nCount > m_nPathLen)		pOutBuf[--nCount] = -1;	int i = m_nPathLen;	GAction* pAction = m_pLastAction;	while(i > nCount)	{		i--;		pAction = pAction->GetPrev();	}	while(i > 0)	{		pOutBuf[--i] = pAction->GetAction();		pAction = pAction->GetPrev();	}}double GActionPath::Critique(){	return m_pHeadState->CritiquePath(m_nPathLen, m_pLastAction);}

⌨️ 快捷键说明

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