Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Tutorial] Genetic Algorithm C#

Discussion in 'Community Learning & Teaching' started by Kryzarel, Jun 22, 2017.

  1. Kryzarel

    Kryzarel

    Joined:
    Jul 30, 2013
    Posts:
    34
    Hey everyone, I wrote this Genetic Algorithm in C# for a college project, and I thought the implementation turned out pretty nice, figured someone might find it useful, so I decided to make a few videos explaining how to implement it from scratch :)

    The code is also available on this repository: https://bitbucket.org/kryzarel/generic-genetic-algorithm

    Part 1 - Base Implementation:


    Part 2 - Fitness Function and Optimizations:


    Part 3 - Adding Individuals on New Generations (and usage in a game)


    Part 4 - Serialization and Saving/Loading to files


    Feel free to use it however you like!
    This is licensed under "The Unlicense", which basically states you can do whatever you want with it, commercial or not, no attribution required. License file is in the repository :)
     
    Last edited: Nov 15, 2017
  2. kart_ranger

    kart_ranger

    Joined:
    Sep 12, 2014
    Posts:
    2
    Awesome .The generics part is neat.
     
    xZEEx likes this.
  3. Kryzarel

    Kryzarel

    Joined:
    Jul 30, 2013
    Posts:
    34
    Thanks! Glad you liked it. Feel free to ask questions if you have any :)
     
  4. Aa22041100

    Aa22041100

    Joined:
    Oct 22, 2015
    Posts:
    7
    Really help me for starting to learn genetic algorithm!! Thank you very much.
     
  5. Kryzarel

    Kryzarel

    Joined:
    Jul 30, 2013
    Posts:
    34
    Just to let everyone know (the 4 people that are interested xD) that the 3rd video has been released. Edited the original post to include the link.

    Good game developings everyone!
     
    Last edited: Jan 4, 2018
  6. Alex__

    Alex__

    Joined:
    Mar 23, 2015
    Posts:
    5
    This is awesome! Thanks a lot!
     
    Kryzarel likes this.
  7. landBass

    landBass

    Joined:
    Jun 13, 2016
    Posts:
    3
    exactly what i needed, can't thank you enough.
     
    Kryzarel likes this.
  8. Leunam

    Leunam

    Joined:
    Jan 30, 2013
    Posts:
    4
    Very interesting! I really appreciate it, keep up the good work.
     
    Kryzarel likes this.
  9. unity_c3Z8hg6NHcx8OA

    unity_c3Z8hg6NHcx8OA

    Joined:
    Feb 17, 2018
    Posts:
    1
    Hello...
    I know im late, but I could not figure out how to use your code for, as an example, input three different booleans and receive one float and other two booleans.
     
  10. lefevrerapha

    lefevrerapha

    Joined:
    Apr 18, 2020
    Posts:
    1
    There is an erro or it's me?
    In GeneticAlgorithm.NewGeneration
    here:
    else if (i < Population.Count || crossoverNewDNA)

    for me it have to be
    else if (i < Population.Count && crossoverNewDNA)

    other way we will never create a new child without crossoverNewDNA, do you agree?