Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7437 was 7437, checked in by abeham, 12 years ago

#1614

  • Allowed to view the solution of a certain point in the pareto chart
  • Added multi crossovers and manipulators
  • Added population diversity analyzer
  • Fixed a few bugs
File size: 3.9 KB
Line 
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
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 {
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 Apply() {
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}
Note: See TracBrowser for help on using the repository browser.