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

📄 main.c

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

static void test()
{
	printf("Testing\n");
	unpack(&programs[0], code0);
	memset(memory0, 0, maxMemory * sizeof(int));
	exec(code0, programs[0].size, memory0);
	while (gsp != gmemory)
		printf("%d\n", pop());
}

static void save()
{
	FILE *f = fopen("data", "w");
	if (!f)
	{
		printf("Unable to create data file");
		exit(EXIT_FAILURE);
	}
	writeParameters(f);
	fputc('\n', f);
	writeStatistics(f);
	writePopulation(f);
	fclose(f);
}

static void autosave()
{
	if (!autosaveFrequency)
		return;
	if (generations % autosaveFrequency)
		return;
	printf("Autosaving\n");
	save();
}

static void run()
{
LOOP:
	printf("Starting\n");
	while (!_kbhit())
	{
		if (evaluated >= population)
		{
			autosave();
			writeLog();
			nextGeneration();
		}
		evaluate();
		evaluated++;
	}
	if (_getch() == ' ')
	{
		printf("Stopped\n");
		if (_getch() == ' ')
			goto LOOP;
	}
}

int __cdecl main(int argc, char **argv)
{
	FILE *f;

	if (argc > 2 ||
		argc == 2 && strcmp(argv[1], "-t"))
	{
		printf("Lithos version 1.0\n"
			"Usage: lithos [-t]\n");
		return 1;
	}

	initRandom();
	f= fopen("data", "r");
	if (f)
	{
		readParameters(f);
		initPopulation();
		readStatistics(f);
		readPopulation(f);
		fclose(f);
	}
	else
	{
		f = fopen("params", "r");
		if (f)
		{
			readParameters(f);
			fclose(f);
		}
		initPopulation();
		printf("Generation 0\n");
		if (logFrequency)
		{
			f = fopen("log", "w");
			if (!f)
			{
				printf("Unable to create log file");
				return 1;
			}
			fprintf(f, "Generation\tComplexity\tDiversity\tScore\n");
			fclose(f);
		}
	}
	initCode();

	if (argc == 2)
	{
		test();
		return 0;
	}

	startTime = time(0);
	run();

	printf("Saving\n");
	save();
	printf("Done\n");
	return 0;
}

⌨️ 快捷键说明

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