示例#1
0
        private void ChangeExplorerPositions(int i)
        {
            KnowlegeBasePCRules temp = new KnowlegeBasePCRules();

            temp = ExplorerParticles[i];
            for (int k = 0; k < temp.TermsSet.Count; k++)
            {
                for (int j = 0; j < temp.TermsSet[k].Parametrs.Length; j++)
                {
                    temp.TermsSet[k].Parametrs[j] += VelocityVector.TermsSet[k].Parametrs[j];
                }
            }

            if (result.ClassifyLearnSamples(ExplorerParticles[i]) < result.ClassifyLearnSamples(ParticlesBest[ExplorerParticles[i]]))
            {
                ParticlesBest.Remove(ExplorerParticles[i]);
                ParticlesBest.Add(temp, ExplorerParticles[i]);
            }
            else
            {
                KnowlegeBasePCRules tmp = new KnowlegeBasePCRules();
                tmp = ParticlesBest[ExplorerParticles[i]];
                ParticlesBest.Remove(ExplorerParticles[i]);
                ParticlesBest.Add(temp, tmp);
            }
            ExplorerParticles[i] = temp;
        }
示例#2
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf)
        {
            preIterate(Classify, conf);
            //Оптимизируем параметры
            iter = 1;
            while (iter <= MaxIter)
            {
                if (iter % changeParts == 0)
                {
                    swapParticles();
                }
                oneIterate();
            }
            //Выводим точность классификации для лучшей частицы из каждой популяции
            for (int j = 0; j < Populations.Count; j++)
            {
                Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], result);
                Console.WriteLine("Популяция №" + j + ":");
                Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2));
                Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2));
            }
            //Находим самое лучшее решение из всех популяций
            List <KnowlegeBasePCRules> BestPopulation = new List <KnowlegeBasePCRules>();

            for (int i = 0; i < numberOfPopulations; i++)
            {
                BestPopulation.Add(Populations[i][0]);
            }
            BestPopulation             = ListPittsburgClassifierTool.SortRules(BestPopulation, result);
            result.RulesDatabaseSet[0] = BestPopulation[0];
            //Возвращаем результат
            return(result);
        }
示例#3
0
        /// <summary>
        /// Last step
        /// </summary>
        protected virtual void prepareFinalFuzzySystem()
        {
            double startPrecission = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
            double afterPrecission = result.ClassifyLearnSamples(newSolution);

            if (startPrecission < afterPrecission)
            {
                result.RulesDatabaseSet[0] = newSolution;
            }
        }
示例#4
0
        private void SortPopulation()
        {
            Dictionary <int[], double> PopulationWithAccuracy = new Dictionary <int[], double>();
            double accuracy = 0;

            for (int i = 0; i < Population.Count; i++)
            {
                for (int j = 0; j < Population[i].Length; j++)
                {
                    if (Population[i][j] == 0)
                    {
                        result.AcceptedFeatures[j] = false;
                    }
                    else
                    {
                        result.AcceptedFeatures[j] = true;
                    }
                }
                accuracy = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                PopulationWithAccuracy.Add(Population[i], accuracy);
            }
            Population.Clear();
            foreach (var pair in PopulationWithAccuracy.OrderByDescending(pair => pair.Value))
            {
                Population.Add(pair.Key);
            }
            PopulationWithAccuracy.Clear();
        }
示例#5
0
 public double getGoodsImproove(PCFuzzySystem FS, double baseValue)
 {
     thePositionOfBee    = checkAndCorrect(thePositionOfBee);
     FS.AcceptedFeatures = thePositionOfBee;
     accuracy            = FS.ClassifyLearnSamples(FS.RulesDatabaseSet[0]);
     goods = accuracy - baseValue;
     return(goods);
 }
示例#6
0
        public virtual void oneIterate(PCFuzzySystem result)
        {
            for (int j = 0; j < count_particle; j++)
            {
                w = 1 / (1 + Math.Exp(-(Errors[j] - OldErrors[j]) / 0.01));
                for (int k = 0; k < X[j].TermsSet.Count; k++)
                {
                    for (int q = 0; q < X[j].TermsSet[k].CountParams; q++)
                    {
                        double bp = Pi[j].TermsSet[k].Parametrs[q];
                        V[j].TermsSet[k].Parametrs[q] = V[j].TermsSet[k].Parametrs[q] * w + c1 * rnd.NextDouble() * (bp - X[j].TermsSet[k].Parametrs[q]) +
                                                        c2 * rnd.NextDouble() * (Pg.TermsSet[k].Parametrs[q] - X[j].TermsSet[k].Parametrs[q]);
                        X[j].TermsSet[k].Parametrs[q] += V[j].TermsSet[k].Parametrs[q];
                    }
                }
                double[] bf  = new double[V[j].Weigths.Length];
                double[] bfw = new double[V[j].Weigths.Length];
                for (int k = 0; k < V[j].Weigths.Length; k++)
                {
                    bfw[k] = V[j].Weigths[k] * w + c1 * rnd.NextDouble() * (Pi[j].Weigths[k] - X[j].Weigths[k]) +
                             c2 * rnd.NextDouble() * (Pg.Weigths[k] - X[j].Weigths[k]);
                    double sw = X[j].Weigths[k] + bfw[k];
                    if (sw > 0 && sw <= 2)
                    {
                        bf[k] = sw;
                    }
                    else
                    {
                        bf[k]  = X[j].Weigths[k];
                        bfw[k] = V[j].Weigths[k];
                    }
                }
                X[j].Weigths = bf;
                V[j].Weigths = bfw;
                double newError = 0;
                bool   success  = true;
                try
                {
                    newError = result.ClassifyLearnSamples(X[j]);
                }
                catch (Exception)
                {
                    success = false;
                }
                if (success && (newError > Errors[j]))
                {
                    OldErrors[j] = Errors[j];
                    Errors[j]    = newError;

                    Pi[j] = new KnowlegeBasePCRules(X[j]);
                }
                if (minError < newError)
                {
                    minError = newError;
                    Pg       = new KnowlegeBasePCRules(X[j]);
                }
            }
        }
