Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP/3.3/Recombination/LangdonHomologousCrossOver.cs @ 5740

Last change on this file since 5740 was 835, checked in by gkronber, 16 years ago
  • added another abstract base class for GP crossover operators with maxsize and maxheight constraints
  • changed StandardCrossOver to inherit from SizeConstrictedGPCrossoverBase
  • changed SizeFairCrossOver to inherit from SizeConstrictedGPCrossoverBase
  • generally improved code of SizeFairCrossOver
  • changed LangdonHomologousCrossOver to inherit from SizeFairCrossOver and implemented only the method to finally select branches.

#393 (Refactor GP crossover operators to extract common code into the abstract base class GPCrossoverBase)

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Data;
30using HeuristicLab.Constraints;
31using System.Diagnostics;
32
33namespace HeuristicLab.GP {
34  /// <summary>
35  /// Implementation of a homologous crossover operator as described in:
36  /// William B. Langdon
37  /// Size Fair and Homologous Tree Genetic Programming Crossovers,
38  /// Genetic Programming and Evolvable Machines, Vol. 1, Number 1/2, pp. 95-119, April 2000
39  /// </summary>
40  public class LangdonHomologousCrossOver : SizeFairCrossOver {
41    protected override IFunctionTree SelectReplacement(MersenneTwister random, List<int> replacedTrail, List<CrossoverPoint> crossoverPoints) {
42      List<CrossoverPoint> bestPoints = new List<CrossoverPoint> { crossoverPoints[0] };
43      int bestMatchLength = MatchingSteps(replacedTrail, crossoverPoints[0].trail);
44      for (int i = 1; i < crossoverPoints.Count; i++) {
45        int currentMatchLength = MatchingSteps(replacedTrail, crossoverPoints[i].trail);
46        if (currentMatchLength > bestMatchLength) {
47          bestMatchLength = currentMatchLength;
48          bestPoints.Clear();
49          bestPoints.Add(crossoverPoints[i]);
50        } else if (currentMatchLength == bestMatchLength) {
51          bestPoints.Add(crossoverPoints[i]);
52        }
53      }
54      return bestPoints[random.Next(bestPoints.Count)].tree;
55    }
56    private int MatchingSteps(List<int> t1, List<int> t2) {
57      int n = Math.Min(t1.Count, t2.Count);
58      for (int i = 0; i < n; i++) if (t1[i] != t2[i]) return i;
59      return n;
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.