Esempio n. 1
0
        public double approxLearnSamples(KnowlegeBaseTSARules Solution) //attention only for multicore processor optimized
        {
            Requires(Solution != null);
            double sum = LearnSamplesSet.DataRows.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism).Select(x => Math.Pow(x.DoubleOutput - approx_base(x.InputAttributeValue, Solution), 2)).Sum();

            return((double)Math.Sqrt(sum) / (double)LearnSamplesSet.CountSamples);
        }
Esempio n. 2
0
        public double approx_base(double[] object_c, KnowlegeBaseTSARules Solution)
        {
            Requires(Solution != null);
            Requires(object_c != null);

            double sum  = 0;
            double sum2 = 0;

            foreach (var Rule in Solution.RulesDatabase)
            {
                //Calc Anticedient
                double mul = Rule.ListTermsInRule.Where(x => AcceptedFeatures[x.NumVar]).Select(y => y.LevelOfMembership(object_c)).Aggregate(1.0, (p, n) => p * n);
                sum2 += mul;

                //Calc Consiquent
                double value = Rule.IndependentConstantConsequent;
                for (int i = 0; i < Rule.RegressionConstantConsequent.Length; i++)
                {
                    if (AcceptedFeatures[i] == false)
                    {
                        continue;
                    }
                    value += Rule.RegressionConstantConsequent[i] * object_c[i];
                }
                //End

                sum += mul * value;
            }
            if (sum2 != 0)
            {
                return(sum / (double)sum2);
            }

            return(double.MaxValue);
        }
Esempio n. 3
0
        private void ChangeExplorerPositions(int i)
        {
            KnowlegeBaseTSARules temp = new KnowlegeBaseTSARules();

            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.approxLearnSamples(ExplorerParticles[i]) < result.approxLearnSamples(ParticlesBest[ExplorerParticles[i]]))
            {
                ParticlesBest.Remove(ExplorerParticles[i]);
                ParticlesBest.Add(temp, ExplorerParticles[i]);
            }
            else
            {
                KnowlegeBaseTSARules tmp = new KnowlegeBaseTSARules();
                tmp = ParticlesBest[ExplorerParticles[i]];
                ParticlesBest.Remove(ExplorerParticles[i]);
                ParticlesBest.Add(temp, tmp);
            }
            ExplorerParticles[i] = temp;
        }
Esempio n. 4
0
        public double approxTestSamples(KnowlegeBaseTSARules forTest)
        {
            Requires(forTest != null);
            double sum = LearnSamplesSet.DataRows.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism).Select(x => Math.Pow(x.DoubleOutput - approx_base(x.InputAttributeValue, forTest), 2)).Sum();

            return((double)Math.Sqrt(sum) / (double)TestSamplesSet.CountSamples);
        }
Esempio n. 5
0
        private void SetPopulation()
        {
            Population = new KnowlegeBaseTSARules[numberOfAllParts];
            KnowlegeBaseTSARules TempRule = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);

            Population[0] = TempRule;
            Universal     = TempRule;
            for (int i = 1; i < numberOfAllParts; i++)
            {
                Population[i] = new KnowlegeBaseTSARules(TempRule);
                for (int j = 0; j < Population[i].TermsSet.Count; j++)
                {
                    for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++)
                    {
                        Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]);
                    }
                }
                result.UnlaidProtectionFix(Population[i]);
            }
            Universal = new KnowlegeBaseTSARules(TempRule);
            for (int i = 0; i < Universal.TermsSet.Count; i++)
            {
                for (int j = 0; j < Universal.TermsSet[i].Parametrs.Length; j++)
                {
                    Universal.TermsSet[i].Parametrs[j] = GaussRandom.Random_gaussian(rand, Universal.TermsSet[i].Parametrs[j], 0.1 * Universal.TermsSet[i].Parametrs[j]);
                }
            }
        }