示例#7
0
        private void Chiu(PCFuzzySystem result)
        {
            double best_res = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
            double res      = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);

            for (int j = 0; j < result.AcceptedFeatures.Length; j++)
            {
                int n = 0;
                for (int i = 0; i < result.AcceptedFeatures.Length; i++)
                {
                    if (result.AcceptedFeatures[i] == true)
                    {
                        result.AcceptedFeatures[i] = false;
                        if (result.ClassifyLearnSamples(result.RulesDatabaseSet[0]) >= res)
                        {
                            res = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                            n   = i;
                        }
                        else
                        {
                            result.AcceptedFeatures[i] = true;
                        }
                    }
                }
                if (res >= best_res)
                {
                    best_res = res;
                }
                else
                {
                    break;
                }
            }

            //for (int i = 0; i < result.AcceptedFeatures.Length; i++)
            //{
            //    if (result.AcceptedFeatures[i] == true)
            //        Console.Write(Convert.ToString(i) + ' ');
            //}

            //Console.WriteLine();
        }
示例#8
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classif, ILearnAlgorithmConf conf)
        {
            result           = Classif;
            numberOfFeatures = result.CountFeatures;
            Init(conf);

            HeadLeader    = new int[numberOfFeatures];
            BestParticles = new List <int[]>();
            for (int i = 0; i < numberOfFeatures; i++)
            {
                BestParticles.Add(new int[2]);
                BestParticles[i][0] = 0;
                BestParticles[i][1] = 0;
            }

            SetPopulation();

            iter = 0;
            while (iter < MaxIter)
            {
                SortPopulation();

                for (int i = 0; i < Population[0].Length; i++)
                {
                    BestParticles[i][Population[0][i]] += 1;
                }

                HeadLeader = Population[0];

                ChangeParticles();

                iter++;
            }

            SortPopulation();

            for (int j = 0; j < Population[0].Length; j++)
            {
                Console.Write(Convert.ToString(Population[0][j]) + ' ');
                if (Population[0][j] == 0)
                {
                    result.AcceptedFeatures[j] = false;
                }
                else
                {
                    result.AcceptedFeatures[j] = true;
                }
            }
            Console.WriteLine();
            Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(result.RulesDatabaseSet[0]), 2));
            Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(result.RulesDatabaseSet[0]), 2));
            return(result);
        }
示例#9
0
        /// <summary>
        /// Step 1
        /// </summary>
        /// <param name="Classifier"></param>
        /// <param name="config"></param>

        protected virtual void init(PCFuzzySystem Classifier, ACOSearchConf config)
        {
            ACO_iterationCount       = config.ACOCountIteration;
            ACO_antCount             = config.ACOCountAnt;
            ACO_decisionArchiveCount = 2;
            ACO_q          = config.ACOQ;
            ACO_xi         = config.ACOXi;
            result         = Classifier;
            colonyCount    = result.CountFeatures;
            colonyList     = new List <Colony>();
            basePrecission = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
        }
示例#10
0
        public void rouletteSelection()
        {
            double[] currentError = new double[childrenMassive.Count()];
            double   efficient    = 0;

            for (int i = 0; i < childrenMassive.Count(); i++)
            //Parallel.For(0, childrenMassive.Count(), i =>
            {
                fullFuzzySystem.RulesDatabaseSet.Add(childrenMassive[i]);
                fullFuzzySystem.UnlaidProtectionFix(childrenMassive[i]);
                currentError[i] = fullFuzzySystem.ClassifyLearnSamples(fullFuzzySystem.RulesDatabaseSet[i + 1]);

                efficient += currentError[i];
                //});
            }
            for (int i = 0; i < currentError.Count(); i++)
            //Parallel.For(0, currentError.Count(), i =>
            {
                currentError[i] = ((currentError[i] / efficient));
                //});
            }
            Array.Sort(currentError, childrenMassive);
            for (int i = 0; i < populationMassive.Count(); i++)
            //Parallel.For(0, current.Count(), i =>
            {
                double a    = allRandom.NextDouble();
                double summ = 0;
                for (int j = 0; j < currentError.Count(); j++)
                {
                    if ((a >= summ) && (a <= (summ + currentError[j])))
                    {
                        populationMassive[i] = childrenMassive[j];
                        break;
                    }
                    summ += currentError[j];
                }
            }
            //});
            fullFuzzySystem.RulesDatabaseSet.RemoveRange(1, childrenMassive.Count());
        }
示例#11
0
        public override string ToString(bool with_param = false)
        {
            if (with_param)
            {
                string result = "Алгоритм пчелинной колонии дискретный {";
                result += "Количество разведчиков= " + countScouts.ToString() + " ;" + Environment.NewLine;
                result += "Количество рабочих пчел= " + countWorkers.ToString() + " ;" + Environment.NewLine;
                for (int z = solutionInfo.Count - countBestBase; z < solutionInfo.Count; z++)
                {
                    int saved = solutionInfo[z].PositionOfBee.Where(x => x == true).Count();
                    int lost  = solutionInfo[z].PositionOfBee.Length - saved;
                    theFuzzySystem.AcceptedFeatures = solutionInfo[z].PositionOfBee;
                    double accuracyLearn = theFuzzySystem.ClassifyLearnSamples(theFuzzySystem.RulesDatabaseSet[0]);
                    double accuracyTest  = theFuzzySystem.ClassifyTestSamples(theFuzzySystem.RulesDatabaseSet[0]);
                    result += $"Оставшиеся признаки {saved}:{accuracyLearn}||{accuracyTest} [ ";
                    for (int i = 0; i < solutionInfo[z].PositionOfBee.Length; i++)
                    {
                        if (solutionInfo[z].PositionOfBee[i] == true)
                        {
                            result += (i + 1).ToString() + ", ";
                        }
                    }
                    result += "]" + Environment.NewLine;
                    result += $"Усеченные признаки {saved}:{accuracyLearn}||{accuracyTest} [ ";
                    for (int i = 0; i < solutionInfo[z].PositionOfBee.Length; i++)
                    {
                        if (solutionInfo[z].PositionOfBee[i] == false)
                        {
                            result += (i + 1).ToString() + ", ";
                        }
                    }

                    result += "]" + Environment.NewLine;
                }

                result += "}";
                return(result);
            }
            return("Алгоритм пчелинной колонии дискретный");
        }
