示例#1
0
    public GAIndividual(int nvert)
    {
        // Create a random individual of size numv,
        // numv: number of vertices of the grapg

        this.nvert = nvert;
        int leftsize = randg.Next(nvert - 1) + 1;

        left  = new int[leftsize];
        right = new int[nvert - leftsize];

        int[] tmp = GAUtils.randperm(nvert);
        for (int i = 0; i < left.Length; i++)
        {
            left[i] = tmp[i];
        }
        for (int i = left.Length; i < nvert; i++)
        {
            right[i - left.Length] = tmp[i];
        }

        picklist = GAUtils.perm2picklist(tmp);

        evalFitness();         // evaluate fitness of this new individual
    }
示例#2
0
        private void Init(int numItems)
        {
            Started    = false;
            Generation = 0;

            // Create the sack items
            ExternalItems = new List <SackItem>();
            do
            {
                SackItem item = new SackItem(GAUtils.RandInt(0, 100), GAUtils.RandInt(0, 10));
                if (!ExternalItems.Any(i => i.Weight == item.Weight))
                {
                    ExternalItems.Add(item);
                }
            } while (ExternalItems.Count < numItems);

            int geneCombinations = numItems;
            int chromosoneLength = geneCombinations * 1; // 1 genes per chromosone (item in or out)

            // initialize the GA
            Algorithm = new GeneticAlgorithm <KnapsackGenome>(
                GeneticAlgorithm <KnapsackGenome> .DefaultCrossoverRate,
                GeneticAlgorithm <KnapsackGenome> .DefaultMutationRate,
                GeneticAlgorithm <KnapsackGenome> .DefaultPopulationSize,
                chromosoneLength,
                geneCombinations);

            // grab the genomes
            GenomePopulation = Algorithm.GrabGenomes();
        }
示例#3
0
    public static void Main()
    {
        GAIndividual.adj_matrix = GAUtils.readMatrix(@"d:\graph.txt");
        int nvert = GAIndividual.adj_matrix.Length;         // number of vertices

        GAEvolve.evolveAndMakeMFile(@"d:\ga.m", 1, 400, 200, nvert, 50, 50);
    }
示例#4
0
        /// <summary>
        /// Takes 2 parent gene vectors, selects a midpoint and then swaps the ends
        /// of each genome creating 2 new genomes which are stored in baby1 and baby2
        /// </summary>
        /// <param name="other"></param>
        /// <param name="baby1"></param>
        /// <param name="baby2"></param>
        public void CrossOver(Genome <TMainModel, TCombinationModel> other, Genome <TMainModel, TCombinationModel> baby1, Genome <TMainModel, TCombinationModel> baby2)
        {
            // just return parents as offspring dependent on the rate
            // or if parents are the same
            if ((GAUtils.RandDouble() > _settings.CrossoverRate) || (this == other))
            {
                baby1.Genes = this.Genes;
                baby2.Genes = other.Genes;
                return;
            }

            if (baby1.Genes.Count <= 1 || baby2.Genes.Count <= 1)
            {
                baby1.Genes = this.Genes;
                baby2.Genes = other.Genes;
                return;
            }


            // determine a crossover point
            int crossOverPoint = GAUtils.RandInt(0, this.Genes.Count - 1); // = 29

            for (int i = 0; i < this.Genes.Count; i++)
            {
                var crossed = Genes[i].Cross(other.Genes[i], i < crossOverPoint);
                baby1.Genes.Add(crossed[0]);
                baby2.Genes.Add(crossed[1]);
            }
        }
示例#5
0
    public static void Main(string[] args)
    {
        int[] a = new int[] { 2, 4, 3, 1, 0, 5 };
        int[] b = GAUtils.perm2picklist(a);

        float[][] matrix = GAUtils.readMatrix("graph.txt");
        for (int i = 0; i < matrix.Length; i++)
        {
            Console.WriteLine(GAUtils.array2string(matrix[i]));
        }
    }
示例#6
0
    public static void main(string[] args)
    {
        GAIndividual.adj_matrix = GAUtils.readMatrix("graph.txt");
        int nvert = GAIndividual.adj_matrix.Length;         // number of vertices

        GAIndividual i1 = new GAIndividual(nvert);
        GAIndividual i2 = new GAIndividual(nvert);

        for (int i = 0; i < 1000; i++)
        {
            Console.WriteLine(new GAIndividual(nvert));
        }
    }
示例#7
0
    public static void Main()
    {
        GAIndividual.adj_matrix = GAUtils.readMatrix(@"d:\graph.txt");
        int nvert = GAIndividual.adj_matrix.Length;         // number of vertices

        GAPopulation gap = new GAPopulation(100, nvert);

        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine(gap.ind[gap.best_index]);
            gap = GAPopulation.generate(gap, 60, 30);
        }
        Console.WriteLine(gap.ind[gap.best_index]);
    }
