View difference between Paste ID: Gfy1Wy9J and 4rMsrC61
SHOW: | | - or go back to the newest paste.
1
        public List<mPoint> Compute(testFunctions t, String fc, ILPanel ilPanel, ILPlotCube cube)
2
        {
3
            List<mPoint> rtn = new List<mPoint>();
4
            float[] _leader = new float[3];
5
            //generuju nahoodu populaci a z ni si vytahnu leadera, jeho z je nejmenší
6
            var population = getStartPopulation(t, fc);
7
            if (population != null && population.Count > 0)
8
            {
9
                population = population.OrderBy(ele => ele[2]).ToList();
10
                _leader = population.First(); //leader
11
            }
12
            //cyklus reprezentujici migraci populace - all to BEST!
13
            int migration = 0;
14
            while (migration < 50) {
15
                //pro každeho kromě leadera počitam toto:
16
                foreach (var person in population)
17
                {
18
                    //1. peburtační vektor - dve varianty, odviji se od vygenerovaneho Rka, Rko generuju zvlašt pro px a zvlašt pro py
19
                    float px, py;
20
                    px = getR();
21
                    py = getR();
22
                    //Nejlepši je na začatku hledani právě on samotný
23
                    float[] currBest = { person[0], person[1], person[2] };
24
                    //Vynechavam leadera
25
                    if (person == population.First())
26
                    {
27
                        continue;
28
                    }
29
                    else
30
                    {
31
                        for (float currStep = stepSize; currStep < pathLenght; currStep += stepSize)
32
                        {
33
                            float x1_ml = person[0] + (_leader[0] - person[0]) * currStep * px;
34
                            float y1_ml = person[1] + (_leader[1] - person[1]) * currStep * py;
35
                            if ((x1_ml > this.xMAX || x1_ml < this.xMIN) || (y1_ml > this.yMAX || y1_ml < this.yMIN)) continue;
36
                            float z1_ml = selFunctionCalculate(t, fc, x1_ml, y1_ml);
37
                            //je lepši než current best?
38
                            if (z1_ml < currBest[2])
39
                            {
40
                                currBest[0] = x1_ml;
41
                                currBest[1] = y1_ml;
42
                                currBest[2] = z1_ml;
43
                            }
44
                        }
45
                        //Přepišu toho člena populace na noveho (vysledek hledani - the best, the best, the BEEST!
46
                        person[0] = currBest[0];
47
                        person[1] = currBest[1];
48
                        person[2] = currBest[2];
49
                    }
50
                }
51
                //konec migračního cyklu - vizualizace - musim převest populaci na array
52
                float[,] setArr = new float[10, 3];
53
                for(int i=0;i< population.Count; i++){
54
                    setArr[i, 0] = population[i][0];
55
                    setArr[i, 1] = population[i][1];
56
                    setArr[i, 2] = population[i][2];
57
                }
58
                visualize(setArr, ilPanel, cube);
59
                migration++;
60
            }
61
            //finální vizualizace - sežazení a zjištění nalezeného minima
62
            for (int i = 0; i < population.Count; i++)
63
            {
64
                mPoint toAdd =new mPoint(population[i][0], population[i][1], population[i][2]);
65
                rtn.Add(toAdd);
66
            }
67
            return rtn;
68
        }
69
        //------------------------------------------------------------------------------------------
70
        private float getR() {
71
            Random rnd = new Random();
72
            float r = (float)rnd.NextDouble();
73
            if (r < PRT)
74
            {
75
                return 1;
76
            }
77
            else
78
            {
79
                return 0;
80
            }
81
        }
82
        //------------------------------------------------------------------------------------------
83
        private List<float[]> getStartPopulation(testFunctions t, String fc)
84
        {
85
            Random rnd = new Random();
86
            List<mPoint> startPopulation = new List<mPoint>();
87
            List<float[]> rtn = new List<float[]>();
88
            for (int j = 0; j < 10; j++)
89
            {
90
                float x, y;
91
                do
92
                {
93
                    x = rnd.Next((int)(this.xMIN * 100), (int)(this.xMAX * 100) + 1) / 100F;
94
                    y = rnd.Next((int)(this.yMIN * 100), (int)(this.yMAX * 100) + 1) / 100F;
95
                } while ((x > this.xMAX || x < this.xMIN)||(y > this.yMAX || y < this.yMIN));
96
                //x = rnd.Next((int)(this.xMIN * 100), (int)(this.xMAX * 100) + 1) / 100F;
97
                //y = rnd.Next((int)( this.yMIN * 100), (int)(this.yMAX * 100) + 1) / 100F;
98
                float[] f = { x, y, selFunctionCalculate(t, fc, x, y) };
99
                rtn.Add(f);
100
            }
101
            return rtn;
102
        }