Changeset 11575
- Timestamp:
- 11/24/14 17:08:24 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryEncoding.cs
r11559 r11575 116 116 ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>()); 117 117 ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>()); 118 ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>()); 118 119 ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()); 119 120 } … … 142 143 } 143 144 } 145 private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) { 146 foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) { 147 oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove"; 148 } 149 } 144 150 private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) { 145 151 foreach (var shakingOperator in shakingOperators) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs
r11559 r11575 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Core; 24 27 using HeuristicLab.Data; 25 28 using HeuristicLab.Encodings.PermutationEncoding; 29 using HeuristicLab.Encodings.RealVectorEncoding; 26 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.PluginInfrastructure; 27 32 28 33 namespace HeuristicLab.Problems.Programmable { 29 34 [Item("PermutationEncoding", "Describes a permutation encoding.")] 30 35 [StorableClass] 31 public class PermutationEncoding : Encoding<IPermutationCreator> { 36 public sealed class PermutationEncoding : Encoding<IPermutationCreator> { 37 #region encoding parameters 32 38 [Storable] 33 private I ntValue length;34 public I ntValue Length{35 get { return length ; }39 private IFixedValueParameter<IntValue> lengthParameter; 40 public IFixedValueParameter<IntValue> LengthParameter { 41 get { return lengthParameter; } 36 42 set { 37 if (length == value) return; 38 length = value; 39 OnParameterConfigurationChanged(); 43 if (value == null) throw new ArgumentNullException("Length parameter must not be null."); 44 if (lengthParameter == value) return; 45 lengthParameter = value; 46 OnLengthParameterChanged(); 40 47 } 41 48 } 42 49 43 50 [Storable] 44 private PermutationType type;45 public PermutationType Type{46 get { return type; }51 private IFixedValueParameter<PermutationType> permutationTypeParameter; 52 public IFixedValueParameter<PermutationType> PermutationTypeParameter { 53 get { return permutationTypeParameter; } 47 54 set { 48 if (type == value) return; 49 type = value; 50 OnParameterConfigurationChanged(); 51 } 55 if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null."); 56 if (permutationTypeParameter == value) return; 57 permutationTypeParameter = value; 58 OnPermutationTypeParameterChanged(); 59 } 60 } 61 62 public override IEnumerable<IValueParameter> Parameters { 63 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, PermutationTypeParameter }); } 64 } 65 #endregion 66 67 public int Length { 68 get { return LengthParameter.Value.Value; } 69 set { LengthParameter.Value.Value = value; } 70 } 71 72 public PermutationTypes Type { 73 get { return PermutationTypeParameter.Value.Value; } 74 set { PermutationTypeParameter.Value.Value = value; } 52 75 } 53 76 54 77 [StorableConstructor] 55 protected PermutationEncoding(bool deserializing) : base(deserializing) { } 56 protected PermutationEncoding(PermutationEncoding original, Cloner cloner) 78 private PermutationEncoding(bool deserializing) : base(deserializing) { } 79 [StorableHook(HookType.AfterDeserialization)] 80 private void AfterDeserialization() { 81 RegisterParameterEvents(); 82 DiscoverOperators(); 83 } 84 85 public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); } 86 private PermutationEncoding(PermutationEncoding original, Cloner cloner) 57 87 : base(original, cloner) { 58 length = cloner.Clone(original.length); 59 type = cloner.Clone(original.type); 88 lengthParameter = cloner.Clone(original.lengthParameter); 89 permutationTypeParameter = cloner.Clone(original.permutationTypeParameter); 90 RegisterParameterEvents(); 60 91 } 61 92 public PermutationEncoding(string name, int length, PermutationTypes type) 62 93 : base(name) { 63 this.length = new IntValue(length); 64 this.type = new PermutationType(type); 65 } 66 67 public override IDeepCloneable Clone(Cloner cloner) { 68 return new PermutationEncoding(this, cloner); 69 } 70 94 Length = length; 95 Type = type; 96 RegisterParameterEvents(); 97 DiscoverOperators(); 98 } 99 100 private void OnLengthParameterChanged() { 101 RegisterParameterEvents(); 102 ConfigureOperators(Operators); 103 } 104 105 private void OnPermutationTypeParameterChanged() { 106 RegisterParameterEvents(); 107 ConfigureOperators(Operators); 108 } 109 110 private void RegisterParameterEvents() { 111 RegisterLengthParameterEvents(); 112 RegisterPermutationTypeParameterEvents(); 113 } 114 private void RegisterLengthParameterEvents() { 115 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 116 } 117 private void RegisterPermutationTypeParameterEvents() { 118 PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 119 } 120 121 #region Operator Discovery 122 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 123 static PermutationEncoding() { 124 encodingSpecificOperatorTypes = new List<Type>() { 125 typeof (IPermutationOperator), 126 typeof (IPermutationCreator), 127 typeof (IPermutationCrossover), 128 typeof (IPermutationManipulator), 129 typeof (IPermutationMultiNeighborhoodShakingOperator), 130 typeof (IPermutationMoveOperator), 131 typeof (IPermutationInversionMoveOperator), 132 typeof (IPermutationScrambleMoveOperator), 133 typeof (IPermutationSwap2MoveOperator), 134 typeof (IPermutationTranslocationMoveOperator) 135 }; 136 } 137 private void DiscoverOperators() { 138 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 139 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 140 var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList(); 141 142 ConfigureOperators(newOperators); 143 encodingOperators.AddRange(newOperators); 144 } 145 #endregion 146 147 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 148 base.ConfigureOperators(operators); 149 ConfigureCreators(Operators.OfType<IPermutationCreator>()); 150 ConfigureCrossovers(Operators.OfType<IPermutationCrossover>()); 151 ConfigureManipulators(Operators.OfType<IPermutationManipulator>()); 152 ConfigureShakingOperators(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()); 153 ConfigureMoveOperators(Operators.OfType<IPermutationMoveOperator>()); 154 ConfigureInversionMoveOperators(Operators.OfType<IPermutationInversionMoveOperator>()); 155 ConfigureScrambleMoveOperators(Operators.OfType<IPermutationScrambleMoveOperator>()); 156 ConfigureSwap2MoveOperators(Operators.OfType<IPermutationSwap2MoveOperator>()); 157 ConfigureTranslocationMoveOperators(Operators.OfType<IPermutationTranslocationMoveOperator>()); 158 } 159 160 #region specific operator wiring 161 private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) { 162 foreach (var creator in creators) { 163 creator.LengthParameter.ActualName = LengthParameter.Name; 164 creator.PermutationParameter.ActualName = Name; 165 creator.PermutationTypeParameter.Value.Value = Type; 166 } 167 } 168 private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) { 169 foreach (var crossover in crossovers) { 170 crossover.ChildParameter.ActualName = Name; 171 crossover.ParentsParameter.ActualName = Name; 172 } 173 } 174 private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) { 175 foreach (var manipulator in manipulators) { 176 manipulator.PermutationParameter.ActualName = Name; 177 } 178 } 179 private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) { 180 foreach (var shakingOperator in shakingOperators) { 181 shakingOperator.PermutationParameter.ActualName = Name; 182 } 183 } 184 private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) { 185 foreach (var moveOperator in moveOperators) { 186 moveOperator.PermutationParameter.ActualName = Name; 187 } 188 } 189 private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) { 190 foreach (var inversionMoveOperator in inversionMoveOperators) { 191 inversionMoveOperator.InversionMoveParameter.ActualName = Name + "_InversionMove"; 192 } 193 } 194 private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) { 195 foreach (var scrambleMoveOperator in scrambleMoveOperators) { 196 scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + "_ScambleMove"; 197 } 198 } 199 private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) { 200 foreach (var swap2MoveOperator in swap2MoveOperators) { 201 swap2MoveOperator.Swap2MoveParameter.ActualName = Name + "_Swap2Move"; 202 } 203 } 204 private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) { 205 foreach (var translocationMoveOperator in translocationMoveOperators) { 206 translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + "_TranslocationMove"; 207 } 208 } 209 210 #endregion 71 211 } 72 212 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r11561 r11575 211 211 var permEnc = encoding as PermutationEncoding; 212 212 if (permEnc != null) { 213 var l = new ValueParameter<IntValue>(permEnc.Name + "Length", permEnc.Length);213 var l = new ValueParameter<IntValue>(permEnc.Name + "Length", new IntValue(permEnc.Length)); 214 214 DynamicEncodingParameters.Add(l); 215 215 … … 217 217 creator.PermutationParameter.ActualName = permEnc.Name; 218 218 creator.LengthParameter.ActualName = l.Name; 219 creator.PermutationTypeParameter.Value = permEnc.Type;219 creator.PermutationTypeParameter.Value = new PermutationType(permEnc.Type); 220 220 return creator; 221 221 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11561 r11575 205 205 if (intEnc != null) { 206 206 DynamicEncodingParameters.AddRange(intEnc.Parameters); 207 return intEnc.DefaultSolutionCreator; 207 return intEnc.SolutionCreator; 208 } 209 #endregion 210 #region Configure Permutation Creator 211 var permEnc = encoding as PermutationEncoding; 212 if (permEnc != null) { 213 DynamicEncodingParameters.AddRange(permEnc.Parameters); 214 return permEnc.SolutionCreator; 208 215 } 209 216 #endregion … … 219 226 } 220 227 #endregion 221 #region Configure Permutation Creator222 var permEnc = encoding as PermutationEncoding;223 if (permEnc != null) {224 var l = new ValueParameter<IntValue>(permEnc.Name + "Length", permEnc.Length);225 DynamicEncodingParameters.Add(l);226 227 var creator = new RandomPermutationCreator();228 creator.PermutationParameter.ActualName = permEnc.Name;229 creator.LengthParameter.ActualName = l.Name;230 creator.PermutationTypeParameter.Value = permEnc.Type;231 return creator;232 }233 #endregion234 228 throw new ArgumentException(string.Format("Encoding {0} is unknown.", encoding != null ? encoding.GetType().FullName : "(null)")); 235 229 } 236 230 237 238 private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator {239 return ApplicationManager.Manager.GetInstances<T>()240 .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>());241 }242 231 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) { 243 232 // remove all multiencoding operators … … 283 272 #endregion 284 273 #region Configure Operators for PermutationEncoding 285 var permCreator = newCreator as IPermutationCreator; 286 if (permCreator != null) { 287 var paramName = permCreator.PermutationParameter.ActualName; 274 var permEncoding = encoding as PermutationEncoding; 275 if (permEncoding != null) { 288 276 // do not replace a permutation creator that was manually set 289 277 if (!(SolutionCreator is IPermutationCreator) 290 || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != perm Creator.PermutationParameter.ActualName) {278 || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permEncoding.Name) { 291 279 Operators.Remove(SolutionCreator); 292 280 SolutionCreator = newCreator; 293 Operators.Add(SolutionCreator); 294 } 295 296 #region Wire Permutation Crossovers 297 var crossovers = Operators.OfType<IPermutationCrossover>() 298 .Union(ApplicationManager.Manager.GetInstances<IPermutationCrossover>(), new TypeEqualityComparer<IPermutationCrossover>()) 299 .ToList(); 300 foreach (var xo in crossovers) { 301 xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName; 302 xo.ChildParameter.Hidden = true; 303 xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName; 304 xo.ParentsParameter.Hidden = true; 305 } 306 Operators.AddRange(crossovers.Except(Operators.OfType<IPermutationCrossover>())); 307 #endregion 308 #region Wire Permutation Manipulators 309 var manipulators = Operators.OfType<IPermutationManipulator>() 310 .Union(ApplicationManager.Manager.GetInstances<IPermutationManipulator>(), new TypeEqualityComparer<IPermutationManipulator>()) 311 .ToList(); 312 foreach (var m in manipulators) { 313 m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName; 314 m.PermutationParameter.Hidden = true; 315 } 316 Operators.AddRange(manipulators.Except(Operators.OfType<IPermutationManipulator>())); 317 #endregion 318 #region Wire Permutation ShakingOperators 319 var shakingOperators = Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>() 320 .Union(ApplicationManager.Manager.GetInstances<IPermutationMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IPermutationMultiNeighborhoodShakingOperator>()) 321 .ToList(); 322 foreach (var op in shakingOperators) { 323 op.PermutationParameter.ActualName = paramName; 324 op.PermutationParameter.Hidden = true; 325 } 326 Operators.AddRange(shakingOperators.Except(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())); 327 #endregion 281 } 328 282 } else { 329 283 Operators.RemoveAll(x => x is IPermutationCrossover
Note: See TracChangeset
for help on using the changeset viewer.