예제 #1
0
        //    public float probPermutation;
        //    public float                    probEncaptilation;
        //    public bool                     bEditing;
        //    public bool                     bDecimation;
        ///   public float probReproduction;



        public GPParameters()
        {
            //initial value of GA
            algorithmType  = Algorithm.GP;
            chromosomeKind = GAChromosome.Continue;

            einitializationMethod = GPInitializationMethod.FullInitialization;
            eselectionMethod      = GPSelectionMethod.Rankselection;
            GPFitness             = null;//new RMSEFitness();
            SelParam1             = 3;
            SelParam2             = 0;
            elitism                     = 1;
            maxInitLevel                = 5;
            probCrossover               = 0.99F;
            probMutation                = 0.10F;
            probReproduction            = 0.20F;
            popSize                     = 1000;
            broodSize                   = 1;
            isProtectedOperationEnabled = true;
            rConstFrom                  = -1;
            rConstTo                    = 1;
            rConstNum                   = 5;

            bParalelGP = false;
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chromose"></param>
        private void Initializer(GAChromosome chromose)
        {
            bool [] PointSel = new bool[Points.Count];
            bool    bStop    = false;

            do
            {
                int iSel = RndObj.Next(0, Points.Count);
                if (!PointSel[iSel])
                {
                    PointSel[iSel] = true;
                    chromose.AddGene(new GAGene(iSel.ToString()));
                }

                for (int i = 0; i < Points.Count; i++)
                {
                    if (!PointSel[i])
                    {
                        break;
                    }
                    if (i == Points.Count - 1)
                    {
                        bStop = true;
                    }
                }
            }while(!bStop);
        }
예제 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            //初始化
            int p           = int.Parse(this.textBox2.Text);
            int Generations = int.Parse(this.textBox4.Text);//迭代次数

            if (p <= Parameter.m)
            {
                Parameter.p = p;
                GA GAAlgo = new GA();
                System.Collections.ArrayList bestFitness = new System.Collections.ArrayList();
                GAAlgo.MutationRate = double.Parse(this.textBox5.Text);
                GAAlgo.CrossRate    = double.Parse(this.textBox6.Text);
                if (comboBox1.Text == (string)comboBox1.Items[1])
                {
                    GAAlgo.Selection = GA.SelectionType.Tournment;
                }
                if (comboBox2.Text == (string)comboBox2.Items[1])
                {
                    GAAlgo.Crosstype = 2;
                }
                //寻优
                GAAlgo.Initialize();//产生第一代群体
                progressBar1.Value = 0;
                GAChromosome GAChr = new GAChromosome();
                bestFitness.Clear();
                while (GAAlgo.GenerationNum < Generations)//产生下一代群体
                {
                    GAAlgo.CreateNextGeneration();
                    GAChr = GAAlgo.GetBestChromosome();
                    bestFitness.Add(GAChr.Fitness);
                }
                progressBar1.Value = 100;
                //记录结果
                Parameter.x    = GAChr.ToArray();
                Parameter.opti = 100 / GAChr.Fitness;
                //显示结果
                label10.Text = (100 / GAChr.Fitness).ToString();
                //图像分析
                GAChartForm.chartControl1.Series.Clear();
                if (checkBox1.Checked)
                {
                    ChartSeries fitnessSeries = new ChartSeries("bestFitness");
                    fitnessSeries.SeriesIndexedModelImpl = new StringIndexedModel(fitnessSeries, (double[])bestFitness.ToArray(typeof(double)));
                    GAChartForm.chartControl1.Series.Add(fitnessSeries);
                }
                if (checkBox2.Checked)
                {
                    ChartSeries total_fitnessSeries = new ChartSeries("totalFitness");
                    total_fitnessSeries.SeriesIndexedModelImpl = new StringIndexedModel(total_fitnessSeries, (double[])GAAlgo.TotalFitness.ToArray(typeof(double)));
                    GAChartForm.chartControl1.Series.Add(total_fitnessSeries);
                }
                for (int i = 0; i < GAChartForm.chartControl1.Series.Count; i++)
                {
                    GAChartForm.chartControl1.Series[i].Type = ChartSeriesType.Line;
                }
            }
        }
예제 #4
0
 private void Clear_Click(object sender, System.EventArgs e)
 {
     m_chro = null;
     this.Refresh();
     Points.Clear();
     //新加的部分
     points.Clear();
     temps.Clear();
 }
