Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs @ 17317

Last change on this file since 17317 was 11858, checked in by bburlacu, 10 years ago

#1772: Micro optimization inside the TraceCalculator and inside the GenealogyGraphNode (Children and Parents properties), resulting in maybe 25% speed gain. Corrected item name for the BeforeCrossoverOperator, very minor refactoring inside the BeforeManipulatorOperator. Updated project file.

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionTracking {
31  [StorableClass]
32  [Item("BeforeCrossoverOperator", "A generic operator that can record genealogical relationships between crossover parents and children.")]
33  public class BeforeCrossoverOperator<T> : EvolutionTrackingOperator<T>, ICrossoverOperator<T> where T : class,IItem {
34    private const string ParentsParameterName = "Parents";
35    private const string ChildParameterName = "Child";
36
37    public IScopeTreeLookupParameter<T> ParentsParameter {
38      get { return (IScopeTreeLookupParameter<T>)Parameters[ParentsParameterName]; }
39    }
40    public ILookupParameter<T> ChildParameter {
41      get { return (ILookupParameter<T>)Parameters[ChildParameterName]; }
42    }
43
44    public IValueParameter<IntValue> ExecutionCountParameter {
45      get { return (IValueParameter<IntValue>)Parameters["ExecutionCount"]; }
46    }
47
48    protected BeforeCrossoverOperator(BeforeCrossoverOperator<T> original, Cloner cloner)
49      : base(original, cloner) {
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new BeforeCrossoverOperator<T>(this, cloner);
54    }
55
56    [StorableConstructor]
57    protected BeforeCrossoverOperator(bool deserializing) : base(deserializing) { }
58
59    public BeforeCrossoverOperator() {
60      Parameters.Add(new ScopeTreeLookupParameter<T>(ParentsParameterName));
61      Parameters.Add(new LookupParameter<T>(ChildParameterName));
62      Parameters.Add(new ValueParameter<IntValue>("ExecutionCount", new IntValue(0)));
63    }
64
65    private readonly Func<IScope, string> getScopeId = s => ((StringValue)s.Variables["Id"].Value).Value;
66    public override IOperation Apply() {
67      ExecutionCountParameter.Value.Value++;
68
69      var subScopes = ExecutionContext.Scope.SubScopes;
70      var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetById(getScopeId(x))).ToList();
71      if (!parentVertices.Any())
72        throw new InvalidOperationException("Could not retrieve parent vertices.");
73
74      var parents = ParentsParameter.ActualValue.ToList();
75
76      var childVertex = new GenealogyGraphNode<T>(parents[0]) { Rank = parentVertices[0].Rank + 1 };
77
78      GenealogyGraph.AddVertex(childVertex);
79
80      foreach (var parentVertex in parentVertices)
81        GenealogyGraph.AddArc(parentVertex, childVertex);
82
83      ExecutionContext.Scope.Variables.Add(new Variable("Id", new StringValue(childVertex.Id)));
84      return base.Apply();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.