- Timestamp:
- 11/12/14 23:48:56 (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/Encoding.cs
r11543 r11546 45 45 } 46 46 47 //public event EventHandler ParameterConfigurationChanged;47 public event EventHandler ParameterConfigurationChanged; 48 48 protected virtual void OnParameterConfigurationChanged() { 49 //var handler = ParameterConfigurationChanged;50 //if (handler != null) handler(this, EventArgs.Empty);49 var handler = ParameterConfigurationChanged; 50 if (handler != null) handler(this, EventArgs.Empty); 51 51 } 52 52 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11543 r11546 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using System.Linq; … … 31 32 using HeuristicLab.Parameters; 32 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.PluginInfrastructure; 33 35 34 36 namespace HeuristicLab.Problems.Programmable { … … 42 44 get { return lengthParameter; } 43 45 set { 44 if (value == null) throw new ArgumentNullException("Length parameter must not be null.");46 if (value == null) throw new ArgumentNullException("Length parameter must not be null."); 45 47 if (lengthParameter == value) return; 46 48 lengthParameter = value; … … 74 76 } 75 77 76 public I SolutionCreatorSolutionCreator {78 public IRealVectorCreator DefaultSolutionCreator { 77 79 get { 78 80 var creator = new UniformRandomRealVectorCreator(); … … 83 85 } 84 86 } 85 87 86 88 [StorableConstructor] 87 89 private RealEncoding(bool deserializing) : base(deserializing) { } 90 [StorableHook(HookType.AfterDeserialization)] 91 private void AfterDeserialization() { 92 RegisterParameterEvents(); 93 } 94 95 public override IDeepCloneable Clone(Cloner cloner) { return new RealEncoding(this, cloner); } 88 96 private RealEncoding(RealEncoding original, Cloner cloner) 89 97 : base(original, cloner) { 90 98 lengthParameter = cloner.Clone(original.lengthParameter); 91 99 boundsParameter = cloner.Clone(original.boundsParameter); 92 } 100 RegisterParameterEvents(); 101 } 102 93 103 public RealEncoding(string name, int length, double min, double max) 94 104 : base(name) { … … 101 111 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 102 112 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 103 } 113 RegisterParameterEvents(); 114 } 115 104 116 public RealEncoding(string name, int length, IList<double> min, IList<double> max) 105 117 : base(name) { … … 115 127 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 116 128 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 117 } 118 119 public override IDeepCloneable Clone(Cloner cloner) { 120 return new RealEncoding(this, cloner); 129 RegisterParameterEvents(); 121 130 } 122 131 123 132 private void OnLengthParameterChanged() { 124 //TODO rewire parameters 125 } 126 133 RegisterLengthParameterEvents(); 134 } 127 135 private void OnBoundsParameterChanged() { 128 //TODO rewire parameters 129 } 130 131 136 RegisterBoundsParameterEvents(); 137 } 138 private void RegisterParameterEvents() 139 { 140 RegisterLengthParameterEvents(); 141 RegisterBoundsParameterEvents(); 142 } 143 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>()); 147 } 148 private void RegisterBoundsParameterEvents() { 149 //TODO fire correct event 150 BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>()); 151 boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>()); 152 } 153 154 public void DiscoverOperators() { 155 var types = new List<Type>() 156 { 157 typeof (IRealVectorCreator), 158 typeof (IRealVectorCrossover), 159 typeof (IRealVectorManipulator), 160 typeof (IRealVectorStdDevStrategyParameterOperator), 161 typeof (IRealVectorParticleCreator), 162 typeof (IRealVectorParticleUpdater), 163 typeof (IRealVectorParticleUpdater), 164 }; 165 var discoveredTypes = ApplicationManager.Manager.GetTypes(types, true, false, false); 166 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)).ToList(); 167 } 168 169 public void ConfigureOperators(IEnumerable<IOperator> operators) { 170 ConfigureCreators(operators.OfType<IRealVectorCreator>()); 171 ConfigureCrossovers(operators.OfType<IRealVectorCrossover>()); 172 ConfigureManipulators(operators.OfType<IRealVectorManipulator>()); 173 ConfigureStdDevStrategyParameterOperators(operators.OfType<IRealVectorStdDevStrategyParameterOperator>()); 174 ConfigureSwarmUpdaters(operators.OfType<IRealVectorSwarmUpdater>()); 175 ConfigureParticleCreators(operators.OfType<IRealVectorParticleCreator>()); 176 ConfigureParticleUpdaters(operators.OfType<IRealVectorParticleUpdater>()); 177 ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()); 178 } 179 180 private void ConfigureCreators(IEnumerable<IRealVectorCreator> creators) { 181 foreach (var creator in creators) { 182 creator.RealVectorParameter.ActualName = Name; 183 creator.LengthParameter.ActualName = LengthParameter.Name; 184 creator.BoundsParameter.ActualName = BoundsParameter.Name; 185 } 186 } 187 private void ConfigureCrossovers(IEnumerable<IRealVectorCrossover> crossovers) { 188 foreach (var crossover in crossovers) { 189 crossover.ChildParameter.ActualName = Name; 190 crossover.ParentsParameter.ActualName = Name; 191 crossover.BoundsParameter.ActualName = BoundsParameter.Name; 192 } 193 } 194 private void ConfigureManipulators(IEnumerable<IRealVectorManipulator> manipulators) { 195 foreach (var manipulator in manipulators) { 196 manipulator.RealVectorParameter.ActualName = Name; 197 manipulator.BoundsParameter.ActualName = BoundsParameter.Name; 198 manipulator.BoundsParameter.Hidden = true; 199 var sm = manipulator as ISelfAdaptiveManipulator; 200 if (sm != null) { 201 var p = sm.StrategyParameterParameter as ILookupParameter; 202 if (p != null) { 203 p.ActualName = Name + "Strategy"; 204 p.Hidden = true; 205 } 206 } 207 } 208 } 209 private void ConfigureStdDevStrategyParameterOperators(IEnumerable<IRealVectorStdDevStrategyParameterOperator> strategyOperators) { 210 var bounds = (DoubleMatrix)Bounds.Clone(); 211 for (var i = 0; i < bounds.Rows; i++) { 212 bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]); 213 bounds[i, 0] = 0; 214 } 215 foreach (var s in strategyOperators) { 216 var c = s as IRealVectorStdDevStrategyParameterCreator; 217 if (c != null) { 218 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 219 c.LengthParameter.ActualName = LengthParameter.Name; 220 c.StrategyParameterParameter.ActualName = Name + "Strategy"; 221 } 222 var m = s as IRealVectorStdDevStrategyParameterManipulator; 223 if (m != null) { 224 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone(); 225 m.StrategyParameterParameter.ActualName = Name + "Strategy"; 226 } 227 var mm = s as StdDevStrategyVectorManipulator; 228 if (mm != null) { 229 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length)); 230 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length))); 231 } 232 var x = s as IRealVectorStdDevStrategyParameterCrossover; 233 if (x != null) { 234 x.ParentsParameter.ActualName = Name + "Strategy"; 235 x.StrategyParameterParameter.ActualName = Name + "Strategy"; 236 } 237 } 238 } 239 private void ConfigureSwarmUpdaters(IEnumerable<IRealVectorSwarmUpdater> swarmUpdaters) { 240 foreach (var su in swarmUpdaters) { 241 su.RealVectorParameter.ActualName = Name; 242 } 243 } 244 private void ConfigureParticleCreators(IEnumerable<IRealVectorParticleCreator> particleCreators) { 245 foreach (var particleCreator in particleCreators) { 246 particleCreator.RealVectorParameter.ActualName = Name; 247 particleCreator.BoundsParameter.ActualName = BoundsParameter.Name; 248 particleCreator.ProblemSizeParameter.ActualName = LengthParameter.Name; 249 } 250 } 251 private void ConfigureParticleUpdaters(IEnumerable<IRealVectorParticleUpdater> particleUpdaters) { 252 foreach (var particleUpdater in particleUpdaters) { 253 particleUpdater.RealVectorParameter.ActualName = Name; 254 particleUpdater.BoundsParameter.ActualName = BoundsParameter.Name; 255 } 256 } 257 private void ConfigureShakingOperators(IEnumerable<IRealVectorMultiNeighborhoodShakingOperator> shakingOperators) { 258 foreach (var shakingOperator in shakingOperators) { 259 shakingOperator.RealVectorParameter.ActualName = Name; 260 } 261 } 132 262 } 133 } 263 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r11543 r11546 218 218 if (realEnc != null) { 219 219 DynamicEncodingParameters.AddRange(realEnc.EncodingParameters); 220 return realEnc. SolutionCreator;220 return realEnc.DefaultSolutionCreator; 221 221 } 222 222 #endregion -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11543 r11546 107 107 private void RegisterEventHandlers() { 108 108 ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged; 109 Register HostInstanceChanges();109 RegisterProblemDefinitionEventHandlers(); 110 110 } 111 111 112 112 private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) { 113 Register HostInstanceChanges();113 RegisterProblemDefinitionEventHandlers(); 114 114 Parameterize(); 115 115 } 116 116 117 private void Register HostInstanceChanges() {117 private void RegisterProblemDefinitionEventHandlers() { 118 118 ProblemDefinitionParameter.Value.ProblemDefinitionChanged += ProblemDefinitionChanged; 119 ProblemDefinitionParameter.Value.NameChanged += ProblemDefinition HostOnNameChanged;120 } 121 122 private void ProblemDefinition HostOnNameChanged(object sender, EventArgs eventArgs) {119 ProblemDefinitionParameter.Value.NameChanged += ProblemDefinitionNameChanged; 120 } 121 122 private void ProblemDefinitionNameChanged(object sender, EventArgs eventArgs) { 123 123 if (sender != ProblemDefinitionParameter.Value) return; 124 124 Name = ProblemDefinitionParameter.Value.Name; … … 182 182 183 183 protected virtual ISolutionCreator ConfigureCreator(Encoding encoding) { 184 #region Configure RealVector Creator 185 var realEnc = encoding as RealEncoding; 186 if (realEnc != null) { 187 DynamicEncodingParameters.AddRange(realEnc.EncodingParameters); 188 return realEnc.DefaultSolutionCreator; 189 } 190 #endregion 184 191 #region Configure MultiEncoding Creator 185 192 var multiEncoding = encoding as MultiEncoding; … … 220 227 } 221 228 #endregion 222 #region Configure RealVector Creator223 var realEnc = encoding as RealEncoding;224 if (realEnc != null) {225 DynamicEncodingParameters.AddRange(realEnc.EncodingParameters);226 return realEnc.SolutionCreator;227 }228 #endregion229 229 #region Configure Permutation Creator 230 230 var permEnc = encoding as PermutationEncoding; … … 243 243 } 244 244 245 246 private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator 247 { 248 return ApplicationManager.Manager.GetInstances<T>() 249 .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>()); 250 } 245 251 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, Encoding encoding) { 246 252 // remove all multiencoding operators … … 249 255 || x is MultiEncodingCreator); 250 256 257 #region Configure Operators for RealVectorEncoding 258 var realCreator = newCreator as IRealVectorCreator; 259 var realEncoding = encoding as RealEncoding; 260 if (realEncoding != null) { 261 // do not replace a real vector creator that was manually set 262 if (!(SolutionCreator is IRealVectorCreator) 263 || 264 ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != 265 realCreator.RealVectorParameter.ActualName) { 266 Operators.Remove(SolutionCreator); 267 SolutionCreator = newCreator; 268 Operators.Add(SolutionCreator); 269 } 270 271 Operators.AddRange(GetDiscoveredOperators<IRealVectorCrossover>()); 272 Operators.AddRange(GetDiscoveredOperators<IRealVectorManipulator>()); 273 Operators.AddRange(GetDiscoveredOperators<IRealVectorStdDevStrategyParameterOperator>()); 274 Operators.AddRange(GetDiscoveredOperators<IRealVectorParticleCreator>()); 275 Operators.AddRange(GetDiscoveredOperators<IRealVectorParticleUpdater>()); 276 Operators.AddRange(GetDiscoveredOperators<IRealVectorSwarmUpdater>()); 277 Operators.AddRange(GetDiscoveredOperators<IRealVectorMultiNeighborhoodShakingOperator>()); 278 279 foreach (var su in Operators.OfType<IRealVectorSwarmUpdater>()) { 280 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 281 } 282 realEncoding.ConfigureOperators(Operators.OfType<IOperator>()); 283 284 } else { 285 Operators.RemoveAll(x => x is IRealVectorCrossover 286 || x is IRealVectorManipulator 287 || x is IRealVectorStdDevStrategyParameterOperator 288 || x is IRealVectorParticleCreator 289 || x is IRealVectorParticleUpdater 290 || x is IRealVectorSwarmUpdater 291 || x is IRealVectorMultiNeighborhoodShakingOperator); 292 } 293 #endregion 251 294 #region Configure Operators for BinaryVectorEncoding 252 295 var binCreator = newCreator as IBinaryVectorCreator; … … 402 445 || x is IIntegerVectorStdDevStrategyParameterOperator 403 446 || x is IIntegerVectorMultiNeighborhoodShakingOperator); 404 }405 #endregion406 #region Configure Operators for RealVectorEncoding407 var realCreator = newCreator as IRealVectorCreator;408 if (realCreator != null) {409 var paramName = realCreator.RealVectorParameter.ActualName;410 // do not replace a real vector creator that was manually set411 if (!(SolutionCreator is IRealVectorCreator)412 || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {413 Operators.Remove(SolutionCreator);414 SolutionCreator = newCreator;415 Operators.Add(SolutionCreator);416 }417 418 #region Wire RealVector Crossovers419 var crossovers = Operators.OfType<IRealVectorCrossover>()420 .Union(ApplicationManager.Manager.GetInstances<IRealVectorCrossover>(), new TypeEqualityComparer<IRealVectorCrossover>())421 .ToList();422 foreach (var xo in crossovers) {423 xo.ChildParameter.ActualName = paramName;424 xo.ChildParameter.Hidden = true;425 xo.ParentsParameter.ActualName = paramName;426 xo.ParentsParameter.Hidden = true;427 xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;428 xo.BoundsParameter.Hidden = true;429 }430 Operators.AddRange(crossovers.Except(Operators.OfType<IRealVectorCrossover>()));431 #endregion432 #region Wire RealVector Manipulators433 var manipulators = Operators.OfType<IRealVectorManipulator>()434 .Union(ApplicationManager.Manager.GetInstances<IRealVectorManipulator>(), new TypeEqualityComparer<IRealVectorManipulator>())435 .ToList();436 foreach (var m in manipulators) {437 m.RealVectorParameter.ActualName = paramName;438 m.RealVectorParameter.Hidden = true;439 m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;440 m.BoundsParameter.Hidden = true;441 var sm = m as ISelfAdaptiveManipulator;442 if (sm != null) {443 var p = sm.StrategyParameterParameter as ILookupParameter;444 if (p != null) {445 p.ActualName = paramName + "Strategy";446 p.Hidden = true;447 }448 }449 }450 Operators.AddRange(manipulators.Except(Operators.OfType<IRealVectorManipulator>()));451 #region Wire RealVector Strategy Parameters for SelfAdaptiveManipulators452 var strategyOperators = Operators.OfType<IRealVectorStdDevStrategyParameterOperator>()453 .Union(ApplicationManager.Manager.GetInstances<IRealVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IRealVectorStdDevStrategyParameterOperator>())454 .ToList();455 var problemSize = ((RealEncoding)encoding).Length;456 var bounds = (DoubleMatrix)((RealEncoding)encoding).Bounds.Clone();457 for (var i = 0; i < bounds.Rows; i++) {458 bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]);459 bounds[i, 0] = 0;460 }461 foreach (var s in strategyOperators) {462 var c = s as IRealVectorStdDevStrategyParameterCreator;463 if (c != null) {464 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();465 c.LengthParameter.ActualName = realCreator.LengthParameter.ActualName;466 c.StrategyParameterParameter.ActualName = paramName + "Strategy";467 }468 var m = s as IRealVectorStdDevStrategyParameterManipulator;469 if (m != null) {470 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();471 m.StrategyParameterParameter.ActualName = paramName + "Strategy";472 }473 var mm = s as Encodings.RealVectorEncoding.StdDevStrategyVectorManipulator;474 if (mm != null) {475 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize));476 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize)));477 }478 var x = s as IRealVectorStdDevStrategyParameterCrossover;479 if (x != null) {480 x.ParentsParameter.ActualName = paramName + "Strategy";481 x.StrategyParameterParameter.ActualName = paramName + "Strategy";482 }483 }484 Operators.AddRange(strategyOperators.Except(Operators.OfType<IRealVectorStdDevStrategyParameterOperator>()));485 #endregion486 #endregion487 #region Wire RealVector ParticleCreators488 var particleCreators = Operators.OfType<IRealVectorParticleCreator>()489 .Union(ApplicationManager.Manager.GetInstances<IRealVectorParticleCreator>(), new TypeEqualityComparer<IRealVectorParticleCreator>())490 .ToList();491 foreach (var pc in particleCreators) {492 pc.RealVectorParameter.ActualName = paramName;493 pc.RealVectorParameter.Hidden = true;494 pc.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;495 pc.BoundsParameter.Hidden = true;496 pc.ProblemSizeParameter.ActualName = realCreator.LengthParameter.ActualName;497 pc.ProblemSizeParameter.Hidden = true;498 }499 Operators.AddRange(particleCreators.Except(Operators.OfType<IRealVectorParticleCreator>()));500 #endregion501 #region Wire RealVector ParticleUpdaters502 var particleUpdaters = Operators.OfType<IRealVectorParticleUpdater>()503 .Union(ApplicationManager.Manager.GetInstances<IRealVectorParticleUpdater>(), new TypeEqualityComparer<IRealVectorParticleUpdater>())504 .ToList();505 foreach (var pu in particleUpdaters) {506 pu.RealVectorParameter.ActualName = paramName;507 pu.RealVectorParameter.Hidden = true;508 pu.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;509 pu.BoundsParameter.Hidden = true;510 }511 Operators.AddRange(particleUpdaters.Except(Operators.OfType<IRealVectorParticleUpdater>()));512 #endregion513 #region Wire RealVector SwarmUpdaters514 var swarmUpdaters = Operators.OfType<IRealVectorSwarmUpdater>()515 .Union(ApplicationManager.Manager.GetInstances<IRealVectorSwarmUpdater>(), new TypeEqualityComparer<IRealVectorSwarmUpdater>())516 .ToList();517 foreach (var su in swarmUpdaters) {518 su.RealVectorParameter.ActualName = paramName;519 su.RealVectorParameter.Hidden = true;520 su.MaximizationParameter.ActualName = MaximizationParameter.Name;521 su.MaximizationParameter.Hidden = true;522 }523 Operators.AddRange(swarmUpdaters.Except(Operators.OfType<IRealVectorSwarmUpdater>()));524 #endregion525 #region Wire RealVector ShakingOperators526 var shakingOperators = Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()527 .Union(ApplicationManager.Manager.GetInstances<IRealVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IRealVectorMultiNeighborhoodShakingOperator>())528 .ToList();529 foreach (var so in shakingOperators) {530 so.RealVectorParameter.ActualName = paramName;531 so.RealVectorParameter.Hidden = true;532 }533 Operators.AddRange(shakingOperators.Except(Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()));534 #endregion535 } else {536 Operators.RemoveAll(x => x is IRealVectorCrossover537 || x is IRealVectorManipulator538 || x is IRealVectorStdDevStrategyParameterOperator539 || x is IRealVectorParticleCreator540 || x is IRealVectorParticleUpdater541 || x is IRealVectorSwarmUpdater542 || x is IRealVectorMultiNeighborhoodShakingOperator);543 447 } 544 448 #endregion … … 795 699 } 796 700 } 797 798 701 protected virtual void UpdateMoveOperators() { 799 702 if (!Operators.Any(x => x is SingleObjectiveMoveGenerator))
Note: See TracChangeset
for help on using the changeset viewer.