Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by polgoku8 · Dec 13, 2017 at 12:09 PM · arrayaioverflow

Genetic Algorithm OverflowException: Number overflow:

Hi i'm starting on genetic algorithm and i'm getting and overflow error, i know i'm getting a negative array on line 34 of my script and i think it's because it;s not getting initialized, i'm trying to make an AI that runs through and obstacle course and learns it through generations please help I apologize for the amount of code

using System; using System.Collections.Generic;

 public class GeneticAlgorithm<T>
 {
     public List<DNA<T>> Population { get; private set; }
     public int Generation { get; private set; }
     public float BestFitness { get; private set; }
     public T[] BestGenes { get; private set; }
 
     public int Elitism;
     public float MutationRate;
 
     private List<DNA<T>> newPopulation;
     private Random random;
     private float fitnessSum;
     private int dnaSize;
     private Func<T> getRandomGene;
     private Func<int, float> fitnessFunction;
 
     public GeneticAlgorithm(int populationSize, int dnaSize, Random random, Func<T> getRandomGene, Func<int, float> fitnessFunction,
         int elitism, float mutationRate = 0.01f)
     {
         Generation = 1;
         Elitism = elitism;
         MutationRate = mutationRate;
         Population = new List<DNA<T>>(populationSize);
         newPopulation = new List<DNA<T>>(populationSize);
         this.random = random;
         this.dnaSize = dnaSize;
         this.getRandomGene = getRandomGene;
         this.fitnessFunction = fitnessFunction;
 
         BestGenes = new T[dnaSize];  /*This is the array that gives me trouble*/
 
         for (int i = 0; i < populationSize; i++)
         {
             Population.Add(new DNA<T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true));
         }
     }
 
     public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false)
     {
         int finalCount = Population.Count + numNewDNA;
 
         if (finalCount <= 0) {
             return;
         }
 
         if (Population.Count > 0) {
             CalculateFitness();
             Population.Sort(CompareDNA);
         }
         newPopulation.Clear();
 
         for (int i = 0; i < Population.Count; i++)
         {
             if (i < Elitism && i < Population.Count)
             {
                 newPopulation.Add(Population[i]);
             }
             else if (i < Population.Count || crossoverNewDNA)
             {
                 DNA<T> parent1 = ChooseParent();
                 DNA<T> parent2 = ChooseParent();
 
                 DNA<T> child = parent1.Crossover(parent2);
 
                 child.Mutate(MutationRate);
 
                 newPopulation.Add(child);
             }
             else
             {
                 newPopulation.Add(new DNA<T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true));
             }
         }
 
         List<DNA<T>> tmpList = Population;
         Population = newPopulation;
         newPopulation = tmpList;
 
         Generation++;
     }
 
     private int CompareDNA(DNA<T> a, DNA<T> b)
     {
         if (a.Fitness > b.Fitness) {
             return -1;
         } else if (a.Fitness < b.Fitness) {
             return 1;
         } else {
             return 0;
         }
     }
 
     private void CalculateFitness()
     {
         fitnessSum = 0;
         DNA<T> best = Population[0];
 
         for (int i = 0; i < Population.Count; i++)
         {
             fitnessSum += Population[i].CalculateFitness(i);
 
             if (Population[i].Fitness > best.Fitness)
             {
                 best = Population[i];
             }
         }
 
         BestFitness = best.Fitness;
         best.Genes.CopyTo(BestGenes, 0);
     }
 
     private DNA<T> ChooseParent()
     {
         double randomNumber = random.NextDouble() * fitnessSum;
 
         for (int i = 0; i < Population.Count; i++)
         {
             if (randomNumber < Population[i].Fitness)
             {
                 return Population[i];
             }
 
             randomNumber -= Population[i].Fitness;
         }
 
         return null;
     }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GAManager : MonoBehaviour {
 
     private GeneticAlgorithm<MovementData> ga;
     private System.Random random;
 
     //Movement
     Transform finish;
     Transform start;
     float xstart;
     float xfinish;
     int distance;
     int topScore = 15;
     int maxSpeed = 15;
     int minSpeed = 1;
     int maxJump = 15;
     int minJump = 1;
 
     //Genetic Algorithm
     public int populationSize = 1;
     int elitism = 1;
     float mutationRate = 0.01f;
 
     // Use this for initialization
     void Start () {
         finish = GameObject.Find("Finish").GetComponent<Transform>();
         start = GameObject.Find("Start").GetComponent<Transform>();
         xstart = start.position.x;
         xfinish = finish.position.y;
         distance = (int)xstart - (int)xfinish;
         random = new System.Random();
         ga = new GeneticAlgorithm<MovementData>(populationSize, distance, random, GetRandomMovement, FitnessFunction, elitism, mutationRate);
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     public MovementData GetRandomMovement ()
     {
         MovementData movementData = new MovementData();
     movementData.speed = Random.Range (minSpeed, maxSpeed);
         movementData.jumpForce = Random.Range(minJump, maxJump);
         return movementData;
     }
 
     private float FitnessFunction(int index)
     {
         float score = 0;
         DNA<MovementData> dna = ga.Population [index];
 
         for (int i = 0; i < dna.Genes.Length; i++) 
         {
             if (dna.Genes [i].distanceAI == distance)
             {
                 score += 1;
             }
 
             score += dna.Genes[i].triggersHad / topScore;
         }
 
         return score;
     }
 
     void OnTriggerEnter(Collider other)
     {
         MovementData movetriggers = new MovementData ();
         if (other.tag == "AddScore") {
             Debug.Log("Increase Score");
             movetriggers.triggersHad++;
         } else if (other.tag == "DecreaseScore") {
             Debug.Log("Decrease Score");
             movetriggers.triggersHad--;
         }
     }
 
 }
 // This is my the manager that gets all the data i need for the AI


 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [Serializable]
 public class MovementData
 {
     public float speed;
     public float jumpForce;
     public float distanceAI;
     public int triggersHad;
 }
 
 public class Movement : MonoBehaviour {
 
     public Rigidbody rb;
     public float gravity;
     public bool hasJumped;
     public MovementData movementData;
 
     private GeneticAlgorithm<MovementData> ga;
 
     public enum State
     {
         Jump,
         Run
     }
 
     public State state;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
 
         state = State.Run;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetButtonDown("Jump"))
         {
             state = State.Jump;
         }
         else if(Input.GetButtonUp("Jump"))
         {
                 state = State.Run;
         }
 
         switch (state)
         {
             case State.Jump:
                 {
                     Jump();
                     break;
                 }
             case State.Run:
                 {
                     Run();
                     break;
                 }
         } 
     }
 
     void Jump()
     {
         rb.AddForce(Vector3.up * movementData.jumpForce, ForceMode.Impulse);
         rb.AddForce(Physics.gravity * gravity, ForceMode.Acceleration);
         hasJumped = true;
     }
 
     void Run()
     {
         rb.AddForce(Vector3.right * movementData.speed, ForceMode.Force);
         hasJumped = false;
     }
 }
 
 
 // And this is a simple AI movement script i made maybe it's relevant thank you for your help
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
0

Answer by polgoku8 · Dec 13, 2017 at 01:04 PM

never mind its solved

Comment
Add comment · 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

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

176 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

Related Questions

Choosing non null values from arrays for pathfinding 2 Answers

Need assistance with an primitive AI. 0 Answers

How to store interaction using Hashtable and use AI to act on it 0 Answers

how in order to pursue many targets? 0 Answers

How do I make AI objects not walk on top of each other? 5 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