Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void DifferentialAlg(int numOfGen, string chosen, int dim, bool isMax)
- {
- FormMain fmDif = FormMain.fm;
- double CR = 0.8;
- double F = 0.5;
- List<Point> newPop = new List<Point>();
- for (int throughGens = 0; throughGens < numOfGen; throughGens++)
- {
- for (int throughPop = 0; throughPop < fmDif.population.Count; throughPop++)
- {
- List<int> randIndexes = new List<int>();
- while (randIndexes.Count < 3)
- {
- int randIndex = rand.Next(0, fmDif.population.Count - 1);
- if (!randIndexes.Contains(randIndex))
- {
- randIndexes.Add(randIndex);
- }
- }
- Point rand1 = fmDif.population[randIndexes[0]];
- Point rand2 = fmDif.population[randIndexes[1]];
- Point rand3 = fmDif.population[randIndexes[2]];
- Point v = new Point(0, 0, 0);
- for (int i = 0; i < rand1.coordinates.Count; i++)
- {
- v.coordinates[i] = rand3.coordinates[i] + F * (rand3.coordinates[i] - rand3.coordinates[i]);
- if (v.coordinates[i] < fmDif.Min || v.coordinates[i] > fmDif.Max)
- {
- v.coordinates[i] = rand.NextDouble() * (fmDif.Max - fmDif.Min) + fmDif.Min;
- }
- }
- Point trial = new Point();
- for (int i = 0; i < v.coordinates.Count; i++)
- {
- double randomNum = rand.NextDouble();
- if (randomNum < CR)
- {
- trial.coordinates[i] = v.coordinates[i];
- }
- else
- {
- trial.coordinates[i] = fmDif.population[throughPop].coordinates[i];
- }
- }
- trial.coordinates[dim] = Functions.UseFcn(chosen, trial);
- if ((isMax && trial.coordinates[dim] > fmDif.population[throughPop].coordinates[dim]) || (!isMax && trial.coordinates[dim] < fmDif.population[throughPop].coordinates[dim]))
- {
- newPop.Add(trial);
- }
- else
- {
- newPop.Add(fmDif.population[throughPop]);
- }
- }
- fmDif.population = newPop;
- newPop = new List<Point>();
- fmDif.Draw(dim);
- }
- }
Add Comment
Please, Sign In to add comment