Monday, September 23, 2013

Genetic Algorithm Manipulation in Maya

//Genetic Algorithm structure
/*main()
    AllocateMemory()
    DoRun()
        Spawn()
       
        Loop()
            PrintGeneration()
            FitnessFunction()
                KillOffLower50()
            Breed()
                SelectOrganism()
           
*/
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

int main(){
    int finalGeneration;
   
    AllocateMemory();
    finalGeneration = DoRun();
    printf("The final generation was: %d\n", finalGeneration);
    phenoOut();
    }
#define GENE_POOL 100
#define GENE_COUNT 4

char **currentGen, **nextGen,; //globals
char *modelOrganism;
int *organismFitness;

void AllocateMemory(void){
    int organism;
   
    currentGen =
        (char**)malloc(sizeof(char*) * GENE_POOL);
    nextGen =
        (char**)malloc(sizeof(char*) * GENE_POOL);
    modelOrganism =
        (char*)malloc(sizeof(char) * gene_count);
    organismFitness =
        (int*)malloc(sizeof(int) * GENE_POOL);
       
    for(organism = 0; organism<GENE_POOL; ++organism){
        currentGen[organism] =
            (char*)malloc(sizeofchar) * GENE_COUNT);
        nextGen[organism] =
            (char*)malloc(sizeofchar) * GENE_COUNT);
        }
    }
#define FALSE 0
#define TRUE 1

int DoRun(void){
    int generations=1;
    int perfectGen = FALSE;
   
    InitialOrganisms();
   
    while(TRUE){
        perfectGen = EvaluateOrganism();
        if (perfectGen==TRUE ) return generations;
        ProduceNextGen();
        ++generations;
        }
    }
   
   
#include <stdlib.h>

#define GENO 4

void InitializeOrganism(void){
    int organism;
    int gene;
   
    //initialize the base organisms
    for (organism=0; organism<GENE_POOL; ++organism){
        for(gene=0 gene<GENE_COUNT; ++gene){
            currentGen[organism][gene] = rand()%GENO;
        }
    }
    //initialize the model organism
    for(gene=0; gene<GENE_COUNT; ++gene){
        modelOrganism[gene] = rand()%GENO
    }
}


//FITNESS FUNCTION NEEDS TO BE DEFINED AS A COMPARISON OF GENES TO EACH OTHER: GENE1 +(-) GENE2 < 3 then ++genefitness
#define MAXIMUM_FITNESS NUMBER_GENES

int totalOfFitness;

int EvaluateOrganism(void){
    int organism;
    int gene;
    int currentOrganismFitnessTally;
   
    totalOfFitness = 0;
   
    for (organism=0; organism<GENE_POOL; ++organism){
       
        //tally up the organism's fitness
        for (gene=0; gene<GENE_COUNT; ++gene){
            if( currentGen[organism][gene] ==modelOrganism[gene]){
                ++currentOrganismFitnessTally;
            }
        }
        //Save out the tally data
        //add the tally score to generation score
        organismFitness[organism] = currentOrganismFitnessTally;
        totalOfFitness +=currentOrgansimFitnessTally;
       
        //check if generation is perfect
        if (currentOrganismsFitnessTally ==MAXIMUM_FITNESS){
            return TRUE;
        }
    return FALSE;
    }
}

#define MUTATION_RATE 0.001

void ProduceNextGen(void){
    int organism;
    int gene;
    int parentOne;
    int parenttwo;
    int crossoverPoint;
    int mutateThisGene;
   
    //dill the nextGen data structure with the Children
    for (organism=0;organism<GENE_POOL;++organism){
        paretnOne = SelectOneOrganism();
        parentTwo = SelectOneOrganism();
        crossoverPoint = rand() %GENE_COUNT;
       
        for(gene=0; gene<GENE_COUNT; ++gene;){
            //copy over a single gene
            mutateThisGene = rand() %(int)(1.0/MUTATUIN_RATE);
            if(mutateThisGene == 0){
               
                //This is the gene mutation is true condition
                nextGen[organism][gene] = rand() %GENO;
            }else{
                //This is the true inheritance condition
                nextGen[organism][gene] = (currentGen[parentOne][gene] + currentGen[parentTwo][gene])/2;
            }
        }
    }
    //copy the children, nextGen into the new currentGen
    for (organism=0; organism<GENE_POOL; ++organism){
        for(gene=0; gene<GENE_COUNT; ++gene;){
            currentGen[organism][gene] = nextGen[organism][gene];
        }
    }
}

int SelectOneOrganism(void){
    int organism;
    int runningTotal;
    int randomSelectPoint;
   
    running Total = 0;
    randomSelectPoint = rand() % (totalOfFitness + 1);
   
    for(organism=0; organism<GENE_POOL; ++organism;){
        runningTotal += organismFitness[organism];
        if(runningTotal >= randomSelectPoint) return organism;
    }
}
void PhenoOut(int x){
    int organism;
    ofstream pheno_out;
   
    pheno_out.open ("pheno_out.txt");
    for(organism=0;organism<GENE_POOL;++organism){
        pheno_out << " Organism: ";
        pheno_out << organism;
        pheno_out << " Gene Head: ";
        pheno_out << gene;
        pheno_out << " Gene Body: ";
        pheno_out << gene;
        pheno_out << " Gene Front Legs: ";
        pheno_out << gene;
        pheno_out << " Gene Rear Legs: ";
    }
    pheno_close();
    return 0;
    }

No comments:

Post a Comment