Changeset 11561
- Timestamp:
- 11/20/14 17:20:29 (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/IntegerEncoding.cs
r11559 r11561 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.PluginInfrastructure; 30 32 31 33 namespace HeuristicLab.Problems.Programmable { … … 33 35 [StorableClass] 34 36 public class IntegerEncoding : Encoding<IIntegerVectorCreator> { 37 #region Encoding Parameters 35 38 [Storable] 36 private I ntValue length;37 public I ntValue Length{38 get { return length ; }39 private IFixedValueParameter<IntValue> lengthParameter; 40 public IFixedValueParameter<IntValue> LengthParameter { 41 get { return lengthParameter; } 39 42 set { 40 if (length == value) return; 41 length = value; 42 OnParameterConfigurationChanged(); 43 if (value == null) throw new ArgumentNullException("Length parameter must not be null."); 44 if (lengthParameter == value) return; 45 lengthParameter = value; 46 OnLengthParameterChanged(); 43 47 } 44 48 } 45 49 46 50 [Storable] 47 private IntMatrix bounds; 51 private IValueParameter<IntMatrix> boundsParameter; 52 public IValueParameter<IntMatrix> BoundsParameter { 53 get { return boundsParameter; } 54 set { 55 if (value == null) throw new ArgumentNullException("Bounds parameter must not be null."); 56 if (boundsParameter == value) return; 57 boundsParameter = value; 58 OnBoundsParameterChanged(); 59 } 60 } 61 62 public override IEnumerable<IValueParameter> Parameters { 63 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); } 64 } 65 #endregion 66 67 public int Length { 68 get { return LengthParameter.Value.Value; } 69 set { LengthParameter.Value.Value = value; } 70 } 48 71 public IntMatrix Bounds { 49 get { return bounds; } 50 set { 51 if (bounds == value) return; 52 bounds = value; 53 OnParameterConfigurationChanged(); 54 } 72 get { return BoundsParameter.Value; } 73 set { BoundsParameter.Value = value; } 55 74 } 56 75 57 76 [StorableConstructor] 58 77 protected IntegerEncoding(bool deserializing) : base(deserializing) { } 78 [StorableHook(HookType.AfterDeserialization)] 79 private void AfterDeserialization() { 80 RegisterParameterEvents(); 81 DiscoverOperators(); 82 } 83 59 84 protected IntegerEncoding(IntegerEncoding original, Cloner cloner) 60 85 : base(original, cloner) { 61 length = cloner.Clone(original.length); 62 bounds = cloner.Clone(original.bounds); 63 } 86 lengthParameter = cloner.Clone(original.lengthParameter); 87 boundsParameter = cloner.Clone(original.boundsParameter); 88 RegisterParameterEvents(); 89 DiscoverOperators(); 90 } 91 public override IDeepCloneable Clone(Cloner cloner) { return new IntegerEncoding(this, cloner); } 92 64 93 public IntegerEncoding(string name, int length, int min, int max, int? step = null) 65 94 : base(name) { 66 95 if (min >= max) throw new ArgumentException("min must be less than max", "min"); 67 96 if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step"); 68 this.length = new IntValue(length); 69 bounds = new IntMatrix(1, step.HasValue ? 3 : 2); 70 bounds[0, 0] = min; 71 bounds[0, 1] = max; 72 if (step.HasValue) bounds[0, 2] = step.Value; 97 Length = length; 98 Bounds = new IntMatrix(1, step.HasValue ? 3 : 2); 99 Bounds[0, 0] = min; 100 Bounds[0, 1] = max; 101 if (step.HasValue) Bounds[0, 2] = step.Value; 102 RegisterParameterEvents(); 103 DiscoverOperators(); 73 104 } 74 105 public IntegerEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null) … … 78 109 if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step"); 79 110 if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min"); 80 this.length = new IntValue(length);81 bounds = new IntMatrix(min.Count, step != null ? 3 : 2);111 Length = length; 112 Bounds = new IntMatrix(min.Count, step != null ? 3 : 2); 82 113 for (int i = 0; i < min.Count; i++) { 83 bounds[i, 0] = min[i]; 84 bounds[i, 1] = max[i]; 85 if (step != null) bounds[i, 2] = step[i]; 86 } 87 } 88 89 public override IDeepCloneable Clone(Cloner cloner) { 90 return new IntegerEncoding(this, cloner); 91 } 92 114 Bounds[i, 0] = min[i]; 115 Bounds[i, 1] = max[i]; 116 if (step != null) Bounds[i, 2] = step[i]; 117 } 118 RegisterParameterEvents(); 119 DiscoverOperators(); 120 } 121 122 private void OnLengthParameterChanged() { 123 RegisterLengthParameterEvents(); 124 ConfigureOperators(Operators); 125 } 126 private void OnBoundsParameterChanged() { 127 RegisterBoundsParameterEvents(); 128 ConfigureOperators(Operators); 129 } 130 131 private void RegisterParameterEvents() { 132 RegisterLengthParameterEvents(); 133 RegisterBoundsParameterEvents(); 134 } 135 private void RegisterLengthParameterEvents() { 136 LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 137 LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 138 } 139 private void RegisterBoundsParameterEvents() { 140 BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 141 boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators); 142 } 143 144 145 #region Operator Discovery 146 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 147 static IntegerEncoding() { 148 encodingSpecificOperatorTypes = new List<Type>() { 149 typeof (IIntegerVectorOperator), 150 typeof (IIntegerVectorCreator), 151 typeof (IIntegerVectorCrossover), 152 typeof (IIntegerVectorManipulator), 153 typeof (IIntegerVectorStdDevStrategyParameterOperator), 154 typeof (IIntegerVectorMultiNeighborhoodShakingOperator), 155 }; 156 } 157 private void DiscoverOperators() { 158 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 159 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 160 var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList(); 161 162 ConfigureOperators(newOperators); 163 encodingOperators.AddRange(newOperators); 164 } 165 #endregion 166 167 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 168 ConfigureBoundedOperators(Operators.OfType<IBoundedIntegerVectorOperator>()); 169 ConfigureCreators(Operators.OfType<IIntegerVectorCreator>()); 170 ConfigureCrossovers(Operators.OfType<IIntegerVectorCrossover>()); 171 ConfigureManipulators(Operators.OfType<IIntegerVectorManipulator>()); 172 ConfigureShakingOperators(Operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>()); 173 ConfigureStrategyVectorOperator(Operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>()); 174 } 175 176 #region Specific Operator Wiring 177 private void ConfigureBoundedOperators(IEnumerable<IBoundedIntegerVectorOperator> boundedOperators) { 178 foreach (var boundedOperator in boundedOperators) { 179 boundedOperator.BoundsParameter.ActualName = BoundsParameter.Name; 180 } 181 } 182 183 private void ConfigureCreators(IEnumerable<IIntegerVectorCreator> creators) { 184 foreach (var creator in creators) { 185 creator.IntegerVectorParameter.ActualName = Name; 186 creator.BoundsParameter.ActualName = BoundsParameter.Name; 187 creator.LengthParameter.ActualName = LengthParameter.Name; 188 } 189 } 190 191 private void ConfigureCrossovers(IEnumerable<IIntegerVectorCrossover> crossovers) { 192 foreach (var crossover in crossovers) { 193 crossover.ChildParameter.ActualName = Name; 194 crossover.ParentsParameter.ActualName = Name; 195 } 196 } 197 198 private void ConfigureManipulators(IEnumerable<IIntegerVectorManipulator> manipulators) { 199 foreach (var manipulator in manipulators) { 200 manipulator.IntegerVectorParameter.ActualName = Name; 201 manipulator.IntegerVectorParameter.Hidden = true; 202 var sm = manipulator as ISelfAdaptiveManipulator; 203 if (sm != null) { 204 var p = sm.StrategyParameterParameter as ILookupParameter; 205 if (p != null) { 206 p.ActualName = Name + "Strategy"; 207 } 208 } 209 } 210 } 211 212 private void ConfigureShakingOperators(IEnumerable<IIntegerVectorMultiNeighborhoodShakingOperator> shakingOperators) { 213 foreach (var shakingOperator in shakingOperators) { 214 shakingOperator.IntegerVectorParameter.ActualName = Name; 215 } 216 } 217 218 private void ConfigureStrategyVectorOperator(IEnumerable<IIntegerVectorStdDevStrategyParameterOperator> strategyVectorOperators) { 219 var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns); 220 for (var i = 0; i < bounds.Rows; i++) { 221 bounds[i, 1] = (int)Math.Ceiling(0.33 * (Bounds[i, 1] - Bounds[i, 0])); 222 bounds[i, 0] = 0; 223 if (bounds.Columns > 2) bounds[i, 2] = Bounds[i, 2]; 224 } 225 foreach (var s in strategyVectorOperators) { 226 var c = s as IIntegerVectorStdDevStrategyParameterCreator; 227 if (c != null) { 228 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 229 c.LengthParameter.ActualName = Name; 230 c.StrategyParameterParameter.ActualName = Name + "Strategy"; 231 } 232 var m = s as IIntegerVectorStdDevStrategyParameterManipulator; 233 if (m != null) { 234 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 235 m.StrategyParameterParameter.ActualName = Name + "Strategy"; 236 } 237 var mm = s as StdDevStrategyVectorManipulator; 238 if (mm != null) { 239 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length)); 240 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length))); 241 } 242 var x = s as IIntegerVectorStdDevStrategyParameterCrossover; 243 if (x != null) { 244 x.ParentsParameter.ActualName = Name + "Strategy"; 245 x.StrategyParameterParameter.ActualName = Name + "Strategy"; 246 } 247 } 248 } 249 #endregion 93 250 } 94 251 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11559 r11561 148 148 static RealEncoding() { 149 149 encodingSpecificOperatorTypes = new List<Type>() { 150 typeof (IRealVectorOperator),151 typeof (IRealVectorCreator),152 typeof (IRealVectorCrossover),153 typeof (IRealVectorManipulator),154 typeof (IRealVectorStdDevStrategyParameterOperator),155 typeof (IRealVectorSwarmUpdater),156 typeof (IRealVectorParticleCreator),157 typeof (IRealVectorParticleUpdater),158 typeof (IRealVectorMultiNeighborhoodShakingOperator),159 typeof (IRealVectorBoundsChecker),160 typeof (IRealVectorMoveOperator),161 typeof (IRealVectorMoveGenerator)162 };150 typeof (IRealVectorOperator), 151 typeof (IRealVectorCreator), 152 typeof (IRealVectorCrossover), 153 typeof (IRealVectorManipulator), 154 typeof (IRealVectorStdDevStrategyParameterOperator), 155 typeof (IRealVectorSwarmUpdater), 156 typeof (IRealVectorParticleCreator), 157 typeof (IRealVectorParticleUpdater), 158 typeof (IRealVectorMultiNeighborhoodShakingOperator), 159 typeof (IRealVectorBoundsChecker), 160 typeof (IRealVectorMoveOperator), 161 typeof (IRealVectorMoveGenerator) 162 }; 163 163 } 164 164 private void DiscoverOperators() { … … 223 223 if (p != null) { 224 224 p.ActualName = Name + "Strategy"; 225 p.Hidden = true;226 225 } 227 226 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r11553 r11561 190 190 } 191 191 #endregion 192 #region Configure IntegerVector Creator 193 var intEnc = encoding as IntegerEncoding; 194 if (intEnc != null) { 195 DynamicEncodingParameters.AddRange(intEnc.Parameters); 196 return intEnc.DefaultSolutionCreator; 197 } 198 #endregion 192 199 #region Configure MultiEncoding Creator 193 200 var multiEncoding = encoding as MultiEncoding; … … 198 205 creator.Operators.Add(ConfigureCreator(enc)); 199 206 } 200 return creator;201 }202 #endregion203 #region Configure IntegerVector Creator204 var intEnc = encoding as IntegerEncoding;205 if (intEnc != null) {206 var l = new ValueParameter<IntValue>(intEnc.Name + "Length", intEnc.Length);207 var b = new ValueParameter<IntMatrix>(intEnc.Name + "Bounds", intEnc.Bounds);208 DynamicEncodingParameters.Add(l);209 DynamicEncodingParameters.Add(b);210 211 var creator = new UniformRandomIntegerVectorCreator();212 creator.IntegerVectorParameter.ActualName = intEnc.Name;213 creator.LengthParameter.ActualName = l.Name;214 creator.BoundsParameter.ActualName = b.Name;215 207 return creator; 216 208 } … … 342 334 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IIntegerVectorStdDevStrategyParameterOperator>()) 343 335 .ToList(); 344 var problemSize = ((IntegerEncoding)encoding).Length .Value;336 var problemSize = ((IntegerEncoding)encoding).Length; 345 337 var b = ((IntegerEncoding)encoding).Bounds; 346 338 var bounds = new DoubleMatrix(b.Rows, b.Columns); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11553 r11561 201 201 } 202 202 #endregion 203 #region Configure IntegerVector Creator 204 var intEnc = encoding as IntegerEncoding; 205 if (intEnc != null) { 206 DynamicEncodingParameters.AddRange(intEnc.Parameters); 207 return intEnc.DefaultSolutionCreator; 208 } 209 #endregion 203 210 #region Configure MultiEncoding Creator 204 211 var multiEncoding = encoding as MultiEncoding; … … 209 216 creator.Operators.Add(ConfigureCreator(enc)); 210 217 } 211 return creator;212 }213 #endregion214 #region Configure IntegerVector Creator215 var intEnc = encoding as IntegerEncoding;216 if (intEnc != null) {217 var l = new ValueParameter<IntValue>(intEnc.Name + "Length", intEnc.Length);218 var b = new ValueParameter<IntMatrix>(intEnc.Name + "Bounds", intEnc.Bounds);219 DynamicEncodingParameters.Add(l);220 DynamicEncodingParameters.Add(b);221 222 var creator = new UniformRandomIntegerVectorCreator();223 creator.IntegerVectorParameter.ActualName = intEnc.Name;224 creator.LengthParameter.ActualName = l.Name;225 creator.BoundsParameter.ActualName = b.Name;226 218 return creator; 227 219 } … … 280 272 #endregion 281 273 #region Configure Operators for IntegerVectorEncoding 282 var intCreator = newCreator as IIntegerVectorCreator; 283 if (intCreator != null) { 284 var paramName = intCreator.IntegerVectorParameter.ActualName; 274 var intEncoding = encoding as IntegerEncoding; 275 if (intEncoding != null) { 285 276 // do not replace an integer vector creator that was manually set 286 277 if (!(SolutionCreator is IIntegerVectorCreator) 287 || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != int Creator.IntegerVectorParameter.ActualName) {278 || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intEncoding.Name) { 288 279 Operators.Remove(SolutionCreator); 289 280 SolutionCreator = newCreator; 290 Operators.Add(SolutionCreator); 291 } 292 293 #region Wire IntegerVector Crossovers 294 var crossovers = Operators.OfType<IIntegerVectorCrossover>() 295 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>(), new TypeEqualityComparer<IIntegerVectorCrossover>()) 296 .ToList(); 297 foreach (var xo in crossovers) { 298 xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName; 299 xo.ChildParameter.Hidden = true; 300 xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName; 301 xo.ParentsParameter.Hidden = true; 302 var bx = xo as IBoundedIntegerVectorOperator; 303 if (bx != null) { 304 bx.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName; 305 bx.BoundsParameter.Hidden = true; 306 } 307 } 308 Operators.AddRange(crossovers.Except(Operators.OfType<IIntegerVectorCrossover>())); 309 #endregion 310 #region Wire IntegerVector Manipulators 311 var manipulators = Operators.OfType<IIntegerVectorManipulator>() 312 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>(), new TypeEqualityComparer<IIntegerVectorManipulator>()) 313 .ToList(); 314 foreach (var m in manipulators) { 315 m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName; 316 m.IntegerVectorParameter.Hidden = true; 317 var sm = m as ISelfAdaptiveManipulator; 318 if (sm != null) { 319 var p = sm.StrategyParameterParameter as ILookupParameter; 320 if (p != null) { 321 p.ActualName = paramName + "Strategy"; 322 p.Hidden = true; 323 } 324 } 325 var bm = m as IBoundedIntegerVectorOperator; 326 if (bm != null) { 327 bm.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName; 328 bm.BoundsParameter.Hidden = true; 329 } 330 } 331 Operators.AddRange(manipulators.Except(Operators.OfType<IIntegerVectorManipulator>())); 332 #region Wire IntegerVector StrategyParameters for SelfAdaptiveManipulators 333 var strategyOperators = Operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>() 334 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IIntegerVectorStdDevStrategyParameterOperator>()) 335 .ToList(); 336 var problemSize = ((IntegerEncoding)encoding).Length.Value; 337 var b = ((IntegerEncoding)encoding).Bounds; 338 var bounds = new DoubleMatrix(b.Rows, b.Columns); 339 for (var i = 0; i < bounds.Rows; i++) { 340 bounds[i, 1] = (int)Math.Ceiling(0.33 * (b[i, 1] - b[i, 0])); 341 bounds[i, 0] = 0; 342 if (bounds.Columns > 2) bounds[i, 2] = b[i, 2]; 343 } 344 foreach (var s in strategyOperators) { 345 var c = s as IIntegerVectorStdDevStrategyParameterCreator; 346 if (c != null) { 347 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 348 c.LengthParameter.ActualName = intCreator.LengthParameter.ActualName; 349 c.StrategyParameterParameter.ActualName = paramName + "Strategy"; 350 } 351 var m = s as IIntegerVectorStdDevStrategyParameterManipulator; 352 if (m != null) { 353 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 354 m.StrategyParameterParameter.ActualName = paramName + "Strategy"; 355 } 356 var mm = s as Encodings.IntegerVectorEncoding.StdDevStrategyVectorManipulator; 357 if (mm != null) { 358 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize)); 359 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize))); 360 } 361 var x = s as IIntegerVectorStdDevStrategyParameterCrossover; 362 if (x != null) { 363 x.ParentsParameter.ActualName = paramName + "Strategy"; 364 x.StrategyParameterParameter.ActualName = paramName + "Strategy"; 365 } 366 } 367 Operators.AddRange(strategyOperators.Except(Operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>())); 368 #endregion 369 #endregion 370 #region Wire IntegerVector ShakingOperators 371 var shakingOperators = Operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>() 372 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IIntegerVectorMultiNeighborhoodShakingOperator>()) 373 .ToList(); 374 foreach (var so in shakingOperators) { 375 so.IntegerVectorParameter.ActualName = paramName; 376 so.IntegerVectorParameter.Hidden = true; 377 } 378 Operators.AddRange(shakingOperators.Except(Operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>())); 379 #endregion 380 } else { 381 Operators.RemoveAll(x => x is IIntegerVectorCrossover 382 || x is IIntegerVectorManipulator 383 || x is IIntegerVectorStdDevStrategyParameterOperator 384 || x is IIntegerVectorMultiNeighborhoodShakingOperator); 281 } 385 282 } 386 283 #endregion
Note: See TracChangeset
for help on using the changeset viewer.