Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by NoahDaNerd · Aug 11, 2020 at 10:16 AM · c#ailistarrays

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 11, 2020 at 01:54 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

215 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges