Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs @ 11838

Last change on this file since 11838 was 11838, checked in by bgoldman, 9 years ago

#2282: Added BEACON to the copyright on P3 files and included comments referring to the publication

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Threading.Tasks;
27using HeuristicLab.Core;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
31  // This code is based off the publication
32  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
33  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
34  public static class LinkageCrossover {
35    // In the GECCO paper, Figure 3
36    public static double ImproveUsingTree(LinkageTree tree, IList<bool[]> donors, bool[] solution, double fitness, IBinaryVectorProblem problem, IRandom rand) {
37      var options = Enumerable.Range(0, donors.Count).ToArray();
38      foreach (var cluster in tree.Clusters) {
39        // Find a donor which has at least one gene value different
40        // from the current solution for this cluster of genes
41        bool donorFound = false;
42        foreach (var donorIndex in options.ShuffleList(rand)) {
43          // Attempt the donation
44          fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, out donorFound);
45          if (donorFound) break;
46        }
47      }
48      return fitness;
49    }
50
51    private static double Donate(bool[] solution, double fitness, bool[] source, IEnumerable<int> cluster, IBinaryVectorProblem problem, out bool changed) {
52      // keep track of which bits flipped to make the donation
53      List<int> flipped = new List<int>();
54      foreach (var index in cluster) {
55        if (solution[index] != source[index]) {
56          flipped.Add(index);
57          solution[index] = !solution[index];
58        }
59      }
60      changed = flipped.Count > 0;
61      if (changed) {
62        double newFitness = problem.Evaluate(solution);
63        // if the original is strictly better, revert the change
64        if (problem.IsBetter(fitness, newFitness)) {
65          foreach (var index in flipped) {
66            solution[index] = !solution[index];
67          }
68        } else {
69          // new solution is no worse than original, keep change to solution
70          fitness = newFitness;
71        }
72      }
73      return fitness;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.