- Timestamp:
- 11/13/14 16:46:40 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11546 r11550 41 41 protected Encoding(string name) : base(name) { } 42 42 43 public virtual IEnumerable<IValueParameter> EncodingParameters {43 public virtual IEnumerable<IValueParameter> Parameters { 44 44 get { return Enumerable.Empty<IValueParameter>(); } 45 } 46 47 public virtual IEnumerable<IOperator> Operators { 48 get { return Enumerable.Empty<IOperator>(); } 49 protected set { } 45 50 } 46 51 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11546 r11550 24 24 using System.Collections.Generic; 25 25 using System.Linq; 26 using System.Runtime.CompilerServices; 26 27 using System.Text; 27 28 using HeuristicLab.Common; … … 38 39 [StorableClass] 39 40 public sealed class RealEncoding : Encoding { 40 41 #region Encoding Parameters 41 42 [Storable] 42 43 private IFixedValueParameter<IntValue> lengthParameter; … … 63 64 } 64 65 65 public override IEnumerable<IValueParameter> EncodingParameters { 66 get { return base.EncodingParameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); } 66 public override IEnumerable<IValueParameter> Parameters { 67 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); } 68 } 69 #endregion 70 71 private List<IOperator> encodingOperators = new List<IOperator>(); 72 [Storable] 73 public override IEnumerable<IOperator> Operators { 74 get { return encodingOperators; } 75 protected set { encodingOperators = new List<IOperator>(value); } 76 } 77 78 public IRealVectorCreator DefaultSolutionCreator { 79 get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); } 67 80 } 68 81 … … 76 89 } 77 90 78 public IRealVectorCreator DefaultSolutionCreator { 79 get { 80 var creator = new UniformRandomRealVectorCreator(); 81 creator.RealVectorParameter.ActualName = Name; 82 creator.LengthParameter.ActualName = LengthParameter.Name; 83 creator.BoundsParameter.ActualName = BoundsParameter.Name; 84 return creator; 85 } 86 } 91 87 92 88 93 [StorableConstructor] … … 91 96 private void AfterDeserialization() { 92 97 RegisterParameterEvents(); 98 DiscoverOperators(); 93 99 } 94 100 … … 98 104 lengthParameter = cloner.Clone(original.lengthParameter); 99 105 boundsParameter = cloner.Clone(original.boundsParameter); 106 encodingOperators = original.Operators.Select(cloner.Clone).ToList(); 100 107 RegisterParameterEvents(); 101 108 } 109 110 102 111 103 112 public RealEncoding(string name, int length, double min, double max) … … 112 121 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 113 122 RegisterParameterEvents(); 123 DiscoverOperators(); 114 124 } 115 125 … … 128 138 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 129 139 RegisterParameterEvents(); 140 DiscoverOperators(); 130 141 } 131 142 132 143 private void OnLengthParameterChanged() { 133 144 RegisterLengthParameterEvents(); 145 ConfigureOperators(Operators); 134 146 } 135 147 private void OnBoundsParameterChanged() { 136 148 RegisterBoundsParameterEvents(); 137 }138 private void RegisterParameterEvents()139 {149 ConfigureOperators(Operators); 150 } 151 private void RegisterParameterEvents() { 140 152 RegisterLengthParameterEvents(); 141 153 RegisterBoundsParameterEvents(); 142 154 } 143 155 private void RegisterLengthParameterEvents() { 144 //TODO fire correct event 145 LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>()); 146 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>()); 156 LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 157 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 147 158 } 148 159 private void RegisterBoundsParameterEvents() { 149 //TODO fire correct event150 BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());151 boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());152 } 153 154 p ublic void DiscoverOperators() {155 var types = new List<Type>()156 {160 BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 161 boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators); 162 } 163 164 #region Operator Discovery 165 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 166 static RealEncoding() { 167 encodingSpecificOperatorTypes = new List<Type>() { 157 168 typeof (IRealVectorCreator), 158 169 typeof (IRealVectorCrossover), 159 170 typeof (IRealVectorManipulator), 160 171 typeof (IRealVectorStdDevStrategyParameterOperator), 172 typeof (IRealVectorSwarmUpdater), 161 173 typeof (IRealVectorParticleCreator), 162 174 typeof (IRealVectorParticleUpdater), 163 typeof (IRealVectorParticleUpdater), 175 typeof (IRealVectorMultiNeighborhoodShakingOperator), 176 typeof (IRealVectorBoundsChecker), 177 typeof (IRealVectorMoveOperator), 178 typeof (IRealVectorMoveGenerator) 164 179 }; 165 var discoveredTypes = ApplicationManager.Manager.GetTypes(types, true, false, false); 166 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)).ToList(); 167 } 180 } 181 private void DiscoverOperators() { 182 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IRealVectorOperator)); 183 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 184 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 185 var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList(); 186 187 ConfigureOperators(newOperators); 188 encodingOperators.AddRange(newOperators); 189 } 190 #endregion 168 191 169 192 public void ConfigureOperators(IEnumerable<IOperator> operators) { … … 176 199 ConfigureParticleUpdaters(operators.OfType<IRealVectorParticleUpdater>()); 177 200 ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()); 178 } 179 201 ConfigureBoundsCheckers(operators.OfType<IRealVectorBoundsChecker>()); 202 ConfigureMoveOperators(operators.OfType<IRealVectorMoveOperator>()); 203 ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>()); 204 } 205 206 #region Specific Operator Wiring 180 207 private void ConfigureCreators(IEnumerable<IRealVectorCreator> creators) { 181 208 foreach (var creator in creators) { … … 260 287 } 261 288 } 289 private void ConfigureBoundsCheckers(IEnumerable<IRealVectorBoundsChecker> boundsCheckers) { 290 foreach (var boundsChecker in boundsCheckers) { 291 boundsChecker.RealVectorParameter.ActualName = Name; 292 boundsChecker.BoundsParameter.ActualName = BoundsParameter.Name; 293 } 294 } 295 private void ConfigureMoveOperators(IEnumerable<IRealVectorMoveOperator> moveOperators) { 296 foreach (var moveOperator in moveOperators) 297 moveOperator.RealVectorParameter.ActualName = Name; 298 } 299 300 private void ConfigureMoveGenerators(IEnumerable<IRealVectorMoveGenerator> moveGenerators) { 301 foreach (var moveGenerator in moveGenerators) 302 moveGenerator.BoundsParameter.ActualName = BoundsParameter.Name; 303 } 304 #endregion 262 305 } 263 306 }
Note: See TracChangeset
for help on using the changeset viewer.