Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/MultiPermutationManipulator.cs @ 4501

Last change on this file since 4501 was 4373, checked in by swinkler, 14 years ago

Corrected comment and description in MultiPermutationManipulator and PathTSPTourView. (#1182)

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Encodings.PermutationEncoding {
33  [Item("MultiPermutationManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
34  [StorableClass]
35  public class MultiPermutationManipulator : StochasticMultiBranch<IPermutationManipulator>, IPermutationManipulator, IStochasticOperator {
36    public override bool CanChangeName {
37      get { return false; }
38    }
39    protected override bool CreateChildOperation {
40      get { return true; }
41    }
42
43    public ILookupParameter<Permutation> PermutationParameter {
44      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
45    }
46
47    [StorableConstructor]
48    private MultiPermutationManipulator(bool deserializing) : base(deserializing) { }
49    public MultiPermutationManipulator()
50      : base() {
51      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that is being manipulated."));
52
53      foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IPermutationManipulator))) {
54        if (!typeof(MultiOperator<IPermutationManipulator>).IsAssignableFrom(type))
55          Operators.Add((IPermutationManipulator)Activator.CreateInstance(type), true);
56      }
57    }
58
59    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
60      base.Operators_ItemsReplaced(sender, e);
61      ParameterizeManipulators();
62    }
63
64    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
65      base.Operators_ItemsAdded(sender, e);
66      ParameterizeManipulators();
67    }
68
69    private void ParameterizeManipulators() {
70      foreach (IPermutationManipulator manipulator in Operators.OfType<IPermutationManipulator>()) {
71        manipulator.PermutationParameter.ActualName = PermutationParameter.Name;
72      }
73      foreach (IStochasticOperator op in Operators.OfType<IStochasticOperator>()) {
74        op.RandomParameter.ActualName = RandomParameter.Name;
75      }
76    }
77
78    public override IOperation Apply() {
79      if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one permutation manipulator to choose from.");
80      return base.Apply();
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.