Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Crossovers/MultiLLECrossover.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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