Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs @ 11858

Last change on this file since 11858 was 11858, checked in by bburlacu, 9 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
RevLine 
[10347]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;
[10674]26using HeuristicLab.Data;
[10347]27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionTracking {
31  [StorableClass]
[11858]32  [Item("BeforeCrossoverOperator", "A generic operator that can record genealogical relationships between crossover parents and children.")]
[10650]33  public class BeforeCrossoverOperator<T> : EvolutionTrackingOperator<T>, ICrossoverOperator<T> where T : class,IItem {
[10830]34    private const string ParentsParameterName = "Parents";
35    private const string ChildParameterName = "Child";
[10347]36
[10830]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    }
[10347]43
[10888]44    public IValueParameter<IntValue> ExecutionCountParameter {
45      get { return (IValueParameter<IntValue>)Parameters["ExecutionCount"]; }
46    }
47
[10347]48    protected BeforeCrossoverOperator(BeforeCrossoverOperator<T> original, Cloner cloner)
49      : base(original, cloner) {
50    }
[11390]51
[10347]52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new BeforeCrossoverOperator<T>(this, cloner);
54    }
55
[11227]56    [StorableConstructor]
57    protected BeforeCrossoverOperator(bool deserializing) : base(deserializing) { }
58
[10347]59    public BeforeCrossoverOperator() {
[10830]60      Parameters.Add(new ScopeTreeLookupParameter<T>(ParentsParameterName));
61      Parameters.Add(new LookupParameter<T>(ChildParameterName));
[10888]62      Parameters.Add(new ValueParameter<IntValue>("ExecutionCount", new IntValue(0)));
[10347]63    }
64
[10884]65    private readonly Func<IScope, string> getScopeId = s => ((StringValue)s.Variables["Id"].Value).Value;
[10347]66    public override IOperation Apply() {
[10888]67      ExecutionCountParameter.Value.Value++;
68
[10884]69      var subScopes = ExecutionContext.Scope.SubScopes;
[11233]70      var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetById(getScopeId(x))).ToList();
[11253]71      if (!parentVertices.Any())
72        throw new InvalidOperationException("Could not retrieve parent vertices.");
[10347]73
74      var parents = ParentsParameter.ActualValue.ToList();
75
[10897]76      var childVertex = new GenealogyGraphNode<T>(parents[0]) { Rank = parentVertices[0].Rank + 1 };
[10888]77
[10347]78      GenealogyGraph.AddVertex(childVertex);
[10888]79
[11253]80      foreach (var parentVertex in parentVertices)
[10886]81        GenealogyGraph.AddArc(parentVertex, childVertex);
[11253]82
[10674]83      ExecutionContext.Scope.Variables.Add(new Variable("Id", new StringValue(childVertex.Id)));
[10347]84      return base.Apply();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.