Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<mPoint> Compute(testFunctions t, String fc, ILPanel ilPanel, ILPlotCube cube)
- {
- List<mPoint> rtn = new List<mPoint>();
- float[] _leader = new float[3];
- //generuju nahoodu populaci a z ni si vytahnu leadera, jeho z je nejmenší
- var population = getStartPopulation(t, fc);
- if (population != null && population.Count > 0)
- {
- population = population.OrderBy(ele => ele[2]).ToList();
- _leader = population.First(); //leader
- }
- //cyklus reprezentujici migraci populace - all to BEST!
- int migration = 0;
- while (migration < 50) {
- //pro každeho kromě leadera počitam toto:
- foreach (var person in population)
- {
- //1. peburtační vektor - dve varianty, odviji se od vygenerovaneho Rka, Rko generuju zvlašt pro px a zvlašt pro py
- float px, py;
- px = getR();
- py = getR();
- //Nejlepši je na začatku hledani právě on samotný
- float[] currBest = { person[0], person[1], person[2] };
- //Vynechavam leadera
- if (person == population.First())
- {
- continue;
- }
- else
- {
- for (float currStep = stepSize; currStep < pathLenght; currStep += stepSize)
- {
- float x1_ml = person[0] + (_leader[0] - person[0]) * currStep * px;
- float y1_ml = person[1] + (_leader[1] - person[1]) * currStep * py;
- if ((x1_ml > this.xMAX || x1_ml < this.xMIN) || (y1_ml > this.yMAX || y1_ml < this.yMIN)) continue;
- float z1_ml = selFunctionCalculate(t, fc, x1_ml, y1_ml);
- //je lepši než current best?
- if (z1_ml < currBest[2])
- {
- currBest[0] = x1_ml;
- currBest[1] = y1_ml;
- currBest[2] = z1_ml;
- }
- }
- //Přepišu toho člena populace na noveho (vysledek hledani - the best, the best, the BEEST!
- person[0] = currBest[0];
- person[1] = currBest[1];
- person[2] = currBest[2];
- }
- }
- //konec migračního cyklu - vizualizace - musim převest populaci na array
- float[,] setArr = new float[10, 3];
- for(int i=0;i< population.Count; i++){
- setArr[i, 0] = population[i][0];
- setArr[i, 1] = population[i][1];
- setArr[i, 2] = population[i][2];
- }
- visualize(setArr, ilPanel, cube);
- migration++;
- }
- //finální vizualizace - sežazení a zjištění nalezeného minima
- for (int i = 0; i < population.Count; i++)
- {
- mPoint toAdd =new mPoint(population[i][0], population[i][1], population[i][2]);
- rtn.Add(toAdd);
- }
- return rtn;
- }
- //------------------------------------------------------------------------------------------
- private float getR() {
- Random rnd = new Random();
- float r = (float)rnd.NextDouble();
- if (r < PRT)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //------------------------------------------------------------------------------------------
- private List<float[]> getStartPopulation(testFunctions t, String fc)
- {
- Random rnd = new Random();
- List<mPoint> startPopulation = new List<mPoint>();
- List<float[]> rtn = new List<float[]>();
- for (int j = 0; j < 10; j++)
- {
- float x, y;
- do
- {
- x = rnd.Next((int)(this.xMIN * 100), (int)(this.xMAX * 100) + 1) / 100F;
- y = rnd.Next((int)(this.yMIN * 100), (int)(this.yMAX * 100) + 1) / 100F;
- } while ((x > this.xMAX || x < this.xMIN)||(y > this.yMAX || y < this.yMIN));
- //x = rnd.Next((int)(this.xMIN * 100), (int)(this.xMAX * 100) + 1) / 100F;
- //y = rnd.Next((int)( this.yMIN * 100), (int)(this.yMAX * 100) + 1) / 100F;
- float[] f = { x, y, selFunctionCalculate(t, fc, x, y) };
- rtn.Add(f);
- }
- return rtn;
- }
Add Comment
Please, Sign In to add comment