public void RandomizeWeights() { foreach (Connection c in connections) { c.weight = (float)EvoGame.RandomFloat() * 2 - 1; } }
public Creature(Vector2 pos, float viewAngle) { id = currentId++; this.pos = pos; this.viewAngle = viewAngle; inBias.SetName(NAME_IN_BIAS); inFoodValuePosition.SetName(NAME_IN_FOODVALUEPOSITION); inFoodValueFeeler.SetName(NAME_IN_FOODVALUEFEELER); inOcclusionFeeler.SetName(NAME_IN_OCCLUSIONFEELER); inEnergy.SetName(NAME_IN_ENERGY); inAge.SetName(NAME_IN_AGE); inGeneticDifference.SetName(NAME_IN_GENETICDIFFERENCE); inWasAttacked.SetName(NAME_IN_WASATTACKED); inWaterOnFeeler.SetName(NAME_IN_WATERONFEELER); inWaterOnCreature.SetName(NAME_IN_WATERONCREATURE); inMemory1.SetName(NAME_IN_MEMORY1); outBirth.SetName(NAME_OUT_BIRTH); outRotate.SetName(NAME_OUT_ROTATE); outForward.SetName(NAME_OUT_FORWARD); outFeelerAngle.SetName(NAME_OUT_FEELERANGLE); outAttack.SetName(NAME_OUT_ATTACK); outEat.SetName(NAME_OUT_EAT); outMemory1.SetName(NAME_OUT_MEMORY1); brain = new NeuronalNetwork(); brain.AddInputNeuron(inBias); brain.AddInputNeuron(inFoodValuePosition); brain.AddInputNeuron(inFoodValueFeeler); brain.AddInputNeuron(inOcclusionFeeler); brain.AddInputNeuron(inEnergy); brain.AddInputNeuron(inAge); brain.AddInputNeuron(inGeneticDifference); brain.AddInputNeuron(inWasAttacked); brain.AddInputNeuron(inWaterOnFeeler); brain.AddInputNeuron(inWaterOnCreature); brain.AddInputNeuron(inMemory1); brain.GenerateHiddenNeurons(10); brain.AddOutputNeuron(outBirth); brain.AddOutputNeuron(outRotate); brain.AddOutputNeuron(outForward); brain.AddOutputNeuron(outFeelerAngle); brain.AddOutputNeuron(outAttack); brain.AddOutputNeuron(outEat); brain.AddOutputNeuron(outMemory1); brain.GenerateFullMesh(); brain.RandomizeAllWeights(); CalculateFeelerPos(MAXIMUMFEELERDISTANCE); color = new Color(EvoGame.RandomFloat(), EvoGame.RandomFloat(), EvoGame.RandomFloat()); GenerateColorInv(); CalculateCollisionGridPos(); }
public void calculate() { int currentFrequencyX = startFrequencyX; int currentFrequencyY = startFrequencyY; float currentAlpha = 1; for (int oc = 0; oc < octaves; oc++) { if (oc > 0) { currentFrequencyX *= 2; currentFrequencyY *= 2; currentAlpha /= 2; } float[,] discretePoints = new float[currentFrequencyX + 1, currentFrequencyY + 1]; for (int i = 0; i < currentFrequencyX + 1; i++) { for (int k = 0; k < currentFrequencyY + 1; k++) { discretePoints[i, k] = (float)EvoGame.RandomFloat() * currentAlpha; } } for (int i = 0; i < WIDTH; i++) { for (int k = 0; k < HEIGHT; k++) { float currentX = i / (float)WIDTH * currentFrequencyX; float currentY = k / (float)HEIGHT * currentFrequencyY; int indexX = (int)currentX; int indexY = (int)currentY; float w0 = interpolate(discretePoints[indexX, indexY], discretePoints[indexX + 1, indexY], currentX - indexX); float w1 = interpolate(discretePoints[indexX, indexY + 1], discretePoints[indexX + 1, indexY + 1], currentX - indexX); float w = interpolate(w0, w1, currentY - indexY); heightMap[i, k] += w; } } } normalize(); }
public Creature(Creature mother) { id = currentId++; //this.mother = mother; generation = mother.generation + 1; if (generation > _maximumGeneration) { _maximumGeneration = generation; } this.pos = mother.pos; this.viewAngle = EvoGame.RandomFloat() * Mathf.PI * 2; this.brain = mother.brain.CloneFullMesh(); SetupVariablesFromBrain(); CalculateFeelerPos(MAXIMUMFEELERDISTANCE); for (int i = 0; i < 10; i++) { brain.RandomMutation(0.1f); } int r = mother.color.R; int g = mother.color.G; int b = mother.color.B; r += EvoGame.RandomInt(-5, 6); g += EvoGame.RandomInt(-5, 6); b += EvoGame.RandomInt(-5, 6); r = Mathf.ClampColorValue(r); g = Mathf.ClampColorValue(g); b = Mathf.ClampColorValue(b); color = new Color(r, g, b); GenerateColorInv(); if (CreatureManager.SelectedCreature == null || CreatureManager.SelectedCreature.Energy < 100) { CreatureManager.SelectedCreature = this; } }
protected override void Update(GameTime deltaTime) { while (Creatures.Count < 50) { Creature justSpawned = new Creature( new Vector2( EvoGame.RandomFloat() * game.tileMap.GetWorldWidth(), EvoGame.RandomFloat() * game.tileMap.GetWorldHeight()), EvoGame.RandomFloat() * Mathf.PI * 2); justSpawned.Manager = this; Creatures.Add(justSpawned); } for (int i = 0; i < AmountOfCores; i++) { int upperBound = Creatures.Count * (i + 1) / AmountOfCores; if (upperBound > Creatures.Count) { upperBound = Creatures.Count; } int lowerBound = Creatures.Count * i / AmountOfCores; MultithreadingHelper.StartWork((object state) => { for (int k = lowerBound; k < upperBound; k++) { Creatures[k].ReadSensors(); } MultithreadingHelper.PulseAndFinish(); }); } MultithreadingHelper.WaitForEmptyThreadPool(); for (int i = 0; i < AmountOfCores; i++) { int upperBound = Creatures.Count * (i + 1) / AmountOfCores; if (upperBound > Creatures.Count) { upperBound = Creatures.Count; } int lowerBound = Creatures.Count * i / AmountOfCores; MultithreadingHelper.StartWork((object state) => { for (int k = lowerBound; k < upperBound; k++) { Creatures[k].Act(deltaTime); } MultithreadingHelper.PulseAndFinish(); }); } MultithreadingHelper.WaitForEmptyThreadPool(); numberOfDeaths += CreaturesToKill.Count; RemoveCreaturesFromDeathList(); MergeCreaturesAndSpawnCreatures(); year += (float)deltaTime.ElapsedGameTime.TotalSeconds; HandleCollision(); if (Creatures.Count > 0) { OldestCreatureAlive = Creatures[0]; foreach (Creature c in Creatures) { if (c.Age > OldestCreatureAlive.Age) { OldestCreatureAlive = c; } } } AliveCreaturesRecord.Add(Creatures.Count); }
public void RandomMutation(float MutationRate) { Connection c = connections[EvoGame.RandomInt(connections.Count)]; c.weight += (float)EvoGame.RandomFloat() * 2 * MutationRate - MutationRate; }