示例#12
0
 public void updateFS(PCFuzzySystem FS)
 {
     backup = FS.AcceptedFeatures[Position];
     FS.AcceptedFeatures[Position] = Value;
     try
     {
         Precision = FS.ClassifyLearnSamples(FS.RulesDatabaseSet[0]);
     }
     catch (Exception)
     {
         FS.AcceptedFeatures[Position] = backup;
     }
 }
示例#13
0
        /// <summary>
        /// Step 1
        /// </summary>
        /// <param name="Classifier"></param>
        /// <param name="config"></param>

        protected virtual void init(PCFuzzySystem Classifier, ACOSearchConf config)
        {
            ACO_iterationCount       = config.ACOCountIteration;
            ACO_antCount             = config.ACOCountAnt;
            ACO_decisionArchiveCount = config.ACODescisionArchiveSize;
            ACO_q       = config.ACOQ;
            ACO_xi      = config.ACOXi;
            result      = Classifier;
            colonyCount = result.RulesDatabaseSet[0].TermsSet.Count;
            colonyList  = new List <Colony>();
            newSolution = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);

            basePrecission = result.ClassifyLearnSamples(newSolution);
        }
示例#14
0
 public virtual void CalcPecission(PCFuzzySystem FS)
 {
     backup = FS.AcceptedFeatures[Position];
     FS.AcceptedFeatures[Position] = Value;
     try
     {
         Precision = FS.ClassifyLearnSamples(FS.RulesDatabaseSet[0]);
     }
     catch (Exception)
     {
         Precision = double.PositiveInfinity;
     }
     FS.AcceptedFeatures[Position] = backup;
 }
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf)
        {
            count_iteration = ((WeigthsRandomSearchConfig)conf).WRSCCountIteration;
            count_generation_by_iteration =
                ((WeigthsRandomSearchConfig)conf).WRSCCountRules;

            PCFuzzySystem result = Classifier;


            for (int i = 0; i < count_iteration; i++)
            {
                double [][] Weigths = new double[count_generation_by_iteration + 1][];
                Weigths[0] = Classifier.RulesDatabaseSet[0].Weigths;
                double best_result = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);

                int best_index = 0;
                for (int j = 1; j < count_generation_by_iteration + 1; j++)
                {
                    Weigths[j] = new double[Weigths[0].Count()];
                    for (int k = 0; k < Weigths[0].Count(); k++)
                    {
                        Weigths[j][k] = rand.NextDouble();
                    }

                    result.RulesDatabaseSet[0].Weigths = Weigths[j];
                    double current_result = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                    if (current_result > best_result)
                    {
                        best_result = current_result;
                        best_index  = j;
                    }
                    result.RulesDatabaseSet[0].Weigths = Weigths[best_index];
                }
            }
            result.RulesDatabaseSet[0].TermsSet.Trim();
            return(result);
        }
示例#16
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf)
        {
            result           = Classifier;
            numberOfFeatures = result.CountFeatures;
            groups           = new List <int[][]>();
            Init(conf);
            SetPopulation();
            NS = new int[m];
            for (int i = 0; i < m; i++)
            {
                NS[i] = (N - 1) / m;
            }
            cur_iter = 0;
            double featurecount = 0;

            while (cur_iter < iter)
            {
                //Console.WriteLine("\nИТЕРАЦИЯ " + cur_iter);

                SortPopulation();

                groups = GroupStream();
                if (p_one > rand.NextDouble())
                {
                    ChooseOneCluster();
                }
                else
                {
                    ChooseTwoClusters();
                }

                SortPopulation();
                cur_iter++;
            }
            for (int j = 0; j < Population[0].Length; j++)
            {
                if (Population[0][j] == true)
                {
                    featurecount += 1;
                }
            }
            result.AcceptedFeatures = Population[0];
            Console.WriteLine();
            Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(result.RulesDatabaseSet[0]), 2));
            Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(result.RulesDatabaseSet[0]), 2));
            Console.WriteLine("Признаки:" + featurecount);
            featurecount = 0;
            return(result);
        }
        public double DB(double[] shrapnel, int count_terms, int type, PCFuzzySystem Classifier)
        {
            int k = 1;

            for (int l = 0; l < count_terms; l++)
            {
                for (int p = 0; p < type; p++)
                {
                    Classifier.RulesDatabaseSet[0].TermsSet[l].Parametrs[p] = shrapnel[k];
                    k++;
                }
            }
            double MSE = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet[0]);

            return(MSE);
        }
        private string ErrorInfoPC(IFuzzySystem FS)
        {
            PCFuzzySystem IFS = FS as PCFuzzySystem;

            if (IFS.RulesDatabaseSet.Count < 1)
            {
                return("Точность нечеткой системы недоступна");
            }
            classLearnResult.Add(IFS.ClassifyLearnSamples(IFS.RulesDatabaseSet[0]));
            classTestResult.Add(IFS.ClassifyTestSamples(IFS.RulesDatabaseSet[0]));
            classErLearn.Add(IFS.ErrorLearnSamples(IFS.RulesDatabaseSet[0]));
            classErTest.Add(IFS.ErrorTestSamples(IFS.RulesDatabaseSet[0]));

            return("Точностью на обучающей выборке  " + classLearnResult[classLearnResult.Count - 1].ToString() + " , Точность на тестовой выборке  " + classTestResult[classTestResult.Count - 1].ToString() + " " + Environment.NewLine +
                   "Ошибкой на обучающей выборке  " + classErLearn[classErLearn.Count - 1].ToString() + " , Ошибкой на тестовой выборке  " + classErTest[classErTest.Count - 1].ToString() + " " + Environment.NewLine);
        }
