Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs @ 11892

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

#2174:

  • Branched ExternalEvaluation
    • Changed to use SingleObjectiveBasicProblem
    • Increased minor version number
  • Created view for MultiEncoding
  • Created dialog to construct encodings in the GUI

Setting up an external evaluation problem in HeuristicLab has finally become simple.

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