Ejemplo n.º 1
0
        public object Clone()
        {  // make a copy of self and return it in object
            GAMember g1 = new GAMember(this.memSize, DistMatrix);

            for (int i = 0; i < g1.memSize; i++)
            {
                g1.mem[i] = this.mem[i];
            }
            g1.memSize = this.memSize;
            g1.fitness = this.fitness;
            return(g1);
        }
Ejemplo n.º 2
0
 public TSGA(int popSz, int numCities, float mutRate, float crossRate,
             int[,] distMat, ref object lockobj) // for mulithreaded version
 {                                               // constructor
     this.mutationRate  = mutRate;
     this.memSize       = numCities + 1;         // start and end city is 0
     this.crossoverRate = crossRate;
     DistMat            = distMat;
     this.pop           = new GAMember[popSz];
     for (int i = 0; i < popSz; i++)
     {
         pop[i] = new GAMember(memSize, distMat);
     }
     this.popSize = popSz; //60
     this.lockobj = lockobj;
 }
        void RunGA()//NOT USED
        {
            //----------
            ga.InitializePopulation();
            ga.EvaluatePopulation();
            Array.Sort(ga.pop);              // sort population
            //int bestFitness = ga.pop[0].fitness;
            int bestFitness = ga[0].fitness; // because of indexer

            lock (this)
            {
                bestMember = (GAMember)ga[0].Clone();
            }
            for (int j = 0; j < TSGAConstants.NumIterations; j++)
            {
                // each loop is one generation
                ga.SelectPopulation();
                ga.CrossoverPopulation();
                ga.EvaluatePopulation();
                Array.Sort(ga.pop);  // sort population
                lblStatus2.Text = ("Best cand:" + j.ToString() + " " + ga.pop[0].fitness.ToString() +
                                   "  " + ga.pop[0].ToString());

                lock (this)
                {
                    if (ga.pop[0].fitness < bestMember.fitness)
                    {
                        bestMember = (GAMember)ga[0].Clone();
                    }
                }

                ga.MutatePopulation();
                ga.EvaluatePopulation();
                Array.Sort(ga.pop);  // sort population

                if (ga.pop[0].fitness < bestFitness)
                {
                    bestFitness     = ga.pop[0].fitness;
                    lblStatus2.Text = bestFitness.ToString();
                }
            }
        }
Ejemplo n.º 4
0
        public void RunGA() // overall GA
        {
            //----------
            InitializePopulation();
            EvaluatePopulation();             //get fitness for every GAmember
            Array.Sort(pop);                  // sort population by fitness
            int bestFitness = pop[0].fitness; // because of indexer

            bestMember = (GAMember)pop[0].Clone();
            for (int j = 0; j < TSGAConstants.NumIterations; j++)//50000 iter.
            {
                // each loop is one generation
                SelectPopulation(); //duplicate top 30% into worst 30%
                CrossoverPopulation();
                EvaluatePopulation();
                Array.Sort(pop);  // sort population

                lock (lockobj)
                {
                    if (pop[0].fitness < bestMember.fitness)
                    {
                        bestMember = (GAMember)pop[0].Clone();
                    }
                    iterationCount = j;
                }

                MutatePopulation();
                EvaluatePopulation();
                Array.Sort(pop);  // sort population

                lock (lockobj)
                {
                    if (pop[0].fitness < bestFitness)
                    {
                        bestFitness = pop[0].fitness;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public TSGA(int popSz, int numCities, float mutRate, float crossRate,
                    int[,] distMat, ref object lockobj, ref object[] synchObjsA, ref object[] synchObjsB,
                    int[] dataready, int[] xchgdone, int threadNum) // for mulithreaded version
        {                                                           // constructor
            this.mutationRate  = mutRate;
            this.memSize       = numCities + 1;                     // start and end city is 0
            this.crossoverRate = crossRate;
            this.pop           = new GAMember[popSz];
            for (int i = 0; i < popSz; i++)
            {
                pop[i] = new GAMember(memSize, DistMat);
            }
            this.popSize = popSz;
            DistMat      = distMat;
            this.lockobj = lockobj;

            this.threadNum  = threadNum;
            this.synchObjsA = synchObjsA;
            this.synchObjsB = synchObjsB;
            this.dataready  = dataready;
            this.xchgdone   = xchgdone;
        }
Ejemplo n.º 6
0
        public void RunGA_XCHG() // overall GA with XCHG capabilities
        {
            InitializePopulation();
            EvaluatePopulation();
            Array.Sort(pop);                  // sort population
            int bestFitness = pop[0].fitness; // because of indexer

            bestMember = (GAMember)pop[0].Clone();
            for (int j = 0; j < TSGAConstants.NumIterations; j++)
            {
                // each loop is one generation
                SelectPopulation();
                CrossoverPopulation();
                EvaluatePopulation();
                Array.Sort(pop);  // sort population

                lock (lockobj)
                {
                    if (pop[0].fitness < bestMember.fitness)
                    {
                        bestMember = (GAMember)pop[0].Clone();
                    }
                    iterationCount = j;
                }

                if (((j + 1) % TSGAConstants.ExchangeAfterIterations) == 0)  // xchange pop every few hundred(500) generations
                {
                    lock (lockobj)
                    {
                        dataready[threadNum] = 1; // data is ready
                    }
                    lock (synchObjsA[threadNum])
                    {
                        Monitor.PulseAll(synchObjsA[threadNum]); // main will xchg and signal back
                    }

                    if (xchgdone[threadNum] == 0)
                    {
                        lock (synchObjsB[threadNum])
                        {
                            Monitor.Wait(synchObjsB[threadNum]); // wait for main to get done with xchg
                        }
                    }
                    lock (lockobj)
                    {
                        xchgdone[threadNum] = 0;
                    }
                }


                MutatePopulation();
                EvaluatePopulation();
                Array.Sort(pop);  // sort population

                lock (lockobj)
                {
                    if (pop[0].fitness < bestFitness)
                    {
                        bestFitness = pop[0].fitness;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public int CompareTo(Object rhs)  // for sorting
        {
            GAMember mr = (GAMember)rhs;

            return(this.fitness.CompareTo(mr.fitness));  // index 0 is best
        }