- Home /
Why is everything being set the same? Please Help!
I am making a neural network thing that uses evolution, but the iteration was not working. (You don't need to know anything about neural networks, just that weights and biases are arrays/lists of values specific to each instance of the neural net, and that my program works where the best neural net instance is chosen to be the starting weights and biases of the next generation) The iteration is supposed to go through all of the robots, set their weights and biases to the best one of the previous generation, and slightly change their weights and biases, but they were not. I checked the weight values at the time of iteration, and one frame later, and they showed that it was working, but being set after to the starting values. I can't find where it is being set, because there is only one time that they are, and that is immediately followed up by incrementing them. Please Help! Thanks in advance.
Here is the code for each neural net:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Robot : MonoBehaviour
{
[Header("Input")]
public Vector3 velocity;
private float velX;
private float velZ;
public Vector3 robotEuler;
private float rotX;
private float rotZ;
[SerializeField] private float maxForce = 5f;
[SerializeReference] private List<float> inputNeurons;
public List<float> weights;
[Header("Output")]
[SerializeReference] private Vector3 force;
Rigidbody rb;
private float tempNeuron;
[SerializeReference] private List<float> outputNeurons;
public List<float> biases;
public bool dead = false;
private void Awake()
{
rb = GetComponent<Rigidbody>();
inputNeurons = new List<float>() { velX, velZ, rotX, rotZ };
outputNeurons = new List<float>() { 0, 0 };
biases = new List<float>();
weights = new List<float>();
for (int i = 0; i < outputNeurons.Count * inputNeurons.Count; i++)
{
weights.Add(Random.Range(-1f, 1f));
}
for (int i = 0; i < outputNeurons.Count; i++)
{
biases.Add(Random.Range(-1f, 1f));
}
}
private void Update()
{
SetInputs();
for (int i = 0; i < outputNeurons.Count; i++)
{
for (int x = 0; x < inputNeurons.Count; x++)
{
tempNeuron += weights[(i * inputNeurons.Count) + x] * inputNeurons[x];
}
outputNeurons[i] = (float)System.Math.Tanh(tempNeuron + biases[i]);
}
force = new Vector3(outputNeurons[0], 0, outputNeurons[1]) * maxForce;
rb.AddForce(force);
}
private void SetInputs()
{
robotEuler = transform.parent.Find("Body").eulerAngles;
velocity = rb.velocity;
velX = velocity.x;
velZ = velocity.z;
rotX = robotEuler.x;
rotZ = robotEuler.z;
if (rotX > 180)
{
rotX = -(180 - (rotX - 180));
}
if (rotZ > 180)
{
rotZ = -(180 - (rotZ - 180));
}
inputNeurons = new List<float>() { velX, velZ, rotX, rotZ };
}
public void Iterate(float step)
{
for (int i = 0; i < weights.Count; i++)
{
weights[i] += Random.Range(-step, step);
}
for (int i = 0; i < biases.Count; i++)
{
biases[i] += Random.Range(-step, step);
}
}
}
and here is the evolution manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EvolutionManager : MonoBehaviour
{
public GameObject robotPrefab;
[SerializeField] private float robotSpace = 2f;
[SerializeField] private int robots = 100;
[SerializeReference] private List<float> currentStartingWeights = new List<float>();
[SerializeReference] private List<float> currentStartingBiases = new List<float>();
[SerializeField] private float randomStep = 0.1f;
[SerializeReference] private bool trialComplete;
private GameObject robot;
private List<Robot> robotScripts = new List<Robot>();
private List<GameObject> robotList = new List<GameObject>();
private Robot bestRobot;
private float bestLife;
private void Start()
{
InstantiateRobots();
}
private void Update()
{
trialComplete = true;
foreach (Robot script in robotScripts)
{
if (!script.dead)
{
trialComplete = false;
}
}
if (trialComplete)
{
trialComplete = false;
NextTrial();
}
}
void InstantiateRobots()
{
robotList = new List<GameObject>();
robotScripts = new List<Robot>();
for (int x = 0; x < robots; x++)
{
for (int i = 0; i < robots; i++)
{
robot = Instantiate(robotPrefab, new Vector3(robotSpace * (i + 0.5f), 0, robotSpace * (x + 0.5f)), Quaternion.identity, transform);
robotScripts.Add(robot.GetComponentInChildren<Robot>());
robotList.Add(robot);
}
}
}
void NextTrial()
{
foreach (Robot script in robotScripts)
{
if (script.transform.parent.Find("Body").GetComponent<RobotAlive>().lifetime > bestLife)
{
bestLife = script.transform.parent.Find("Body").GetComponent<RobotAlive>().lifetime;
bestRobot = script;
}
}
currentStartingWeights = bestRobot.weights;
currentStartingBiases = bestRobot.biases;
foreach (GameObject gObj in robotList)
{
Destroy(gObj);
}
InstantiateRobots();
foreach (Robot script in robotScripts)
{
script.weights = currentStartingWeights;
script.biases = currentStartingBiases;
for (int i = 0; i < script.weights.Count; i++)
{
script.weights[i] += Random.Range(-randomStep, randomStep);
}
for (int i = 0; i < script.biases.Count; i++)
{
script.biases[i] += Random.Range(-randomStep, randomStep);
}
StartCoroutine(DebugStuff(script));
}
}
IEnumerator DebugStuff(Robot script)
{
print(script.weights[0]);
//yield return new WaitForEndOfFrame();
//script.Iterate(randomStep);
yield return new WaitForEndOfFrame();
print(script.weights[0]);
}
}
Answer by Bunny83 · Aug 11, 2020 at 01:51 PM
In line 82 / 83 of your "EvolutionManager":
script.weights = currentStartingWeights;
script.biases = currentStartingBiases;
you replace the whole list in each individual robot with the same two lists. So all those robots will be using the exact same list instance. Lists are objects and therefore reference types. All you did is giving each robot the same reference to the same List object.
You have to either create new lists like that:
script.weights = new List<float>(currentStartingWeights);
script.biases = new List<float>(currentStartingBiases);
or to avoid unnecessary garbage, you can simply reuse the old lists in each
script.weights.Clear();
script.weights.AddRange(currentStartingWeights);
script.biases.Clear();
script.biases.AddRange(currentStartingBiases);
edit
ps:
Instead of your two for loop to randomize your weights and biases after copying them inside your EvolutionManager you probably want to just use
script.Iterate(randomStep);
Just by reading your title I was already 98% sure that something like this is the issue. So, that's a great title. Not many are able to formulate a proper title and usually end up with "need help" or "doesn't work" :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
Using PlayerPrefs to save a C# Generic List 1 Answer
Put GameObject array into Transform Array? 1 Answer
Query with C# arrays and List<> 2 Answers