Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            desc.Clear();
            alg = new PGA();
            OptimizationModel optModel = new OptimizationModel(func);
            AlgorithmSettings settings = new AlgorithmSettings()
            {
                InitialLoadType             = (InitialLoadType)comboBox1.SelectedItem,
                OptModel                    = optModel,
                InitialPointCount           = (int)numericUpDown1.Value,
                SelectionType               = (SelectionType)comboBox2.SelectedItem,
                EndCondition                = (EndCondition)comboBox3.SelectedItem,
                MaxGenerationCount          = (int)numericUpDown2.Value,
                SurvivedCount               = (int)numericUpDown3.Value,
                MutationChance              = (double)numericUpDown4.Value,
                CrossingGenNumber           = (int)numericUpDown5.Value,
                Tolerance                   = (double)numericUpDown6.Value,
                MutationChanceAfterCrossing = (double)numericUpDown7.Value,
                MutationType                = (MutationType)comboBox4.SelectedItem
            };

            alg.Run(settings);
            DrawRezult(alg);
            WriteRezult(alg);
        }
Beispiel #2
0
        /// <summary>
        /// Set PGA ( Programmable Gain Amplifier ) Gain
        /// </summary>
        /// <param name="pga">PGA</param>
        public void SetPGA(PGA pga)
        {
            byte[] buff = new byte[1];

            sensor.WriteRead(new byte[] { 0x01 }, buff);
            int reg1 = buff[0];

            sensor.WriteRead(new byte[] { 0x04 }, buff);
            int reg3 = buff[0];

            int pgaVal = (int)pga << 3;

            reg1 = (reg1 & 0xC7) | pgaVal;

            switch (pga)
            {
            case PGA.PGA_0dB:
                reg3 = (reg3 & 0xCF) | (3 << 4);
                break;

            case PGA.PGA_4dB:
                reg3 = (reg3 & 0xCF) | (3 << 4);
                break;

            case PGA.PGA_8dB:
                reg3 = (reg3 & 0xCF) | (3 << 4);
                break;

            case PGA.PGA_12dB:
                reg3 = (reg3 & 0xCF) | (3 << 4);
                break;

            case PGA.PGA_N0dB:
                reg3 = (reg3 & 0xCF) | (0 << 4);
                break;

            case PGA.PGA_N4dB:
                reg3 = (reg3 & 0xCF) | (0 << 4);
                break;

            case PGA.PGA_N8dB:
                reg3 = (reg3 & 0xCF) | (0 << 4);
                break;

            case PGA.PGA_N12dB:
                reg3 = (reg3 & 0xCF) | (0 << 4);
                break;

            default:
                break;
            }

            sensor.Write(new byte[] { 0x04, (byte)reg3 });
            sensor.Write(new byte[] { 0x01, (byte)reg1 });
        }
 public Individual[] RunParallelGATest(int NumberOfParallelPopulations, TxtParser TextParser, int PopulationQuantity, double MutationRate,
                                       double CrossoverRate, int GenerationsNumber, int NumberOfTestRuns)
 {
     for (currentTestNumber = 0; currentTestNumber < NumberOfTestRuns; currentTestNumber++)
     {
         PGA.RunGA(NumberOfParallelPopulations, TextParser, PopulationQuantity, MutationRate, CrossoverRate, GenerationsNumber);
         for (int j = 0; j < NumberOfParallelPopulations; j++)
         {
             AddOneBestIndividualToList(PGA.PopulationList[j]);
         }
     }
     return(individualList);
 }
Beispiel #4
0
        private void DrawRezult(PGA alg)
        {
            numericUpDown8.Maximum = alg.Generations.Count - 1;

            desc.Clear();
            desc.DrawMesh(alg.InitialMesh, Pens.Red);
            for (int i = 0; i < alg.Generations.Count; i++)
            {
                if (alg.Generations[i].AllFinishChromosome != null)
                {
                    foreach (var item in alg.Generations[i].AllFinishChromosome)
                    {
                        SolidBrush b = new SolidBrush(pens[item.GId].Color);

                        desc.AddCircle((float)item.X1, (float)item.X2, 3, pens[item.GId], true);
                        desc.AddText(item.GId + "." + item.Id, (float)item.X1, (float)item.X2, SystemFonts.SmallCaptionFont, b);
                    }
                }
            }
            desc.Update();
        }
Beispiel #5
0
        /// <summary>
        /// Initialize KT0803L
        /// </summary>
        /// <param name="mhz">FM Channel ( Range from 70Mhz to 108Mhz )</param>
        /// <param name="country">Region</param>
        /// <param name="rfgain">Transmission Power</param>
        /// <param name="pga">PGA ( Programmable Gain Amplifier ) Gain</param>
        public async Task InitializeAsync(double mhz, Country country = Country.CHINA, RFGAIN rfgain = RFGAIN.RFGAIN_98_9dBuV, PGA pga = PGA.PGA_0dB)
        {
            var settings = new I2cConnectionSettings(KT_ADDR);

            settings.BusSpeed = I2cBusSpeed.StandardMode;

            var controller = await I2cController.GetDefaultAsync();

            sensor = controller.GetDevice(settings);

            SetChannel(mhz);
            SetRFGAIN(rfgain);
            SetPGA(pga);
            SetPHTCNST(country);
        }
