1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
11 | [StorableClass]
|
---|
12 | [Item("EvolutionTrackingAfterCrossoverOperator", "An operator that performs additional operators after crossover")]
|
---|
13 | public sealed class EvolutionTrackingAfterCreationOperator : SingleSuccessorOperator {
|
---|
14 | private const string ResultsParameterName = "Results";
|
---|
15 | private const string PopulationGraphParameterName = "PopulationGraph";
|
---|
16 | private const string GenerationsParameterName = "Generations";
|
---|
17 | private const string CurrentScopeParameterName = "CurrentScope";
|
---|
18 | #region Parameter Properties
|
---|
19 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
20 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
21 | }
|
---|
22 | private ScopeParameter CurrentScopeParameter {
|
---|
23 | get { return (ScopeParameter)Parameters[CurrentScopeParameterName]; }
|
---|
24 | }
|
---|
25 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
26 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
27 | }
|
---|
28 | public ILookupParameter<ItemArray<IItem>> ParentsParameter {
|
---|
29 | get { return (ScopeTreeLookupParameter<IItem>)Parameters["Parents"]; }
|
---|
30 | }
|
---|
31 | public ILookupParameter<IItem> ChildParameter {
|
---|
32 | get { return (ILookupParameter<IItem>)Parameters["Child"]; }
|
---|
33 | }
|
---|
34 | #endregion
|
---|
35 | #region Properties
|
---|
36 | public IScope CurrentScope {
|
---|
37 | get { return CurrentScopeParameter.ActualValue; }
|
---|
38 | }
|
---|
39 | public ResultCollection Results {
|
---|
40 | get { return ResultsParameter.ActualValue; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IntValue Generations {
|
---|
44 | get { return GenerationsParameter.ActualValue; }
|
---|
45 | }
|
---|
46 | private GenealogyGraph<IGenealogyGraphNode> GenealogyGraph {
|
---|
47 | get {
|
---|
48 | IResult result;
|
---|
49 | if (!Results.ContainsKey(PopulationGraphParameterName)) {
|
---|
50 | result = new Result(PopulationGraphParameterName, new GenealogyGraph<IGenealogyGraphNode>());
|
---|
51 | Results.Add(result);
|
---|
52 | } else {
|
---|
53 | result = Results[PopulationGraphParameterName];
|
---|
54 | }
|
---|
55 | var graph = (GenealogyGraph<IGenealogyGraphNode>)result.Value;
|
---|
56 | return graph;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 | public EvolutionTrackingAfterCreationOperator() {
|
---|
61 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection."));
|
---|
62 | Parameters.Add(new ScopeParameter(CurrentScopeParameterName, "The current scope from which sub-scopes should be selected."));
|
---|
63 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of elapsed generations."));
|
---|
64 | Parameters.Add(new ScopeTreeLookupParameter<IItem>("Parents", "The parent individuals which should be crossed."));
|
---|
65 | Parameters.Add(new LookupParameter<IItem>("Child", "The child individual resulting from the crossover."));
|
---|
66 | }
|
---|
67 | private EvolutionTrackingAfterCreationOperator(EvolutionTrackingAfterCreationOperator original, Cloner cloner)
|
---|
68 | : base(original, cloner) {
|
---|
69 | }
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new EvolutionTrackingAfterCreationOperator(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IOperation Apply() {
|
---|
75 | var individuals = CurrentScope.SubScopes.Select(s => s.Variables.First().Value).ToList();
|
---|
76 | foreach (var ind in individuals) {
|
---|
77 | var graphNode = new GenealogyGraphNode {
|
---|
78 | Rank = 0
|
---|
79 | };
|
---|
80 | GenealogyGraph.AddVertex(graphNode);
|
---|
81 | }
|
---|
82 | return base.Apply();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|