Beispiel #1
0
    /*-------------------------------------------------------------------------------------------------------------*/
    /** <summary> Called to start running the algorithm in the scene. Implementation is
     * specific to the child type. </summary> */
    public override void Initiate_algorithm()
    {
        genome = new GAGenome();

        //create a new flock
        flock = base.Create_flock(Prefab.FLOCK_PREFAB, new HCVectorSet(genome), NUM_BOIDS);

        //notify all listeners
        Notify ();
    }
Beispiel #2
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary> Called to start running the algorithm in the scene. Implementation is
     * specific to the child type. </summary> */
    override public void Initiate_algorithm()
    {
        genome = new GAGenome();

        //create a new flock
        flock = base.Create_flock(Prefab.FLOCK_PREFAB, new HCVectorSet(genome), NUM_BOIDS);

        //notify all listeners
        Notify();
    }
Beispiel #3
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary>
     * Creates a new flock, instantiating it, populating it, changes the camera target, and assigns a <see cref="VectorSet"/>.
     * Most of this is done outside the class. </summary>
     * <param name="genome"> The <see cref="GAGenome"/> for the new flock </param> */
    private void Start_new_flock(GAGenome genome)
    {
        //create a new flock. base takes care of everything
        flock = base.Create_flock(Prefab.GA_FLOCK_PREFAB, new GAVectorSet(genome), NUM_BOIDS);

        //increment membersCounter
        membersCounter++;

        //notify all listeners that there's a new flock and information has been updated
        Notify();
    }
Beispiel #4
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary>
     * Sets up the scene to begin running the algorithm. </summary> */
    override public void Initiate_algorithm()
    {
        //initialize GAGenome to find out the length of binary strings
        GAGenome.Initialize();

        //set up the arrays
        currentGeneration = new GAGenome[MEMBERS];

        //randomly generate first generation
        for (int i = 0; i < MEMBERS; i++)
        {
            currentGeneration[i] = new GAGenome();             //create a random genome
        }

        //begin the update loop!
        StartCoroutine(Time_flock());
    }
Beispiel #5
0
    /** <summary>
     * Order the population members by their fitness level. Uses bubble sort. </summary> */
    private void OrderByFitness()
    {
        GAGenome temp = null;

        //order the array
        for (int write = 0; write < currentGeneration.Length; write++)
        {
            //for each subsequent fitness
            for (int sort = 0; sort < currentGeneration.Length - 1; sort++)
            {
                if (currentGeneration[sort].Fitness < currentGeneration[sort + 1].Fitness)
                {
                    temp = currentGeneration[sort + 1];
                    currentGeneration[sort + 1] = currentGeneration[sort];
                    currentGeneration[sort]     = temp;
                }
            }
        }
    }
Beispiel #6
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary>
     * Creates a new generation of binary strings. This involves preserving elites,
     * selecting mates, crossing over genomes, and mutating offspring. This method assumes that
     * the currentGeneration's genomes are in order from greatest to least fitness score. </summary>
     * <returns> A generation bred from the current generation </returns> */
    private GAGenome[] Breed()
    {
        //array for the next generation.
        GAGenome[] nextGeneration = new GAGenome[MEMBERS];
        string[]   mates          = new string[2], offspring = new string[2];

        int arrayPlace;

        //carry the elites over into the new generation
        for (arrayPlace = 0; arrayPlace < ELITES; arrayPlace++)
        {
            nextGeneration[arrayPlace] = new GAGenome(currentGeneration[arrayPlace].BinaryString);
        }

        //perform crossovers
        //if (MEMBERS - ELITES - RANDOM_MEMBERS) / 2 is not a whole number, a random genome will also be generated later
        for (int i = 0; i < ((MEMBERS - ELITES - RANDOM_MEMBERS) / 2); i++, arrayPlace += 2)
        {
            //make the selection. Options: Roulette, Rank,
            mates = Selection(ROULETTE_SELECTION);

            //cross the genomes
            offspring = CrossGenomes(mates, 1);

            //mutate the offspring
            offspring[0] = Mutate(offspring[0]);
            offspring[1] = Mutate(offspring[1]);

            //save the offspring to the next generation array
            nextGeneration[arrayPlace]     = new GAGenome(offspring[0]);
            nextGeneration[arrayPlace + 1] = new GAGenome(offspring[1]);
        }

        //fill the remainder with random new genomes
        while (arrayPlace < MEMBERS)
        {
            nextGeneration[arrayPlace++] = new GAGenome();
        }

        //replace the currentGeneration
        return(nextGeneration);
    }
Beispiel #7
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary>
     * Called by the Algorithm under observation. This retrieves the GAGenome of the flock
     * currently active in the scene so that updated information can be displayed. </summary> */
    override public void Update_listener()
    {
        genome = ((HillClimbing)subject).Get_current_genome();
    }