示例#19
0
        private void SortPopulation()
        {
            Dictionary <bool[], double> PopulationWithAccuracy = new Dictionary <bool[], double>();
            double accuracy = 0;

            for (int i = 0; i < Population.Count; i++)
            {
                result.AcceptedFeatures = Population[i];
                accuracy = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                PopulationWithAccuracy.Add(Population[i], accuracy);
            }
            Population.Clear();
            foreach (var pair in PopulationWithAccuracy.OrderByDescending(pair => pair.Value))
            {
                Population.Add(pair.Key);
            }
            PopulationWithAccuracy.Clear();
        }
示例#20
0
        protected void preIterate(PCFuzzySystem Classifier)
        {
            for (int i = 0; i < count_particle; i++)
            {
                KnowlegeBasePCRules temp_c_Rule = new KnowlegeBasePCRules(Classifier.RulesDatabaseSet[0]);
                X[i]         = temp_c_Rule;
                Errors[i]    = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet[0]);
                OldErrors[i] = Errors[i];
                Pi[i]        = new KnowlegeBasePCRules(X[i]);
                V[i]         = new KnowlegeBasePCRules(X[i]);
                //
                for (int j = 0; j < V[i].TermsSet.Count; j++)
                {
                    for (int k = 0; k < Term.CountParamsinSelectedTermType(V[i].TermsSet[j].TermFuncType); k++)
                    {
                        if (i == 0)
                        {
                            V[i].TermsSet[j].Parametrs[k] = 0;
                        }
                        else
                        {
                            V[i].TermsSet[j].Parametrs[k] = rnd.NextDouble() - 0.5;
                        }
                    }
                }
                double[] bf = new double[V[i].Weigths.Length];
                for (int k = 0; k < V[i].Weigths.Length; k++)
                {
                    if (i == 0)
                    {
                        bf[k] = 1;
                    }
                    else
                    {
                        //System.Windows.Forms.MessageBox.Show(rnd.NextDouble().ToString());
                        bf[k] = rnd.NextDouble() / 200;
                    }
                }
                V[i].Weigths = bf;
            }

            Pg       = new KnowlegeBasePCRules(Classifier.RulesDatabaseSet[0]);
            minError = Errors[0];
        }
示例#21
0
        private void addClassifierValue(PCFuzzySystem Classifier)
        {
            double Value = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet[0]);

            ValueLGoodsPercent.Add(Value);
            ValueLGoodsError.Add(100 - Value);

            Value = Classifier.ClassifyTestSamples(Classifier.RulesDatabaseSet[0]);
            ValueTGoodsPercent.Add(Value);
            ValueTGoodsError.Add(100 - Value);


            Value = Classifier.getComplexit();
            ValueComplexityFull.Add(Value);
            Value = Classifier.getRulesCount();
            ValueComplexityRules.Add(Value);

            Value = Classifier.getNormalIndex();
            ValueInterpretyNominal.Add(Value);
            Value = Classifier.getIndexReal();
            ValueInterpretyReal.Add(Value);
        }
      public override void oneIterate(PCFuzzySystem result)
      {
          base.oneIterate(result);
          counterIter++;
          if (counterIter == interPSOtoSend)
          {
              Pi = sortSolution(Pi);
              savetoUFS(Pi.ToList(), 0, sendPSO, trySend);
              BacteryRunner();
              trySend++;

              List <KnowlegeBasePCRules> tempRes = loadDatabase().ToList();

              int size = tempRes.Count;
              for (int p = tempRes.Count - 1; p >= 0; p--)
              {
                  X[p] = tempRes[0];

                  double newError = result.ClassifyLearnSamples(X[p]);

                  if (newError > Errors[p])
                  {
                      Pi[p]        = new KnowlegeBasePCRules(X[p]);
                      OldErrors[p] = Errors[p];
                      Errors[p]    = newError;

                      if (minError < newError)
                      {
                          minError = newError;
                          Pg       = new KnowlegeBasePCRules(X[p]);
                      }
                  }
                  tempRes.RemoveAt(0);
              }

              counterIter = 0;
          }
      }
示例#23
0
        private void SortPopulation()

        {
            Dictionary <int, Tuple <bool[], double> > PopulationWithAccuracy = new Dictionary <int, Tuple <bool[], double> >();

            double accuracy = 0;


            for (int i = 0; i < Population.Count; i++)

            {
                result.AcceptedFeatures = Population[i];

                accuracy = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                Tuple <bool[], double> tuple = new Tuple <bool[], double>(Population[i], accuracy);
                try
                {
                    PopulationWithAccuracy.Add(i, tuple);
                }
                catch (ArgumentException)
                {
                    //Console.WriteLine(i);
                }
            }
            double[] accuracylist = new double[Population.Count];

            Population.Clear();

            foreach (var pair in PopulationWithAccuracy.OrderByDescending(pair => pair.Value.Item2))

            {
                accuracylist[pair.Key] = pair.Value.Item2;
                Population.Add(pair.Value.Item1);
            }

            PopulationWithAccuracy.Clear();
        }
