1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace 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 | public override bool CanChangeName {
|
---|
39 | get { return false; }
|
---|
40 | }
|
---|
41 | protected override bool CreateChildOperation {
|
---|
42 | get { return true; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
46 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected MultiGQAPManipulator(bool deserializing) : base(deserializing) { }
|
---|
51 | protected MultiGQAPManipulator(MultiGQAPManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public MultiGQAPManipulator()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
|
---|
55 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IGQAPManipulator))) {
|
---|
56 | if (!typeof(MultiOperator<IGQAPManipulator>).IsAssignableFrom(type))
|
---|
57 | Operators.Add((IGQAPManipulator)Activator.CreateInstance(type), true);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new MultiGQAPManipulator(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPManipulator>> e) {
|
---|
66 | base.Operators_ItemsAdded(sender, e);
|
---|
67 | Parameterize();
|
---|
68 | }
|
---|
69 | protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPManipulator>> e) {
|
---|
70 | base.Operators_ItemsRemoved(sender, e);
|
---|
71 | Parameterize();
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void Parameterize() {
|
---|
75 | foreach (var op in Operators.OfType<IGQAPManipulator>())
|
---|
76 | op.AssignmentParameter.ActualName = AssignmentParameter.Name;
|
---|
77 | foreach (var op in Operators.OfType<IStochasticOperator>())
|
---|
78 | op.RandomParameter.ActualName = RandomParameter.Name;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IOperation InstrumentedApply() {
|
---|
82 | if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one manipulator to choose from.");
|
---|
83 | return base.Apply();
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|