进击的小白Day022——遗传算法(四)

博主在尝试实现遗传算法的过程中遇到困扰,程序在for循环中出现意外中断,循环次数在10几次到50次之间随机停止。尽管代码已贴出,但问题原因尚未查明。
摘要由CSDN通过智能技术生成

基本流程实现,但程序在for循环及过程中出现问题,设定的50或100次循环,程序运行后会莫名其妙中断在第十几次循环,且每次循环中断的次数随机,目前还没找到问题。

贴代码:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <time.h>
#define PI 3.1415926
#define BOUND_DOWN 0
#define BOUND_UP (0.9 * PI)
#define POP_SIZE 2
#define CHROM_LEN 5
#define EVO_NUM 500
#define CROSS_PROBABILITY 0.6
#define VARY_PROBABILITY 0.6

/*目标函数值*/
float objFuncVal(float *a)
{
	float x[5];
	float f = 0;
	memset(x, 0, sizeof(x));
	x[0] = *a;
	x[1] = *(a + 1);
	x[2] = *(a + 2);
	x[3] = *(a + 3);
	x[4] = *(a + 4);
	f = -5 * sin(x[0])*sin(x[1])*sin(x[2])*sin(x[3])*sin(x[4]) -
		sin(5 * x[0])*sin(5 * x[1])*sin(5 * x[2])*sin(5 * x[3])*sin(5 * x[4]) + 8;
	return f;
}

/*初始化*/
void creat(float chrom[POP_SIZE][CHROM_LEN])
{
	int i = 0, j = 0;	
	for (i = 0; i < POP_SIZE; i++)
	{
		for (j = 0; j < CHROM_LEN; j++)
		{
			chrom[i][j] = BOUND_UP * ((float)rand() / RAND_MAX);
		}
	}
}

/*计算适应度*/
float fit(float *a)
{
	float funcVal = 0, fitness = 0;
	funcVal = objFuncVal(a);
	fitness = 1 / funcVal;
	return fitness;
}

/*计算选择概率和累积概率*/
void fitnessP(float *fitness, float *chooseP, float *accuP)
{
	float fitnessSum = 0;
	int i = 0;
	for (i = 0; i < POP_SIZE; i++)
	{
		fitnessSum += *(fitness + i);
	}
	for (i = 0; i < POP_SIZE; i++)
	{
		*(chooseP + i) = (*(fitness + i)) / fitnessSum;
		if (i == 0)
		{
			*accuP = *chooseP;
		}
		else
		{
			*(accuP + i) = *(accuP + (i - 1)) + *(chooseP + i);
		}
	}
}

void roulette(float *accuP, float chrom[POP_SIZE][CHROM_LEN])
{
	float m = 0;
	float tmp[POP_SIZE][CHROM_LEN];
	int i = 0, j = 0, k = 0;
	for (i = 0; i < POP_SIZE; i++)
	{
		memset(tmp[i], 0, sizeof(tmp[i]));
	}
	for (i = 0; i < POP_SIZE; i++)  //控制进行i次轮盘赌
	{
		m = ((float)rand()) / RAND_MAX;
		//printf("m=%f\n", m);
		if (m <= *accuP)
		{
			for (k = 0; k < CHROM_LEN; k++)
			{
				tmp[i][k] = chrom[0][k];
			}
		}
		else
		{
			for (j = 0; j < POP_SIZE; j++)  //控制轮盘赌后的比较
			{
				if (m >= *(accuP + j) && m < *(accuP + (j + 1)))
				{
					for (k = 0; k < CHROM_LEN; k++)
					{
						tmp[i][k] = chrom[j + 1][k];
					}
				}
			}
		}
		/*for (int h = 0; h < CHROM_LEN; h++)
		{
			printf("tmp[%d][%d]=%f  ", i, h, tmp[i][h]);
		}
		printf("\n");*/
	}
	for (i = 0; i < POP_SIZE; i++)
	{
		for (j = 0; j < CHROM_LEN; j++)
		{
			chrom[i][j] = tmp[i][j];
		}
	}
}

