private bool frndInClique(Friend f1, Gene g) { for (int i = 0; i < g.bitString.Length; i++) { if (g.bitString[i] == true) { Friend f2 = _friends.First(f => f.localID == i); if (!neo.friends(f1, f2)) return false; } } return true; }
private void removeSmallest(Gene g) { for (int i = g.bitString.Length - 1; i > 0; i--) if (g.bitString.ElementAt<bool>(i) == true) { g.bitString[i] = false; break; } }
private void recombineParents() { childred.Clear(); Gene p1 = selected.ElementAt<Gene>(0); Gene p2 = selected.ElementAt<Gene>(1); Gene c1 = new Gene(p1.bitString.Length); Gene c2 = new Gene(p2.bitString.Length); double t = (p1.bitString.Length / crossoverPoints); int split = (int) Math.Floor(t); bool flip = false; for (int i = 0; i < p1.bitString.Length; i++) { if (flip == true) { c1.bitString[i] = p1.bitString[i]; c2.bitString[i] = p2.bitString[i]; } else { c1.bitString[i] = p2.bitString[i]; c2.bitString[i] = p1.bitString[i]; } if ((i % split) == 0) { if (flip == true) flip = false; else flip = true; } } childred.Add(c1); childred.Add(c2); }
private bool isClique(Gene g) { for (int i = 0; i < g.bitString.Length; i++) if (g.bitString.ElementAt<bool>(i)==true) for (int j = i + 1; j < g.bitString.Length; j++) if (g.bitString.ElementAt<bool>(j) == true) { _friends.First(f => f.localID == i); if (!neo.friends(_friends.First(f => f.localID == i), _friends.First(f => f.localID == j))) return false; } return true; }
private void initialize() { // get an array copy of _friends because // it's easier to address by index Friend[] frndArray = _friends.ToArray(); for (int i = 0; i < population.Length; i++) { population[i] = new Gene(numFriends); population[i].ID = idx; idx++; // select a random friend f1 from the array Friend rnd = frndArray[rand.Next(frndArray.Length)]; if (rnd.numFriends > 0) { // get f1 and all of f1's friends UserNFriends f1nFriends = neo.getUserNFriends(rnd); // set f1 as part of the clique population[i].bitString[f1nFriends.user.localID] = true; // pop a random friend f2 from f1's list of friends Friend f2 = f1nFriends.popRndFrnd(rand); // set f2 as part of the clique population[i].bitString[f2.localID] = true; // iterate through the remainder of f1's friend list while (f1nFriends.notEmpty()) { // pop random friend fx from f1's list of friends Friend fx = f1nFriends.popRndFrnd(rand); // if part of the clique if (frndInClique(fx, population[i])) // set fx as part of the clique population[i].bitString[fx.localID] = true; } } else population[i].bitString[rnd.localID] = true; // calculate the fitness of each gene // as they are created population[i].calcFitness(); } }
private int hammingDistance(Gene g1, Gene g2) { int d = 0; bool[] distance = new bool[numFriends]; for (int i = 0; i < numFriends; i++) { distance[i] = (g1.bitString[i] ^ g2.bitString[i]); } foreach (bool b in distance) { if (b == true) d++; } return d; }