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

📄 particletsp.cpp

📁 c++语言实现的遗传算法的例子,需要的快下啊
💻 CPP
字号:
// ParticleTSP.cpp: implementation of the ParticleTSP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ParticleTSP.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ParticleTSP::ParticleTSP(int d,Rand * rand)
{
	dimension=d;
	location=new double[dimension];
	length=rand->randint(dimension);
	speed=new int * [length];
	indiv_optim_loc=new double[dimension];
	for(int i=0;i<dimension;i++)
	{
		location[i]=i;
	}
	for(int j=0;j<rand->randint(dimension);j++)
	{
		int n=rand->randint(dimension);
		int m=rand->randint(dimension);
		double temp=location[n];
		location[n]=location[m];
		location[m]=temp;
	}
	for(j=0;j<length;j++)
	{
		speed[j]=new int[2];
		speed[j][0]=rand->randint(dimension);
		speed[j][1]=rand->randint(dimension);
	}
	for(i=0;i<dimension;i++)
	{
		indiv_optim_loc[i]=location[i];
	}
}

void ParticleTSP::SetSpeed(int * * s,int l)
{
	for(int i=0;i<length;i++)
		delete [] speed[i];
	delete[] speed;
	length=l;
	speed=new int *[length];
	for(i=0;i<length;i++)
	{
		speed[i]=new int[2];
		speed[i][0]=s[i][0];
		speed[i][1]=s[i][1];
	}
}

void ParticleTSP::SetIO()
{
	for(int i=0;i<dimension;i++)
		indiv_optim_loc[i]=location[i];
	optim_solution=solution;
}

void ParticleTSP::SetIOL(double * li)
{
	for(int i=0;i<dimension;i++)
		indiv_optim_loc[i]=li[i];
}

void ParticleTSP::SetOS(double s)
{
	optim_solution=s;
}

void ParticleTSP::SetLocation(double * l)
{
	for(int i=0;i<dimension;i++)
		location[i]=l[i];
}

void ParticleTSP::SetSolution(double s)
{
	solution=s;
}

double ParticleTSP::GetOS()
{
	return optim_solution;
}

double ParticleTSP::GetSolution()
{
	return solution;
}

double * ParticleTSP::GetLocation()
{
	double * temp=new double[dimension];
	Copy(location,temp);
	return temp;
}

double * ParticleTSP::GetIOL()
{
	double * temp=new double[dimension];
	Copy(indiv_optim_loc,temp);
	return temp;
}

int * * ParticleTSP::GetSpeed()
{
	int * * temp=new int *[length];
	for(int i=0;i<length;i++)
	{
		temp[i]=new int[2];
		temp[i][0]=speed[i][0];
		temp[i][1]=speed[i][1];
	}
	return temp;
}

int ParticleTSP::GetSL()
{
	return length;
}

void ParticleTSP::Copy(double * operand1,double * operand2)
{
	for(int i=0;i<dimension;i++)
		operand2[i]=operand1[i];
}
ParticleTSP::~ParticleTSP()
{
	delete[] location;
	for(int i=0;i<length;i++)
		delete [] speed[i];
	delete[] speed;
	delete[] indiv_optim_loc;
}

⌨️ 快捷键说明

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