示例#24
0
        public override FuzzySystem.PittsburghClassifier.PCFuzzySystem TuneUpFuzzySystem(FuzzySystem.PittsburghClassifier.PCFuzzySystem Classifier, ILearnAlgorithmConf conf)
        {
            PCFuzzySystem result = Classifier;

            count_iteration = ((CuckooConf)conf).CuckooCountIterate;
            count_particle  = ((CuckooConf)conf).CuckooPopulationSize;
            m    = ((CuckooConf)conf).CuckooWorse;
            p    = ((CuckooConf)conf).CuckooLifeChance;
            beta = ((CuckooConf)conf).CuckooBeta;


            KnowlegeBasePCRules[] X = new KnowlegeBasePCRules[count_particle + 1];
            double[] Errors         = new double[count_particle + 1];
            double[] Er             = new double[count_particle + 1];

            Random rnd  = new Random();
            int    best = 0;

            for (int i = 0; i < count_particle + 1; i++)
            {
                KnowlegeBasePCRules temp_c_Rule = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);
                X[i]      = temp_c_Rule;
                Errors[i] = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
            }
            ///////////
            for (int i = 0; i < count_iteration; i++)
            {
                X[0] = new  KnowlegeBasePCRules(X[0]);
                for (int k = 0; k < X[0].TermsSet.Count; k++)
                {
                    for (int q = 0; q < X[0].TermsSet[k].CountParams; q++)
                    {
                        double b = (rnd.Next(1000, 2000) / Convert.ToDouble(1000));
                        X[0].TermsSet[k].Parametrs[q] = X[0].TermsSet[k].Parametrs[q] + Levi(BM(sigu(beta)), BM(1.0), beta);
                    }
                }

                for (int k = 0; k < X[0].Weigths.Length; k++)
                {
                    X[0].Weigths[k] = rnd.NextDouble() / 200;
                }

                result.RulesDatabaseSet.Add(X[0]);
                int temp_index = result.RulesDatabaseSet.Count - 1;
                Errors[0] = result.ClassifyLearnSamples(result.RulesDatabaseSet[temp_index]);
                result.RulesDatabaseSet.RemoveAt(temp_index);

                int s = rnd.Next(1, count_particle + 1);

                if (Errors[0] > Errors[s])
                {
                    X[s]      = X[0];
                    Errors[s] = Errors[0];
                }
                else
                {
                    X[0]      = X[s];
                    Errors[0] = Errors[s];
                }

                for (int v = 0; v < m; v++)
                {
                    double max = Errors[1];
                    int    ind = 1;
                    for (int r = 2; r < count_particle + 1; r++)
                    {
                        if (Errors[r] < max)
                        {
                            max = Errors[r];
                            ind = r;
                        }
                        else
                        {
                        };
                    }
                    double h = (rnd.Next(1, 1000) / Convert.ToDouble(1000));
                    if (h > p)
                    {
                        X[ind] = new KnowlegeBasePCRules(X[ind]);
                        for (int j = 0; j < X[ind].TermsSet.Count; j++)
                        {
                            for (int k = 0; k < X[ind].TermsSet[j].CountParams; k++)
                            {
                                X[ind].TermsSet[j].Parametrs[k] = X[0].TermsSet[j].Parametrs[k] + (rnd.Next(-1000, 1000) / Convert.ToDouble(1000));
                            }
                            for (int k = 0; k < X[ind].Weigths.Length; k++)
                            {
                                X[ind].Weigths[k] = X[0].Weigths[k] + (rnd.Next(1, 1000) / Convert.ToDouble(10000));
                            }
                        }
                        result.RulesDatabaseSet.Add(X[ind]);
                        temp_index  = result.RulesDatabaseSet.Count - 1;
                        Errors[ind] = result.ClassifyLearnSamples(result.RulesDatabaseSet[temp_index]);
                        result.RulesDatabaseSet.RemoveAt(temp_index);
                    }
                }
            }

            double min = Errors[0];

            best = 0;
            for (int g = 1; g < count_particle + 1; g++)
            {
                if (Errors[g] > min)
                {
                    min  = Errors[g];
                    best = g;
                }
            }

            X[0] = X[best];

            result.RulesDatabaseSet.Add(X[0]);
            int t_index = result.RulesDatabaseSet.Count - 1;

            Errors[0] = result.ClassifyLearnSamples(result.RulesDatabaseSet[t_index]);
            result.RulesDatabaseSet.RemoveAt(t_index);


            result.RulesDatabaseSet[0] = X[0];
            result.RulesDatabaseSet[0].TermsSet.Trim();
            return(result);
        }
