public override List<Element> Run(List<Element> population, Lib.func f, bool integer) { if (ps["Migration"] <= 0) return population; //krizeni smeruje potomky //t = krok = 0 (while t<pathlen) // Element leader = population[0]; Element worst = population[0]; foreach (Element e in population) { if (e.Z < leader.Z) leader = e; if (e.Z > worst.Z) worst = e; } if (worst.Z - leader.Z < ps["MinDiv"]) return population; Random r = new Random(); List<Element> result = new List<Element>(); for (int k = 0; k < population.Count; k++) { Element e = population[k]; if (e == leader) { result.Add(e); continue; } List<Element> jumps = new List<Element>(); for (int i = 0; i < ps["PathLength"] / ps["Step"]; i++) { List<int> prtv = GetPRTVector(2, r); Element leaderdiff = new Element(leader.X - e.X, leader.Y - e.Y, 0); Element onejump = new Element(e.X + leaderdiff.X * i * ps["Step"] * prtv[0], e.Y + leaderdiff.Y * i * ps["Step"] * prtv[1], f); if (onejump.IsInInterval()) jumps.Add(onejump); } int bestindex = -1; for (int j = 0; j < jumps.Count; j++) { if (jumps[j].Z < e.Z) bestindex = j; } result.Add((bestindex == -1) ? population[k] : jumps[bestindex]); } ps["Migration"]--; return result; }
public override List<Element> Run(List<Element> population, Lib.func f, bool integer) { if (population.Count < 4) // too small return population; Random r = new Random(); List<Element> result = new List<Element>(); for (int i = 0; i < population.Count; i++) //for each element { //get 3 different elements in interval int[] rands = new int[3]; Element[] parents = new Element[4]; Element diff = new Element(0, 0, 0); //unassigned value, will (should!) be different Element noisy = new Element(0, 0, 0);//unassigned value, will (should!) be different Element trial = new Element(0, 0, 0);//unassigned value, will (should!) be different while (true) //until child in interval is found { do { rands[0] = r.Next(population.Count); } while (rands[0] == i); do { rands[1] = r.Next(population.Count); } while (rands[1] == i || rands[1] == rands[0]); do { rands[2] = r.Next(population.Count); } while (rands[2] == i || rands[2] == rands[0] || rands[2] == rands[1]); parents[0] = population[i]; parents[1] = population[rands[0]]; parents[2] = population[rands[1]]; parents[3] = population[rands[2]]; //get noisy vector (mutation) diff = new Element(parents[1].X - parents[2].X, parents[1].Y - parents[2].Y, 0); noisy = new Element(parents[3].X + ps["F"] * diff.X, parents[3].Y + ps["F"] * diff.Y, 0); //get trial element (intersection) trial = new Element((r.Next(1) < ps["CR"] ? noisy.X : parents[0].X), (r.Next(1) < ps["CR"] ? noisy.Y : parents[0].Y), f); if (trial.IsInInterval()) break; } //use parent or trial, whichever is better if (trial.Z < parents[0].Z) result.Add(trial); else result.Add(parents[0]); } return result; }