├── README.md ├── bird.c ├── bird.h ├── engine.c ├── engine.h ├── graphics.c ├── main.c ├── makefile ├── types.h ├── virtualMachine.c └── virtualMachine.h /README.md: -------------------------------------------------------------------------------- 1 | Corvus is an experiement in genetic programming. 2 | 3 | ![screenshot](http://i.imgur.com/nvtkkkG.gif) 4 | 5 | It creates a number of birds (BF virtual machines), each of which try to find food (f(x)), in a given environment (f). The birds reproduce upon finding enough food, or starve after trying too long. 6 | 7 | To modify the function the corvus birds are trying to find, modify targetFunction in main. 8 | 9 | VIRTUAL MACHINE: 10 | Corvus uses a modified version of BF, in that input is always given by the x for a function. 11 | 12 | So if I were to ',' for f(4), then ',' would store 4; 13 | 14 | SPEED: 15 | To find f(x) where f(x)=1, it takes apoximately...hold on...still waiting... 16 | 17 | -------------------------------------------------------------------------------- /bird.c: -------------------------------------------------------------------------------- 1 | Strand mutate(Strand parentStrand){ 2 | Strand strand = malloc(MAX_TEXT_SPACE); 3 | memcpy(strand,parentStrand,MAX_TEXT_SPACE); 4 | int i,j; 5 | for(i=0;i (ba.birds[fattest].fat)) 65 | fattest=i; 66 | return &(ba.birds[fattest]); 67 | } 68 | int updateBird(Bird * bird){ 69 | int j; 70 | if((bird->fat)>0){ 71 | bird->fat--; 72 | j=step(&(bird->vm),bird->x); 73 | if(j!=-1) 74 | return j; 75 | }else 76 | bird->fat=0; 77 | return -1; 78 | } 79 | void freeBird(Bird * bird){ 80 | //obligatory AND THIS BIRD YOU CANNOT CHAAAAAAAAAAAAAAAAAAAAAAAAAAANGE 81 | freeVirtualMachine(bird->vm); 82 | } 83 | void replaceBird(Bird * to,Bird * from){ 84 | freeBird(to); 85 | *to = createBird(from->strand); 86 | } 87 | -------------------------------------------------------------------------------- /bird.h: -------------------------------------------------------------------------------- 1 | #include "virtualMachine.h" 2 | 3 | //how much getting the right answer gives 4 | #define CARBS 200 5 | //how much getting the wrong answer takes away 6 | #define POISON 200 7 | #define MAX_BIRDS 24 8 | #define STARTING_FAT 500 9 | typedef struct{ 10 | int fat;//how much food the bird has eaten, at 0, dead bird 11 | int x;//what x the bird is currently on 12 | Strand strand;//Used to generate the VM text space. 13 | VirtualMachine vm; 14 | } Bird; 15 | typedef struct{ 16 | int size; 17 | Bird * birds; 18 | } BirdArray; 19 | #include "bird.c" 20 | -------------------------------------------------------------------------------- /engine.c: -------------------------------------------------------------------------------- 1 | int tick(Engine * engine){ 2 | engine->iterations++; 3 | int i,j=0; 4 | for(i=0;i<(engine->birdArray.size);i++){ 5 | j=updateBird(&engine->birdArray.birds[i]); 6 | if(j==engine->f(engine->birdArray.birds[i].x)){ 7 | engine->birdArray.birds[i].x++; 8 | engine->birdArray.birds[i].fat+=CARBS; 9 | if(engine->birdArray.birds[i].x>250){ 10 | endwin(); 11 | fprintf(stderr,"\n###WINNING STRAND###\n%s\n",engine->birdArray.birds[i].strand); 12 | exit(5); 13 | } 14 | }else if(j!=(-1)){ 15 | engine->birdArray.birds[i].fat-=POISON; 16 | } 17 | if(engine->birdArray.birds[i].fat<=0) 18 | replaceBird( &engine->birdArray.birds[i], 19 | fattest(engine->birdArray)); 20 | } 21 | } 22 | void freeEngine(Engine * engine){ 23 | for(;engine->birdArray.size>0;engine->birdArray.size--) 24 | freeBird(&engine->birdArray.birds[engine->birdArray.size]); 25 | free(engine->birdArray.birds); 26 | } 27 | -------------------------------------------------------------------------------- /engine.h: -------------------------------------------------------------------------------- 1 | #include "bird.h" 2 | /*The Engine is like a game engine, it handles everthing in the virtual 3 | little world of birds*/ 4 | typedef struct{ 5 | byte (*f)(byte x);//the target function the birds are trying to reach 6 | unsigned long long int iterations;//how many times we have ticked 7 | BirdArray birdArray; 8 | } Engine; 9 | #include "engine.c" 10 | -------------------------------------------------------------------------------- /graphics.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void initGraphics(){ 4 | initscr(); 5 | start_color(); 6 | use_default_colors(); 7 | init_pair(0,COLOR_WHITE,-1); 8 | init_pair(1,COLOR_YELLOW,-1);//(cyan on default) 9 | init_pair(2,COLOR_YELLOW,COLOR_WHITE); 10 | init_pair(3,COLOR_WHITE,COLOR_YELLOW); 11 | cbreak(); 12 | noecho(); 13 | } 14 | void drawBirds(BirdArray birdArray){ 15 | int i,j=0; 16 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include "types.h" 6 | #include "engine.h" 7 | #include "graphics.c" 8 | 9 | byte targetFunction(byte x){ 10 | /*this is the function the birds try to reach*/ 11 | return x; 12 | } 13 | int main(){ 14 | srand(time(NULL));//yay for !random 15 | Engine engine; 16 | engine.iterations=0; 17 | engine.f=targetFunction; 18 | engine.birdArray=generateBirds(); 19 | initGraphics(); 20 | while(1){ 21 | tick(&engine); 22 | redraw(engine); 23 | } 24 | endwin(); 25 | } 26 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc main.c -lncurses -ggdb -o corvus 3 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | typedef unsigned char byte; 2 | typedef byte *Strand; 3 | -------------------------------------------------------------------------------- /virtualMachine.c: -------------------------------------------------------------------------------- 1 | int isOpcode(byte o){ 2 | return (o=='>'||o=='<'|| 3 | o=='+'||o=='-'|| 4 | o=='.'||o==','|| 5 | o=='['||o==']'); 6 | } 7 | void freeVirtualMachine(VirtualMachine vm){ 8 | free(vm.text); 9 | free(vm.data); 10 | //other two are pointers to above so we don't have to worry about them 11 | } 12 | byte * jumpDestination(VirtualMachine vm){ 13 | byte target; 14 | int movement; 15 | int level = 0; 16 | byte * destination=vm.pc; 17 | if(*vm.pc=='['){ 18 | target=']'; 19 | movement=1; 20 | }else if(*vm.pc==']'){ 21 | target='['; 22 | movement=-1; 23 | }else 24 | return vm.pc; 25 | while(!(level<1&&*destination==target)){ 26 | destination+=movement; 27 | if(destination>=(vm.text+MAX_TEXT_SPACE) || destination<=vm.text) 28 | return vm.pc; 29 | if(*destination==*vm.pc) 30 | level++; 31 | if(*destination==target) 32 | level--; 33 | } 34 | return destination; 35 | } 36 | int step(VirtualMachine * vm,int x){//do one instruction 37 | if((vm->pc)>=(vm->text+MAX_TEXT_SPACE) || (vm->pc)<=vm->text) 38 | vm->pc=vm->text; 39 | if((vm->dc)>=(vm->data+MAX_DATA_SPACE) || (vm->dc)<=vm->data) 40 | vm->dc=vm->data + (MAX_DATA_SPACE/2); 41 | switch(*vm->pc){ 42 | case '>': 43 | vm->dc++; 44 | break; 45 | case '<': 46 | vm->dc--; 47 | break; 48 | case '+': 49 | *vm->dc++; 50 | break; 51 | case '-': 52 | *vm->dc--; 53 | break; 54 | case '.': 55 | *vm->pc++; 56 | return *vm->dc; 57 | case ',': 58 | *vm->dc=(byte)x; 59 | break; 60 | case '[': 61 | if(*vm->dc) 62 | break; 63 | case ']': 64 | if(!*vm->dc) 65 | break; 66 | vm->pc=jumpDestination(*vm); 67 | default: 68 | break; 69 | } 70 | vm->pc++; 71 | return -1; 72 | } 73 | VirtualMachine createVirtualMachine(byte * text){ 74 | VirtualMachine vm; 75 | if(text==NULL) 76 | text = malloc(MAX_TEXT_SPACE); 77 | else 78 | vm.text = text; 79 | vm.data = calloc(sizeof(byte),MAX_DATA_SPACE); 80 | vm.pc = vm.text;//program counter at start of program 81 | vm.dc = vm.data + (MAX_DATA_SPACE/2);//we want to start in the middle 82 | return vm; 83 | } 84 | -------------------------------------------------------------------------------- /virtualMachine.h: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | 3 | #define MAX_TEXT_SPACE 80 4 | #define MAX_DATA_SPACE 1028 5 | 6 | typedef struct{ 7 | byte * pc;//program counter 8 | byte * dc;//data counter 9 | byte * text;//array of text (program code) space 10 | byte * data;//array of data space 11 | } VirtualMachine; 12 | #include "virtualMachine.c" 13 | --------------------------------------------------------------------------------