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

📄 misc.c

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

int score;

void *alloc(int bytes)
{
	void *p = malloc(bytes);
	if (!p)
	{
		printf("Out of memory\n");
		exit(EXIT_FAILURE);
	}
	return p;
}

void writeLog()
{
	FILE *f;
	int i;
	char species[INSTRUCTIONS][INSTRUCTIONS];
	int diversity = 0;

	if (logFrequency == 0)
		return;
	if (generations % logFrequency)
		return;

	memset(species, 0, sizeof species);
	for (i = 0; i < population; i++)
	{
		Program *p = &programs[i];
		int first;
		int last;
		if (!p->size)
			continue;
		first = p->code[0];
		last = p->code[p->size - 1];
		if (!species[first][last])
		{
			species[first][last] = 1;
			diversity++;
		}
	}

	f = fopen("log", "a");
	if (!f)
	{
		printf("Unable to open log file\n");
		exit(EXIT_FAILURE);
	}
	fprintf(f, "%d\t%d\t%d\t%d\n",
		generations, programs[0].size, diversity, score);
	fclose(f);
}

⌨️ 快捷键说明

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