Esempio n. 6
0
        private void calculateVLL(KnowlegeBaseTSARules Explorer, KnowlegeBaseTSARules ExplorerBestPosition, int index)
        {
            KnowlegeBaseTSARules part1 = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            KnowlegeBaseTSARules part2 = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);

            for (int i = 0; i < part1.TermsSet.Count; i++)
            {
                for (int j = 0; j < part1.TermsSet[i].Parametrs.Length; j++)
                {
                    part1.TermsSet[i].Parametrs[j] = (ExplorerBestPosition.TermsSet[i].Parametrs[j] - Explorer.TermsSet[i].Parametrs[j]) * rand.NextDouble();
                }
            }

            for (int i = 0; i < part2.TermsSet.Count; i++)
            {
                for (int j = 0; j < part2.TermsSet[i].Parametrs.Length; j++)
                {
                    part2.TermsSet[i].Parametrs[j] = (LocalLeaders[index].TermsSet[i].Parametrs[j] - Explorer.TermsSet[i].Parametrs[j]) * rand.NextDouble();
                }
            }

            for (int i = 0; i < VelocityVectorLL.TermsSet.Count; i++)
            {
                for (int j = 0; j < VelocityVectorLL.TermsSet[i].Parametrs.Length; j++)
                {
                    VelocityVectorLL.TermsSet[i].Parametrs[j] = (part1.TermsSet[i].Parametrs[j] + part2.TermsSet[i].Parametrs[j]);
                }
            }
        }
Esempio n. 7
0
        public KnowlegeBaseTSARules(KnowlegeBaseTSARules source, List <bool> used_rules = null)
        {
            for (int i = 0; i < source.TermsSet.Count; i++)
            {
                Term temp_term = new Term(source.TermsSet[i]);
                TermsSet.Add(temp_term);
            }

            for (int j = 0; j < source.RulesDatabase.Count; j++)
            {
                if ((used_rules == null) || (used_rules[j]))
                {
                    int[] order = new int[source.RulesDatabase[j].ListTermsInRule.Count];
                    for (int k = 0; k < source.RulesDatabase[j].ListTermsInRule.Count; k++)
                    {
                        Term temp_term = source.RulesDatabase[j].ListTermsInRule[k];
                        order[k] = source.TermsSet.FindIndex(x => x == temp_term);
                    }
                    double   temp_DoubleOutputs = source.RulesDatabase[j].IndependentConstantConsequent;
                    double[] temp_approx_RegressionConstantConsequent = source.RulesDatabase[j].RegressionConstantConsequent.Clone() as double [];
                    TSARule  temp_rule = new TSARule(TermsSet, order, temp_DoubleOutputs, temp_approx_RegressionConstantConsequent);
                    RulesDatabase.Add(temp_rule);
                }
            }
        }
Esempio n. 8
0
        private void ChangeAimlessPosition(KnowlegeBaseTSARules Aimless)
        {
            KnowlegeBaseTSARules GlobalRand = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);

            SSVector_gen();
            for (int i = 0; i < Aimless.TermsSet.Count; i++)
            {
                for (int j = 0; j < Aimless.TermsSet[i].Parametrs.Length; j++)
                {
                    Aimless.TermsSet[i].Parametrs[j] = ((rand.NextDouble() + 0.5) * (SSVector.TermsSet[i].Parametrs[j]));
                }
            }
        }
Esempio n. 9
0
 private void SetRoles()
 {
     HeadLeader = Population[0];
     for (int i = 1; i <= numberOfLocalLeaders; i++)
     {
         LocalLeaders[i - 1] = Population[i];
     }
     for (int i = numberOfAllParts - numberOfAimlessParts; i < numberOfAllParts; i++)
     {
         AimlessParticles[i - numberOfAllParts + numberOfAimlessParts] = Population[i];
     }
     for (int i = numberOfLocalLeaders + 1; i < numberOfAllParts - numberOfAimlessParts; i++)
     {
         ExplorerParticles[i - numberOfLocalLeaders - 1] = Population[i];
     }
 }
Esempio n. 10
0
        private double Distance(KnowlegeBaseTSARules x, KnowlegeBaseTSARules y)
        {
            double sum = 0;

            for (int i = 0; i < x.TermsSet.Count; i++)
            {
                for (int j = 0; j < x.TermsSet[i].Parametrs.Length; j++)
                {
                    sum += Math.Pow(x.TermsSet[i].Parametrs[j] - y.TermsSet[i].Parametrs[j], 2);
                }
            }
            for (int i = 0; i < x.RulesDatabase.Count; i++)
            {
                sum += Math.Pow(x.RulesDatabase[i].IndependentConstantConsequent - y.RulesDatabase[i].IndependentConstantConsequent, 2);
            }
            return(sum);
        }
