Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/MultiLLECrossover.cs @ 12737

Last change on this file since 12737 was 12737, checked in by abeham, 9 years ago

#2319: merged 12650,12701 to stable

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.LinearLinkageEncoding {
36  [Item("Multi LLE Crossover", "Randomly selects and applies one of its crossovers every time it is called.")]
37  [StorableClass]
38  public class MultiLinearLinkageCrossover : StochasticMultiBranch<ILinearLinkageCrossover>, ILinearLinkageCrossover, IStochasticOperator {
39    public override bool CanChangeName {
40      get { return false; }
41    }
42    protected override bool CreateChildOperation {
43      get { return true; }
44    }
45
46    public IScopeTreeLookupParameter<LinearLinkage> ParentsParameter {
47      get { return (IScopeTreeLookupParameter<LinearLinkage>)Parameters["Parents"]; }
48    }
49
50    public ILookupParameter<LinearLinkage> ChildParameter {
51      get { return (ILookupParameter<LinearLinkage>)Parameters["Child"]; }
52    }
53
54    [StorableConstructor]
55    protected MultiLinearLinkageCrossover(bool deserializing) : base(deserializing) { }
56    protected MultiLinearLinkageCrossover(MultiLinearLinkageCrossover original, Cloner cloner) : base(original, cloner) { }
57    public MultiLinearLinkageCrossover()
58      : base() {
59      Parameters.Add(new ScopeTreeLookupParameter<LinearLinkage>("Parents", "The parent LLE which should be crossed."));
60      ParentsParameter.ActualName = "LLE";
61      Parameters.Add(new LookupParameter<LinearLinkage>("Child", "The child LLE resulting from the crossover."));
62      ChildParameter.ActualName = "LLE";
63
64      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ILinearLinkageCrossover), typeof(LinearLinkageEncoding).Assembly)) {
65        if (!typeof(MultiOperator<ILinearLinkageCrossover>).IsAssignableFrom(type))
66          Operators.Add((ILinearLinkageCrossover)Activator.CreateInstance(type), true);
67      }
68
69      SelectedOperatorParameter.ActualName = "SelectedCrossoverOperator";
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new MultiLinearLinkageCrossover(this, cloner);
74    }
75
76    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ILinearLinkageCrossover>> e) {
77      base.Operators_ItemsReplaced(sender, e);
78      ParameterizeCrossovers(e.Items);
79    }
80
81    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<ILinearLinkageCrossover>> e) {
82      base.Operators_ItemsAdded(sender, e);
83      ParameterizeCrossovers(e.Items);
84    }
85
86    private void ParameterizeCrossovers(IEnumerable<IndexedItem<ILinearLinkageCrossover>> crossovers) {
87      foreach (var c in crossovers.Select(x => x.Value)) {
88        c.ChildParameter.ActualName = ChildParameter.Name;
89        c.ParentsParameter.ActualName = ParentsParameter.Name;
90        var stOp = c as IStochasticOperator;
91        if (stOp != null) stOp.RandomParameter.ActualName = RandomParameter.Name;
92      }
93    }
94
95    public override IOperation InstrumentedApply() {
96      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one LLE crossover to choose from.");
97      return base.InstrumentedApply();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.