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 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); } } } }