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

📄 rnn.cpp

📁 一个用于JSP神经网络的演示软件
💻 CPP
字号:
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include "RNN.h"


RNN::RNN(int n, int m, int** machine/* = NULL */, int** protime/* = NULL */, int mcost)
:HNN(n, m, machine, protime), mincost(mcost)
{
	srand(time(0));
}



void RNN::setAPPara(double tmax, double tmin, double tr, int tk)
{
	Tmax = tmax;
	Tmin = tmin;
	r = tr;
	k = tk;
}


bool RNN::run()
{
	initI();
	initNeros();
	cost = HUGENUM; //模拟退火前将成本置为无穷大

	double starttime = GetTickCount();
	for (double T = Tmax; T > Tmin; T = T*r)
	{
		int ck = k;
		while (ck > 0)
		{
			//按随机爬山依次调整每个神经元输出
			cout<<endl<<"T: "<<T<<endl;
			int res = randomSearch(T);
			if (res == 0) 
			{
				double endtime = GetTickCount();
				double runtime = (endtime - starttime)/1000.0;
				formatTime(runtime);
				return true;
			}
			if (res == 1) break;
			ck--;
		}
	}
	double endtime = GetTickCount();
	double runtime = (endtime - starttime)/1000.0;
	formatTime(runtime);

	return chart->isValid();

}


int RNN::randomSearch(double T)
{
	int pcost;			//上一次最大成本
	int dcost;          //最大成本变化量
	double dE = 0;

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N+1; j++)
		{

			//第一步:运行网络并达到稳定状态
			for(int t = 0; t <1000; t++)
			{
				//解神经元运动方程 c*du/dt = -u/r+M
				solveMotionEqu();
				
				//计算神经元输出
				neroOutputs();	

				//判断神经网络是否已经收敛	
				double pE = E;	
				countE();
				if (fabs(E-pE)<0.00001)  break;		
			}

	
			//第二步:随机爬山
			pcost = cost; 
			
			if (abs(E)>0.0001)	cost = HUGENUM;
			else
			{
				chart->setMatrix(v);
				chart->constructGantt();
				cost = chart->getMaxTime();
			}
			cout<<cost<<" ";

			if (mincost > 0 && cost <= mincost) return 0;
			dcost = cost - pcost;
			if (dcost < 0) return 1;
			
			dE = -M[i][j]; //dE/dvij刚好是-M[i][j] (文档中有说明)
			if (dE < 0)
				dE += dcost;
			else
				dE -= dcost;
			double p = 1/(1+exp(-dE/T));
			
			srand(time(0));
			double ran = (double)rand()/(double)RAND_MAX;
			if (p > ran)	
			{
				v[i][j] = 1;
				u[i][j] = 0.4;
			}
			else	
			{
				v[i][j] = 0;
				u[i][j] = -0.4;
			}

		}		
	}

	return 2;
}

⌨️ 快捷键说明

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