예제 #5
0
 /// <summary>
 /// Steps
 ///			1- locate any gene locus randomaly in dad
 ///			2- locate this gene index in mum
 ///			3- Start Copying new genes from dad direction right
 ///			4- Start Copying new genes from mum direction left
 ///			5- When exit loop copy the rest of genes
 /// </summary>
 /// <param name="Dad"></param>
 /// <param name="Mum"></param>
 /// <param name="child1"></param>
 /// <param name="child2"></param>
 private void CrossOver
 (
     GAChromosome Dad
     , GAChromosome Mum
     , ref GAChromosome child1
     , ref GAChromosome child2
 )
 {
     GreedyCrossOver(Dad, Mum, ref child1);
     GreedyCrossOver(Mum, Dad, ref child2);
 }
예제 #6
0
        private void btn_Run_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (Points.Count < 2)
                {
                    MessageBox.Show("No Enough cities");
                    return;
                }
                this.m_chro = null;
                Refresh();
                //新加的部分
                points.Clear();
                temps.Clear();
                DrawPoints();
                GALib.Initializer newItializer = new GALib.Initializer(this.Initializer);
                GALib.Mutate      mutater      = new GALib.Mutate(this.ChromoseCompraror);
                GALib.Fitness     fitmethod    = new GALib.Fitness(this.Fitness);
                GALib.CrossOver   CrossMethod  = new GALib.CrossOver(this.CrossOver);

                GALib.GA GAAlgo = new GA(newItializer, fitmethod, mutater, CrossMethod);
                GAAlgo.Generations    = long.Parse(this.num_Gnr.Value.ToString());
                GAAlgo.PopulationSize = ushort.Parse(this.num_PopSiz.Value.ToString());
                GAAlgo.Mutation       = double.Parse(this.num_Mutation.Value.ToString());
                GAAlgo.CrossOver      = double.Parse(this.numCO.Value.ToString());

                if (Tool_MainBar.Buttons[1].Pushed)
                {
                    GAAlgo.EnableLogging = true;
                    GAAlgo.LogFilePath   = this.openFileDialog.FileName;
                }

                GAAlgo.Initialize();

                while (!GAAlgo.IsDone())
                {
                    GAAlgo.CreateNextGeneration();
                }

                m_chro = GAAlgo.GetBestChromosome();
                DrawCitiesPath();
            }
            catch (System.FormatException exp)
            {
                MessageBox.Show("Please check your Input Parameters " + exp);
            }
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chromose"></param>
        private void Fitness(GAChromosome chromosome)
        {
            double fitness = 0;

            int CityIndex1 = 0;
            int CityIndex2 = 1;

            while (CityIndex2 < chromosome.GeneLength)
            {
                string strGene1    = ((GAGene)chromosome[CityIndex1]).Value;
                int    Pointindex1 = int.Parse(strGene1);
                string strGene2    = ((GAGene)chromosome[CityIndex2]).Value;
                int    PointIndex2 = int.Parse(strGene2);
                fitness += Distance((Point)Points[Pointindex1], (Point)Points[PointIndex2]);
                CityIndex1++;
                CityIndex2++;
            }
            //Fitness increases when distance decreases
            chromosome.Fitness = 1000 / fitness;
        }
예제 #8
0
        /// <summary>
        ///  Based On 2opt Algo
        /// </summary>
        /// <param name="chromose"></param>
        private void ChromoseCompraror(GAChromosome chromosome)
        {
            double [,] NighborMatrix = new double[Points.Count, Points.Count];
            for (int i = 0; i < Points.Count; i++)
            {
                NighborMatrix[i, i] = -1;                //distance diagonal
            }
            //start filling the Connection Matrix with cities and distances between each other
            for (int i = 0; i < Points.Count - 1; i++)
            {
                int City1Index = int.Parse(((GAGene)chromosome[i]).Value);
                for (int j = i + 1; j < Points.Count; j++)
                {
                    int    City2Index = int.Parse(((GAGene)chromosome[j]).Value);
                    double distance   = Distance((Point)Points[City1Index], (Point)Points[City2Index]);
                    NighborMatrix[City1Index, City2Index] = distance;
                    NighborMatrix[City2Index, City1Index] = distance;
                }
            }

            GAChromosome newChromosome = new GAChromosome();
            //Start By Random Selection
            int    iCurrentSel = RndObj.Next(0, Points.Count);
            GAGene Gene        = (GAGene)chromosome[iCurrentSel];

            newChromosome.AddGene(new GAGene(Gene.Value));
            //left cities for visiting
            int iLeftPoints     = Points.Count - 1;
            int iCurrentCitySel = int.Parse(Gene.Value);

            do
            {
                int iNearstNeighbor = GetNearstNeighbor(NighborMatrix, iCurrentCitySel);
                newChromosome.AddGene(new GAGene(iNearstNeighbor.ToString()));
                iLeftPoints--;
                iCurrentCitySel = iNearstNeighbor;
            }while(iLeftPoints > 0);
            chromosome.CopyChromosome(newChromosome);
        }
