[12286] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12286] | 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[12286] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
| 35 | [Item("Multi LLE Crossover", "Randomly selects and applies one of its crossovers every time it is called.")]
|
---|
[16565] | 36 | [StorableType("276A73EA-3642-4B8F-9CD4-74C1B2772728")]
|
---|
[12286] | 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]
|
---|
[16565] | 54 | protected MultiLinearLinkageCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[12286] | 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 |
|
---|
[12701] | 63 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(ILinearLinkageCrossover), typeof(LinearLinkageEncoding).Assembly)) {
|
---|
[12286] | 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 | }
|
---|