public void CreateCreature(Creature_V2 parent1, Creature_V2 parent2) { float energy = 1f; float life = 1f; float veloForward = 0f; float veloAngular = 0f; int[] randomTile = map_v2.RandomFloorTile(); Vector3 bodyPosition = parent1.position - (parent1.trans.up * 2f * parent1.GetRadius()); Vector3 leftPos = Vector3.zero; Vector3 rightPos = Vector3.zero; GameObject creatureGameObject = Instantiate(creaturePrefab, bodyPosition, creaturePrefab.transform.rotation) as GameObject; GameObject leftLineGameObject = Instantiate(linePrefab) as GameObject; GameObject rightLineGameObject = Instantiate(linePrefab) as GameObject; leftLineGameObject.transform.parent = creatureGameObject.transform; rightLineGameObject.transform.parent = creatureGameObject.transform; LineRenderer leftLine = leftLineGameObject.GetComponent <LineRenderer>(); LineRenderer rightLine = rightLineGameObject.GetComponent <LineRenderer>(); leftLine.SetWidth(0.02f, 0.02f); rightLine.SetWidth(0.02f, 0.02f); LineRenderer[] lineSensor = new LineRenderer[4]; for (int i = 0; i < lineSensor.Length; i++) { GameObject newLine = Instantiate(linePrefab) as GameObject; newLine.transform.parent = creatureGameObject.transform; lineSensor[i] = newLine.GetComponent <LineRenderer>(); lineSensor[i].SetWidth(0.02f, 0.02f); } GameObject spikeLineGameObject = Instantiate(linePrefab) as GameObject; spikeLineGameObject.transform.parent = creatureGameObject.transform; LineRenderer spikeLine = spikeLineGameObject.GetComponent <LineRenderer>(); spikeLine.SetWidth(0.02f, 0.02f); Creature_V2 strongerParent = parent1.GetEnergy() > parent2.GetEnergy() ? parent1 : parent2; Creature_V2 weakerParent = parent1.GetEnergy() > parent2.GetEnergy() ? parent2 : parent1; Brain_V2 brain = new Brain_V2(strongerParent.GetBrain(), totalCreaturesCount); brain.Mutate(strongerParent.GetBrain().GetWeights(), weakerParent.GetEnergy() / strongerParent.GetEnergy()); creatureGameObject.transform.GetChild(1).GetComponent <TextMesh>().text = brain.GetName(); string parentNames = strongerParent.GetName() + "@" + weakerParent.GetName(); Creature_V2 creature = new Creature_V2(totalCreaturesCount, strongerParent.GetGeneration() + 1, creatureGameObject.transform, leftLine, rightLine, lineSensor, spikeLine, brain, new HSBColor(1f, 0f, 0f), bodyPosition, leftPos, rightPos, 0.5f, UnityEngine.Random.Range(0f, 360f), worldDeltaTime, creatureGameObject.transform.localScale.x / 2f, energy, energy, life, minLife, lifeDecrease, eatDamage, veloDamage, angDamage, fightDamage, veloForward, veloAngular, map_v2, this, parentNames); creatureList.Add(creature); totalCreaturesCount++; parent1.AddChildren(creature); parent2.AddChildren(creature); }
private void SaveWorld() { string filename = "world_snapshot.lses"; StreamWriter writer = new StreamWriter(filename); writer.Write(brainNetwork.Length + " "); for (int i = 0; i < brainNetwork.Length; i++) { writer.Write(brainNetwork[i] + " "); } writer.Write(creatureList.Count + " "); for (int i = 0; i < creatureList.Count; i++) { Creature_V2 creature = creatureList[i]; Brain_V2 brain = creature.GetBrain(); //float[][] neurons = brain.GetNeurons(); float[][][] connections = brain.GetWeights(); writer.Write(creature.GetName() + " " + creature.GetParentNames() + " " + creature.GetEnergy() + " " + creature.GetLife() + " " + creature.position.x + " " + creature.position.y + " " + creature.rotation + " " + creature.veloForward + " " + creature.veloAngular + " "); for (int j = 0; j < connections.Length; j++) { for (int k = 0; k < connections[j].Length; k++) { for (int l = 0; l < connections[j][k].Length; l++) { writer.Write(connections[j][k][l] + " "); } } } } Tile_V2[,] tiles = map_v2.GetTilesArray(); for (int j = 0; j < 100; j++) { for (int k = 0; k < 100; k++) { writer.Write(tiles[j, k].currentEnergy + " "); } } writer.Close(); }
private void DrawBrain() { if (brain != null) { float xOff = (int)(screenWidth / (neurons.Length - 1)); float yOff = (int)(screenHeight * 0.025f); float rectWidth = (int)(screenWidth * 0.075f); float rectHeight = (int)(rectWidth / 1.5f); if (drawNetState == true) { for (int layerIndex = 0; layerIndex < connections.Length; layerIndex++) { float currentLayerYRatio = (screenHeight / 2f) / (float)connections[layerIndex].Length; float currentXPos = (int)((layerIndex + 1) * xOff); float previousXPos = (int)((layerIndex) * xOff); for (int neuronOfLayerIndex = 0; neuronOfLayerIndex < connections[layerIndex].Length; neuronOfLayerIndex++) { float previousLayerYRatio = (screenHeight / 2f) / (float)connections[layerIndex][neuronOfLayerIndex].Length; for (int previousLayerNeuronIndex = 0; previousLayerNeuronIndex < connections[layerIndex][neuronOfLayerIndex].Length; previousLayerNeuronIndex++) { Vector2 pointA = new Vector2(currentXPos + (int)(rectWidth / 2f), currentLayerYRatio * neuronOfLayerIndex + yOff + (int)(rectHeight / 2f)); Vector2 pointB = new Vector2(previousXPos + (int)(rectWidth / 2f), previousLayerYRatio * previousLayerNeuronIndex + yOff + (int)(rectHeight / 2f)); Color lineColor = positiveLineColor; if (connections[layerIndex][neuronOfLayerIndex][previousLayerNeuronIndex] <= 0f) { lineColor = negativeLineColor; } Drawing.DrawLine(pointA, pointB, lineColor, 2f, texture); } } } } else { Vector2 mousePosition = Event.current.mousePosition; for (int x = 0; x < neurons.Length; x++) { float numberOfNeuronsInLayer = neurons[x].Length; float yRatio = (screenHeight / 2f) / numberOfNeuronsInLayer; float xpos = x * xOff; float ypos = 0f; for (int y = 0; y < numberOfNeuronsInLayer; y++) { ypos = y * yRatio + yOff; Rect rec = new Rect(xpos, ypos, rectWidth, rectHeight); if (rec.Contains(mousePosition) == true) { if (x == 0) { float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x + 1].Length; float nextXPos = (int)((x + 1) * xOff); float currentXPos = (int)((x) * xOff); for (int z = 0; z < neurons[x + 1].Length; z++) { Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f)); Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f)); Color lineColor = positiveLineColor; if (connections[x][z][y] <= 0f) { lineColor = negativeLineColor; } Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture); } break; } else if (x == (neurons.Length - 1)) { float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x - 1].Length; float nextXPos = (int)((x - 1) * xOff); for (int z = 0; z < neurons[x - 1].Length; z++) { Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f)); Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f)); Color lineColor = positiveLineColor; if (connections[x - 1][y][z] <= 0f) { lineColor = negativeLineColor; } Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture); } break; } else { float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x - 1].Length; float nextXPos = (int)((x - 1) * xOff); for (int z = 0; z < neurons[x - 1].Length; z++) { Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f)); Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f)); Color lineColor = positiveLineColor; if (connections[x - 1][y][z] <= 0f) { lineColor = negativeLineColor; } Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture); } float nextLayerYRatio2 = (screenHeight / 2f) / (float)neurons[x + 1].Length; float nextXPos2 = (int)((x + 1) * xOff); for (int z = 0; z < neurons[x + 1].Length; z++) { Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f)); Vector2 pointB = new Vector2(nextXPos2 + (int)(rectWidth / 2f), nextLayerYRatio2 * z + yOff + (int)(rectHeight / 2f)); Color lineColor = positiveLineColor; if (connections[x][z][y] <= 0f) { lineColor = negativeLineColor; } Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture); } break; } } } } } GUIStyle myStyle = new GUIStyle(); myStyle.fontStyle = FontStyle.Bold; myStyle.fontSize = (int)(screenWidth * 0.025f); for (int x = 0; x < neurons.Length; x++) { float numberOfNeuronsInLayer = neurons[x].Length; float yRatio = (screenHeight / 2f) / numberOfNeuronsInLayer; float xpos = x * xOff; float ypos = 0f; for (int y = 0; y < numberOfNeuronsInLayer; y++) { ypos = y * yRatio + yOff; GUI.color = Color.white; GUI.DrawTexture(new Rect(xpos, ypos, rectWidth, rectHeight), texture); myStyle.normal.textColor = Color.black; GUI.Label(new Rect((int)(xpos), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), neurons[x][y].ToString("0.000") + "", myStyle); /*if (x == 0) * { * myStyle.normal.textColor = Color.green; * GUI.Label(new Rect((int)(xpos + (rectWidth / 0.85f)), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), inputNames[y], myStyle); * } * else if (x == neurons.Length - 1) * { * myStyle.normal.textColor = Color.green; * GUI.Label(new Rect((int)(xpos - (rectWidth / 0.475f)), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), outputNames[y], myStyle); * }*/ } } for (int i = 0; i < treeDataList.Count; i++) { myStyle.normal.textColor = treeDataList[i].color; if (i == 0) { string state = creature.IsAlive() == true ? "[ALIVE]" : "[DEAD]"; string cretureInformation = treeDataList[i].name + " " + state + "\n Parents: [" + creature.GetParentNames() + "]" + "\n Child Count: " + creature.GetChildCount() + "\n Generation: " + creature.GetGeneration() + "\n Time Lived: " + creature.GetTimeLived().ToString("0.000") + "\n Life: " + creature.GetLife().ToString("0.000") + "\n Energy: " + creature.GetEnergy().ToString("0.000") + "\n Delta: " + creature.GetDeltaEnergy(); GUI.Label(new Rect(1f, screenHeight / 1.9f + rectHeight + i * (yOff / 1.5f), rectWidth, rectHeight), cretureInformation, myStyle); } else { GUI.Label(new Rect(1f, screenHeight / 1.9f + rectHeight + i * (yOff / 1.5f), rectWidth, rectHeight), treeDataList[i].name, myStyle); } } } }
public void UpdateCreatureEnergy(int x, int y, float[] output, float groundHue, float mouthHue, Creature_V2 spikeCreature) { float accelForward = output[0]; float accelAngular = output[1]; float eatFood = output[4]; float birth = output[5]; float fight = output[6]; deltaEnergy = 0f; deltaEnergy -= (worldDeltaTime / 5f) * (Mathf.Sqrt(Mathf.Max(currentEnergy / initialEnergy, 1f))); //natural energy loss (creature will die in 5 years) deltaEnergy -= (Mathf.Abs(accelForward) * worldDeltaTime) / veloDamage /*5f3f*/; //if creature keep moving at max speed it will die in 5 years deltaEnergy -= (Mathf.Abs(accelAngular) * worldDeltaTime) / angDamage /*1f0.5f*/; //if creature keeps turing at max acceleration it will die in 2 years // At worst if the creatures keep turning and moving at max rate it will die in 1.1 year int tileType = map.GetTileType(x, y); if (tileType == Tile_V2.TILE_WATER) { deltaEnergy -= worldDeltaTime * 20f; } else if (eatFood > 0 && tileType != Tile_V2.TILE_INFERT) { deltaEnergy += map.Eat(x, y) / 1.1f; // 1 (cirmum) = 2*pi*r // 1/(pi*2) = r = 0.15915494309189533576888376337251 // turn r = 1 will make c = 2*pi // lenght of two chords combined = 2*sin(theta/2) float mouthX = Mathf.Cos(mouthHue * Mathf.PI * 2); float mouthY = Mathf.Sin(mouthHue * Mathf.PI * 2); float groundX = Mathf.Cos(groundHue * Mathf.PI * 2); float groundY = Mathf.Sin(groundHue * Mathf.PI * 2); float dist = Mathf.Pow(Mathf.Pow(mouthX - groundX, 2) + Mathf.Pow(mouthY - groundY, 2), 0.5f); deltaEnergy -= (dist * worldDeltaTime * eatDamage); //2 is good } if (fight > 0 && /*maturity > MIN_BIRTH_MATURITY &&*/ spikeCreature != null && maturity > MIN_FIGHT_MATURITY) { float enemyEnergy = spikeCreature.GetEnergy(); if (enemyEnergy > 0f) { float energySuck = fightDamage * (Mathf.Max(currentEnergy, 0f)); enemyEnergy -= energySuck; if (enemyEnergy < 0f) { energySuck = (energySuck + enemyEnergy) + 0.005f; } deltaEnergy += (energySuck / 1.25f); spikeCreature.RemoveEnergy(energySuck); } /*float enemyLife = spikeCreature.GetLife(); * if (enemyLife > 0f) * { * float lifeSuck = fightDamage * (Mathf.Max(currentEnergy, 0f)) * 0.1f; * * enemyLife -= lifeSuck; * * life += (lifeSuck / 1.25f); * spikeCreature.RemoveLife(lifeSuck); * }*/ } if (currentEnergy > MIN_BRITH_ENERGY && birth > 0f && birthFrameCounter == 0) { birthFrameCounter++; } else if (birthFrameCounter < 6 && birthFrameCounter > 0) { birthFrameCounter++; } else if (birthFrameCounter == 6) { deltaEnergy -= 1.5f; giveBirth = true; birthFrameCounter = 0; } currentEnergy += deltaEnergy; if (currentEnergy <= 0f || life <= 0) { isAlive = false; } maturity += worldDeltaTime; //life -= ((worldDeltaTime /3) / Mathf.Pow((Mathf.Max(currentEnergy, 1f) / initialEnergy), 0.25f)); life -= ((worldDeltaTime / minLife) / Mathf.Pow(Mathf.Max(currentEnergy, 1f), lifeDecerase)); //life -= (worldDeltaTime / (5 + (Mathf.Max(currentEnergy, 0f))*2f)); }