예제 #9
0
        /// <summary>
        /// Mutation is done By Swapping elements from Chromosome
        /// </summary>
        /// <param name="chromose"></param>
        private void Mutator(GAChromosome chromose)
        {
            System.Random rnd = new Random();

            int ChromoLen = chromose.Capacity;

            int iSelection1 = rnd.Next(0, ChromoLen);
            int iSelection2 = iSelection1;

            while (iSelection2 == iSelection1)
            {
                iSelection2 = rnd.Next(0, ChromoLen);
            }

            GAGene Gene1 = (GAGene)chromose[iSelection1];
            GAGene Gene2 = (GAGene)chromose[iSelection2];

            chromose.RemoveAt(iSelection1);
            chromose.Insert(iSelection1, Gene2);

            chromose.RemoveAt(iSelection2);
            chromose.Insert(iSelection1, Gene1);
        }
예제 #10
0
        /// <summary>
        /// Mutation is done By Swapping elements from Chromosome
        /// </summary>
        /// <param name="chromose"></param>
        private void Mutator(GAChromosome chromose)
        {
            System.Random rnd = new Random();

            int ChromoLen	= chromose.Capacity;

            int iSelection1 = rnd.Next(0, ChromoLen);
            int iSelection2 = iSelection1;

            while(iSelection2 == iSelection1)
                iSelection2 = rnd.Next(0, ChromoLen);

            GAGene Gene1 = (GAGene)chromose[iSelection1];
            GAGene Gene2 = (GAGene)chromose[iSelection2];

            chromose.RemoveAt(iSelection1);
            chromose.Insert(iSelection1,Gene2);

            chromose.RemoveAt(iSelection2);
            chromose.Insert(iSelection1,Gene1);
        }
예제 #11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="chromose"></param>
        private void Initializer(GAChromosome chromose)
        {
            bool []PointSel = new bool[Points.Count];
            bool bStop = false;
            do
            {
                int iSel = RndObj.Next(0,Points.Count);
                if (!PointSel[iSel])
                {
                    PointSel[iSel] = true;
                    chromose.AddGene(new GAGene(iSel.ToString()));

                }

                for(int i=0; i < Points.Count ;i++)
                {
                    if (!PointSel[i])
                        break;
                    if (i == Points.Count -1)
                        bStop = true;
                }
            }while(!bStop);
        }
예제 #12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="chromose"></param>
        private void Fitness(GAChromosome chromosome)
        {
            double fitness = 0;

            int CityIndex1 = 0;
            int CityIndex2 = 1;

            while (CityIndex2 < chromosome.GeneLength)
            {
                string strGene1 = ((GAGene)chromosome[CityIndex1]).Value ;
                int Pointindex1 = int.Parse(strGene1);
                string strGene2 = ((GAGene)chromosome[CityIndex2]).Value ;
                int PointIndex2 = int.Parse(strGene2);
                fitness += Distance((Point)Points[Pointindex1],(Point)Points[PointIndex2]);
                CityIndex1++;
                CityIndex2++;
            }
            //Fitness increases when distance decreases
            chromosome.Fitness = 1000/fitness;
        }
예제 #13
0
 /// <summary>
 /// Steps 
 ///			1- locate any gene locus randomaly in dad
 ///			2- locate this gene index in mum
 ///			3- Start Copying new genes from dad direction right
 ///			4- Start Copying new genes from mum direction left 
 ///			5- When exit loop copy the rest of genes
 /// </summary>
 /// <param name="Dad"></param>
 /// <param name="Mum"></param>
 /// <param name="child1"></param>
 /// <param name="child2"></param>
 private void CrossOver(	
     GAChromosome Dad
     , GAChromosome Mum
     , ref GAChromosome child1
     , ref GAChromosome child2
     )
 {
     GreedyCrossOver(Dad, Mum, ref child1);
     GreedyCrossOver(Mum, Dad, ref child2);
 }
