示例#1
0
        static public double[][] SubstractiveClustering(double[][] x, double arad, double brad)
        {
            double[] P = new double[x.Length];
            arad = (arad / 2.0) * (arad / 2.0);
            brad = (brad / 2.0) * (brad / 2.0);
            int progress = 0;

            Parallel.For(0, x.Length, row =>
            {
                for (int col = row + 1; col < x.Length; col++)
                {
                    double dist = math.EuclidianDistance2(x[row], x[col]);
                    double add  = Math.Exp(-dist / arad);
                    if (add != 0)
                    {
                        lock (o)
                        {
                            P[row] += add;
                            P[col] += add;
                        }
                    }
                }
                if (progress++ % 100 == 0)
                {
                    InMemoryLogger.PrintMessage($"Potential {progress}/{x.Length}");
                }
            });

            List <double[]> c = new List <double[]>();

            double D   = P.Max();
            double cap = 0.01 * D;

            double[] wn = x[P.FindIndex(z => z == D)];
            c.Add(wn);
            while (D > cap)
            {
                InMemoryLogger.PrintMessage($"Iteration {c.Count} [{D} cap {cap}]");
                progress = 0;
                Parallel.For(0, x.Length, row =>
                {
                    double dist = math.EuclidianDistance2(x[row], wn);
                    double add  = -D * Math.Exp(-dist / brad);
                    P[row]     += add;

                    //if (progress++ % 100 == 0)
                    //{
                    //    Console.Write("\rPotential {0}/{1}", progress, x.Length);
                    //}
                });
                D  = P.Max();
                wn = x[P.FindIndex(z => z == D)];
                c.Add(wn);
            }
            return(c.ToArray());
        }
示例#2
0
        public static double[][] clustering(double[][] x, int k, kmeansType initType, out double error)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            double[][] m = null;
            //Console.WriteLine("Initialization...");
            switch (initType)
            {
            case kmeansType.Forgy: m = ForgyInit(x, k); break;

            case kmeansType.kmeanspp: m = kmeansppinit(x, k); break;

            case kmeansType.RandomPartition: m = RandomPartion(x, k); break;
            }
            int confirmations = 0;

            int[] a          = new int[x.Length];
            int   reassigned = 0;
            int   iters      = 0;

            bool[]   ntu   = new bool[k];
            DateTime start = DateTime.Now;

            do
            {
                for (int i = 0; i < k; i++)
                {
                    ntu[i] = true;
                }
                do
                {
                    sw.Restart();
                    reassigned = Assignment(x, m, ref a, ref ntu);
                    if (reassigned != 0)
                    {
                        m = Update(x, m, a, ntu, k);
                    }
                    int unmoved = ntu.Count(z => !z);

                    InMemoryLogger.PrintMessage($"[{(DateTime.Now - start).TotalMilliseconds} ms] Iteration {iters++}, reassigned {reassigned} unmoved {unmoved} Time {sw.ElapsedMilliseconds} ms     ");

                    if (reassigned != 0)
                    {
                        confirmations = 0;
                    }
                }while (reassigned != 0);
                confirmations++;
                InMemoryLogger.PrintMessage($"CONFIRMATION {confirmations}");
            } while (confirmations < 2);

            error = Error(x, m, a);
            return(m);
        }
示例#3
0
        public static double[][] clustering(double[][] x, int k, int restarts, kmeansType initType)
        {
            double[][][] pool  = new double[restarts][][];
            double[]     error = new double[restarts];
            for (int r = 0; r < restarts; r++)
            {
                InMemoryLogger.PrintMessage($"Restart {r}");
                double er = 0.0;
                pool[r]  = clustering(x, k, initType, out er);
                error[r] = er;
                InMemoryLogger.PrintMessage($"Error {er}");
            }
            double min = error.Min();
            int    opt = error.FindIndex(z => z == min);

            return(pool[opt]);
        }