示例#25
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf)
        {
            result = Classify;
            //Узнаем название папки с данными
            string path_name   = "../../OLD/Data/Keel/Classifier/KEEL-10/";
            string folder_name = "";

            foreach (var letter in result.LearnSamplesSet.FileName)
            {
                if (letter != '-')
                {
                    folder_name += letter;
                }
                else
                {
                    break;
                }
            }
            //Инициализируем параметры
            Init(conf);
            //Создаем новые обучающую и тестовую выбоки и удаляем из них некоторое количество случайных элементов
            List <PCFuzzySystem> results = new List <PCFuzzySystem>();

            for (int i = 0; i < numberOfPopulations; i++)
            {
                SampleSet new_learn = new SampleSet(path_name + folder_name + "/" + result.LearnSamplesSet.FileName);
                SampleSet new_test  = new SampleSet(path_name + folder_name + "/" + result.TestSamplesSet.FileName);
                results.Add(new PCFuzzySystem(new_learn, new_test));
                int ground = (int)Math.Round(results[i].LearnSamplesSet.DataRows.Count * 0.25);
                for (int j = 0; j < ground; j++)
                {
                    results[i].LearnSamplesSet.DataRows.RemoveAt(rand.Next(0, results[i].LearnSamplesSet.DataRows.Count));
                }
            }
            //Инициализируем и зануляем вектора алгоритма
            HeadLeader       = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);
            VelocityVector   = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);
            VelocityVectorLL = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);
            VelocityVectorHL = new KnowlegeBasePCRules(result.RulesDatabaseSet[0]);
            for (int i = 0; i < VelocityVector.TermsSet.Count; i++)
            {
                for (int j = 0; j < VelocityVector.TermsSet[i].Parametrs.Length; j++)
                {
                    VelocityVector.TermsSet[i].Parametrs[j]   = 0;
                    VelocityVectorLL.TermsSet[i].Parametrs[j] = 0;
                    VelocityVectorHL.TermsSet[i].Parametrs[j] = 0;
                }
            }
            //Создаем популяции и архив лучших положений каждой частицы
            Populations = new List <List <KnowlegeBasePCRules> >();
            for (int i = 0; i < numberOfPopulations; i++)
            {
                Populations.Add(SetPopulation(new List <KnowlegeBasePCRules>()));
            }
            ParticlesBest = new Dictionary <KnowlegeBasePCRules, KnowlegeBasePCRules>();
            foreach (var Population in Populations)
            {
                foreach (var Particle in Population)
                {
                    ParticlesBest.Add(Particle, Universal);
                }
            }
            //Инициализируем роли частиц
            LocalLeaders      = new KnowlegeBasePCRules[numberOfLocalLeaders];
            ExplorerParticles = new KnowlegeBasePCRules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1];
            AimlessParticles  = new KnowlegeBasePCRules[numberOfAimlessParts];
            //Оптимизируем параметры
            iter = 1;
            while (iter < MaxIter)
            {
                for (int p_i = 0; p_i < Populations.Count; p_i++)
                {
                    Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], results[p_i]);
                    SetRoles(Populations[p_i]);
                    ChangeExplorersPositions();
                    ChangeAimlessPositions();
                    Populations[p_i] = DiscardRoles(Populations[p_i]);
                }
                iter++;
            }
            //Выводим точность классификации для лучшей частицы из каждой популяции
            for (int j = 0; j < Populations.Count; j++)
            {
                Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], results[j]);
                Console.WriteLine("Популяция №" + j + ":");
                Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2));
                Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2));
            }
            //Допобавляем в базу правил лучшие решения
            if (result.RulesDatabaseSet.Count == 1)
            {
                result.RulesDatabaseSet.Clear();
            }
            for (int i = 0; i < Populations.Count; i++)
            {
                result.RulesDatabaseSet.Add(Populations[i][0]);
            }
            //Возвращаем результат
            return(result);
        }
示例#26
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf)
        {
            result = Classify;
            string folder_name = "";

            foreach (var letter in result.LearnSamplesSet.FileName)
            {
                if (letter != '-')
                {
                    folder_name += letter;
                }
                else
                {
                    break;
                }
            }
            numberOfFeatures = result.CountFeatures;
            Init(conf);
            HeadLeader       = new bool[numberOfFeatures];
            VelocityVector   = new bool[numberOfFeatures];
            VelocityVectorLL = new bool[numberOfFeatures];
            VelocityVectorHL = new bool[numberOfFeatures];

            SetPopulation();

            LocalLeaders      = new List <bool[]>();
            ExplorerParticles = new List <bool[]>();
            AimlessParticles  = new List <bool[]>();

            iter = 0;
            while (iter < MaxIter)
            {
                SortPopulation();

                SetRoles();

                ChangeExplorersPositions();
                ChangeAimlessPositions();

                DiscardRoles();

                iter++;
            }

            SortPopulation();

            result.AcceptedFeatures = Population[0];
            for (int j = 0; j < Population[0].Length; j++)
            {
                if (Population[0][j])
                {
                    Console.Write("1 ");
                }
                else
                {
                    Console.Write("0 ");
                }
            }
            Console.WriteLine();
            Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(result.RulesDatabaseSet[0]), 2));
            Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(result.RulesDatabaseSet[0]), 2));
            return(result);
        }
