Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/MultiGQAPCrossover.cs @ 17578

Last change on this file since 17578 was 16712, checked in by gkronber, 6 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.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;
33using HEAL.Attic;
34
35namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
36  [Item("MultiGQAPCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
37  [StorableType("C8AA967E-11CF-48A0-BEC2-71A9ABEB1801")]
38  public class MultiGQAPCrossover : StochasticMultiBranch<IGQAPCrossover>, IGQAPCrossover, IProblemInstanceAwareGQAPOperator, 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<IntegerVector> ParentsParameter {
47      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Parents"]; }
48    }
49    public ILookupParameter<IntegerVector> ChildParameter {
50      get { return (ILookupParameter<IntegerVector>)Parameters["Child"]; }
51    }
52    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
53      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
54    }
55
56    [StorableConstructor]
57    protected MultiGQAPCrossover(StorableConstructorFlag _) : base(_) { }
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"));
63      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
64
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;
87        op.ParentsParameter.Hidden = true;
88        op.ChildParameter.ActualName = ChildParameter.Name;
89        op.ChildParameter.Hidden = true;
90      }
91      foreach (var op in Operators.OfType<IStochasticOperator>()) {
92        op.RandomParameter.ActualName = RandomParameter.Name;
93        op.RandomParameter.Hidden = true;
94      }
95      foreach (var op in Operators.OfType<IProblemInstanceAwareGQAPOperator>()) {
96        op.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
97        op.ProblemInstanceParameter.Hidden = true;
98      }
99    }
100
101    public override IOperation InstrumentedApply() {
102      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one crossover to choose from.");
103      return base.InstrumentedApply();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.