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

📄 pso.cpp

📁 粒子群算法与遗传算法用于优化的问题求解,可以解决一些
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// pso.cpp : Defines the entry point for the console application.
//
//pso 根据   ga-3自适应改进2   修改。
//2005 4.28
//2005 10.29
#include <stdio.h>
#include "stdafx.h"
#include "iomanip.h"

ofstream  outfile("pso_output.txt");		//输出文件
ofstream  outfilezz("pso_outputzz.txt");






//输入参数
int popsize;			//种群大小
int chromosize;			//染色体片断长度
int M;					//重复代数
int EG;					//PSO进化代数
int Numofpso;               //pso进化的周期数;

int GLs;                     //GA进化的周期数
int GEG;					//GA一个周期进化的代数



//中间参数
int chromo_length;		//染色体的总长度
int cycle;				//当前周期
int gen;				//当前代数
int	best_cycle;			//最佳周期
int best_gen;			//最佳个体生成代
int min_sequence_length=65535;//sequence中最短的长度
int max_sequence_length=0;//sequence中最长的长度
double sumfitness;//种群适应度的总和
double avefitness;//种群适应度的平均值
double maxfitness;//种群最大适应度
double minfitness = 50000;//种群最小适应度

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!add for pso
float maxv;    //最大速度
float minv;    //最小速度
int minx ;    //最小变量值
int maxx ;    //最大变量值

float w ;//= 1 ;
float c1 = 1.8f ;
float c2 = 1.8f ;
float c3 = 1.8f;


//float gbestfit; //最大的适应值

//指针定义
int   *gbestp;  //最佳位置

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!add for pso

//数据输入
char sequence[19][70]={
"ccuagugacaauagcggagaggaaacacccguucccaucccgaacacggaaguuaag",
"ccuaguggugauagcggaggggaaacacccguucccaucccgaacacggaaguuaag",
"uuugguggcgauagcgaagaggucacacccguucccauaccgaacacggaaguuaag",
"uuugguggcgauagcgaagaggucacacccguucucaugccgaacacggaaguuaag",
"ucugguggcgauagcgaagaggucacacccguucccauaccgaacacggaaguuaag",
"uguugugaugauggcauugaggucacaccuguucccauaccgaacacagaaguuaag",
"ugccuggcggccguagcgcgguggucccaccugaccccaugccgaacucagaagugaaa",
"ugucuggcggccauagcgcaguggucccaccugaucccaugccgaacucagaagugaaa",
"ugcuuggcgaccauagcguuauggacccaccugaucccuugccgaacucaguagugaaa",
"guuucgguggucauagcgugagggaaacgcccgguuacauuccgaacccggaagcuaag",
"uccagugucuaugacuuagagguaacacuccuucccauuccgaacaggcagguuaag",
"uguucuuugacgaguaguagcauuggaacaccugaucccaucccgaacucagaggugaaa",
"uggccugguggucauugcgggcucgaaacacccgaucccaucccgaacucggccgugaaa",
"uccuggugucuauggcgguauggaaccacucugaccccaucccgaacucaguugugaaa",
"uauucugguguccuaggcguagaggaaccacaccaauccaucccgaacuuggugguuaaa",
"uauucuggugcuccuaggcguagaggaaccaaaccaauccaucccgaacuuggugguuaaa",
"aauccccgcccuuagcggcguggaacacccguucccauuccgaacacggaagugaaa",
"uuaaggcggccauagcggugggguuacucccguacccaucccgaacacggaagauaag",
"guucacauccgccaggacgcggcgauuacacccgguauccagcccgaacccggaagcgaaa"};
const int sequence_number=19;
int sequence_length[19]={57,57,57,57,57,57,59,59,59,59,57,60,60,59,60,61,57,58,61};


class RandomNumber  impor;     //产生随机数需要的类

struct individual		//个体定义
{
	int *chromosome;		//染色体
	double fitness;				//个体适应度

	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!add for pso
	int *lbestp;           //个体局部最优位置
	double  lbestfit;            //个体局部最佳适应度
    double  *speed;              //个体速度
	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!add for pso

};

struct individual *oldpop; //当前代种群
struct individual *newpop; //新一代种群
struct individual *midpop;//中间种群,保存父子两代种群,然后选择
struct individual *temper;	//临时种群
struct individual *optimal_indivi;	//最佳个体
struct individual *oldpopz1; //用于原方法
struct individual *oldpopz2; //用于改进方法
struct individual *pz; //当前代种群平均


struct individual *oldpopz3; //用于原方法
struct individual *oldpopz4; //用于改进方法


void inputdata()		//遗传算法参数输入
{
	cout<<"请输入种群大小和染色体片断长度.(10 6)\n";
	cin>>popsize>>chromosize;
	cout<<"请输入GA进化周期数和进化代数(20 2000)\n";
	cin>>GLs>>GEG;
	cout<<"请输入PSO进化周期数,PSO进化代数(1000 1000)\n";
	cin>>Numofpso>>EG;

}