Esempio n. 11
0
        public override TSAFuzzySystem TuneUpFuzzySystem(TSAFuzzySystem Approx, ILearnAlgorithmConf conf)
        {
            result = Approx;
            Init(conf);
            HeadLeader       = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVector   = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVectorLL = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
            VelocityVectorHL = new KnowlegeBaseTSARules(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;
                }
            }
            SetPopulation();
            ParticlesBest = new Dictionary <KnowlegeBaseTSARules, KnowlegeBaseTSARules>();
            foreach (var Particle in Population)
            {
                ParticlesBest.Add(Particle, Universal);
            }
            LocalLeaders      = new KnowlegeBaseTSARules[numberOfLocalLeaders];
            ExplorerParticles = new KnowlegeBaseTSARules[numberOfAllParts - numberOfAimlessParts - numberOfLocalLeaders - 1];
            AimlessParticles  = new KnowlegeBaseTSARules[numberOfAimlessParts];
            iter = 0;
            while (iter < MaxIter)
            {
                Population = ListTakagiSugenoApproximateTool.SortRules(Population, result);
                SetRoles();
                ChangeExplorersPositions();
                ChangeAimlessPositions();
                DiscardRoles();
                if (iter == (MaxIter - 1) || iter == 0)
                {
                    Console.WriteLine("Iteration: " + (iter + 1).ToString());
                    Console.WriteLine(Math.Round(result.RMSEtoMSEforLearn(result.approxLearnSamples(Population[0])), 3));
                    Console.WriteLine(Math.Round(result.RMSEtoMSEforTest(result.approxTestSamples(Population[0])), 3));
                }
                iter++;
            }

            result.RulesDatabaseSet[0] = Population[0];
            return(result);
        }
Esempio n. 12
0
        private void SetPopulation()
        {
            Population = new KnowlegeBaseTSARules[Npop];
            KnowlegeBaseTSARules TempRule = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);

            Population[0] = TempRule;
            for (int i = 1; i < Npop; i++)
            {
                TempRule      = new KnowlegeBaseTSARules(result.RulesDatabaseSet[0]);
                Population[i] = TempRule;
                for (int j = 0; j < Population[i].TermsSet.Count; j++)
                {
                    for (int k = 0; k < Population[i].TermsSet[j].Parametrs.Length; k++)
                    {
                        Population[i].TermsSet[j].Parametrs[k] = GaussRandom.Random_gaussian(rand, Population[i].TermsSet[j].Parametrs[k], 0.1 * Population[i].TermsSet[j].Parametrs[k]);
                    }
                }
            }
        }
Esempio n. 13
0
 public virtual void SSVector_gen()
 {
     SSVector = new KnowlegeBaseTSARules(Population[0]);
     for (int j = 1; j < numberOfAllParts; j++)
     {
         for (int k = 0; k < Population[j].TermsSet.Count; k++)
         {
             for (int q = 0; q < Population[j].TermsSet[k].CountParams; q++)
             {
                 SSVector.TermsSet[k].Parametrs[q] += Population[j].TermsSet[k].Parametrs[q];
             }
         }
     }
     for (int k = 0; k < SSVector.TermsSet.Count; k++)
     {
         for (int q = 0; q < SSVector.TermsSet[k].CountParams; q++)
         {
             SSVector.TermsSet[k].Parametrs[q] /= numberOfAllParts;
         }
     }
 }
Esempio n. 14
0
        private int findNearestLocalLeader(KnowlegeBaseTSARules Explorer)
        {
            int    index   = 0;
            double minimum = 999999999999;

            for (int k = 0; k < numberOfLocalLeaders; k++)
            {
                double distance = 0;
                for (int i = 0; i < LocalLeaders[k].TermsSet.Count; i++)
                {
                    for (int j = 0; j < numberOfParametrs; j++)
                    {
                        distance += Math.Pow(Explorer.TermsSet[i].Parametrs[j] - LocalLeaders[k].TermsSet[i].Parametrs[j], 2);
                    }
                }
                distance = Math.Sqrt(distance);
                if (distance < minimum)
                {
                    minimum = distance;
                    index   = k;
                }
            }
            return(index);
        }
Esempio n. 15
0
        public override double ErrorLearnSamples(KnowlegeBaseRules Source)
        {
            KnowlegeBaseTSARules temp = Source as KnowlegeBaseTSARules;

            return(approxLearnSamples(temp));
        }