void cross(float chrom[POP_SIZE][CHROM_LEN])
{
	float b = 0, tmp_1, tmp_2, p = 0;
	int pick_1 = 0, pick_2 = 0, i = 0, j = 0, flag = 0;
	for (i = 0; i < POP_SIZE; i++)
	{
		p = ((float)rand()) / RAND_MAX;
		if (p > CROSS_PROBABILITY)
		{
			continue;
		}
		pick_1 = rand() % POP_SIZE;
		pick_2 = rand() % POP_SIZE;
		j = rand() % CHROM_LEN;
		while (flag == 0)
		{
			b = ((float)rand()) / RAND_MAX;
			tmp_1 = chrom[pick_1][j] * (1 - b) + chrom[pick_2][j] * b;
			tmp_2 = chrom[pick_2][j] * (1 - b) + chrom[pick_1][j] * b;
			chrom[pick_1][j] = tmp_1;
			chrom[pick_2][j] = tmp_2;
			if (chrom[pick_1][j] <= BOUND_UP && chrom[pick_1][j] >= BOUND_DOWN && chrom[pick_2][j] <= BOUND_UP && chrom[pick_2][j] >= BOUND_DOWN)
			{
				flag = 1;
			}
		}
	}
	//printf("pick_1=%d,pick_2=%d,j=%d\n", pick_1, pick_2, j);
}

void vary(float chrom[POP_SIZE][CHROM_LEN])
{
	float r_1 = 0, r_2 = 0, ff = 0, p = 0;
	int i = 0, j = 0;
	for (i = 0; i < POP_SIZE; i++)
	{
		p = ((float)rand()) / RAND_MAX;
		if (p > VARY_PROBABILITY)
		{
			continue;
		}
		r_1 = ((float)rand()) / RAND_MAX;
		r_2 = ((float)rand()) / RAND_MAX;
		j = rand() % CHROM_LEN;
		ff = r_2 * pow(1 - ((float)i / EVO_NUM), 2);
		if (r_1 >= 0.5)
		{
			chrom[i][j] = chrom[i][j] + (chrom[i][j] - BOUND_UP) * ff;
		}
		else
		{
			chrom[i][j] = chrom[i][j] + (BOUND_DOWN - chrom[i][j]) * ff;
		}
	}
}

void main(void)
{
	float chrom[POP_SIZE][CHROM_LEN];
	float fitness[POP_SIZE], chooseP[POP_SIZE], accuP[POP_SIZE], funcVal[POP_SIZE];
	int i = 0, j = 0, a = 0, b = 0, h = 0;

	for (i = 0; i < POP_SIZE; i++)
	{
		memset(chrom[i], 0, sizeof(chrom[i]));
	}
	memset(fitness, 0, sizeof(fitness));
	memset(chooseP, 0, sizeof(chooseP));
	memset(accuP, 0, sizeof(accuP));
	memset(funcVal, 0, sizeof(fitness));

	srand(time(NULL));

	creat(chrom);

	for (i = 0; i < POP_SIZE; i++)
	{
		for (j = 0; j < CHROM_LEN; j++)
		{
			printf("chrom[%d][%d]=%f  ", i, j, chrom[i][j]);
		}
		printf("\n");
	}

	for (i = 0; i < 100; i++)
	{
		h++;
		printf("h=%d\n", h);
		for (j = 0; j < POP_SIZE; j++)
		{
			fitness[j] = fit(chrom[j]);
			funcVal[j] = objFuncVal(chrom[j]);
		}

		fitnessP(fitness, chooseP, accuP);

		/*for (j = 0; j < POP_SIZE; j++)
		{
			printf("chooseP[%d]=%f  ", j, chooseP[j]);
		}
		printf("\n");
		for (j = 0; j < POP_SIZE; j++)
		{
			printf("accuP[%d]=%f  ", j, accuP[j]);
		}
		printf("\n");*/

		roulette(accuP, chrom);

		cross(chrom);

		/*for (a = 0; a < POP_SIZE; a++)
		{
			for (b = 0; b < CHROM_LEN; b++)
			{
				printf("chrom[%d][%d]=%f  ", a, b, chrom[a][b]);
			}
			printf("\n");
		}
		printf("\n");*/

		vary(chrom);

		/*for (a = 0; a < POP_SIZE; a++)
		{
			for (b = 0; b < CHROM_LEN; b++)
			{
				printf("chrom[%d][%d]=%f  ", a, b, chrom[a][b]);
			}
			printf("\n");
		}
		printf("\n");*/
	}
	for (j = 0; j < POP_SIZE; j++)
	{
		fitness[j] = fit(chrom[j]);
		funcVal[j] = objFuncVal(chrom[j]);
		printf("fitness[%d]=%f\n", j, fitness[j]);
		printf("funcVal[%d]=%f\n", j, funcVal[j]);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值