예제 #14
0
 private void Clear_Click(object sender, System.EventArgs e)
 {
     m_chro = null ;
     this.Refresh();
     Points.Clear();
     //�¼ӵIJ���
     points.Clear();
     temps.Clear();
 }
예제 #15
0
        /// <summary>
        ///  Based On 2opt Algo
        /// </summary>
        /// <param name="chromose"></param>
        private void ChromoseCompraror(GAChromosome chromosome)
        {
            double [,] NighborMatrix = new double[Points.Count , Points.Count];
            for (int i = 0; i < Points.Count ; i++)
                NighborMatrix[i,i] = -1; //distance diagonal

            //start filling the Connection Matrix with cities and distances between each other
            for (int i = 0 ; i < Points.Count - 1 ; i++)
            {
                int City1Index = int.Parse(((GAGene)chromosome[i]).Value);
                for (int j = i + 1; j < Points.Count ; j++)
                {
                    int City2Index  = int.Parse(((GAGene)chromosome[j]).Value);
                    double distance = Distance((Point)Points[City1Index],(Point)Points[City2Index]);
                    NighborMatrix[City1Index, City2Index] = distance ;
                    NighborMatrix[City2Index, City1Index] = distance ;
                }
            }

            GAChromosome newChromosome = new GAChromosome();
            //Start By Random Selection
            int iCurrentSel		=  RndObj.Next(0, Points.Count);
            GAGene Gene			= (GAGene)chromosome[iCurrentSel];
            newChromosome.AddGene(new GAGene(Gene.Value));
            //left cities for visiting
            int iLeftPoints		= Points.Count - 1;
            int iCurrentCitySel = int.Parse(Gene.Value);
            do
            {
                int iNearstNeighbor = GetNearstNeighbor(NighborMatrix, iCurrentCitySel);
                newChromosome.AddGene(new GAGene(iNearstNeighbor.ToString()));
                iLeftPoints--;
                iCurrentCitySel = iNearstNeighbor;

            }while(iLeftPoints > 0);
             chromosome.CopyChromosome(newChromosome);
        }
예제 #16
0
        private void btn_Run_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (Points.Count < 2)
                {
                    MessageBox.Show("No Enough cities");
                    return;
                }
                this.m_chro = null;
                Refresh();
                //�¼ӵIJ���
                points.Clear();
                temps.Clear();
                DrawPoints();
                GALib.Initializer newItializer = new GALib.Initializer(this.Initializer);
                GALib.Mutate	  mutater	   = new GALib.Mutate(this. ChromoseCompraror);
                GALib.Fitness	  fitmethod	   = new GALib.Fitness(this.Fitness);
                GALib.CrossOver	  CrossMethod  = new GALib.CrossOver(this.CrossOver);

                GALib.GA GAAlgo			= new GA(newItializer,fitmethod,mutater,CrossMethod);
                GAAlgo.Generations		= long.Parse(this.num_Gnr.Value.ToString());
                GAAlgo.PopulationSize	= ushort.Parse(this.num_PopSiz.Value.ToString());
                GAAlgo.Mutation			= double.Parse(this.num_Mutation.Value.ToString());
                GAAlgo.CrossOver		= double.Parse(this.numCO.Value.ToString());

                if(Tool_MainBar.Buttons[1].Pushed)
                {
                    GAAlgo.EnableLogging = true;
                    GAAlgo.LogFilePath   = this.openFileDialog.FileName;

                }

                GAAlgo.Initialize();

                while (!GAAlgo.IsDone())
                    GAAlgo.CreateNextGeneration();

                m_chro = GAAlgo.GetBestChromosome();
                DrawCitiesPath();
            }
            catch(System.FormatException exp)
            {
                MessageBox.Show("Please check your Input Parameters "+exp);
            }
        }
