Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2708_ScopedAlgorithms/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationEncoding.cs @ 17514

Last change on this file since 17514 was 13396, checked in by abeham, 9 years ago

#2521:

  • Refactored QuadraticAssignmentProblem to use new SingleObjectiveProblem
    • Removed QAPEvaluator
    • Adapted RobustTabooSearch
  • Introduced several interfaces in PermutationEncoding necessary for wiring
  • Changed all Encodings to use IItem instead of IOperator in ConfigureOperators (name still unchanged)
  • Added a protected MaximizationParameter property in SingleObjectiveProblem (necessary for wiring)
  • Changed AlleleFrequnencyAnalyzer to use ISolution interface instead of IItem
  • Added a comment to ISolutionCreator<TSolution> of some changes that would be welcomed
File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Encodings.PermutationEncoding {
34  [Item("PermutationEncoding", "Describes a permutation encoding.")]
35  [StorableClass]
36  public sealed class PermutationEncoding : Encoding<Permutation> {
37    #region encoding parameters
38    [Storable]
39    private IFixedValueParameter<IntValue> lengthParameter;
40    public IFixedValueParameter<IntValue> LengthParameter {
41      get { return lengthParameter; }
42      set {
43        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
44        if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
45        if (lengthParameter == value) return;
46
47        if (lengthParameter != null) Parameters.Remove(lengthParameter);
48        lengthParameter = value;
49        Parameters.Add(lengthParameter);
50        OnLengthParameterChanged();
51      }
52    }
53
54    [Storable]
55    private IFixedValueParameter<PermutationType> permutationTypeParameter;
56    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
57      get { return permutationTypeParameter; }
58      set {
59        if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
60        if (value.Value == null) throw new ArgumentNullException("Permutation type parameter value must not be null.");
61        if (permutationTypeParameter == value) return;
62
63        if (permutationTypeParameter != null) Parameters.Remove(permutationTypeParameter);
64        permutationTypeParameter = value;
65        Parameters.Add(permutationTypeParameter);
66        OnPermutationTypeParameterChanged();
67      }
68    }
69    #endregion
70
71    public int Length {
72      get { return LengthParameter.Value.Value; }
73      set { LengthParameter.Value.Value = value; }
74    }
75
76    public PermutationTypes Type {
77      get { return PermutationTypeParameter.Value.Value; }
78      set { PermutationTypeParameter.Value.Value = value; }
79    }
80
81    [StorableConstructor]
82    private PermutationEncoding(bool deserializing) : base(deserializing) { }
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterParameterEvents();
86      DiscoverOperators();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
90    private PermutationEncoding(PermutationEncoding original, Cloner cloner)
91      : base(original, cloner) {
92      lengthParameter = cloner.Clone(original.lengthParameter);
93      permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
94      RegisterParameterEvents();
95    }
96
97
98    public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
99    public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
100    public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
101    public PermutationEncoding(string name, int length, PermutationTypes type)
102      : base(name) {
103      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
104      permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
105      Parameters.Add(lengthParameter);
106      Parameters.Add(permutationTypeParameter);
107
108      SolutionCreator = new RandomPermutationCreator();
109      RegisterParameterEvents();
110      DiscoverOperators();
111    }
112
113    private void OnLengthParameterChanged() {
114      RegisterLengthParameterEvents();
115      ConfigureOperators(Operators);
116    }
117
118    private void OnPermutationTypeParameterChanged() {
119      RegisterPermutationTypeParameterEvents();
120      ConfigureOperators(Operators);
121    }
122
123    private void RegisterParameterEvents() {
124      RegisterLengthParameterEvents();
125      RegisterPermutationTypeParameterEvents();
126    }
127    private void RegisterLengthParameterEvents() {
128      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
129    }
130    private void RegisterPermutationTypeParameterEvents() {
131      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
132    }
133
134    #region Operator Discovery
135    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
136    static PermutationEncoding() {
137      encodingSpecificOperatorTypes = new List<Type>() {
138          typeof (IPermutationOperator),
139          typeof (IPermutationCreator),
140          typeof (IPermutationCrossover),
141          typeof (IPermutationManipulator),
142          typeof (IPermutationMultiNeighborhoodShakingOperator),
143          typeof (IPermutationMoveOperator),
144          typeof (IPermutationInversionMoveOperator),
145          typeof (IPermutationScrambleMoveOperator),
146          typeof (IPermutationSwap2MoveOperator),                   
147          typeof (IPermutationTranslocationMoveOperator),
148          typeof (IPermutationLocalImprovementOperator),
149          typeof (IPermutationSolutionOperator),
150          typeof (IPermutationSolutionsOperator),
151      };
152    }
153    private void DiscoverOperators() {
154      var assembly = typeof(IPermutationOperator).Assembly;
155      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
156      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
157      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
158
159      ConfigureOperators(newOperators);
160      foreach (var @operator in newOperators)
161        AddOperator(@operator);
162    }
163    #endregion
164
165    public override void ConfigureOperators(IEnumerable<IItem> operators) {
166      ConfigureCreators(operators.OfType<IPermutationCreator>());
167      ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
168      ConfigureManipulators(operators.OfType<IPermutationManipulator>());
169      ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
170      ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
171      ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
172      ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
173      ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
174      ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
175      ConfigureLocalImprovementOperators(operators.OfType<IPermutationLocalImprovementOperator>());
176      ConfigureSolutionOperators(operators.OfType<IPermutationSolutionOperator>());
177      ConfigureSolutionsOperators(operators.OfType<IPermutationSolutionsOperator>());
178    }
179
180    #region specific operator wiring
181    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
182      foreach (var creator in creators) {
183        creator.LengthParameter.ActualName = LengthParameter.Name;
184        creator.PermutationTypeParameter.Value.Value = Type;
185      }
186    }
187    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
188      foreach (var crossover in crossovers) {
189        crossover.ChildParameter.ActualName = Name;
190        crossover.ParentsParameter.ActualName = Name;
191      }
192    }
193    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
194      // IPermutationManipulator does not contain additional parameters (already contained in IPermutationSolutionOperator)
195    }
196    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
197      foreach (var shakingOperator in shakingOperators) {
198        shakingOperator.PermutationParameter.ActualName = Name;
199      }
200    }
201    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
202      foreach (var moveOperator in moveOperators) {
203        moveOperator.PermutationParameter.ActualName = Name;
204      }
205    }
206    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
207      foreach (var inversionMoveOperator in inversionMoveOperators) {
208        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
209      }
210    }
211    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
212      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
213        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
214      }
215    }
216    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
217      foreach (var swap2MoveOperator in swap2MoveOperators) {
218        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
219      }
220    }
221    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
222      foreach (var translocationMoveOperator in translocationMoveOperators) {
223        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
224      }
225    }
226    private void ConfigureLocalImprovementOperators(IEnumerable<IPermutationLocalImprovementOperator> localImprovementOperators) {
227      // IPermutationLocalImprovementOperator does not contain additional parameters (already contained in IPermutationSolutionOperator)
228    }
229    private void ConfigureSolutionOperators(IEnumerable<IPermutationSolutionOperator> solutionOperators) {
230      foreach (var solutionOperator in solutionOperators) {
231        solutionOperator.PermutationParameter.ActualName = Name;
232      }
233    }
234    private void ConfigureSolutionsOperators(IEnumerable<IPermutationSolutionsOperator> solutionsOperators) {
235      foreach (var solutionsOperator in solutionsOperators) {
236        solutionsOperator.PermutationsParameter.ActualName = Name;
237      }
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.