[645] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Random;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Constraints;
|
---|
| 31 | using System.Diagnostics;
|
---|
| 32 |
|
---|
| 33 | namespace 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>
|
---|
[835] | 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]);
|
---|
[645] | 52 | }
|
---|
| 53 | }
|
---|
[835] | 54 | return bestPoints[random.Next(bestPoints.Count)].tree;
|
---|
[645] | 55 | }
|
---|
[835] | 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;
|
---|
[645] | 59 | return n;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|