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;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.ProgramSynthesis {
|
---|
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 | }
|
---|