示例#8
0
        public void Initialize()
        {
            int geneSize = _settings.InitialPopulation.Count; // number of all items to be combined

            Parts.Clear();                                    // for re-initialisation when there is already a gene that's the same
            for (int i = 0; i < geneSize; i++)
            {
                Parts.Add(new GenePart <TCombinationModel>
                {
                    Model  = _settings.InitialPopulation[i],
                    Active = GAUtils.RandDouble() > 0.5
                });
            }
        }
示例#9
0
    public virtual GAIndividual mutate()
    {
        int[] child_picklist = new int[nvert];
        for (int i = 0; i < nvert; i++)
        {
            child_picklist[i] = picklist[i];
        }

        // mutate one point:
        int m_point = randg.Next(nvert - 1);         // mutation point

        child_picklist[m_point] = randg.Next(nvert - m_point);

        int[] tmp = GAUtils.picklist2perm(child_picklist);
        int[] l, r;

        if (nextBoolean() == true)
        {
            // this mutation preserves structure:
            if (nextBoolean() == true)
            {
                l = new int[left.Length];
                r = new int[right.Length];
            }
            else
            {
                l = new int[right.Length];
                r = new int[left.Length];
            }
        }
        else
        {
            // mutate structure:
            int dpoint = 1 + randg.Next(nvert - 1);             // tmp division point
            l = new int[dpoint];
            r = new int[nvert - dpoint];
        }

        for (int i = 0; i < l.Length; i++)
        {
            l[i] = tmp[i];
        }
        for (int i = 0; i < r.Length; i++)
        {
            r[i] = tmp[l.Length + i];
        }

        return(new GAIndividual(l, r));
    }
示例#10
0
 /// <summary>
 /// Iterates through each gene flipping the bits acording to the mutation rate
 /// </summary>
 public void Mutate()
 {
     // Go through each gene
     for (int i = 0; i < Genes.Count; i++)
     {
         Gene <TCombinationModel> gene = Genes[i];
         int mutationCounter           = 0;
         for (int j = 0; j < gene.Parts.Count; j++)
         {
             //do we flip this bit?
             if (GAUtils.RandDouble() < _settings.MutationRate)
             {
                 //flip the bit
                 gene.Parts[j].Active = gene.Parts[j].Active == false;
                 mutationCounter++;
             }
         }
         //Debug.WriteLineIf(mutationCounter > 0, $"> Mutated {mutationCounter}/{gene.Parts.Count} items");
     }
 }
示例#11
0
    public static GAIndividual xover1p(GAIndividual f, GAIndividual m)
    {
        // 1-point cross over
        int xpoint = 1 + randg.Next(f.nvert - 1);

        int[] child_picklist = new int[f.nvert];
        for (int i = 0; i < xpoint; i++)
        {
            child_picklist[i] = f.picklist[i];
        }

        for (int i = xpoint; i < f.nvert; i++)
        {
            child_picklist[i] = m.picklist[i];
        }

        int[] tmp = GAUtils.picklist2perm(child_picklist);
        int[] l, r;

        if (nextBoolean() == true)
        {
            l = new int[f.left.Length]; // WHY?
            r = new int[tmp.Length - f.left.Length];
        }
        else
        {
            l = new int[m.left.Length]; // WHY?
            r = new int[tmp.Length - m.left.Length];
        }

        for (int i = 0; i < l.Length; i++)
        {
            l[i] = tmp[i];
        }
        for (int i = 0; i < r.Length; i++)
        {
            r[i] = tmp[l.Length + i];
        }

        return(new GAIndividual(l, r));
    }
示例#12
0
        private Genome <TMainModel, TCombinationModel> RouletteWheelSelection()
        {
            double fSlice = GAUtils.RandDouble() * TotalFitnessScore;

            double cfTotal = 0.0;

            int selectedGenome = 0;

            for (int i = 0; i < Genomes.Count; ++i)
            {
                cfTotal += Genomes[i].Fitness;

                if (cfTotal > fSlice)
                {
                    selectedGenome = i;
                    break;
                }
            }

            return(Genomes[selectedGenome]);
        }
示例#13
0
    public GAIndividual(int[] l, int[] r)
    {
        nvert = l.Length + r.Length;
        int[] tmp = new int[nvert];

        left = new int[l.Length];
        for (int i = 0; i < l.Length; i++)
        {
            left[i] = l[i];
            tmp[i]  = l[i];
        }
        right = new int[r.Length];
        for (int i = 0; i < r.Length; i++)
        {
            right[i]          = r[i];
            tmp[l.Length + i] = r[i];
        }

        picklist = GAUtils.perm2picklist(tmp);

        evalFitness();         // evaluate fitness of this new individual
    }