Beispiel #6
0
        private void WriteRezult(PGA alg)
        {
            int i = 0;

            int[] clr = new int[100];
            foreach (var item in alg.Generations)
            {
                WriteLine(string.Format("Покаление {0}", item.Id));

                WriteLine("На входе");
                if (item.InitialChromosomes != null)
                {
                    foreach (var ch in item.InitialChromosomes)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Первая мутация");
                if (item.FirstMutation != null)
                {
                    foreach (var ch in item.FirstMutation)
                    {
                        WriteChomosome(ch.Key, "Родитель");
                        WriteChomosome(ch.Value, "Потомок");
                        WriteLine();
                    }
                }

                WriteLine("Перед скрещиванием");
                if (item.AfterFirstMutation != null)
                {
                    foreach (var ch in item.AfterFirstMutation)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Пары1");
                if (item.Pairs1 != null)
                {
                    foreach (var ch in item.Pairs1)
                    {
                        WriteChomosome(ch.Chr1);
                        WriteChomosome(ch.Chr2);
                        WriteLine();
                    }
                }

                WriteLine("Победители в парах1");
                if (item.Winners1 != null)
                {
                    foreach (var ch in item.Winners1)
                    {
                        WriteChomosome(ch);
                    }
                }


                WriteLine("Пары2");
                if (item.Pairs2 != null)
                {
                    foreach (var ch in item.Pairs2)
                    {
                        WriteChomosome(ch.Chr1);
                        WriteChomosome(ch.Chr2);
                        WriteLine();
                    }
                }

                WriteLine("Победители в парах2");
                if (item.Winners2 != null)
                {
                    foreach (var ch in item.Winners2)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Пары для генетических операций");
                if (item.PairsForGeneticOperation != null)
                {
                    foreach (var ch in item.PairsForGeneticOperation)
                    {
                        WriteChomosome(ch.Chr1);
                        WriteChomosome(ch.Chr2);
                        WriteLine();
                    }
                }

                WriteLine("Результат генетических операций");
                if (item.GeneticOperationRezults != null)
                {
                    foreach (var ch in item.GeneticOperationRezults)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Все хромосомы покаления");
                if (item.AllFinishChromosome != null)
                {
                    foreach (var ch in item.AllFinishChromosome)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Лучшие");
                if (item.Output != null)
                {
                    foreach (var ch in item.Output)
                    {
                        WriteChomosome(ch);
                    }
                }

                WriteLine("Лучшая");
                WriteChomosome(item.BestChromosome);

                i++;
                clr[i] = richTextBox1.Text.Length;
            }
            WriteLine(string.Format("Количество вычислений целевой ф-и {0}", alg.CallCount));
            WriteLine("Лучшая хромосома");
            WriteChomosome(alg.Best);
            for (int j = 0; j < i; j++)
            {
                richTextBox1.Select(clr[j], clr[j + 1] - clr[j]);
                richTextBox1.SelectionColor = pens[j].Color;
            }
        }
Beispiel #7
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int progress = 0;

            optrez.Clear();
            finalRez.Clear();
            alg = new PGA();
            OptimizationModel optModel = new OptimizationModel(func);
            EnumConverter     InitialLoadTypeCollection = new EnumConverter(typeof(InitialLoadType));
            EnumConverter     EndConditionTypeCollecton = new EnumConverter(typeof(EndCondition));
            EnumConverter     MutationTypeCollecton     = new EnumConverter(typeof(MutationType));
            EnumConverter     SelectionTypeCollection   = new EnumConverter(typeof(SelectionType));

            foreach (InitialLoadType il in InitialLoadTypeCollection.GetStandardValues())
            {
                foreach (EndCondition ec in EndConditionTypeCollecton.GetStandardValues())
                {
                    foreach (MutationType mt in MutationTypeCollecton.GetStandardValues())
                    {
                        foreach (SelectionType st in SelectionTypeCollection.GetStandardValues())
                        {
                            double[] f1m = new double[(int)numericUpDown8.Value];
                            double[] f2m = new double[(int)numericUpDown8.Value];
                            double[] fm  = new double[(int)numericUpDown8.Value];
                            double[] x1m = new double[(int)numericUpDown8.Value];
                            double[] x2m = new double[(int)numericUpDown8.Value];
                            for (int i = 0; i < (int)numericUpDown8.Value; i++)
                            {
                                AlgorithmSettings settings = new AlgorithmSettings()
                                {
                                    InitialLoadType             = il,
                                    OptModel                    = optModel,
                                    InitialPointCount           = (int)numericUpDown1.Value,
                                    SelectionType               = st,
                                    EndCondition                = ec,
                                    MaxGenerationCount          = (int)numericUpDown2.Value,
                                    SurvivedCount               = (int)numericUpDown3.Value,
                                    MutationChance              = (double)numericUpDown4.Value,
                                    CrossingGenNumber           = (int)numericUpDown5.Value,
                                    Tolerance                   = (double)numericUpDown6.Value,
                                    MutationChanceAfterCrossing = (double)numericUpDown7.Value,
                                    MutationType                = mt
                                };
                                alg.Run(settings);
                                double x1 = alg.Best.X1;
                                double x2 = alg.Best.X2;
                                double f1 = alg.Best.F;
                                double f2 = alg.CallCount;
                                double f  = GetCriterion(f1, f2);
                                f1m[i] = f1;
                                f2m[i] = f2;
                                fm[i]  = f;
                                x1m[i] = x1;
                                x2m[i] = x2;
                                optrez.Add(new OptRezult()
                                {
                                    I  = il,
                                    E  = ec,
                                    S  = st,
                                    M  = mt,
                                    F1 = f1,
                                    F2 = f2,
                                    X1 = x1,
                                    X2 = x2,
                                    F  = f
                                });
                            }
                            progress++;
                            backgroundWorker1.ReportProgress(progress * 100 / 24);
                            finalRez.Add(new OptRezult()
                            {
                                I  = il,
                                E  = ec,
                                S  = st,
                                M  = mt,
                                X1 = x1m.Average(),
                                X2 = x2m.Average(),
                                F1 = f1m.Average(),
                                F2 = f2m.Average(),
                                F  = fm.Average()
                            });
                        }
                    }
                }
            }
        }