void initreport()      //初始参数输出
{
	outfile<<"                      微粒群算法参数\n";
	outfile<<"--------------------------------------------------------------\n";
	outfile<<"              种群大小(popsize)       ="<<popsize<<endl;
	outfile<<"              染色体片断长度(chromosize)   ="<<chromosize<<endl;
	outfile<<"              GA进化周期数(GLs)          ="<<GLs<<endl;
	outfile<<"              GA进化代数(GEG)            ="<<GEG<<endl;
	outfile<<"              PSO进化周期数(Numofpso)    ="<<Numofpso<<endl;
	outfile<<"              PSO进化代数(EG)            ="<<EG<<endl;
	outfile<<"              PSO最大速度为(vmax)           ="<<maxv<<endl;
	outfile<<"---------------------------------------------------------------\n";

outfilezz<<"                      微粒群算法参数\n";
	outfilezz<<"--------------------------------------------------------------\n";
	outfilezz<<"              种群大小(popsize)       ="<<popsize<<endl;
	outfilezz<<"              染色体片断长度(chromosize)   ="<<chromosize<<endl;
	outfilezz<<"              GA进化周期数(GLs)          ="<<GLs<<endl;
	outfilezz<<"              GA进化代数(GEG)            ="<<GEG<<endl;
	outfilezz<<"              PSO进化周期数(Numofpso)    ="<<Numofpso<<endl;
	outfilezz<<"              PSO进化代数(EG)            ="<<EG<<endl;
	outfilezz<<"              PSO最大速度为(vmax)           ="<<maxv<<endl;
	outfilezz<<"---------------------------------------------------------------\n";





}




void initmalloc()         //为全局数据变量分配空间
{
	if((oldpop=new struct individual[popsize])==NULL)
		cout<<"can't allocate more memory.\n";

	if((oldpopz3=new struct individual[popsize])==NULL)
		cout<<"can't allocate more memory.\n";

	if((oldpopz4=new struct individual[popsize])==NULL)
		cout<<"can't allocate more memory.\n";





	if((newpop=new struct individual[popsize])==NULL)
		cout<<"can't allocate more memory.\n";
	
	if((midpop=new struct individual[popsize])==NULL)
		cout<<"can't allocate more memory.\n";


	if((optimal_indivi=new struct individual)==NULL)
		cout<<"can't allocate more memory.\n";


if((oldpopz1=new struct individual)==NULL)
		cout<<"can't allocate more memory.\n";
if((oldpopz2=new struct individual)==NULL)
		cout<<"can't allocate more memory.\n";
if((pz=new struct individual)==NULL)
		cout<<"can't allocate more memory.\n";


	if((temper= new struct individual)==NULL)
		cout<<"can't allocate more memory.\n";

	if((temper->chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";

	for(int j=0;j<popsize;j++)
	{
		if((oldpop[j].chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpop[j].speed=new double[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpop[j].lbestp=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		
		
	if((oldpopz3[j].chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpopz3[j].speed=new double[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpopz3[j].lbestp=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";



	if((oldpopz4[j].chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpopz4[j].speed=new double[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((oldpopz4[j].lbestp=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";

		
		
		if((newpop[j].chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((newpop[j].speed=new double[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((newpop[j].lbestp=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		
		
		if((midpop[j].chromosome=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((midpop[j].speed=new double[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
		if((midpop[j].lbestp=new int[chromo_length])==NULL)
			cout<<"can't allocate more memory.\n";
	}

	
	
	
	if((optimal_indivi->chromosome=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((optimal_indivi->speed=new double[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((optimal_indivi->lbestp=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";


if((oldpopz1->chromosome=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((oldpopz1->speed=new double[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((oldpopz1->lbestp=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";

if((oldpopz2->chromosome=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((oldpopz2->speed=new double[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((oldpopz2->lbestp=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";


if((pz->chromosome=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((pz->speed=new double[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";
	if((pz->lbestp=new int[chromo_length])==NULL)
		cout<<"can't allocate more memory.\n";

}

RandomNumber::RandomNumber (unsigned long s)     //类函数定义
{
    if (s == 0)
        randSeed = time(0); 
    else
        randSeed = s;       
}

unsigned short RandomNumber::Random (unsigned long n)     //类函数定义
{
    randSeed = multiplier * randSeed + adder;
    return (unsigned short)((randSeed >> 16)  % n);
}

float randomperc()           //随机产生一个0到1之间的小数
{
	unsigned a;
	float count;
	a=impor.Random(10000);
	count=(float)a/10000;
	return count;
}

int rnd(int low,int high)         //随机产生一个low到high之间的整数
{
	int i;
	i=(int)(randomperc()*(high-low+1)+low);
	if(i>high)
		i=high;
	return i;
}

void fitness(struct individual *critter)     //计算适应度函数
{
	int *array;
	int temp;
	int h=0;
	double sum=0.0;
	
	char *alignment[sequence_number];		//定义alignment序列
	
	if((array=new int[chromosize])==NULL)
		cout<<"can't allocate more memory.\n";

	for(int w=0;w<sequence_number;w++)
	{
		if((alignment[w]=new char[chromosize+min_sequence_length])==NULL)
			cout<<"can't allocate more memory.\n";
	}

	for(int i=0;i<sequence_number;i++)
	{
		for(int j=0;j<chromosize-(sequence_length[i]-min_sequence_length);j++)
			array[j]=critter->chromosome[j+h];	//将染色体片断的值放入临时数组array中
		h+=chromosize-(sequence_length[i]-min_sequence_length);

		for(int m=chromosize-(sequence_length[i]-min_sequence_length);m>1;m--)		//冒泡排序 使得array数组中的值从小到大排列
			for(int n=0;n<m-1;n++)
				if(array[n]>array[n+1])
				{
					temp=array[n+1];
					array[n+1]=array[n];
					array[n]=temp;
				}
		

⌨️ 快捷键说明

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