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

📄 pop.c

📁 一遗传算法的例子源程序
💻 C
字号:
#include "lithos.h"

Program *programs;
Program *newPrograms;
Program *tournament;

void initPopulation()
{
	int i;
	programs = alloc(population * sizeof(Program));
	newPrograms = alloc(population * sizeof(Program));
	tournament = alloc(tournamentSize * sizeof(Program));
	for (i = 0; i < population; i++)
	{
		programs[i].code = alloc(maxSize + 1);
		programs[i].size = 0;
		programs[i].fitness = 0;
	}
	for (i = overselectionRate; i < population; i++)
		newPrograms[i].code = alloc(maxSize);
}

void readPopulation(FILE *f)
{
	int i;
	for (i = 0; i < population; i++)
	{
		Program *p = &programs[i];
		int j;

		p->size = readInt(f);
		p->fitness = readInt(f);

		for (j = 0; j < p->size; j++)
			p->code[j] = readInstruction(f);
	}
}

void writePopulation(FILE *f)
{
	int i;
	for (i = 0; i < population; i++)
	{
		Program *p = &programs[i];
		int j;

		fputc('\n', f);

		fputc('[', f);
		writeString(f, "Program ");
		writeInt(f, i);
		fputc(']', f);
		fputc('\n', f);

		writeCommentedInt(f, "Size", p->size);
		writeCommentedInt(f, "Fitness", p->fitness);

		if (p->size)
			fputc('\n', f);

		for (j = 0; j < p->size; j++)
		{
			writeString(f, "\t\t\t");
			writeInstruction(f, p->code[j]);
			fputc('\n', f);
		}
	}
}

⌨️ 快捷键说明

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