示例#4
0
        private void Solve(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop)
        {
            KMEANSExtractorI extractor = new KMEANSExtractorI(int.Parse(txtbxRulesCount.Text));
            var timer = Stopwatch.StartNew();
            var fis   = ANFISBuilder <GaussianRule2> .Build(x, y, extractor, bprop, int.Parse(txtbxMaxIterCount.Text));

            timer.Stop();

            double err  = bprop.Error(tx, ty, fis.RuleBase);
            string line = "";

            double correctClass = 0;

            for (int i = 0; i < tx.Length; i++)
            {
                double[] o = fis.Inference(tx[i]);
                if (tx[i].Length == 4 && o.Length == 3)
                {
                    line = $"input: [{tx[i][0]}, {tx[i][1]}, {tx[i][2]}, {tx[i][3]}] output:[{o[0].ToString("F2")}, {o[1].ToString("F2")}, {o[2].ToString("F2")}] expected output: [{ty[i][0]}, {ty[i][1]}, {ty[i][2]}]";
                }
                for (int j = 0; j < ty[i].Length; j++)
                {
                    if (ty[i][j] == 1.0 && o[j] == o.Max())
                    {
                        correctClass++;
                        line += " OK";
                    }
                }
                if (tx[i].Length == 4 && o.Length == 3)
                {
                    InMemoryLogger.PrintMessage(line);
                }
            }

            InMemoryLogger.PrintMessage(string.Format("Correct answers {5}\tClassification Error {4}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, 1.0 - correctClass / ty.Length, correctClass));
        }
示例#5
0
        public double Iteration(double[][] x, double[][] y, IList <IRule> ruleBase)
        {
Restart:
            _isStop = false;

            if (x.Length != y.Length)
            {
                throw new Exception("Input and desired output lengths not match");
            }
            if (ruleBase == null || ruleBase.Count == 0)
            {
                throw new Exception("Incorrect rulebase");
            }

            int outputDim  = ruleBase[0].Z.Length;
            int numOfRules = ruleBase.Count;

            double globalError = 0.0;

            double[] firings = new double[numOfRules];

            for (int sample = 0; sample < x.Length; sample++)
            {
                double[] o         = new double[outputDim];
                double   firingSum = 0.0;

                for (int i = 0; i < numOfRules; i++)
                {
                    firings[i] = ruleBase[i].Membership(x[sample]);
                    firingSum += firings[i];
                }

                if (UnknownCaseFaced != null && firingSum < _adjustThreshold)
                {
                    int neig = math.NearestNeighbourhood(ruleBase.Select(z => z.Centroid).ToArray(), x[sample]);
                    UnknownCaseFaced(ruleBase, x[sample], y[sample], ruleBase[neig].Centroid);
                    InMemoryLogger.PrintMessage($"Adjusting rule base. Now {ruleBase.Count} are in base.");
                    goto Restart;
                }

                for (int i = 0; i < numOfRules; i++)
                {
                    for (int C = 0; C < outputDim; C++)
                    {
                        o[C] += firings[i] / firingSum * ruleBase[i].Z[C];
                    }
                }

                for (int rule = 0; rule < ruleBase.Count; rule++)
                {
                    double[] parm = ruleBase[rule].Parameters;
                    double[] grad = ruleBase[rule].GetGradient(x[sample]);

                    for (int p = 0; p < parm.Length; p++)
                    {
                        double g = dEdP(y[sample], o, ruleBase, firings, grad, firingSum, rule, outputDim, numOfRules, p);

                        parm[p] -= _learningRate * g;
                    }
                }

                for (int i = 0; i < numOfRules; i++)
                {
                    for (int C = 0; C < outputDim; C++)
                    {
                        ruleBase[i].Z[C] -= _learningRate * (o[C] - y[sample][C]) * firings[i] / firingSum;
                    }
                }

                for (int C = 0; C < outputDim; C++)
                {
                    globalError += Math.Abs(o[C] - y[sample][C]);
                }
            }

            checkStop(globalError);

            return(globalError / x.Length);
        }