[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[10674] | 26 | using HeuristicLab.Data;
|
---|
[10347] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 | }
|
---|