- Timestamp:
- 11/15/14 17:30:02 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryEncoding.cs
r11484 r11553 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; 28 using HeuristicLab.Encodings.BinaryVectorEncoding; 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Parameters; 25 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.PluginInfrastructure; 26 33 27 34 namespace HeuristicLab.Problems.Programmable { 28 35 [Item("BinaryEncoding", "Describes a binary vector encoding.")] 29 36 [StorableClass] 30 public class BinaryEncoding : Encoding { 37 public sealed class BinaryEncoding : Encoding { 38 #region Encoding Parameters 31 39 [Storable] 32 private I ntValue length;33 public I ntValue Length{34 get { return length ; }40 private IFixedValueParameter<IntValue> lengthParameter; 41 public IFixedValueParameter<IntValue> LengthParameter { 42 get { return lengthParameter; } 35 43 set { 36 if (length == value) return; 37 length = value; 38 OnParameterConfigurationChanged(); 44 if (value == null) throw new ArgumentNullException("Length parameter must not be null."); 45 if (lengthParameter == value) return; 46 lengthParameter = value; 47 OnLengthParameterChanged(); 39 48 } 49 } 50 public override IEnumerable<IValueParameter> Parameters { 51 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter }); } 52 } 53 #endregion 54 55 private List<IOperator> encodingOperators = new List<IOperator>(); 56 [Storable] 57 public override IEnumerable<IOperator> Operators { 58 get { return encodingOperators; } 59 protected set { encodingOperators = new List<IOperator>(value); } 60 } 61 62 public override ISolutionCreator DefaultSolutionCreator { 63 get { return Operators.OfType<RandomBinaryVectorCreator>().First(); } 64 } 65 66 public int Length { 67 get { return LengthParameter.Value.Value; } 68 set { LengthParameter.Value.Value = value; } 40 69 } 41 70 42 71 [StorableConstructor] 43 protected BinaryEncoding(bool deserializing) : base(deserializing) { } 44 protected BinaryEncoding(BinaryEncoding original, Cloner cloner) 72 private BinaryEncoding(bool deserializing) : base(deserializing) { } 73 [StorableHook(HookType.AfterDeserialization)] 74 private void AfterDeserialization() { 75 RegisterParameterEvents(); 76 DiscoverOperators(); 77 } 78 public override IDeepCloneable Clone(Cloner cloner) { return new BinaryEncoding(this, cloner); } 79 private BinaryEncoding(BinaryEncoding original, Cloner cloner) 45 80 : base(original, cloner) { 46 length = cloner.Clone(original.length); 81 lengthParameter = cloner.Clone(original.lengthParameter); 82 encodingOperators = original.Operators.Select(cloner.Clone).ToList(); 83 RegisterParameterEvents(); 47 84 } 48 85 public BinaryEncoding(string name, int length) 49 86 : base(name) { 50 this.length = new IntValue(length);87 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 51 88 } 52 89 53 public override IDeepCloneable Clone(Cloner cloner) { 54 return new BinaryEncoding(this, cloner); 90 private void OnLengthParameterChanged() { 91 RegisterLengthParameterEvents(); 92 ConfigureOperators(Operators); 93 } 94 private void RegisterParameterEvents() { 95 RegisterLengthParameterEvents(); 96 } 97 private void RegisterLengthParameterEvents() { 98 LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 99 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 55 100 } 56 101 102 #region Operator Discovery 103 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 104 static BinaryEncoding() { 105 encodingSpecificOperatorTypes = new List<Type>() { 106 typeof (IBinaryVectorOperator), 107 typeof (IBinaryVectorCreator), 108 typeof (IBinaryVectorCrossover), 109 typeof (IBinaryVectorManipulator), 110 typeof (IBinaryVectorMoveOperator), 111 typeof (IBinaryVectorMultiNeighborhoodShakingOperator), 112 }; 113 } 114 private void DiscoverOperators() { 115 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IBinaryVectorOperator)); 116 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 117 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 118 var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList(); 119 120 ConfigureOperators(newOperators); 121 encodingOperators.AddRange(newOperators); 122 } 123 #endregion 124 125 public void ConfigureOperators(IEnumerable<IOperator> operators) { 126 ConfigureCreators(operators.OfType<IBinaryVectorCreator>()); 127 ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>()); 128 ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>()); 129 ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>()); 130 ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()); 131 } 132 133 #region Specific Operator Wiring 134 private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) { 135 foreach (var creator in creators) { 136 creator.BinaryVectorParameter.ActualName = Name; 137 creator.LengthParameter.ActualName = LengthParameter.Name; 138 } 139 } 140 private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) { 141 foreach (var crossover in crossovers) { 142 crossover.ParentsParameter.ActualName = Name; 143 crossover.ChildParameter.ActualName = Name; 144 } 145 } 146 private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) { 147 foreach (var manipulator in manipulators) { 148 manipulator.BinaryVectorParameter.ActualName = Name; 149 } 150 } 151 private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) { 152 foreach (var moveOperator in moveOperators) { 153 moveOperator.BinaryVectorParameter.ActualName = Name; 154 } 155 } 156 private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) { 157 foreach (var shakingOperator in shakingOperators) { 158 shakingOperator.BinaryVectorParameter.ActualName = Name; 159 } 160 } 161 #endregion 57 162 } 58 163 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11550 r11553 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 35 36 } 36 37 37 [StorableConstructor]38 protected Encoding(bool deserializing) : base(deserializing) { }39 protected Encoding(Encoding original, Cloner cloner)40 : base(original, cloner) { }41 protected Encoding(string name) : base(name) { }42 43 38 public virtual IEnumerable<IValueParameter> Parameters { 44 39 get { return Enumerable.Empty<IValueParameter>(); } … … 50 45 } 51 46 47 public virtual ISolutionCreator DefaultSolutionCreator { 48 get { return null; } 49 } 50 51 [StorableConstructor] 52 protected Encoding(bool deserializing) : base(deserializing) { } 53 protected Encoding(Encoding original, Cloner cloner) 54 : base(original, cloner) { } 55 protected Encoding(string name) : base(name) { } 56 52 57 public event EventHandler ParameterConfigurationChanged; 53 58 protected virtual void OnParameterConfigurationChanged() { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11550 r11553 76 76 } 77 77 78 public IRealVectorCreator DefaultSolutionCreator {78 public override ISolutionCreator DefaultSolutionCreator { 79 79 get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); } 80 80 } … … 166 166 static RealEncoding() { 167 167 encodingSpecificOperatorTypes = new List<Type>() { 168 typeof (IRealVectorOperator), 168 169 typeof (IRealVectorCreator), 169 170 typeof (IRealVectorCrossover),
Note: See TracChangeset
for help on using the changeset viewer.