예제 #17
0
        /// <summary>
        /// //
        /// </summary>
        /// <param name="Dad"></param>
        /// <param name="Mum"></param>
        /// <param name="child1"></param>
        private void GreedyCrossOver
        (
            GAChromosome Dad
            , GAChromosome Mum
            , ref GAChromosome child
        )
        {
            int length   = Dad.GeneLength;
            int MumIndex = -1;
            int DadIndex = RndObj.Next(0, length);

            GAGene DadGene = (GAGene)Dad[DadIndex];

            MumIndex = Mum.HasThisGene(DadGene);

            if (MumIndex < 0)
            {
                throw new Exception("Gene not found in mum");
            }

            child.Add(new GAGene(DadGene.Value));

            bool bDadAdded = true;
            bool bMumAdded = true;

            do
            {
                //As long as I can add from dad
                GAGene obMumGene = null;
                GAGene obDadGene = null;
                if (bDadAdded)
                {
                    if (DadIndex > 0)
                    {
                        DadIndex = DadIndex - 1;
                    }
                    else
                    {
                        DadIndex = length - 1;
                    }

                    obDadGene = (GAGene)Dad[DadIndex];
                }
                else
                {
                    bDadAdded = false;
                }
                //As long as I can add from mum
                if (bMumAdded)
                {
                    if (MumIndex < length - 1)
                    {
                        MumIndex = MumIndex + 1;
                    }
                    else
                    {
                        MumIndex = 0;
                    }
                    obMumGene = (GAGene)Mum[MumIndex];
                }
                else
                {
                    bMumAdded = false;
                }
                if (bDadAdded && child.HasThisGene(obDadGene) < 0)
                {
                    //Add to head Dad gene
                    child.Insert(0, obDadGene);
                }
                else
                {
                    bDadAdded = false;
                }

                if (bMumAdded && child.HasThisGene(obMumGene) < 0)
                {
                    //Add to Tail Mum gene
                    child.AddGene(obMumGene);
                }
                else
                {
                    bMumAdded = false;
                }
            }while(bDadAdded || bMumAdded);

            // Add rest of genes by Random Selection
            while (child.GeneLength < length)
            {
                bool bDone = false;
                do
                {
                    int iRandom = this.RndObj.Next(0, length);
                    if (child.HasThisGene(new GAGene(iRandom.ToString())) < 0)
                    {
                        child.Add(new GAGene(iRandom.ToString()));
                        bDone = true;
                    }
                }while(!bDone);
            }
        }
예제 #18
0
        /// <summary>
        /// // 
        /// </summary>
        /// <param name="Dad"></param>
        /// <param name="Mum"></param>
        /// <param name="child1"></param>
        private void GreedyCrossOver(
            GAChromosome Dad
            , GAChromosome Mum
            , ref GAChromosome child
            )
        {
            int length		= Dad.GeneLength;
            int MumIndex	= -1;
            int DadIndex	= RndObj.Next(0,length);

            GAGene DadGene = (GAGene)Dad[DadIndex];
            MumIndex = Mum.HasThisGene(DadGene);

            if (MumIndex < 0 )
                throw new Exception("Gene not found in mum");

            child.Add(new GAGene(DadGene.Value));

            bool bDadAdded = true;
            bool bMumAdded = true;
            do
            {
                //As long as I can add from dad
                GAGene obMumGene = null;
                GAGene obDadGene = null;
                if (bDadAdded )
                {
                    if(DadIndex > 0)
                        DadIndex = DadIndex - 1 ;
                    else
                        DadIndex = length - 1 ;

                    obDadGene = (GAGene)Dad[DadIndex];
                }
                else
                {
                    bDadAdded = false;
                }
                //As long as I can add from mum
                if (bMumAdded)
                {
                    if(MumIndex < length-1)
                        MumIndex  = MumIndex + 1 ;
                    else
                        MumIndex  = 0 ;
                    obMumGene = (GAGene)Mum[MumIndex];
                }
                else
                {
                    bMumAdded = false;
                }
                if(bDadAdded  && child.HasThisGene(obDadGene)< 0)
                {
                    //Add to head Dad gene
                    child.Insert(0,obDadGene);
                }
                else
                    bDadAdded = false;

                if (bMumAdded && child.HasThisGene(obMumGene) < 0)
                {
                    //Add to Tail Mum gene
                    child.AddGene(obMumGene);
                }
                else
                    bMumAdded = false;

            }while(bDadAdded || bMumAdded)	;

            // Add rest of genes by Random Selection
            while (child.GeneLength < length)
            {
                bool bDone = false;
                do
                {
                    int iRandom = this.RndObj.Next(0, length);
                    if (child.HasThisGene(new GAGene(iRandom.ToString()))< 0)
                    {
                        child.Add(new GAGene(iRandom.ToString()));
                        bDone = true;
                    }
                }while(!bDone);
            }
        }