Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationEncoding.cs @ 14554

Last change on this file since 14554 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
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11484]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
[11575]22using System;
23using System.Collections.Generic;
24using System.Linq;
[11484]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[11949]28using HeuristicLab.Optimization;
[11588]29using HeuristicLab.Parameters;
[11484]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[11575]31using HeuristicLab.PluginInfrastructure;
[11484]32
[11949]33namespace HeuristicLab.Encodings.PermutationEncoding {
[11484]34  [Item("PermutationEncoding", "Describes a permutation encoding.")]
35  [StorableClass]
[13366]36  public sealed class PermutationEncoding : Encoding<Permutation> {
[11575]37    #region encoding parameters
[11598]38    [Storable]
[11575]39    private IFixedValueParameter<IntValue> lengthParameter;
40    public IFixedValueParameter<IntValue> LengthParameter {
41      get { return lengthParameter; }
[11484]42      set {
[11575]43        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
[11588]44        if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
[11575]45        if (lengthParameter == value) return;
[11588]46
47        if (lengthParameter != null) Parameters.Remove(lengthParameter);
[11575]48        lengthParameter = value;
[11588]49        Parameters.Add(lengthParameter);
[11575]50        OnLengthParameterChanged();
[11484]51      }
52    }
53
[11598]54    [Storable]
[11575]55    private IFixedValueParameter<PermutationType> permutationTypeParameter;
56    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
57      get { return permutationTypeParameter; }
[11484]58      set {
[11575]59        if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
[11588]60        if (value.Value == null) throw new ArgumentNullException("Permutation type parameter value must not be null.");
[11575]61        if (permutationTypeParameter == value) return;
[11588]62
63        if (permutationTypeParameter != null) Parameters.Remove(permutationTypeParameter);
[11575]64        permutationTypeParameter = value;
[11588]65        Parameters.Add(permutationTypeParameter);
[11575]66        OnPermutationTypeParameterChanged();
[11484]67      }
68    }
[11575]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
[11484]81    [StorableConstructor]
[11575]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)
[11484]91      : base(original, cloner) {
[11575]92      lengthParameter = cloner.Clone(original.lengthParameter);
93      permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
94      RegisterParameterEvents();
[11484]95    }
[11587]96
[11739]97
98    public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
[11892]99    public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
[12015]100    public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
[11484]101    public PermutationEncoding(string name, int length, PermutationTypes type)
102      : base(name) {
[11593]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);
[11587]107
108      SolutionCreator = new RandomPermutationCreator();
[11575]109      RegisterParameterEvents();
110      DiscoverOperators();
[11484]111    }
112
[11575]113    private void OnLengthParameterChanged() {
[11588]114      RegisterLengthParameterEvents();
[11575]115      ConfigureOperators(Operators);
[11484]116    }
117
[11575]118    private void OnPermutationTypeParameterChanged() {
[11588]119      RegisterPermutationTypeParameterEvents();
[11575]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),                   
[13396]147          typeof (IPermutationTranslocationMoveOperator),
148          typeof (IPermutationLocalImprovementOperator),
149          typeof (IPermutationSolutionOperator),
150          typeof (IPermutationSolutionsOperator),
[11575]151      };
152    }
153    private void DiscoverOperators() {
[11773]154      var assembly = typeof(IPermutationOperator).Assembly;
155      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
[11575]156      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
[11587]157      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
[11575]158
159      ConfigureOperators(newOperators);
[11587]160      foreach (var @operator in newOperators)
[11952]161        AddOperator(@operator);
[11575]162    }
163    #endregion
164
[13396]165    public override void ConfigureOperators(IEnumerable<IItem> operators) {
[11746]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>());
[13396]175      ConfigureLocalImprovementOperators(operators.OfType<IPermutationLocalImprovementOperator>());
176      ConfigureSolutionOperators(operators.OfType<IPermutationSolutionOperator>());
177      ConfigureSolutionsOperators(operators.OfType<IPermutationSolutionsOperator>());
[11575]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) {
[13396]194      // IPermutationManipulator does not contain additional parameters (already contained in IPermutationSolutionOperator)
[11575]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) {
[11593]208        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
[11575]209      }
210    }
211    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
212      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
[12015]213        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
[11575]214      }
215    }
216    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
217      foreach (var swap2MoveOperator in swap2MoveOperators) {
[11593]218        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
[11575]219      }
220    }
221    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
222      foreach (var translocationMoveOperator in translocationMoveOperators) {
[11593]223        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
[11575]224      }
225    }
[13396]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    }
[11575]239    #endregion
[11484]240  }
241}
Note: See TracBrowser for help on using the repository browser.