Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/MultiGQAPCrossover.cs

Last change on this file was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 4.6 KB
RevLine 
[7437]1#region License Information
2/* HeuristicLab
[16728]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7437]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.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
[16728]33using HEAL.Attic;
[7437]34
35namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
36  [Item("MultiGQAPCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
[16728]37  [StorableType("C8AA967E-11CF-48A0-BEC2-71A9ABEB1801")]
[15504]38  public class MultiGQAPCrossover : StochasticMultiBranch<IGQAPCrossover>, IGQAPCrossover, IProblemInstanceAwareGQAPOperator, IStochasticOperator {
[7437]39    public override bool CanChangeName {
40      get { return false; }
41    }
42    protected override bool CreateChildOperation {
43      get { return true; }
44    }
45
46    public IScopeTreeLookupParameter<IntegerVector> ParentsParameter {
47      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Parents"]; }
48    }
49    public ILookupParameter<IntegerVector> ChildParameter {
50      get { return (ILookupParameter<IntegerVector>)Parameters["Child"]; }
51    }
[15504]52    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
53      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
54    }
[7437]55
56    [StorableConstructor]
[16728]57    protected MultiGQAPCrossover(StorableConstructorFlag _) : base(_) { }
[7437]58    protected MultiGQAPCrossover(MultiGQAPCrossover original, Cloner cloner) : base(original, cloner) { }
59    public MultiGQAPCrossover()
60      : base() {
61      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Parents", "The parent vectors which should be crossed.", "Assignment"));
62      Parameters.Add(new LookupParameter<IntegerVector>("Child", "The child vector resulting from the crossover.", "Assignment"));
[15504]63      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
64
[7437]65      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IGQAPCrossover))) {
66        if (!typeof(MultiOperator<IGQAPCrossover>).IsAssignableFrom(type))
67          Operators.Add((IGQAPCrossover)Activator.CreateInstance(type), true);
68      }
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new MultiGQAPCrossover(this, cloner);
73    }
74
75    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
76      base.Operators_ItemsAdded(sender, e);
77      Parameterize();
78    }
79    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
80      base.Operators_ItemsRemoved(sender, e);
81      Parameterize();
82    }
83
84    private void Parameterize() {
85      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
86        op.ParentsParameter.ActualName = ParentsParameter.Name;
[15504]87        op.ParentsParameter.Hidden = true;
[7437]88        op.ChildParameter.ActualName = ChildParameter.Name;
[15504]89        op.ChildParameter.Hidden = true;
[7437]90      }
91      foreach (var op in Operators.OfType<IStochasticOperator>()) {
92        op.RandomParameter.ActualName = RandomParameter.Name;
[15504]93        op.RandomParameter.Hidden = true;
[7437]94      }
[15504]95      foreach (var op in Operators.OfType<IProblemInstanceAwareGQAPOperator>()) {
96        op.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
97        op.ProblemInstanceParameter.Hidden = true;
98      }
[7437]99    }
100
[11505]101    public override IOperation InstrumentedApply() {
[7437]102      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one crossover to choose from.");
[15504]103      return base.InstrumentedApply();
[7437]104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.