source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/MultiGQAPManipulator.cs @ 16077

Last change on this file since 16077 was 16077, checked in by abeham, 12 months ago

#2936: Added integration branch

File size: 4.2 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;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
35  [Item("MultiGQAPManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
36  [StorableClass]
37  public class MultiGQAPManipulator : StochasticMultiBranch<IGQAPManipulator>, IGQAPManipulator,
38    IProblemInstanceAwareGQAPOperator {
39    public override bool CanChangeName {
40      get { return false; }
41    }
42    protected override bool CreateChildOperation {
43      get { return true; }
44    }
45
46    public ILookupParameter<IntegerVector> AssignmentParameter {
47      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
48    }
49    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
50      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
51    }
52
53    [StorableConstructor]
54    protected MultiGQAPManipulator(bool deserializing) : base(deserializing) { }
55    protected MultiGQAPManipulator(MultiGQAPManipulator original, Cloner cloner) : base(original, cloner) { }
56    public MultiGQAPManipulator()
57      : base() {
58      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
59      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
60
61      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IGQAPManipulator))) {
62        if (!typeof(MultiOperator<IGQAPManipulator>).IsAssignableFrom(type))
63          Operators.Add((IGQAPManipulator)Activator.CreateInstance(type), true);
64      }
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new MultiGQAPManipulator(this, cloner);
69    }
70
71    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPManipulator>> e) {
72      base.Operators_ItemsAdded(sender, e);
73      Parameterize();
74    }
75    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPManipulator>> e) {
76      base.Operators_ItemsRemoved(sender, e);
77      Parameterize();
78    }
79
80    private void Parameterize() {
81      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
82        op.AssignmentParameter.ActualName = AssignmentParameter.Name;
83        op.AssignmentParameter.Hidden = true;
84      }
85      foreach (var op in Operators.OfType<IStochasticOperator>()) {
86        op.RandomParameter.ActualName = RandomParameter.Name;
87        op.RandomParameter.Hidden = true;
88      }
89      foreach (var op in Operators.OfType<IProblemInstanceAwareGQAPOperator>()) {
90        op.ProblemInstanceParameter.ActualName = ProblemInstanceParameter.Name;
91        op.ProblemInstanceParameter.Hidden = true;
92      }
93    }
94
95    public override IOperation InstrumentedApply() {
96      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one manipulator to choose from.");
97      return base.InstrumentedApply();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.