Пример #1
0
        private void btn_Create1000X_Click(object sender, EventArgs e)
        {
            var lines = File.ReadAllLines(cb_data.SelectedItem.ToString());

            string[] Finall = new string[1000 * lines.Length];
            for (int i = 0; i < lines.Length * 1000; i++)
            {
                int index = (int)GeneralCom.GetRandom(0, lines.Length);
                Finall[i] = lines[index];
            }
            File.WriteAllLines(cb_data.SelectedItem.ToString().Replace(".dat", "X1000.dat"), Finall);
        }
Пример #2
0
        public void CalcFitness(List <Data> datas, double m)
        {
            double sigma = 0;

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < C; j++)
                {
                    sigma += Math.Pow(U[i, j], m) *
                             Math.Pow(GeneralCom.Euclideandistance(datas[i].DataDim, Centers[j]), 2);
                }
            }
            Fitness = sigma;
        }
Пример #3
0
 public void CalcU()
 {
     for (int i = 0; i < N; i++)
     {
         for (int j = 0; j < C; j++)
         {
             double sigma = 0;
             for (int k = 0; k < C; k++)
             {
                 var a = GeneralCom.Euclideandistance(Datas[i].DataDim, Centers[j]);
                 var b = GeneralCom.Euclideandistance(Datas[i].DataDim, Centers[k]);
                 sigma += Math.Pow(a / b, (2 / (M - 1)));
             }
             U[i, j] = 1 / sigma;
         }
     }
 }
Пример #4
0
 public void CalcV(double w, double c1, double c2, Dim[] globalPosition)
 {
     for (int c = 0; c < C; c++)
     {
         for (int d = 0; d < Velocity[c].Val.Length; d++)
         {
             double y1   = GeneralCom.GetRandom(0, 1);
             double y2   = GeneralCom.GetRandom(0, 1);
             double c1y1 = c1 * y1;
             double c2y2 = c2 * y2;
             Velocity[c].Val[d] =
                 w * Velocity[c].Val[d] +
                 c1y1 * (BestPosition[c].Val[d] - Position[c].Val[d])
                 + c2y2 * (globalPosition[c].Val[d] - Position[c].Val[d]);
         }
     }
 }
Пример #5
0
 private void btn_Pso10000_Click(object sender, EventArgs e)
 {
     for (int it = 0; it < 10000; it++)
     {
         W  = GeneralCom.GetRandom(0.1, 0.9);
         C1 = GeneralCom.GetRandom(0.1, 2);
         C2 = GeneralCom.GetRandom(0.1, 2);
         Pso ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, _data, _maxD);
         int i  = 0;
         for (i = 0; i < 200; i++)
         {
             ps.Calc();
             if (ps.Variance < _rate)
             {
                 break;
             }
         }
         File.AppendAllText("PSOIterate", it + ";" + W + ";" + C1 + ";" + C2 + ";" + i + ";" + ps.GloablBestFitness + ";" + "\t\n");
     }
 }
Пример #6
0
        public void CalcError(List <Data> datas, double m)
        {
            double sigma = 0;

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < C; j++)
                {
                    sigma += Math.Pow(U[i, j], m) *
                             Math.Pow(GeneralCom.Euclideandistance(datas[i].DataDim, Position[j]), 2);
                }
            }
            Iteration++;
            Fitness = sigma;

            // File.AppendAllText("FitnessParticle", Fitness + ";" + Index + ";" + Iteration + ";" + BestFitness + ";" + "\t\n");

            Error = 1 / (sigma + 1);
            //File.AppendAllText("dataa", "\t\n" + Error + "\t\n\t\n");
        }
Пример #7
0
        private void btn_FCM_Click(object sender, EventArgs e)
        {
            File.WriteAllText(DateSet + "//FitnessFCM", "");
            //File.WriteAllText(DateSet + "datafcm", "");
            var now = DateTime.Now;

            double[,] U = new double[_n, C];
            for (int i = 0; i < _n; i++)
            {
                double[] tem = new double[C];
                double   sum = 0;
                for (int j = 0; j < C; j++)
                {
                    tem[j] = GeneralCom.GetRandom() + 0.001;
                    sum   += tem[j];
                }
                for (int j = 0; j < C; j++)
                {
                    U[i, j] = tem[j] / sum;
                }
            }
            Fcm fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, _data, U);

            for (int a = 0; a < 100; a++)
            {
                fc.CalcCenter();
                fc.CalcU();
                fc.CalcFitness(_data, M);
                File.AppendAllText(DateSet + "//FitnessFCM", fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t\n");
            }
            //for (int i = 0; i < _n; i++)
            //{
            //    for (int j = 0; j < C; j++)
            //    {
            //        File.AppendAllText(DateSet + "//datafcm", fc.U[i, j].ToString(CultureInfo.InvariantCulture) + ";");
            //    }
            //    File.AppendAllText(DateSet + "//datafcm", "\t\n");
            //}
        }
Пример #8
0
        }                          //sample size

        public void CalcU(List <Data> datas, double m)
        {
            //File.WriteAllText("data.res", "");
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < C; j++)
                {
                    double sum = 0;
                    for (int k = 0; k < C; k++)
                    {
                        var a = GeneralCom.Euclideandistance(datas[i].DataDim, Position[j]);
                        var b = GeneralCom.Euclideandistance(datas[i].DataDim, Position[k]);
                        //  File.AppendAllText("logU", "a;" + a + "\t\n");
                        //   File.AppendAllText("logU", "b;" + b + "\t\n");
                        sum += Math.Pow((a / b), 2 / (m - 1));
                    }
                    U[i, j] = 1 / sum;
                    // File.AppendAllText("dataa", U[i, j].ToString(CultureInfo.InvariantCulture) + ";");
                }
                // File.AppendAllText("dataa", "\t\n");
            }
        }
