Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP.Operators/3.3/Recombination/LangdonHomologousCrossOver.cs @ 3928

Last change on this file since 3928 was 2222, checked in by gkronber, 15 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 2.4 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 HeuristicLab.Random;
25using HeuristicLab.GP.Interfaces;
26
27namespace HeuristicLab.GP.Operators {
28  /// <summary>
29  /// Implementation of a homologous crossover operator as described in:
30  /// William B. Langdon
31  /// Size Fair and Homologous Tree Genetic Programming Crossovers,
32  /// Genetic Programming and Evolvable Machines, Vol. 1, Number 1/2, pp. 95-119, April 2000
33  /// </summary>
34  public class LangdonHomologousCrossOver : SizeFairCrossOver {
35    protected override IFunctionTree SelectReplacement(MersenneTwister random, List<int> replacedTrail, List<CrossoverPoint> crossoverPoints) {
36      List<CrossoverPoint> bestPoints = new List<CrossoverPoint> { crossoverPoints[0] };
37      int bestMatchLength = MatchingSteps(replacedTrail, crossoverPoints[0].trail);
38      for (int i = 1; i < crossoverPoints.Count; i++) {
39        int currentMatchLength = MatchingSteps(replacedTrail, crossoverPoints[i].trail);
40        if (currentMatchLength > bestMatchLength) {
41          bestMatchLength = currentMatchLength;
42          bestPoints.Clear();
43          bestPoints.Add(crossoverPoints[i]);
44        } else if (currentMatchLength == bestMatchLength) {
45          bestPoints.Add(crossoverPoints[i]);
46        }
47      }
48      return bestPoints[random.Next(bestPoints.Count)].tree;
49    }
50    private int MatchingSteps(List<int> t1, List<int> t2) {
51      int n = Math.Min(t1.Count, t2.Count);
52      for (int i = 0; i < n; i++) if (t1[i] != t2[i]) return i;
53      return n;
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.