示例#27
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf)
        {
            iskl_prizn      = "";
            count_iteration = ((Param)conf).Количество_итераций;
            count_populate  = ((Param)conf).Число_осколков;
            exploration     = ((Param)conf).Фактор_исследования;
            reduce_koef     = ((Param)conf).Уменьшающий_коэффициент;
            priznaki_usech  = ((Param)conf).Усечённые_признаки;
            iter_descrete   = ((Param)conf).Итерации_дискр_алг;

            int           iter = 0, iter2, i, j, count_terms, count_iter = 0;
            int           count_cons, count_best2 = 0, best_pred = 0;
            double        RMSE_best, cosFi, RMSE_best2;
            int           Nd, variables, k = 1, best2 = 0;
            PCFuzzySystem result = Classifier;
            int           type   = Classifier.RulesDatabaseSet[0].TermsSet[0].CountParams;

            Nd = Classifier.RulesDatabaseSet[0].TermsSet.Count * type;
            double[] X_best = new double[Nd + 1];
            double[,] X_pred    = new double[2, Nd + 1];
            double[,] direction = new double[count_populate, Nd + 1];
            double[,] d         = new double[count_populate, Nd + 1];
            double[,] explosion = new double[count_populate, Nd + 1];
            double[,] shrapnel  = new double[count_populate, Nd + 1];
            cosFi      = Math.Cos(2 * Math.PI / count_populate);
            RMSE_best  = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet[0]);
            RMSE_best2 = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet[0]);
            count_cons = Classifier.RulesDatabaseSet[0].Weigths.Count();
            double[] RMSE      = new double[count_populate];
            double[] RMSE_all  = new double[iter];
            double[] RMSE_tst  = new double[count_populate];
            double[] RMSE2     = new double[count_populate];
            double[] RMSE_pred = new double[2];
            double[] cons_best = new double[count_cons];
            variables   = Classifier.LearnSamplesSet.CountVars;
            count_terms = Classifier.RulesDatabaseSet[0].TermsSet.Count;
            int[] terms = new int[variables];

            double[] X_best2 = new double[variables];
            double[,] d3      = new double[count_populate, variables];
            double[,] priznak = new double[count_populate, variables];
            for (i = 0; i < variables; i++)
            {
                priznak[0, i] = 1;
                X_best2[i]    = 1;
            }
            KnowlegeBasePCRules[] X = new KnowlegeBasePCRules[count_populate];
            for (int s = 0; s < count_populate - 1; s++)
            {
                X[s] = new KnowlegeBasePCRules(Classifier.RulesDatabaseSet[0]);
                Classifier.RulesDatabaseSet.Add(X[s]);
            }

            for (iter2 = 0; iter2 < iter_descrete; iter2++)
            {
                best2 = 0;
                //if (count_best2 < 10)
                //{
                if (iter == 0)
                {
                    for (k = 0; k < variables; k++)
                    {
                        d3[0, k] = RandomNext(Classifier.LearnSamplesSet.InputAttributes[k].Min, Classifier.LearnSamplesSet.InputAttributes[k].Max);
                    }
                }
                for (i = 0; i < variables; i++)
                {
                    for (j = 1; j < count_populate; j++)
                    {
generate:
                        d3[j, i]      = d3[j - 1, i] * randn();
                        priznak[j, i] = d3[j, i] * cosFi;

                        if ((priznak[j, i] < Classifier.LearnSamplesSet.InputAttributes[i].Min) || (priznak[j, i] > Classifier.LearnSamplesSet.InputAttributes[i].Max))
                        {
                            goto generate;
                        }
                        Random random = new Random();
                        if (random.NextDouble() < descret(priznak[j, i]))
                        {
                            priznak[j, i] = 1;
                        }
                        else
                        {
                            priznak[j, i] = 0;
                        }
                    }
                }


                for (j = 1; j < count_populate; j++)
                {
                    for (int h = 0; h < variables; h++)
                    {
                        if (priznak[j, h] == 1)
                        {
                            Classifier.AcceptedFeatures[h] = true;
                        }
                        else
                        {
                            Classifier.AcceptedFeatures[h] = false;
                        }
                    }
                    RMSE2[j] = Classifier.ClassifyLearnSamples(Classifier.RulesDatabaseSet [0]);
                    if (RMSE2[j] > RMSE_best2)
                    {
                        RMSE_best2 = RMSE2[j];
                        best2      = j;
                    }
                    for (int h = 0; h < variables; h++)
                    {
                        X_best2[h] = priznak[best2, h];
                    }
                }
                if (best_pred == best2)
                {
                    count_best2++;
                }
                else
                {
                    count_best2 = 0;
                }
                for (k = 0; k < variables; k++)
                {
                    priznak[0, k] = priznak[best2, k];
                }
                count_iter++;
                //}
            }

            for (k = 0; k < variables; k++)
            {
                if (priznak[best2, k] == 1)
                {
                    Classifier.AcceptedFeatures[k] = true;
                }
                else
                {
                    Classifier.AcceptedFeatures[k] = false;
                    iskl_prizn += (k + 1).ToString() + " ";
                }
            }

            return(result);
        }
示例#28
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classifier, ILearnAlgorithmConf conf)
        {
            result = Classifier;
            //Узнаем название папки с данными
            string path_name   = "../../OLD/Data/Keel/Classifier/KEEL-10/";
            string folder_name = "";

            foreach (var letter in result.LearnSamplesSet.FileName)
            {
                if (letter != '-')
                {
                    folder_name += letter;
                }
                else
                {
                    break;
                }
            }
            groups = new List <int[]>();
            Init(conf);
            //Создаем новые обучающую и тестовую выбоки и удаляем из них некоторое количество случайных элементов
            List <PCFuzzySystem> results = new List <PCFuzzySystem>();

            for (int i = 0; i < numberOfPopulations; i++)
            {
                SampleSet new_learn = new SampleSet(path_name + folder_name + "/" + result.LearnSamplesSet.FileName);
                SampleSet new_test  = new SampleSet(path_name + folder_name + "/" + result.TestSamplesSet.FileName);
                results.Add(new PCFuzzySystem(new_learn, new_test));
                int ground = (int)Math.Round(results[i].LearnSamplesSet.DataRows.Count * 0.25);
                for (int j = 0; j < ground; j++)
                {
                    results[i].LearnSamplesSet.DataRows.RemoveAt(rand.Next(0, results[i].LearnSamplesSet.DataRows.Count));
                }
            }
            Populations = new List <List <KnowlegeBasePCRules> >();
            for (int i = 0; i < numberOfPopulations; i++)
            {
                Populations.Add(SetPopulation(new List <KnowlegeBasePCRules>()));
                Populations[i] = ListPittsburgClassifierTool.SortRules(Populations[i], result);
            }
            NS = new int[m];
            for (int i = 0; i < m; i++)
            {
                NS[i] = (N - 1) / m;
            }
            cur_iter = 0;
            while (cur_iter < iter)
            {
                for (int p_i = 0; p_i < Populations.Count; p_i++)
                {
                    groups = GroupStream(Populations[p_i]);
                    if (p_one > rand.NextDouble())
                    {
                        ChooseOneCluster(Populations[p_i]);
                    }
                    else
                    {
                        ChooseTwoClusters(Populations[p_i]);
                    }
                    Populations[p_i] = ListPittsburgClassifierTool.SortRules(Populations[p_i], results[p_i]);
                    //Console.WriteLine(cur_iter + " - Итерация");
                    //Console.WriteLine("Обуч. выборка = " + result.ErrorLearnSamples(Populations[p_i][0]));
                    //Console.WriteLine("Тест. выборка = " + result.ErrorTestSamples(Populations[p_i][0]));
                }
                cur_iter++;
            }
            //Выводим точность классификации для лучшей частицы из каждой популяции
            for (int j = 0; j < Populations.Count; j++)
            {
                Populations[j] = ListPittsburgClassifierTool.SortRules(Populations[j], results[j]);
                Console.WriteLine("Популяция №" + j + ":");
                Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(Populations[j][0]), 2));
                Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(Populations[j][0]), 2));
            }
            //Допобавляем в базу правил лучшие решения
            if (result.RulesDatabaseSet.Count == 1)
            {
                result.RulesDatabaseSet.Clear();
            }
            for (int i = 0; i < Populations.Count; i++)
            {
                result.RulesDatabaseSet.Add(Populations[i][0]);
            }
            //Возвращаем результат
            return(result);
        }