Пример #9
0
        private void btn_PSOFCMRR_Click(object sender, EventArgs e)
        {
            _data = _data.OrderBy(x => Guid.NewGuid()).ToList();
            int step     = 100;
            var tempData = _data.Skip(0).Take(step).ToList();
            var now      = DateTime.Now;

            _n = tempData.Count;
            Pso ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, tempData, _maxD);

            for (int i = 0; i < 200; i++)
            {
                ps.Calc();
                if (ps.Variance < _rate)
                {
                    break;
                }
            }
            Fcm fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, tempData, ps.U);

            for (int a = 0; a < 100; a++)
            {
                fc.CalcCenter();
                fc.CalcU();
                fc.CalcFitness(_data, M);
                //File.AppendAllText(DateSet + "//FitnessFCMPSO", fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t\n");
            }
            var aCenters = fc.Centers.OrderBy(p => p.Val[0]).ThenBy(p => p.Val[1]).ThenBy(p => p.Val[2]).ThenBy(p => p.Val[3]).ThenBy(p => p.Val[4]).ToArray();

            for (int it = 0; it < 1000; it++)
            {
                tempData.AddRange(_data.Skip(it * step + step).Take(step).ToList());
                _n = tempData.Count;
                ps = new Pso(C, _n, M, W, C1, C2, 30, _data[0].DataDim.Val.Length, tempData, _maxD, aCenters);
                for (int i = 0; i < 200; i++)
                {
                    ps.Calc();
                    if (ps.Variance < _rate)
                    {
                        break;
                    }
                }
                fc = new Fcm(C, _n, M, 30, _data[0].DataDim.Val.Length, tempData, ps.U);
                for (int a = 0; a < 100; a++)
                {
                    fc.CalcCenter();
                    fc.CalcU();
                    fc.CalcFitness(_data, M);
                }
                double delta    = 0;
                var    bCenters = fc.Centers.OrderBy(p => p.Val[0]).ThenBy(p => p.Val[1]).ThenBy(p => p.Val[2]).ThenBy(p => p.Val[3]).ThenBy(p => p.Val[4]).ToArray();
                for (int d = 0; d < aCenters.Length; d++)
                {
                    delta += GeneralCom.Euclideandistance(aCenters[d], bCenters[d]);
                }
                delta /= aCenters.Length;
                File.AppendAllText(DateSet + "//RRR100", delta + "\t;\t" + fc.Fitness + "\t;\t" + DateTime.Now.Subtract(now).TotalSeconds + "\t;\t" + aCenters.Aggregate("", (current, d) => current + (d + "&")) + "\t;\t" + bCenters.Aggregate("", (current, d) => current + (d + "&")) + "\t\n");
                aCenters = bCenters;
                //MessageBox.Show(delta.ToString());
            }
        }
Пример #10
0
 }                                 //Matrix
 public Pso(int c, int n, double m, double w, double c1, double c2, int num, int dim, List <Data> datas, double[] maxd, Dim[] pastCenters = null)
 {
     Maxd               = maxd;
     C                  = c;
     N                  = n;
     M                  = m;
     Num                = num;
     T                  = 0;
     D                  = dim;
     Datas              = datas;
     W                  = w;
     C1                 = c1;
     C2                 = c2;
     GloablBestError    = double.MinValue;
     GloablBestFitness  = double.MaxValue;
     GloablBestPosition = new Dim[c];
     Particles          = new List <Particle>();
     for (int a = 0; a < Num; a++)
     {
         Particle p = new Particle
         {
             U            = new double[N, c],
             C            = c,
             N            = n,
             Position     = a == 0 && pastCenters != null ? pastCenters : new Dim[c],
             BestPosition = a == 0 && pastCenters != null ? pastCenters : new Dim[c],
             Velocity     = new Dim[c],
             Error        = Double.MinValue,
             BestError    = Double.MinValue,
             Iteration    = 0,
             Index        = a,
             BestFitness  = double.MaxValue,
             Fitness      = double.MaxValue
         };
         if (a == 0 && pastCenters != null)
         {
             p.Position         = pastCenters;
             p.BestPosition     = pastCenters;
             GloablBestPosition = pastCenters;
             for (int i = 0; i < c; i++)
             {
                 p.Velocity[i] = new Dim(D);
             }
         }
         else
         {
             for (int i = 0; i < c; i++)
             {
                 p.Position[i] = new Dim(D);
                 p.Velocity[i] = new Dim(D);
                 if (a == 0)
                 {
                     GloablBestPosition[i] = new Dim(D);
                 }
                 for (int j = 0; j < D; j++)
                 {
                     p.Position[i].Val[j] = GeneralCom.GetRandom(0, Maxd[j]);
                     //p.Velocity[i].Val[j] = GeneralCom.GetRandom(0, Maxd[j]);
                     if (a == 0)
                     {
                         GloablBestPosition[i].Val[j] = GeneralCom.GetRandom(0, Maxd[j]);
                     }
                 }
             }
         }
         p.BestPosition = p.Position;
         p.CalcU(datas, M);
         p.CalcV(W, C1, C2, GloablBestPosition);
         Particles.Add(p);
     }
 }