private void SetHighest() { // We'll need a reference to the highest Forrest in our finished Forrest list ForrestCTRL tF = startFatt[ReturnHighest(startFatt)]; float avgFitTemp = GetAverageFitness(startFatt); // This saves a record of the fitness performances fits.Add(tF.fitness); // This saves a record of the fitness average performances avgs.Add(avgFitTemp); // If this new Forrest beat the Highest Forrest make him the new highest if (tF.fitness > highestFit.fitness) { // Create a new NN to store the highest brain from the training session highestFit = new NN(tF.nn.inputs, tF.nn.hL); // Set that fitness to the new record Forrest which is tF highestFit.SetFitness(tF.fitness); // Then set the weights to the new record Forrest tF highestFit.IniWeights(tF.nn.GetBrain()); // Finally change the highest fit brain string to our new record weights highestFitBrain = highestFit.ReadBrain(); } // If this new Forrest beat the Highest Forrest make him the new highest if (avgFitTemp > highestAvg) { highestAvg = avgFitTemp; } }
// Update is called once per frame void FixedUpdate() { brain = nn.ReadBrain(); // Rotate the charater based on Horizonal Input & later NN Output transform.rotation = Quaternion.Euler(transform.eulerAngles + Vector3.up * movement * 2.5f); // If attempt has ended if (!ended) { // Auto move Forrest forward rb.MovePosition(transform.position + transform.forward * (Time.deltaTime * 10)); } // Set up a raycast hit for knowing what we hit RaycastHit hit; // Set up out 5 feelers for undertanding the world Vector3[] feeler = new Vector3[] { // 0 = L transform.TransformDirection(Vector3.left), // 1 - FL transform.TransformDirection(Vector3.left + Vector3.forward), // 2 - F transform.TransformDirection(Vector3.forward), // 3 = FR transform.TransformDirection(Vector3.right + Vector3.forward), // 4 = R transform.TransformDirection(Vector3.right), }; // Use this to collect all feeler distances, then well pass them through our NN for an output inp = new float[feeler.Length]; // Loop through all feelers for (int i = 0; i < feeler.Length; i++) { // See what all feelers feel if (Physics.Raycast(transform.position, feeler[i], out hit)) { // If feelers feel something other than Forrest & nothing if (hit.collider != null && hit.collider != col) { // Set the input[i] to be the distance of feeler[i] inp[i] = hit.distance; } } // Draw the feelers in the Scene mode Debug.DrawRay(transform.position, feeler[i] * 10, Color.red); } // Add to our fitness every frame fitness += (ended) ? 0 : inp2fit(inp); // This sets the output text display to be the output of our NN if (!menu) { movement = ended ? 0 : ((C.intelli == ctrl.IntelMode.Human) ? Input.GetAxis("Horizontal") : nn.CalculateNN(inp)); } else { movement = ended ? 0 : (nn.CalculateNN(inp)); } // if (!menu && !ended && lap.x > lap.y) { Freeze(); CheckIfLast(); } }