示例#29
0
        public override PCFuzzySystem TuneUpFuzzySystem(PCFuzzySystem Classify, ILearnAlgorithmConf conf)
        {
            result = Classify;
            string folder_name = "";

            foreach (var letter in result.LearnSamplesSet.FileName)
            {
                if (letter != '-')
                {
                    folder_name += letter;
                }
                else
                {
                    break;
                }
            }
            numberOfFeatures = result.CountFeatures;
            Init(conf);
            rand       = new Random();
            HeadLeader = new bool[numberOfFeatures];
            SetPopulation();
            Population[0].CopyTo(HeadLeader, 0);
            result.AcceptedFeatures = HeadLeader;
            double HLAcc = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);

            iter = 0;
            while (iter < MaxIter)
            {
                ChangePositions();
                SortPopulation();
                result.AcceptedFeatures = Population[0];
                if (result.ClassifyLearnSamples(result.RulesDatabaseSet[0]) > HLAcc)
                {
                    HLAcc = result.ClassifyLearnSamples(result.RulesDatabaseSet[0]);
                    Population[0].CopyTo(HeadLeader, 0);
                }
                iter++;
            }
            int count_ones = 0;

            result.AcceptedFeatures = HeadLeader;
            for (int j = 0; j < HeadLeader.Length; j++)
            {
                if (HeadLeader[j])
                {
                    Console.Write("1 ");
                    count_ones++;
                }
                else
                {
                    Console.Write("0 ");
                }
            }
            Console.WriteLine();
            Console.WriteLine("Обуч: " + Math.Round(result.ClassifyLearnSamples(result.RulesDatabaseSet[0]), 2));
            Console.WriteLine("Тест: " + Math.Round(result.ClassifyTestSamples(result.RulesDatabaseSet[0]), 2));
            File.AppendAllText("E:/TUSUR/GPO/Эксперименты/Behavior/RS" + folder_name + ".txt", "Признаки: " + count_ones + Environment.NewLine);
            File.AppendAllText("E:/TUSUR/GPO/Эксперименты/Behavior/RS" + folder_name + ".txt", "Тест: " + Math.Round(result.ClassifyTestSamples(result.RulesDatabaseSet[0]), 2) + Environment.NewLine);
            File.AppendAllText("E:/TUSUR/GPO/Эксперименты/Behavior/RS" + folder_name + ".txt", "Время: " + Environment.NewLine);
            return(result);
        }
示例#30
0
        private void make_Log(Log_line EventCall, PCFuzzySystem FS = null, string name_Alg = "", DateTime TimerValue = new DateTime(), TimeSpan TimerSpan = new TimeSpan())
        {
            switch (EventCall)
            {
            case Log_line.Start:
            {
                LOG += "(" + TimerValue.ToString() + ")" + " Начало построения системы" + Environment.NewLine;
                break;
            }

            case Log_line.StartGenerate:
            {
                LOG += "(" + TimerValue.ToString() + ")" + " Начата генерация системы" + Environment.NewLine;

                break;
            }

            case Log_line.StartOptimaze:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Начата оптимизация системы" + Environment.NewLine;
                break;
            }


            case Log_line.PreGenerate_log:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Генерация алгоритмом " + name_Alg.ToString() + Environment.NewLine;
                break;
            }

            case Log_line.PostGenerate_log:
            {
                double LearnResult = FS.ClassifyLearnSamples();
                double TestResult  = FS.ClassifyTestSamples();


                LOG += "(" + DateTime.Now.ToString() + ")" + " Сгенерирована система сложностью " + FS.ValueComplexity().ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке " + LearnResult.ToString() + ", Точность на тестовой выборке " + TestResult.ToString() + Environment.NewLine;
                LOG += "Использован " + name_Alg.ToString() + Environment.NewLine;
                break;
            }

            case Log_line.PreOptimaze_log:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Оптимизация алгоритмом " + name_Alg.ToString() + Environment.NewLine;

                break;
            }

            case Log_line.PostOptimaze_log:
            {
                double LearnResult = FS.ClassifyLearnSamples();
                double TestResult  = FS.ClassifyTestSamples();


                LOG += "(" + DateTime.Now.ToString() + ")" + " оптимизированная система сложностью " + FS.ValueComplexity().ToString() + Environment.NewLine +
                       "Точностью на обучающей выборке " + LearnResult.ToString() + ", Точность на тестовой выборке " + TestResult.ToString() + Environment.NewLine;
                LOG += "Использован " + name_Alg.ToString() + Environment.NewLine;

                break;
            }


            case Log_line.EndCircle:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Время построения системы" + TimerSpan.TotalSeconds.ToString() + Environment.NewLine; break;
            }

            case Log_line.End:
            {
                LOG += "(" + DateTime.Now.ToString() + ")" + " Время построения всех систем" + TimerSpan.TotalSeconds.ToString() + Environment.NewLine; break;
            }

            default: { LOG += "Не верный вызов" + Environment.NewLine; break; }
            }
        }