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("MultiGQAPCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class MultiGQAPCrossover : StochasticMultiBranch<IGQAPCrossover>, IGQAPCrossover {
|
---|
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 |
|
---|
52 | [StorableConstructor]
|
---|
53 | protected MultiGQAPCrossover(bool deserializing) : base(deserializing) { }
|
---|
54 | protected MultiGQAPCrossover(MultiGQAPCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
55 | public MultiGQAPCrossover()
|
---|
56 | : base() {
|
---|
57 | Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Parents", "The parent vectors which should be crossed.", "Assignment"));
|
---|
58 | Parameters.Add(new LookupParameter<IntegerVector>("Child", "The child vector resulting from the crossover.", "Assignment"));
|
---|
59 | foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IGQAPCrossover))) {
|
---|
60 | if (!typeof(MultiOperator<IGQAPCrossover>).IsAssignableFrom(type))
|
---|
61 | Operators.Add((IGQAPCrossover)Activator.CreateInstance(type), true);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new MultiGQAPCrossover(this, cloner);
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
|
---|
70 | base.Operators_ItemsAdded(sender, e);
|
---|
71 | Parameterize();
|
---|
72 | }
|
---|
73 | protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IGQAPCrossover>> e) {
|
---|
74 | base.Operators_ItemsRemoved(sender, e);
|
---|
75 | Parameterize();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void Parameterize() {
|
---|
79 | foreach (var op in Operators.OfType<IGQAPCrossover>()) {
|
---|
80 | op.ParentsParameter.ActualName = ParentsParameter.Name;
|
---|
81 | op.ChildParameter.ActualName = ChildParameter.Name;
|
---|
82 | }
|
---|
83 | foreach (var op in Operators.OfType<IStochasticOperator>()) {
|
---|
84 | op.RandomParameter.ActualName = RandomParameter.Name;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override IOperation InstrumentedApply() {
|
---|
89 | if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one crossover to choose from.");
|
---|
90 | return base.Apply();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|