Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs @ 12012

Last change on this file since 12012 was 11956, checked in by mkommend, 10 years ago

#2282: Created plugin for binary vector problems.

File size: 3.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Problems.BinaryVector;
26using HeuristicLab.Random;
27
28namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
29  // This code is based off the publication
30  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
31  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
32  public static class LinkageCrossover {
33    // In the GECCO paper, Figure 3
34    public static double ImproveUsingTree(LinkageTree tree, IList<bool[]> donors, bool[] solution, double fitness, IBinaryVectorProblem problem, IRandom rand) {
35      var options = Enumerable.Range(0, donors.Count).ToArray();
36      foreach (var cluster in tree.Clusters) {
37        // Find a donor which has at least one gene value different
38        // from the current solution for this cluster of genes
39        bool donorFound = false;
40        foreach (var donorIndex in options.ShuffleList(rand)) {
41          // Attempt the donation
42          fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, out donorFound);
43          if (donorFound) break;
44        }
45      }
46      return fitness;
47    }
48
49    private static double Donate(bool[] solution, double fitness, bool[] source, IEnumerable<int> cluster, IBinaryVectorProblem problem, out bool changed) {
50      // keep track of which bits flipped to make the donation
51      List<int> flipped = new List<int>();
52      foreach (var index in cluster) {
53        if (solution[index] != source[index]) {
54          flipped.Add(index);
55          solution[index] = !solution[index];
56        }
57      }
58      changed = flipped.Count > 0;
59      if (changed) {
60        double newFitness = problem.Evaluate(solution);
61        // if the original is strictly better, revert the change
62        if (problem.IsBetter(fitness, newFitness)) {
63          foreach (var index in flipped) {
64            solution[index] = !solution[index];
65          }
66        } else {
67          // new solution is no worse than original, keep change to solution
68          fitness = newFitness;
69        }
70      }
71      return fitness;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.