/* This file is: ga_war.c CHANGES: 6/19/2000 It's been a couple of years without any changes but I was running this for old times sake and fix a few tiny bugs that cause bluemoon segfaults. This changes the population pool so numbering starts at 0 instead of 1. by jason boer 3/20/97 jason@ezmo.com Hello, This is a program to evolve REDCODE programs for Corewars using a closed system of unnatural selection. To maximize portability these are the instructions for this program. You are free to do whatever you want with it. You will not owe me even a nickel if you do, but in no case will I owe you if you grind your harddrive to dust. This program does tons of file accesses and is intended to be run from a ramdisk and NOT from a harddrive. To use this code place it in a directory of your choice, then make a directory under that called valhalla. You then need to copy "plant.red" and "ga_war.cfg" into a file they are listed below. Then compile this file. It was made with DJGPP 2.01 a DOS port of gcc. I have tried to make this as portable as possible and in most cases changing the path_symbol in the "ga_war.cfg" file should be all that is need. So type "gcc -Wall -O2 ga_war.c", OK if that didn't work then goodluck. I am not able help you with this kind of problem. Now you have a.exe or a.out so run it. Wait did I say you need to have "pmars" in your path and I can't stress too much this should!!! be a ramdisk. If you don't have a ramdisk try simtel there are some there. Now run the code it will create a population of REDCODE programs and begin the process of unnatural selection on them. The code should be fairly clear in showing the exact way it is doing this. If you make any big breakthroughs with this or make a super new improvement to this code feel free to let me know and tell me what you did. But you are not obligated to do so. Put this in a file called: "plant.red" cut here------------------------- ;redcode-94 ;name plant ;assert CORESIZE == 8000 spl.x # 0,# 0 end cut here------------------------- Put this in a file called: "ga_war.cfg" cut here------------------------- ;the form of this must not be changed ;the parser is very-very-very dumb ; ; value can be {0,1} create_new_population=1 ; value must be {n>1} or else population_size=25 ; value must be {n>1} or else number_of_cycles=250 ; value must be {n>=0} starting_cycle=0 ; value must be {00} number_of_battles=3 ; 2 letters only...dumb population_name=xx ; OK, dos use \\ unix use // path_symbol=\\ cut here------------------------- */ #include #include #include #include /********************/ static unsigned long number_of_opcodes=17; static char * opcodes[]={\ "dat","mov","add","sub",\ "mul","div","mod","jmp",\ "jmz","jmn","djn","spl",\ "slt","cmp","seq","sne",\ "nop"}; static unsigned long number_of_modifiers=7; static char * modifiers[]= {".a ",".b ",".ab",".ba",".f ",".x ",".i "}; static unsigned long number_of_address_modes=7; static char * address_modes[]= {"#","$","@","<",">","*","{","}"}; /************************************/ /*these are set from the config file*/ static char path_symbol[32]=""; static int create_new_population=0; static char population_name[32]=""; static int population_size=0; static int starting_cycle=0; static int number_of_cycles=0; static int number_of_battles=0; static int mutation_rate=0; static int resurrection_rate=0; static int insertion_rate=0; static int removal_rate=0; static int max_number_size=0; static int max_instructions=0; /********************/ static int * valhalla_record=0; /********************/ int a_random_integer(int range){ return (int)range-(rand()%(range*2)); } int a_random_number(int range) { return (int)rand()%range; } /********************/ int a_mutation() { return ((a_random_number(100)+1)<=mutation_rate); } int a_resurrection() { return ((a_random_number(100)+1)<=resurrection_rate); } int a_insertion() { return ((a_random_number(100)+1)<=insertion_rate); } int a_removal() { return ((a_random_number(100)+1)<=removal_rate); } /********************/ void fprint_header(FILE * file,char * file_name) { fprintf(file,";redcode-94\n"); fprintf(file,";assert CORESIZE == 8000\n"); fprintf(file,";name %s\n",file_name); } /********************/ void fprint_body(FILE * target_file,FILE * source_file) { char buffer[256]; strcpy(buffer,";"); while (strstr(buffer,";")!=0) fgets (buffer,255,source_file); while (strstr(buffer,"end")==0) { fprintf(target_file,"%s",buffer); fgets (buffer,255,source_file); } } /********************/ void fprint_end(FILE * target_file,int score) { fprintf(target_file,"end\n"); fprintf(target_file,";%d\n",score); } /********************/ void file_copy(char * source_name,char *target_name) { FILE * target_file=0; FILE * source_file=0; char buffer[256]; if (0==(target_file=fopen(target_name,"w+"))) exit(0); if (0==(source_file=fopen(source_name,"r"))) exit(0); while (fgets(buffer,255,source_file)!=0) fprintf(target_file,"%s",buffer); fclose(target_file); fclose(source_file); } /********************/ void update_cycle_file(int cycle) { FILE * file=0; int index; if (0==(file=fopen("cycle.txt","w+"))) exit(0); fprintf(file,"%d\n",cycle); fprintf(file,"create_new_population=%d\n",create_new_population); fprintf(file,"population_size=%d\n",population_size); fprintf(file,"number_of_cycles=%d\n",number_of_cycles); fprintf(file,"starting_cycle=%d\n",starting_cycle); fprintf(file,"max_instructions=%d\n",max_instructions); fprintf(file,"max_number_size=%d\n",max_number_size); fprintf(file,"mutation_rate=%d\n",mutation_rate); fprintf(file,"insertion_rate=%d\n",insertion_rate); fprintf(file,"removal_rate=%d\n",removal_rate); fprintf(file,"resurrection_rate=%d\n",resurrection_rate); fprintf(file,"number_of_battles=%d\n",number_of_battles); fprintf(file,"population_name=%s\n",population_name); fprintf(file,"path_symbol=%s\n",path_symbol); for (index=0;indexmax_instructions) { printf(";;;;; %s\n",file_name); return 0; } /*test with plant*/ sprintf(command,"pmars -b -o plant.red %s > temp.txt",file_name); system(command); if ((file=fopen("temp.txt","r"))==0) exit(0); while (strstr(buffer,file_name)==0) fgets (buffer,255,file); fclose(file); s_ptr=(strstr(buffer,"scores")); s_ptr+=7; if (atoi(s_ptr)<1) { printf("===== %s %s",file_name,s_ptr); return 0; } else { printf("===== %s %s",file_name,s_ptr); return 1; } }/*test_viable()*/ /********************/ void create_seed_population(int population_size,char * population_name) { FILE * file=0; int index; char buffer[256]; char file_name[256]; for (index=0;index temp.txt", number_of_battles,source_name,t_name); system(command); if ((file=fopen("temp.txt","r"))==0) exit(0); fgets (buffer,255,file); if (strstr(buffer,source_name)==0) { s_ptr=(strstr(buffer,"scores")); s_ptr+=7; t_score=atoi(s_ptr); } else { s_ptr=(strstr(buffer,"scores")); s_ptr+=7; s_score=atoi(s_ptr); } fgets (buffer,255,file); if (strstr(buffer,source_name)==0) { s_ptr=(strstr(buffer,"scores")); s_ptr+=7; t_score=atoi(s_ptr); } else { s_ptr=(strstr(buffer,"scores")); s_ptr+=7; s_score=atoi(s_ptr); } fclose(file); printf("^^^^^%9s %9s:%d %d\n",source_name,t_name,s_score,t_score); if (s_score>=(3*number_of_battles)) return TOTAL_WIN; if (t_score>=(3*number_of_battles)) return TOTAL_LOSS; if (s_score>t_score) return WIN; if (s_score %s\n",source_name,target_name); } /********************/ int valhalla_score(char * file_name) { FILE * file=0; char buffer[256]; if (0==(file=fopen(file_name,"r"))) exit(0); strcpy(buffer,";"); while (strstr(buffer,"end")==0) fgets (buffer,255,file); fgets (buffer,255,file); fclose(file); return atoi((buffer+1)); }/*valhalla_score()*/ /********************/ void send_to_valhalla(int source_number,char * source_file_name) { char target_file_name[256]; /*make vahalla file name*/ sprintf(target_file_name,".%svalhalla%s%s",path_symbol,path_symbol,source_file_name); /*is this one better than the current valhalla file*/ if(valhalla_score(target_file_name)>=valhalla_record[source_number]) return; /*it not better,so copy the file to valhalla*/ file_copy(source_file_name,target_file_name); valhalla_record[source_number]=0; printf(">>>>> %s\n",source_file_name+1); }/*send_to_valhalla()*/ /********************/ void return_from_valhalla(char * target_file_name) { FILE * source_file=0; FILE * target_file=0; char source_file_name[256]; sprintf(source_file_name,".%svalhalla%s%s",path_symbol,path_symbol,target_file_name); /*is this one better than the current valhalla file*/ if(valhalla_score(source_file_name)<=valhalla_score(target_file_name)) return; if (0==(target_file=fopen(target_file_name,"w+"))) exit(0); if (0==(source_file=fopen(source_file_name,"r"))) exit(0); fprint_header(target_file,target_file_name); fprint_body(target_file,source_file); fprint_end(target_file,0); fclose(target_file); fclose(source_file); printf("<<<<< %s\n",target_file_name); }/*return_from_valhalla()*/ /********************/ void add_to_valhalla_score(char * file_name,int target_number,int score) { FILE * source_file=0; FILE * target_file=0; valhalla_record[target_number]+=score; if (0==(source_file=fopen(file_name,"r"))) exit(0); if (0==(target_file=fopen("temp.red","w+"))) exit(0); fprint_header(target_file,file_name); fprint_body(target_file,source_file); fprint_end(target_file,valhalla_record[target_number]); fclose(source_file); fclose(target_file); /*replace it*/ file_copy("temp.red",file_name); }/*add_to_valhalla_score()*/ /********************/ void run_one_cycle(int population_size,char * population_name) { int source_number; int target_number; char source_name[256]; char target_name[256]; /*index through whole population*/ for (source_number=0;source_number