Rocket::Rocket(float lifetime) { dna = DNA(lifetime); location = ofPoint(ofGetWidth()/2,ofGetHeight()); velocity = ofPoint(0,0); acceleration = ofPoint(0,0); }
Population::Population() { for (int i = 0; i < popSize; i++) { population[i] = DNA(); } }
genImg::genImg(ofImage &src, int scale, int nColors) { dna = DNA(src.getWidth()*scale * src.getHeight()*scale, nColors); img.allocate(src.getWidth()*scale, src.getHeight()*scale, OF_IMAGE_COLOR); heatmap.allocate(src.getWidth()*scale, src.getHeight()*scale, OF_IMAGE_COLOR); fitness = 1; }
//-------------------------------------------------------------- void ofApp::setup() { for (int i = 0; i < ArrayCount(population); i++) { population[i] = DNA(); } myFont.loadFont("LiberationMono-Regular.ttf", 12); }
DNA DNA::crossover(DNA partner){ DNA child = DNA(); // Picking a random “midpoint” in the genes array int midpoint = ofRandom(genes.size()); for (int i = 0; i < genes.size(); i++) { // Before midpoint copy genes from one parent, after midpoint copy genes from the other parent if (i>midpoint) child.addGene(genes[i]); else child.addGene(partner.genes[i]); } //Return the new child DNA return child; }
DNA crossover(DNA partner) { DNA child = DNA(); int midpoint = int(ofRandom(0, ArrayCount(genes))); for (int i = 0; i < ArrayCount(genes); i++) { if (i > midpoint)child.genes[i] = genes[i]; else child.genes[i] = partner.genes[i]; } return child; }
Animal::Animal(std::string DNASequence) : Object(OBJ_TYPE_ANIMAL) { energy = 80.0; speed = 80.0; this->dna = DNA(DNASequence); if (!samplesInitialized) { for (int i = 0; i < 16; i++) { voiceSamples[i] = 0; } samplesInitialized = true; } voice = dna.getVoice(); RandomNumberGenerator *rng = RandomNumberGenerator::getInstance(); voiceInterval = ((double) rng->getInt(500, 5000)) / 1000.0; addSensor(dna.getSightSensor()); addSensor(dna.getDigestiveSystem()); addSensor(dna.getHearingSensor()); addSensor(dna.getNervousSystem()); }
DNA Breeder::replicate(const DNA& parent) { Genome replicatedGenome = replicateGenome(parent); Traits newTraits; newTraits.mutationRate = parent.traits.mutationRate + RandomGen::randomInt(-5, 5); newTraits.splitRate = nx::clamp(parent.traits.splitRate + RandomGen::randomFloat(-2.0f, 2.0f), 30.0f, 100.0f); const real32 colorChange = 0.01f; newTraits.red = nx::clamp(parent.traits.red + RandomGen::randomFloat(-colorChange, colorChange), 0.f, 1.f); newTraits.green = nx::clamp(parent.traits.green + RandomGen::randomFloat(-colorChange, colorChange), 0.f, 1.f); newTraits.blue = nx::clamp(parent.traits.blue + RandomGen::randomFloat(-colorChange, colorChange), 0.f, 1.f); const real32 eyeLengthChange = 0.001f; newTraits.eyeLengthA = nx::clamp(parent.traits.eyeLengthA + RandomGen::randomFloat(-eyeLengthChange, eyeLengthChange), minEyeLength, maxEyeLength); newTraits.eyeLengthB = nx::clamp(parent.traits.eyeLengthB + RandomGen::randomFloat(-eyeLengthChange, eyeLengthChange), minEyeLength, maxEyeLength); newTraits.eyeLengthC = nx::clamp(parent.traits.eyeLengthC + RandomGen::randomFloat(-eyeLengthChange, eyeLengthChange), minEyeLength, maxEyeLength); const real32 eyeOffsetChange = 0.001f; newTraits.eyeOffsetA = nx::clamp(parent.traits.eyeOffsetA + RandomGen::randomFloat(-eyeOffsetChange, eyeOffsetChange), 0.f, nx::PiOver4); newTraits.eyeOffsetB = nx::clamp(parent.traits.eyeOffsetB + RandomGen::randomFloat(-eyeOffsetChange, eyeOffsetChange), 0.f, nx::PiOver4); // Copy directly // Copy with an offset // Generate a new random weight. return DNA(std::move(replicatedGenome), newTraits); }