Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/Recombination/SizeFairCrossOver.cs @ 833

Last change on this file since 833 was 833, checked in by gkronber, 16 years ago

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

File size: 6.1 KB
RevLine 
[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
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 size fair 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>
[833]40  public class SizeFairCrossOver : GPCrossoverBase {
[645]41    private const int MAX_RECOMBINATION_TRIES = 20;
42    public override string Description {
43      get {
44        return @"";
45      }
46    }
47    public SizeFairCrossOver()
48      : base() {
49      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
51    }
52
[833]53    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
[645]54      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
55      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
56
[833]57      // when tree0 is terminal then try to cross into tree1, when tree1 is also terminal just return tree0 unchanged.
58      IFunctionTree newTree;
59      if(tree0.SubTrees.Count > 0) {
60        newTree = Cross(gardener, tree0, tree1, random, maxTreeSize, maxTreeHeight);
61      } else if(tree1.SubTrees.Count > 0) {
62        newTree = Cross(gardener, tree1, tree0, random, maxTreeSize, maxTreeHeight);
63      } else newTree = tree0;
[645]64
[833]65      // check if the height and size of the new tree are still in the allowed bounds
66      Debug.Assert(newTree.Height <= maxTreeHeight);
67      Debug.Assert(newTree.Size <= maxTreeSize);
68      return newTree;
[645]69    }
70
[833]71    private IFunctionTree Cross(TreeGardener gardener, IFunctionTree tree0, IFunctionTree tree1, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {
72      int tries = 0;
73      IFunctionTree insertedBranch = null;
74      IFunctionTree crossoverPoint = null;
75      int removedBranchIndex = 0;
76      do {
77        // select a random suboperator of the 'receiving' tree
78        while(crossoverPoint == null) crossoverPoint = gardener.GetRandomParentNode(tree0);
[814]79        removedBranchIndex = random.Next(crossoverPoint.SubTrees.Count);
[833]80        IFunctionTree removedBranch = crossoverPoint.SubTrees[removedBranchIndex];
81        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(crossoverPoint.Function, removedBranchIndex);
82        int removedBranchSize = removedBranch.Size;
83        int maxBranchSize = maxTreeSize - (tree0.Size - removedBranchSize);
84        int maxBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, crossoverPoint);
85        insertedBranch = GetReplacementBranch(random, gardener, allowedFunctions, tree1, removedBranchSize, maxBranchSize, maxBranchHeight);
86      } while(insertedBranch == null && tries++ < MAX_RECOMBINATION_TRIES);
[645]87
[833]88      if(insertedBranch != null) {
[814]89        // replace the branch below the crossoverpoint with the selected branch from root1
90        crossoverPoint.RemoveSubTree(removedBranchIndex);
91        crossoverPoint.InsertSubTree(removedBranchIndex, insertedBranch);
92      }
[833]93      return tree0;
[645]94    }
95
96    private IFunctionTree GetReplacementBranch(IRandom random, TreeGardener gardener, IList<IFunction> allowedFunctions, IFunctionTree tree, int removedBranchSize, int maxBranchSize, int maxBranchHeight) {
[833]97      var branches = gardener.GetAllSubTrees(tree).Where(t => allowedFunctions.Contains(t.Function) && t.Size <= maxBranchSize && t.Height <= maxBranchHeight)
[645]98        .Select(t => new { Tree = t, Size = t.Size }).Where(s => s.Size < 2 * removedBranchSize + 1);
99
100      var shorterBranches = branches.Where(t => t.Size < removedBranchSize);
101      var longerBranches = branches.Where(t => t.Size > removedBranchSize);
102      var equalLengthBranches = branches.Where(t => t.Size == removedBranchSize);
103
[833]104      if(shorterBranches.Count() == 0 || longerBranches.Count() == 0) {
105        if(equalLengthBranches.Count() == 0) {
[645]106          return null;
107        } else {
108          return equalLengthBranches.ElementAt(random.Next(equalLengthBranches.Count())).Tree;
109        }
110      } else {
111        // invariant: |shorterBranches| > 0  and |longerBranches| > 0
112        double pEqualLength = equalLengthBranches.Count() > 0 ? 1.0 / removedBranchSize : 0.0;
113        double pLonger = (1.0 - pEqualLength) / (longerBranches.Count() * (1.0 + longerBranches.Average(t => t.Size) / shorterBranches.Average(t => t.Size)));
114        double pShorter = (1.0 - pEqualLength - pLonger);
115
116        double r = random.NextDouble();
[833]117        if(r < pLonger) {
[645]118          return longerBranches.ElementAt(random.Next(longerBranches.Count())).Tree;
[833]119        } else if(r < pLonger + pShorter) {
[645]120          return shorterBranches.ElementAt(random.Next(shorterBranches.Count())).Tree;
121        } else {
122          return equalLengthBranches.ElementAt(random.Next(equalLengthBranches.Count())).Tree;
123        }
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.