Example #1
0
/* produce the next generation */
void paintatron::next_generation()
{
    gprcm_generation_system(&sys, 200,
                            elitism,
                            mutation_prob,
                            use_crossover, &random_seed,
                            instruction_set, no_of_instructions);
}
Example #2
0
static void violent_crimes_prediction()
{
	int islands = 2;
	int migration_interval = 200;
	int population_per_island = 128;
	int rows = 9, columns = 10;
	int i, gen=0;
	int connections_per_gene = 8;
	int chromosomes = 3;
	gprcm_system sys;
	float min_value = -100;
	float max_value = 100;
	float elitism = 0.2f;
	float mutation_prob = 0.3f;
	int trials = 100;
	int use_crossover = 1;
	int ADF_modules = 0;
	unsigned int random_seed = (unsigned int)time(NULL);
	int sensors=4, actuators=1;
	int integers_only = 0;
	int no_of_test_examples;
	float test_performance;
	FILE * fp;	
	char compile_command[256];
	int instruction_set[64], no_of_instructions=0;
	int data_size=0, data_fields=0;
	char * sensor_names[] = {
		"Population",
		"household size",
		"Race percent black",
		"Race percent White",
		"Race percent Asian",
		"Race percent Hisp",
		"Age percent 12-21",
		"Age percent 12-29",
		"Age percent 16-24",
		"Age percent 65+",
		"Numb Urban",
		"Percent Urban",
		"Median Income",
		"Percent Wage",
		"Percent Farm Self",
		"Percent Inv Inc",
		"Percent Soc Sec",
		"Percent Pub Asst",
		"Percent Retire",
		"Median Family Income",
		"Per Capita Income",
		"White Per Cap",
		"Black Per Cap",
		"Indian Per Cap",
		"Asian Per Cap",
		"Other Per Cap",
		"Hisp Per Cap",
		"Num Under Pov",
		"Percent Pop Under Pov",
		"Percent Less 9th Grade",
		"Percent Not HS Grad",
		"Percent BS or More",
		"Percent Unemployed",
		"Percent Employ",
		"Percent Empl Manu",
		"Percent Empl Prof Serv",
		"Percent Occup Manu",
		"Percent Occup Mgmt Prof",
		"Male Percent Divorce",
		"Male Percent Nev Marr",
		"Female Pct Div",
		"Total Percent Div",
		"Percent Per Fam",
		"Percent Fam 2 Par",
		"Pct Kids 2 Par",
		"Percent Young Kids 2 Par",
		"Pct Teen 2 Par",
		"Percent Work Mom Young Kids",
		"Percent Work Mom",
		"Num Illeg",
		"Percent Illeg",
		"Num Immig",
		"Percent Immig Recent",
		"Percent Immig Rec5",
		"Percent Immig Rec8",
		"Percent Immig Rec10",
		"Percent Recent Immig",
		"Percent Rec Immig5",
		"Percent Rec Immig8",
		"Percent Rec Immig10",
		"Percent Speak Engl Only",
		"Percent Not Speak Engl Well",
		"Percent Larg House Fam",
		"Percent Larg House Occup",
		"Percent Per Occup Hous",
		"Percent Per Own Occ Hous",
		"Percent Per Rent Occ Hous",
		"Percent Pers Own Occup",
		"Percent Pers Dense Hous",
		"Percent Hous Less 3 BR",
		"Median Num BR",
		"Hous Vacant",
		"Percent Hous Occup",
		"Percent Hous Own Occ",
		"Percent Vacant Boarded",
		"Percent Vac More 6 Mos",
		"Med Yr Hous Built",
		"Percent Hous No Phone",
		"Percent WOFullPlumb",
		"Own Occ Low Quart",
		"Own Occ MedVal",
		"Own Occ Hi Quart",
		"Rent Low Q",
		"Rent Median",
		"Rent High Q",
		"Med Rent",
		"Med Rent Pct Hous Inc",
		"Med Own Cost Pct Inc",
		"Med Own Cost Pct Inc No Mtg",
		"Num In Shelters",
		"Num Street",
		"Percent Foreign Born",
		"Percent Born Same State",
		"Percent Same House 85",
		"Percent Same City 85",
		"Percent Same State 85",
		"Lemas Sworn FT",
		"Lemas SwFT Per Pop",
		"Lemas SwFT Field Ops",
		"Lemas SwFT Field Per Pop",
		"Lemas Total Req",
		"Lemas Tot Req Per Pop",
		"Polic Req Per Offic",
		"Polic Per Pop",
		"Racial Match Comm Pol",
		"Percent Polic White",
		"Percent Polic Black",
		"Percent Polic Hisp",
		"Percent Polic Asian",
		"Percent Polic Minor",
		"Offic Assgn Drug Units",
		"Num Kinds Drugs Seiz",
		"Polic Ave OT Worked",
		"Land Area",
		"Pop Dens",
		"Percent Use Pub Trans",
		"Polic Cars",
		"Polic Oper Budg",
		"Lemas Pct Polic On Patr",
		"Lemas Gang Unit Deploy",
		"Lemas Pct Offic Drug Un",
		"Polic Budg Per Pop"
	};
	char * actuator_names[] = {
		"Violent Crimes Per Pop"
	};

	/* load the data */
	no_of_examples = load_data("communities.data",
							   crime_data, MAX_EXAMPLES,
							   &fields_per_example);

	/* create a test data set */
	no_of_test_examples = create_test_data(crime_data,
										   &no_of_examples,
										   fields_per_example,
										   test_data);

	sensors = fields_per_example-1-INITIAL_FIELDS;
	trials = no_of_examples;

	printf("Number of training examples: %d\n",no_of_examples);
	printf("Number of test examples: %d\n",no_of_test_examples);
	printf("Number of fields: %d\n",fields_per_example);

	/* create an instruction set */
	no_of_instructions =
		gprcm_dynamic_instruction_set((int*)instruction_set);

	/* create a population */
	gprcm_init_system(&sys, islands,
					  population_per_island,
					  rows, columns,
					  sensors, actuators,
					  connections_per_gene,
					  ADF_modules,
					  chromosomes,
					  min_value, max_value,
					  integers_only,
					  data_size, data_fields,
					  &random_seed,
					  instruction_set, no_of_instructions);

	gpr_xmlrpc_server("server.rb","crime",3573,
					  "./agent",
					  sensors, actuators);
	
	test_performance = 0;
	while (test_performance < 99) {
		/* use the training data */
		current_data_set = crime_data;

		/* evaluate each individual */
		gprcm_evaluate_system(&sys,
							  trials,0,
							  (*evaluate_features));
		/* produce the next generation */
		gprcm_generation_system(&sys,
								migration_interval,
								elitism,
								mutation_prob,
								use_crossover, &random_seed,
								instruction_set, no_of_instructions);

		/* evaluate the test data set */
		current_data_set = test_data;
		test_performance = evaluate_features(no_of_test_examples,
											 &sys.island[0],
											 0,1);

		/* show the best fitness value */
		printf("Generation %05d  Fitness %.2f/%.2f%% ",
			   gen, gprcm_best_fitness(&sys.island[0]),test_performance);
		for (i = 0; i < islands; i++) {
			printf("  %.5f",gprcm_average_fitness(&sys.island[i]));
		}
		printf("\n");

		if (((gen % 50 == 0) && (gen>0)) || (test_performance > 99)) {
			gprcm_draw_population("population.png",
								  640, 640, &sys.island[0]);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_FITNESS,
									  "fitness.png",
									  "Violent Crimes " \
									  "Prediction Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_AVERAGE,
									  "fitness_average.png",
									  "Violent Crimes " \
									  "Prediction Average Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_DIVERSITY,
									  "diversity.png",
									  "Violent Crimes " \
									  "Prediction Diversity",
									  640, 480);

			gprcm_plot_fitness(&sys.island[0],
							   "fitness_histogram.png",
							   "Violent Crimes Prediction " \
							   "Fitness Histogram",
							   640, 480);

			fp = fopen("agent.c","w");
			if (fp) {
				/* save the best program */
				gprcm_c_program(&sys,
								gprcm_best_individual_system(&sys),
								RUN_STEPS, 0, fp);
				fclose(fp);

				/* compile the program */
				sprintf(compile_command,
						"gcc -Wall -std=c99 -pedantic -o agent agent.c -lm");
				assert(system(compile_command)==0);
			}

			fp = fopen("fittest.dot","w");
			if (fp) {
				gprcm_dot(gprcm_best_individual_system(&sys),
						  &sys.island[0],
						  sensor_names,  actuator_names,
						  fp);
				fclose(fp);
			}
		}

		if (test_performance > 99) break;
		gen++;
	}

	/* free memory */
	gprcm_free_system(&sys);
}
Example #3
0
static void wine_quality()
{
	int islands = 4;
	int migration_interval = 250;
	int population_per_island = 64;
	int rows = 9, columns = 16;
	int i, gen=0;
	int connections_per_gene = GPRC_MAX_ADF_MODULE_SENSORS+1;
	int modules = 0;
	int chromosomes = 3;
	gprcm_system sys;
 	float min_value = -100;
	float max_value = 100;
	float elitism = 0.2f;
	float mutation_prob = 0.2f;
	int trials = 100;
	int use_crossover = 1;
	unsigned int random_seed = (unsigned int)time(NULL);
	int sensors, actuators=1;
	int integers_only = 0;
	int no_of_test_examples;
	float test_performance;
	FILE * fp;
	int data_size=0, data_fields=0;
	char compile_command[256];
	int instruction_set[64], no_of_instructions=0;
	char * sensor_names[] = {
		"Fixed Acidity",
		"Volatile Acidity",
		"Citric Acid",
		"Residual Sugar",
		"Chlorides",
		"Free Sulphur Dioxide",
		"Total Sulfur Dioxide",
		"Density",
		"pH",
		"Sulphates",
		"Alcohol"
	};
	char * actuator_names[] = {
		"Quality"
	};

	/* load the data */
	no_of_examples =
		load_data("winequality-white.csv",
				  wine_data, MAX_EXAMPLES,
				  &fields_per_example);

	/* create a test data set */
	no_of_test_examples = create_test_data(wine_data,
										   &no_of_examples,
										   fields_per_example,
										   test_data);

	sensors = fields_per_example-1;
	trials = no_of_examples;

	printf("Number of training examples: %d\n",no_of_examples);
	printf("Number of test examples: %d\n",no_of_test_examples);
	printf("Number of fields: %d\n",fields_per_example);

	/* create an instruction set */
	no_of_instructions =
		gprcm_equation_instruction_set((int*)instruction_set);

	/* create a population */
	gprcm_init_system(&sys, islands,
					  population_per_island,
					  rows, columns,
					  sensors, actuators,
					  connections_per_gene,
					  modules,
					  chromosomes,
					  min_value, max_value,
					  integers_only,
					  data_size, data_fields,
					  &random_seed,
					  instruction_set, no_of_instructions);

	gpr_xmlrpc_server("server.rb","wine",3573,
					  "./agent",
					  sensors, actuators);

	test_performance = 0;
	while (test_performance < 99) {
		/* use the training data */
		current_data_set = wine_data;

		/* evaluate each individual */
		gprcm_evaluate_system(&sys,
							  trials,0,
							  (*evaluate_features));
		/* produce the next generation */
		gprcm_generation_system(&sys,
								migration_interval,
								elitism,
								mutation_prob,
								use_crossover, &random_seed,
								instruction_set,
								no_of_instructions);

		/* evaluate the test data set */
		current_data_set = test_data;
		test_performance =
			evaluate_features(no_of_test_examples,
							  &sys.island[0], 0, 1);

		/* show the best fitness value calculated from
		   the test data set */
		printf("Generation %05d  Fitness %.2f/%.2f%% ",
			   gen, gprcm_best_fitness(&sys.island[0]),
			   test_performance);
		for (i = 0; i < islands; i++) {
			printf("  %.3f",
				   gprcm_average_fitness(&sys.island[i]));
		}
		printf("\n");

		if (((gen % 50 == 0) && (gen>0)) ||
			(test_performance > 99)) {
			gprcm_draw_population("population.png",
								  640, 640, &sys.island[0]);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_FITNESS,
									  "fitness.png",
									  "Wine Quality " \
									  "Estimation Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_AVERAGE,
									  "fitness_average.png",
									  "Wine Quality Estimation " \
									  "Average Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_DIVERSITY,
									  "diversity.png",
									  "Wine Quality " \
									  "Estimation Diversity",
									  640, 480);

			gprcm_plot_fitness(&sys.island[0],
							   "fitness_histogram.png",
							   "Wine Quality Estimation " \
							   "Fitness Histogram",
							   640, 480);

			fp = fopen("agent.c","w");
			if (fp) {
				/* save the best program */
				gprcm_c_program(&sys,
								gprcm_best_individual_system(&sys),
								RUN_STEPS, 0, fp);
				fclose(fp);

				/* compile the program */
				sprintf(compile_command,
						"gcc -Wall -std=c99 -pedantic " \
						"-o agent agent.c -lm");
				assert(system(compile_command)==0);
			}

			fp = fopen("fittest.dot","w");
			if (fp) {
				gprcm_dot(gprcm_best_individual_system(&sys),
						  &sys.island[0],
						  sensor_names,  actuator_names,
						  fp);
				fclose(fp);
			}

		}

		if (test_performance > 99) break;
		gen++;
	}

	/* free memory */
	gprcm_free_system(&sys);
}
Example #4
0
static void slump_test()
{
	int islands = 2;
	int migration_interval = 200;
	int population_per_island = 512;
	int rows = 9, columns = 10;
	int i, gen=0;
	int connections_per_gene = GPRC_MAX_ADF_MODULE_SENSORS+1;
	int modules = 0;
	int chromosomes=3;
	gprcm_system sys;
	float min_value = -100;
	float max_value = 100;
	float elitism = 0.1f;
	float mutation_prob = 0.1f;
	int trials = 100;
	int use_crossover = 1;
	unsigned int random_seed = (unsigned int)time(NULL);
	int sensors=4, actuators=3;
	int integers_only = 0;
	int no_of_test_examples;
	float test_performance;
	FILE *fp;
	char compile_command[256];
	int instruction_set[64], no_of_instructions=0;
	int data_size=0, data_fields=0;
	char * sensor_names[] = {
		"Cement",
		"Slag",
		"Fly Ash",
		"Water",
		"Sand",
		"Coarse Aggr.",
		"Fine Aggr."
	};
	char * actuator_names[] = { "Slump", "Flow", "Compressive Strength" };

	/* load the data */
	no_of_examples =
		load_data("slump_test.data", concrete_data, MAX_EXAMPLES,
				  &fields_per_example);

	/* create a test data set */
	no_of_test_examples = create_test_data(concrete_data, &no_of_examples,
										   fields_per_example,
										   test_data);

	sensors = fields_per_example-4;
	trials = no_of_examples;

	printf("Number of training examples: %d\n",no_of_examples);
	printf("Number of test examples: %d\n",no_of_test_examples);
	printf("Number of fields: %d\n",fields_per_example);

	/* create an instruction set */
	no_of_instructions = gprcm_default_instruction_set(instruction_set);

	/* create a population */
	gprcm_init_system(&sys, islands,
					  population_per_island,
					  rows, columns,
					  sensors, actuators,
					  connections_per_gene,
					  modules,
					  chromosomes,
					  min_value, max_value,
					  integers_only,
					  data_size, data_fields,
					  &random_seed,
					  instruction_set, no_of_instructions);

	gpr_xmlrpc_server("server.rb","concreteslump",3573,
					  "./agent",
					  sensors, actuators);

	test_performance = 0;
	while (test_performance < 99) {
		/* use the training data */
		current_data_set = concrete_data;

		/* evaluate each individual */
		gprcm_evaluate_system(&sys,
							  trials,0,
							  (*evaluate_features));
		/* produce the next generation */
		gprcm_generation_system(&sys,
								migration_interval,
								elitism,
								mutation_prob,
								use_crossover, &random_seed,
								instruction_set, no_of_instructions);

		/* evaluate the test data set */
		current_data_set = test_data;
		test_performance = evaluate_features(no_of_test_examples,
											 &sys.island[0],
											 0,1);

		/* show the best fitness value calculated from the test data set */
		printf("Generation %05d  Fitness %.2f/%.2f%% ",
			   gen, gprcm_best_fitness(&sys.island[0]),test_performance);
		for (i = 0; i < islands; i++) {
			printf("  %.3f",gprcm_average_fitness(&sys.island[i]));
		}
		printf("\n");

		if (((gen % 100 == 0) && (gen>0)) || (test_performance > 99)) {
			gprcm_draw_population("population.png",
								  640, 640, &sys.island[0]);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_FITNESS,
									  "fitness.png",
									  "Concrete Slump Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_AVERAGE,
									  "fitness_average.png",
									  "Concrete Slump Average Performance",
									  640, 480);

			gprcm_plot_history_system(&sys,
									  GPR_HISTORY_DIVERSITY,
									  "diversity.png",
									  "Concrete Slump Diversity",
									  640, 480);

			gprcm_plot_fitness(&sys.island[0],
							   "fitness_histogram.png",
							   "Fitness Histogram",
							   640, 480);

			fp = fopen("agent.c","w");
			if (fp) {
				/* save the best program */
				gprcm_c_program(&sys,
								gprcm_best_individual_system(&sys),
								RUN_STEPS, 0, fp);
				fclose(fp);

				/* compile the program */
				sprintf(compile_command,
						"gcc -Wall -std=c99 -pedantic -o agent agent.c -lm");
				assert(system(compile_command)==0);
			}

			fp = fopen("fittest.dot","w");
			if (fp) {
				gprcm_dot(gprcm_best_individual_system(&sys),
						  &sys.island[0],
						  sensor_names,  actuator_names,
						  fp);
				fclose(fp);
			}
		}

		if (test_performance > 99) break;
		gen++;
	}

	/* free memory */
	gprcm_free_system(&sys);
}