Changeset 11550
- Timestamp:
- 11/13/14 16:46:40 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
- Files:
-
- 9 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 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs
r11543 r11550 27 27 28 28 namespace HeuristicLab.Problems.Programmable { 29 interface IEncoding : INamedItem {29 public interface IEncoding : INamedItem { 30 30 31 IEnumerable<IValueParameter> Parameters { get; } 32 IEnumerable<IOperator> Operators { get; } 31 33 //event EventHandler ParameterConfigurationChanged; 32 34 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IProblemDefinition.cs
r11484 r11550 26 26 namespace HeuristicLab.Problems.Programmable { 27 27 public interface IProblemDefinition : INamedItem { 28 Encoding Encoding { get; }28 IEncoding Encoding { get; } 29 29 IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual); 30 30 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemScript.cs
r11485 r11550 124 124 } 125 125 126 Encoding IProblemDefinition.Encoding {126 IEncoding IProblemDefinition.Encoding { 127 127 get { return Instance != null ? Instance.Encoding : null; } 128 128 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r11546 r11550 58 58 } 59 59 60 protected IValueParameter< Encoding> EncodingParameter {61 get { return (IValueParameter< Encoding>)Parameters["Encoding"]; }60 protected IValueParameter<IEncoding> EncodingParameter { 61 get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; } 62 62 } 63 63 … … 80 80 : base(new MultiObjectiveEvaluator(), new MultiEncodingCreator()) { 81 81 Parameters.Add(new ValueParameter<IMultiObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new MultiObjectiveProblemScript() { Name = Name })); 82 Parameters.Add(new ValueParameter< Encoding>("Encoding", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));82 Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes which parameters exist, what they're called, what type they are and their bounds if any.")); 83 83 84 84 DynamicEncodingParameters = new List<IParameter>(); … … 140 140 if (definition == null) return; 141 141 142 Encoding encoding = definition.Encoding;142 IEncoding encoding = definition.Encoding; 143 143 EncodingParameter.Value = encoding; 144 144 Maximization = new BoolArray(definition.Maximization); … … 175 175 } 176 176 177 protected virtual ISolutionCreator ConfigureCreator( Encoding encoding) {177 protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) { 178 178 #region Configure MultiEncoding Creator 179 179 var multiEncoding = encoding as MultiEncoding; … … 217 217 var realEnc = encoding as RealEncoding; 218 218 if (realEnc != null) { 219 DynamicEncodingParameters.AddRange(realEnc. EncodingParameters);219 DynamicEncodingParameters.AddRange(realEnc.Parameters); 220 220 return realEnc.DefaultSolutionCreator; 221 221 } … … 237 237 } 238 238 239 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, Encoding encoding) {239 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) { 240 240 // remove all multiencoding operators 241 241 Operators.RemoveAll(x => x is MultiEncodingCrossover -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/ProblemDefinition.cs
r11543 r11550 31 31 public abstract class ProblemDefinition : NamedItem, IProblemDefinition { 32 32 [Storable(Name = "Encoding")] 33 private Encoding encoding;34 public Encoding Encoding {33 private IEncoding encoding; 34 public IEncoding Encoding { 35 35 get { return encoding; } 36 36 protected set { … … 48 48 this.encoding = cloner.Clone(original.encoding); 49 49 } 50 protected ProblemDefinition( Encoding encoding) : this(encoding, "ProblemDefinition") { }51 protected ProblemDefinition( Encoding encoding, string name) : this(encoding, name, string.Empty) { }52 protected ProblemDefinition( Encoding encoding, string name, string description)50 protected ProblemDefinition(IEncoding encoding) : this(encoding, "ProblemDefinition") { } 51 protected ProblemDefinition(IEncoding encoding, string name) : this(encoding, name, string.Empty) { } 52 protected ProblemDefinition(IEncoding encoding, string name, string description) 53 53 : base(name, description) { 54 54 Encoding = encoding; -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemScript.cs
r11485 r11550 122 122 } 123 123 124 Encoding IProblemDefinition.Encoding {124 IEncoding IProblemDefinition.Encoding { 125 125 get { return Instance != null ? Instance.Encoding : null; } 126 126 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11546 r11550 59 59 } 60 60 61 protected IValueParameter<Encoding> EncodingParameter { 62 get { return (IValueParameter<Encoding>)Parameters["Encoding"]; } 63 } 61 protected IValueParameter<IEncoding> EncodingParameter { 62 get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; } 63 } 64 64 65 65 66 public ISingleObjectiveProblemDefinition ProblemDefinition { … … 70 71 [Storable] 71 72 protected List<IParameter> DynamicEncodingParameters; 73 74 protected override IEnumerable<IItem> GetOperators() { 75 return base.GetOperators().Concat(ProblemDefinition.Encoding.Operators); 76 } 77 72 78 73 79 [StorableConstructor] … … 82 88 : base(new SingleObjectiveEvaluator(), new MultiEncodingCreator()) { 83 89 Parameters.Add(new ValueParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new SingleObjectiveProblemScript() { Name = Name })); 84 Parameters.Add(new ValueParameter< Encoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));90 Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 85 91 86 92 DynamicEncodingParameters = new List<IParameter>(); … … 143 149 if (definition == null) return; 144 150 145 Encoding encoding = definition.Encoding;151 IEncoding encoding = definition.Encoding; 146 152 147 153 EncodingParameter.Value = encoding; … … 181 187 } 182 188 183 protected virtual ISolutionCreator ConfigureCreator( Encoding encoding) {189 protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) { 184 190 #region Configure RealVector Creator 185 191 var realEnc = encoding as RealEncoding; 186 192 if (realEnc != null) { 187 DynamicEncodingParameters.AddRange(realEnc. EncodingParameters);193 DynamicEncodingParameters.AddRange(realEnc.Parameters); 188 194 return realEnc.DefaultSolutionCreator; 189 195 } … … 244 250 245 251 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 } 251 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, Encoding encoding) { 252 private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator { 253 return ApplicationManager.Manager.GetInstances<T>() 254 .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>()); 255 } 256 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) { 252 257 // remove all multiencoding operators 253 258 Operators.RemoveAll(x => x is MultiEncodingCrossover … … 266 271 Operators.Remove(SolutionCreator); 267 272 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>()) { 273 } 274 275 foreach (var su in realEncoding.Operators.OfType<IRealVectorSwarmUpdater>()) { 280 276 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 281 277 } 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 } 278 } 293 279 #endregion 294 280 #region Configure Operators for BinaryVectorEncoding
Note: See TracChangeset
for help on using the changeset viewer.