Beispiel #8
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** <summary>
     * Called by the Algorithm under observation. This retrieves the GAGenome of the flock
     * currently active in the scene so that updated information can be displayed. </summary> */
    override public void Update_listener()
    {
        genome = ((GeneticAlgorithm)subject).Get_current_genome();
    }
Beispiel #9
0
 /*-------------------------------------------------------------------------------------------------------------*/
 /** <summary>
  * Primary constructor. </summary>
  * <param name="genome"> The <see cref="GACoefficient"/> information to use </param> */
 public GAVectorSet(GAGenome genome)
 {
     this.genome = genome;
 }
Beispiel #10
0
    /*-------------------------------------------------------------------------------------------------------------*/

    /** Primary Constructor.
     * genome - the coefficient information to use */
    public HCVectorSet(GAGenome genome)
    {
        this.genome = genome;
    }
Beispiel #11
0
    /*-------------------------------------------------------------------------------------------------------------*/
    /** <summary>
     * Creates a new flock, instantiating it, populating it, changes the camera target, and assigns a <see cref="VectorSet"/>.
     * Most of this is done outside the class. </summary>
     * <param name="genome"> The <see cref="GAGenome"/> for the new flock </param> */
    private void Start_new_flock(GAGenome genome)
    {
        //create a new flock. base takes care of everything
        flock = base.Create_flock(Prefab.GA_FLOCK_PREFAB, new GAVectorSet(genome), NUM_BOIDS);

        //increment membersCounter
        membersCounter++;

        //notify all listeners that there's a new flock and information has been updated
        Notify ();
    }
Beispiel #12
0
    /*-------------------------------------------------------------------------------------------------------------*/
    /** <summary>
     * Creates a new generation of binary strings. This involves preserving elites,
     * selecting mates, crossing over genomes, and mutating offspring. This method assumes that
     * the currentGeneration's genomes are in order from greatest to least fitness score. </summary>
     * <returns> A generation bred from the current generation </returns> */
    private GAGenome[] Breed()
    {
        //array for the next generation.
        GAGenome[] nextGeneration = new GAGenome[MEMBERS];
        string[] mates = new string[2], offspring = new string[2];

        int arrayPlace;

        //carry the elites over into the new generation
        for (arrayPlace = 0; arrayPlace < ELITES; arrayPlace++)
            nextGeneration[arrayPlace] = new GAGenome(currentGeneration[arrayPlace].BinaryString);

        //perform crossovers
        //if (MEMBERS - ELITES - RANDOM_MEMBERS) / 2 is not a whole number, a random genome will also be generated later
        for (int i = 0; i < ((MEMBERS - ELITES - RANDOM_MEMBERS) / 2); i++, arrayPlace += 2) {

            //make the selection. Options: Roulette, Rank,
            mates = Selection(ROULETTE_SELECTION);

            //cross the genomes
            offspring = CrossGenomes(mates, 1);

            //mutate the offspring
            offspring[0] = Mutate (offspring[0]);
            offspring[1] = Mutate (offspring[1]);

            //save the offspring to the next generation array
            nextGeneration[arrayPlace] = new GAGenome(offspring[0]);
            nextGeneration[arrayPlace + 1] = new GAGenome(offspring[1]);
        }

        //fill the remainder with random new genomes
        while (arrayPlace < MEMBERS)
            nextGeneration[arrayPlace++] = new GAGenome();

        //replace the currentGeneration
        return nextGeneration;
    }
Beispiel #13
0
    /*-------------------------------------------------------------------------------------------------------------*/
    /** <summary>
     * Sets up the scene to begin running the algorithm. </summary> */
    public override void Initiate_algorithm()
    {
        //initialize GAGenome to find out the length of binary strings
        GAGenome.Initialize();

        //set up the arrays
        currentGeneration = new GAGenome[MEMBERS];

        //randomly generate first generation
        for (int i = 0; i < MEMBERS; i++) {
            currentGeneration[i] = new GAGenome(); //create a random genome
        }

        //begin the update loop!
        StartCoroutine(Time_flock());
    }
Beispiel #14
0
 /*-------------------------------------------------------------------------------------------------------------*/
 /** <summary>
  * Called by the Algorithm under observation. This retrieves the GAGenome of the flock
  * currently active in the scene so that updated information can be displayed. </summary> */
 public override void Update_listener()
 {
     genome = ((GeneticAlgorithm)subject).Get_current_genome ();
 }
Beispiel #15
0
 /*-------------------------------------------------------------------------------------------------------------*/
 /** <summary>
  * Called by the Algorithm under observation. This retrieves the GAGenome of the flock
  * currently active in the scene so that updated information can be displayed. </summary> */
 public override void Update_listener()
 {
     genome = ((HillClimbing)subject).Get_current_genome ();
 }