Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/MultiGQAPCrossover.cs @ 15504

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

#1614: refactored code

  • change problem to derive from basic problem
  • using a combined instance class instead of individual parameters
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
35  [Item("MultiGQAPCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
36  [StorableClass]
37  public class MultiGQAPCrossover : StochasticMultiBranch<IGQAPCrossover>, IGQAPCrossover, IProblemInstanceAwareGQAPOperator, 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<IntegerVector> ParentsParameter {
46      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Parents"]; }
47    }
48    public ILookupParameter<IntegerVector> ChildParameter {
49      get { return (ILookupParameter<IntegerVector>)Parameters["Child"]; }
50    }
51    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
52      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
53    }
54
55    [StorableConstructor]
56    protected MultiGQAPCrossover(bool deserializing) : base(deserializing) { }
57    protected MultiGQAPCrossover(MultiGQAPCrossover original, Cloner cloner) : base(original, cloner) { }
58    public MultiGQAPCrossover()
59      : base() {
60      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Parents", "The parent vectors which should be crossed.", "Assignment"));
61      Parameters.Add(new LookupParameter<IntegerVector>("Child", "The child vector resulting from the crossover.", "Assignment"));
62      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
63
64      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IGQAPCrossover))) {
65        if (!typeof(MultiOperator<IGQAPCrossover>).IsAssignableFrom(type))
66          Operators.Add((IGQAPCrossover)Activator.CreateInstance(type), true);
67      }
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new MultiGQAPCrossover(this, cloner);
72    }
73
74    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
75      base.Operators_ItemsAdded(sender, e);
76      Parameterize();
77    }
78    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
79      base.Operators_ItemsRemoved(sender, e);
80      Parameterize();
81    }
82
83    private void Parameterize() {
84      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
85        op.ParentsParameter.ActualName = ParentsParameter.Name;
86        op.ParentsParameter.Hidden = true;
87        op.ChildParameter.ActualName = ChildParameter.Name;
88        op.ChildParameter.Hidden = true;
89      }
90      foreach (var op in Operators.OfType<IStochasticOperator>()) {
91        op.RandomParameter.ActualName = RandomParameter.Name;
92        op.RandomParameter.Hidden = true;
93      }
94      foreach (var op in Operators.OfType<IProblemInstanceAwareGQAPOperator>()) {
95        op.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
96        op.ProblemInstanceParameter.Hidden = true;
97      }
98    }
99
100    public override IOperation InstrumentedApply() {
101      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one crossover to choose from.");
102      return base.InstrumentedApply();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.