Exemple #1
0
    private List <TrackChromosome> NewPopulation(List <TrackChromosome> old_pop, int new_size)
    {
        List <TrackChromosome> new_pop = new List <TrackChromosome>();


        while (new_pop.Count != new_size)
        {
            TrackChromosome c1 = Selection(old_pop);
            TrackChromosome c2 = Selection(old_pop);

            List <TrackChromosome> crossed = TrackChromosome.Crossover(c1, c2);

            new_pop.Add(crossed[0]);

            if (new_pop.Count < new_size)
            {
                new_pop.Add(crossed[1]);
            }
        }

        for (int i = 0; i < new_size; i++)
        {
            new_pop[i].Mutate();
        }

        return(new_pop);
    }
    public static List <TrackChromosome> Crossover(TrackChromosome c1, TrackChromosome c2)
    {
        List <TrackChromosome> new_cromosomes = new List <TrackChromosome>();

        TrackChromosome new_c1 = new TrackChromosome(c1);
        TrackChromosome new_c2 = new TrackChromosome(c2);

        int x_point = Random.Range(0, c1.genes.Length);

        for (int i = 0; i < c1.genes.Length; i++)
        {
            if (i < x_point)
            {
                new_c1.genes[i] = c2.genes[i];
            }
            else
            {
                new_c2.genes[i] = c1.genes[i];
            }
        }

        new_cromosomes.Add(new_c1);
        new_cromosomes.Add(new_c2);

        return(new_cromosomes);
    }
 public TrackChromosome(TrackChromosome c)
 {
     genes = new int[c.genes.Length];
     for (int i = 0; i < c.genes.Length; i++)
     {
         genes[i] = c.genes[i];
     }
 }
Exemple #4
0
    private TrackChromosome Selection(List <TrackChromosome> chrs)
    {
        TrackChromosome s = chrs[Random.Range(0, chrs.Count)];
        int             tournament_size = 4;

        for (int i = 1; i < tournament_size; i++)
        {
            TrackChromosome new_s = chrs[Random.Range(0, chrs.Count)];
            if (s.GetFitness() < new_s.GetFitness())
            {
                s = new_s;
            }
        }
        return(s);
    }
    public static bool Check(TrackChromosome t)
    {
        int[] track = t.GetGenes();
        //0 = hard right, 1 = hard left, 2 = soft right, 3 = soft left, 4 = straight, 5 = end
        //remove old track

        List <int[]> coords = new List <int[]>();

        int entryDir = 0;

        int[] startingCoords = { 0, 0 };
        coords.Add(startingCoords);

        foreach (int trackElemt in track)
        {
            int[] latestCoords = coords[coords.Count - 1];
            int[] nextCoords   = new int[2];

            if (entryDir == 0)
            {
                if (trackElemt == 0 || trackElemt == 2)
                {
                    nextCoords[0] = latestCoords[0] + 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 1;
                }
                else if (trackElemt == 1 || trackElemt == 3)
                {
                    nextCoords[0] = latestCoords[0] - 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 3;
                }
                else if (trackElemt == 4 || trackElemt == 5)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] + 1;
                    entryDir      = 0;
                }
            }
            else if (entryDir == 1)
            {
                if (trackElemt == 0 || trackElemt == 2)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] - 1;
                    entryDir      = 2;
                }
                else if (trackElemt == 1 || trackElemt == 3)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] + 1;
                    entryDir      = 0;
                }
                else if (trackElemt == 4 || trackElemt == 5)
                {
                    nextCoords[0] = latestCoords[0] + 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 1;
                }
            }
            else if (entryDir == 2)
            {
                if (trackElemt == 0 || trackElemt == 2)
                {
                    nextCoords[0] = latestCoords[0] - 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 3;
                }
                else if (trackElemt == 1 || trackElemt == 3)
                {
                    nextCoords[0] = latestCoords[0] + 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 1;
                }
                else if (trackElemt == 4 || trackElemt == 5)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] - 1;
                    entryDir      = 2;
                }
            }
            else if (entryDir == 3)
            {
                if (trackElemt == 0 || trackElemt == 2)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] + 1;
                    entryDir      = 0;
                }
                else if (trackElemt == 1 || trackElemt == 3)
                {
                    nextCoords[0] = latestCoords[0];
                    nextCoords[1] = latestCoords[1] - 1;
                    entryDir      = 2;
                }
                else if (trackElemt == 4 || trackElemt == 5)
                {
                    nextCoords[0] = latestCoords[0] - 1;
                    nextCoords[1] = latestCoords[1];
                    entryDir      = 3;
                }
            }
            foreach (int[] prevCoord in coords)
            {
                if (prevCoord[0] == nextCoords[0] && prevCoord[1] == nextCoords[1])
                {
                    return(false);
                }
            }
            coords.Add(nextCoords);
        }
        return(true);
    }