public NeuralNetwork(RoomControl rc, FrogMove fm) {//Inputs are fed to the hidden layer which are in turn fed into the visible layer hiddenLayer = new Neuron[hiddenSize]; this.rc = rc; this.fm = fm; for (int i = 0; i < hiddenSize; i++) { hiddenLayer [i] = new Neuron(this, numInputs, 1); } topLayer = new Neuron[numOutputs]; topLayer [0] = new Neuron(this, hiddenSize, 1); topLayer [1] = new Neuron(this, hiddenSize, 1); topLayer [2] = new Neuron(this, hiddenSize, 0); topLayer [3] = new Neuron(this, hiddenSize, 2); }
public Vector2 GetNearestPellet(FrogMove frog) { Vector3 position = frog.transform.position; Vector3 potentialPosition = Vector3.zero; //Potential so that we do not need to make a new float annd Vector3 each time float sqrDistance = Mathf.Infinity; float potentialSqrDistance = -1; foreach (Transform p in pellets) { potentialSqrDistance = (p.transform.position - position).sqrMagnitude; if (potentialSqrDistance < sqrDistance) { sqrDistance = potentialSqrDistance; potentialPosition = p.transform.position; } } return (Vector2)(potentialPosition); }
public NeuralNetwork(RoomControl rc, FrogMove fm, Neuron[] neurons) {//We already have the whole neuron array (called in generations after the 1st) hiddenLayer = new Neuron[hiddenSize]; this.rc = rc; this.fm = fm; int i = 0; for (; i < hiddenSize; i++) { hiddenLayer [i] = neurons [i]; hiddenLayer [i].SetParent(this); } topLayer = new Neuron[numOutputs]; for (int x=0; x<numOutputs; x++) { topLayer [x] = neurons [i + x]; topLayer [x].SetParent(this); } }
void doGeneration() { List<NeuralNetwork> networks = new List<NeuralNetwork>(pelletCount * 5); //Max upper bounds for total points is number of (pellets * 10) / 2 for (int i = 0; i < frogs.Length; i++) { for (int x = 0; x < frogs[i].GetPoints()+1; x++) { //Make sure even the laziest frog has some chance of breeding networks.Add(frogs [i].nn); } } FrogMove[] newFrogs = new FrogMove[frogCount]; //Debug.Log(frogCount); for (int i = 0; i < frogCount; i++) { int lIndex = (int)(Random.value * networks.Count); int rIndex = (int)(Random.value * networks.Count); while (rIndex==lIndex) { //Make sure we are not breeding with ourselves rIndex = (int)(Random.value * networks.Count); } FrogMove fm = ((GameObject)Instantiate(frog, new Vector3(Random.Range(-roomSize, roomSize), Random.Range(-roomSize, roomSize)), Quaternion.identity)).GetComponent<FrogMove>(); //Debug.Log(fm.gameObject.transform.position); Neuron[] neurons = NeuralNetwork.Breed(networks [lIndex].GetNeurons(), networks [rIndex].GetNeurons()); NeuralNetwork nn = new NeuralNetwork(this, fm, neurons); fm.Setup(this, roomSize, nn); newFrogs [i] = fm; } Debug.Log(pelletCount - pellets.Count + ":" + (float)(pelletCount - pellets.Count) / lastScore); lastScore = pelletCount - pellets.Count; while (pellets.Count<pelletCount) { Transform tf = ((GameObject)Instantiate(pellet, new Vector3(Random.Range(-roomSize, roomSize), Random.Range(-roomSize, roomSize)), Quaternion.identity)).transform; pellets.Add(tf); } for (int i = 0; i < frogCount; i++) { Destroy(frogs [i].gameObject); } frogs = newFrogs; Invoke("doGeneration", genTime); }