Changeset 11587
- Timestamp:
- 11/26/14 13:24:20 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
- Files:
-
- 2 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryEncoding.cs
r11582 r11587 48 48 } 49 49 public override IEnumerable<IValueParameter> Parameters { 50 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter }); }50 get { return new IValueParameter[] { LengthParameter }; } 51 51 } 52 52 #endregion … … 73 73 : base(name) { 74 74 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 75 SolutionCreator = new RandomBinaryVectorCreator(); 75 76 RegisterParameterEvents(); 76 77 DiscoverOperators(); 77 SolutionCreator = Operators.OfType<RandomBinaryVectorCreator>().Single();78 78 } 79 79 … … 106 106 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 107 107 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 108 var newOperators = operators.Except( encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();108 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); 109 109 110 110 ConfigureOperators(newOperators); 111 encodingOperators.AddRange(newOperators); 111 foreach (var @operator in newOperators) 112 encodingOperators.Add(@operator); 112 113 } 113 114 #endregion -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11582 r11587 37 37 } 38 38 39 public virtual IEnumerable<IValueParameter> Parameters { 40 get { return Enumerable.Empty<IValueParameter>(); } 41 } 39 public abstract IEnumerable<IValueParameter> Parameters { get; } 42 40 43 protected List<IOperator> encodingOperators = new List<IOperator>();41 protected HashSet<IOperator> encodingOperators = new HashSet<IOperator>(new TypeEqualityComparer<IOperator>()); 44 42 [Storable] 45 43 public IEnumerable<IOperator> Operators { 46 44 get { return encodingOperators; } 47 pr otected set { encodingOperators = new List<IOperator>(value); }45 private set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); } 48 46 } 49 50 47 51 48 ISolutionCreator IEncoding.SolutionCreator { 52 49 get { return SolutionCreator; } 53 50 set { 54 if (!(value is T)) throw new ArgumentException( "???");51 if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName())); 55 52 SolutionCreator = (T)value; 56 53 } … … 64 61 if (value == null) throw new ArgumentNullException("SolutionCreator must not be null."); 65 62 if (solutionCreator == value) return; 63 encodingOperators.Remove(solutionCreator); 64 encodingOperators.Add(value); 66 65 solutionCreator = value; 67 66 OnSolutionCreatorChanged(); … … 74 73 protected Encoding(Encoding<T> original, Cloner cloner) 75 74 : base(original, cloner) { 76 encodingOperators = original.Operators.Select(cloner.Clone).ToList();75 encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone)); 77 76 solutionCreator = cloner.Clone(original.solutionCreator); 78 77 } … … 80 79 81 80 81 public abstract void ConfigureOperators(IEnumerable<IOperator> operators); 82 public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); } 82 83 83 public void ConfigureOperator(IOperator @operator) { 84 ConfigureOperators(new[] { @operator }); 85 } 86 public virtual void ConfigureOperators(IEnumerable<IOperator> operators) { 87 88 } 89 84 public event EventHandler SolutionCreatorChanged; 90 85 protected virtual void OnSolutionCreatorChanged() { 91 86 ConfigureOperator(SolutionCreator); 87 var handler = SolutionCreatorChanged; 88 if (handler != null) handler(this, EventArgs.Empty); 92 89 } 93 90 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs
r11582 r11587 61 61 62 62 public override IEnumerable<IValueParameter> Parameters { 63 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }63 get { return new IValueParameter[] { LengthParameter, BoundsParameter }; } 64 64 } 65 65 #endregion … … 94 94 if (min >= max) throw new ArgumentException("min must be less than max", "min"); 95 95 if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step"); 96 96 97 Length = length; 97 98 Bounds = new IntMatrix(1, step.HasValue ? 3 : 2); … … 99 100 Bounds[0, 1] = max; 100 101 if (step.HasValue) Bounds[0, 2] = step.Value; 102 103 SolutionCreator = new UniformRandomIntegerVectorCreator(); 101 104 RegisterParameterEvents(); 102 105 DiscoverOperators(); 103 SolutionCreator = Operators.OfType<UniformRandomIntegerVectorCreator>().Single();104 106 } 105 107 public IntegerEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null) … … 109 111 if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step"); 110 112 if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min"); 113 111 114 Length = length; 112 115 Bounds = new IntMatrix(min.Count, step != null ? 3 : 2); … … 116 119 if (step != null) Bounds[i, 2] = step[i]; 117 120 } 121 122 SolutionCreator = new UniformRandomIntegerVectorCreator(); 118 123 RegisterParameterEvents(); 119 124 DiscoverOperators(); 120 SolutionCreator = Operators.OfType<UniformRandomIntegerVectorCreator>().Single();121 125 } 122 126 … … 159 163 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 160 164 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 161 var newOperators = operators.Except( encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();165 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); 162 166 163 167 ConfigureOperators(newOperators); 164 encodingOperators.AddRange(newOperators); 168 foreach (var @operator in newOperators) 169 encodingOperators.Add(@operator); 165 170 } 166 171 #endregion -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11559 r11587 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.PermutationEncoding;27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.PluginInfrastructure; 29 using HeuristicLab.Problems.Programmable.Interfaces; 28 30 29 31 namespace HeuristicLab.Problems.Programmable { … … 31 33 [StorableClass] 32 34 public class MultiEncoding : Encoding<MultiEncodingCreator> { 35 36 private readonly List<IEncoding> encodings; 37 33 38 [Storable] 34 public KeyedItemCollection<string, IEncoding> Encodings { get; protected set; } 39 public IEnumerable<IEncoding> Encodings { 40 get { return encodings; } 41 private set { encodings.AddRange(value); } 42 } 43 44 public override IEnumerable<IValueParameter> Parameters { 45 get { return Encodings.SelectMany(e => e.Parameters); } 46 } 35 47 36 48 [StorableConstructor] 37 protected MultiEncoding(bool deserializing) : base(deserializing) { } 49 protected MultiEncoding(bool deserializing) 50 : base(deserializing) { 51 encodings = new List<IEncoding>(); 52 } 53 54 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); } 38 55 protected MultiEncoding(MultiEncoding original, Cloner cloner) 39 56 : base(original, cloner) { 40 Encodings = cloner.Clone(original.Encodings);57 encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone)); 41 58 } 42 59 public MultiEncoding() 43 60 : base("MultiEncoding") { 44 Encodings = new NamedItemCollection<IEncoding>(); 61 encodings = new List<IEncoding>(); 62 SolutionCreator = new MultiEncodingCreator(); 63 64 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) 65 encodingOperators.Add(@operator); 45 66 } 46 67 47 public override IDeepCloneable Clone(Cloner cloner) { 48 return new MultiEncoding(this, cloner); 49 } 68 public MultiEncoding Add(IEncoding encoding) { 69 if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported."); 70 if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name"); 71 encodings.Add(encoding); 50 72 51 public MultiEncoding AddBinaryVector(string variableName, int length) {52 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");53 Encodings.Add(new BinaryEncoding(variableName, length));73 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) { 74 @operator.AddEncoding(encoding); 75 } 54 76 return this; 55 77 } 56 78 57 public MultiEncoding AddIntegerVector(string variableName, int length, int min, int max, int? step = null) { 58 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName"); 59 Encodings.Add(new IntegerEncoding(variableName, length, min, max, step)); 60 return this; 79 public bool Remove(IEncoding encoding) { 80 var success = encodings.Remove(encoding); 81 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) { 82 @operator.RemoveEncoding(encoding); 83 } 84 return success; 61 85 } 62 86 63 public MultiEncoding AddIntegerVector(string variableName, int length, IList<int> min, IList<int> max, IList<int> step = null) { 64 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName"); 65 Encodings.Add(new IntegerEncoding(variableName, length, min, max, step)); 66 return this; 67 } 87 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 68 88 69 public MultiEncoding AddRealVector(string variableName, int length, double min, double max) {70 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");71 Encodings.Add(new RealEncoding(variableName, length, min, max));72 return this;73 }74 75 public MultiEncoding AddRealVector(string variableName, int length, IList<double> min, IList<double> max) {76 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");77 Encodings.Add(new RealEncoding(variableName, length, min, max));78 return this;79 }80 81 public MultiEncoding AddPermutation(string variableName, int length, PermutationTypes type) {82 if (Encodings.ContainsKey(variableName)) throw new ArgumentException("variableName must be unique", "variableName");83 Encodings.Add(new PermutationEncoding(variableName, length, type));84 return this;85 89 } 86 90 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs
r11582 r11587 60 60 61 61 public override IEnumerable<IValueParameter> Parameters { 62 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, PermutationTypeParameter }); }62 get { return new IValueParameter[] { LengthParameter, PermutationTypeParameter }; } 63 63 } 64 64 #endregion … … 89 89 RegisterParameterEvents(); 90 90 } 91 91 92 public PermutationEncoding(string name, int length, PermutationTypes type) 92 93 : base(name) { 93 94 Length = length; 94 95 Type = type; 96 97 SolutionCreator = new RandomPermutationCreator(); 95 98 RegisterParameterEvents(); 96 99 DiscoverOperators(); 97 SolutionCreator = Operators.OfType<RandomPermutationCreator>().Single();98 100 } 99 101 … … 138 140 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 139 141 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 140 var newOperators = operators.Except( encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();142 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); 141 143 142 144 ConfigureOperators(newOperators); 143 encodingOperators.AddRange(newOperators); 145 foreach (var @operator in newOperators) 146 encodingOperators.Add(@operator); 144 147 } 145 148 #endregion 146 149 147 150 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 148 base.ConfigureOperators(operators);149 151 ConfigureCreators(Operators.OfType<IPermutationCreator>()); 150 152 ConfigureCrossovers(Operators.OfType<IPermutationCrossover>()); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11582 r11587 62 62 63 63 public override IEnumerable<IValueParameter> Parameters { 64 get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }64 get { return new IValueParameter[] { LengthParameter, BoundsParameter }; } 65 65 } 66 66 #endregion … … 101 101 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 102 102 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 103 104 SolutionCreator = new UniformRandomRealVectorCreator(); 103 105 RegisterParameterEvents(); 104 106 DiscoverOperators(); 105 SolutionCreator = Operators.OfType<UniformRandomRealVectorCreator>().Single();106 107 } 107 108 … … 119 120 lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length)); 120 121 boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds); 122 123 SolutionCreator = new UniformRandomRealVectorCreator(); 121 124 RegisterParameterEvents(); 122 125 DiscoverOperators(); 123 SolutionCreator = Operators.OfType<UniformRandomRealVectorCreator>().Single();124 126 } 125 127 … … 167 169 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 168 170 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 169 var newOperators = operators.Except( encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();171 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); 170 172 171 173 ConfigureOperators(newOperators); 172 encodingOperators.AddRange(newOperators); 174 foreach (var @operator in newOperators) 175 encodingOperators.Add(@operator); 173 176 174 177 foreach (var strategyVectorCreator in Operators.OfType<IRealVectorStdDevStrategyParameterCreator>()) -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r11574 r11587 136 136 <Compile Include="Helper.cs" /> 137 137 <Compile Include="Interfaces\IEncoding.cs" /> 138 <Compile Include="Interfaces\IMultiEncodingOperator.cs" /> 138 139 <Compile Include="Interfaces\ISingleObjectiveMoveOperator.cs" /> 139 140 <Compile Include="Interfaces\IMultiObjectiveProblemDefinition.cs" /> … … 144 145 <Compile Include="Operators\MultiEncodingManipulator.cs" /> 145 146 <Compile Include="Operators\MultiEncodingCrossover.cs" /> 147 <Compile Include="Operators\MultiEncodingOperator.cs" /> 146 148 <Compile Include="Operators\SingleObjectiveMoveEvaluator.cs" /> 147 149 <Compile Include="Operators\SingleObjectiveMoveGenerator.cs" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs
r11582 r11587 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; … … 34 35 void ConfigureOperator(IOperator @operator); 35 36 void ConfigureOperators(IEnumerable<IOperator> operators); 37 38 event EventHandler SolutionCreatorChanged; 39 //event EventHandler OperatorsChanged; 36 40 } 37 41 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r11582 r11587 27 27 using HeuristicLab.Core; 28 28 using HeuristicLab.Data; 29 using HeuristicLab.Encodings.BinaryVectorEncoding;30 using HeuristicLab.Encodings.IntegerVectorEncoding;31 using HeuristicLab.Encodings.PermutationEncoding;32 using HeuristicLab.Encodings.RealVectorEncoding;33 29 using HeuristicLab.Optimization; 34 30 using HeuristicLab.Parameters; 35 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 36 using HeuristicLab.PluginInfrastructure;37 32 38 33 namespace HeuristicLab.Problems.Programmable { … … 86 81 Operators.Add(new MultiObjectiveAnalyzer()); 87 82 Operators.Add(Evaluator); 88 Operators.Add(SolutionCreator);89 83 90 84 RegisterEventHandlers(); … … 156 150 } 157 151 152 SolutionCreator = encoding.SolutionCreator; 153 158 154 foreach (var param in DynamicEncodingParameters) 159 155 if (Parameters.Contains(param)) Parameters.Remove(param); 160 156 DynamicEncodingParameters.Clear(); 161 157 162 var creator = ConfigureCreator(encoding); 158 DynamicEncodingParameters.AddRange(encoding.Parameters); 159 163 160 164 161 foreach (var param in DynamicEncodingParameters) { … … 167 164 } 168 165 169 var multiEncoding = encoding as MultiEncoding;170 if (multiEncoding != null) {171 ConfigureMultiVectorEncodingOperators((MultiEncodingCreator)creator, multiEncoding);172 } else {173 ConfigureSingleEncodingOperators(creator, encoding);174 }175 166 } 176 177 protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) {178 #region Configure MultiEncoding Creator179 var multiEncoding = encoding as MultiEncoding;180 if (multiEncoding != null) {181 var creator = new MultiEncodingCreator();182 foreach (var enc in multiEncoding.Encodings) {183 if (enc is MultiEncoding) throw new InvalidOperationException("MultiEncoding within a MultiEncoding is not supported.");184 creator.Operators.Add(ConfigureCreator(enc));185 }186 return creator;187 }188 #endregion189 #region Configure RealVector Creator190 var realEnc = encoding as RealEncoding;191 if (realEnc != null) {192 DynamicEncodingParameters.AddRange(realEnc.Parameters);193 return realEnc.SolutionCreator;194 }195 #endregion196 #region Configure BinaryVector Creator197 var binEnc = encoding as BinaryEncoding;198 if (binEnc != null) {199 DynamicEncodingParameters.AddRange(binEnc.Parameters);200 return binEnc.SolutionCreator;201 }202 #endregion203 #region Configure IntegerVector Creator204 var intEnc = encoding as IntegerEncoding;205 if (intEnc != null) {206 DynamicEncodingParameters.AddRange(intEnc.Parameters);207 return intEnc.SolutionCreator;208 }209 #endregion210 #region Configure Permutation Creator211 var permEnc = encoding as PermutationEncoding;212 if (permEnc != null) {213 DynamicEncodingParameters.AddRange(permEnc.Parameters);214 return permEnc.SolutionCreator;215 }216 #endregion217 218 219 throw new ArgumentException(string.Format("Encoding {0} is unknown.", encoding != null ? encoding.GetType().FullName : "(null)"));220 }221 222 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) {223 // remove all multiencoding operators224 Operators.RemoveAll(x => x is MultiEncodingCrossover225 || x is MultiEncodingManipulator226 || x is MultiEncodingCreator);227 228 #region Configure Operators for BinaryVectorEncoding229 var binCreator = newCreator as IBinaryVectorCreator;230 if (binCreator != null) {231 var paramName = binCreator.BinaryVectorParameter.ActualName;232 // do not replace a binary vector creator that was manually set233 if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != paramName) {234 Operators.Remove(SolutionCreator);235 SolutionCreator = newCreator;236 Operators.Add(SolutionCreator);237 }238 239 #region Wire BinaryVector Crossovers240 var crossovers = Operators.OfType<IBinaryVectorCrossover>()241 .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorCrossover>(), new TypeEqualityComparer<IBinaryVectorCrossover>())242 .ToList();243 foreach (var xo in crossovers) {244 xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;245 xo.ChildParameter.Hidden = true;246 xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;247 xo.ParentsParameter.Hidden = true;248 }249 Operators.AddRange(crossovers.Except(Operators.OfType<IBinaryVectorCrossover>()));250 #endregion251 #region Wire BinaryVector Manipulators252 var manipulators = Operators.OfType<IBinaryVectorManipulator>()253 .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorManipulator>(), new TypeEqualityComparer<IBinaryVectorManipulator>())254 .ToList();255 foreach (var m in manipulators) {256 m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;257 m.BinaryVectorParameter.Hidden = true;258 }259 Operators.AddRange(manipulators.Except(Operators.OfType<IBinaryVectorManipulator>()));260 #endregion261 #region Wire BinaryVector ShakingOperators262 var shakingOperators = Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()263 .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IBinaryVectorMultiNeighborhoodShakingOperator>())264 .ToList();265 foreach (var so in shakingOperators) {266 so.BinaryVectorParameter.ActualName = paramName;267 so.BinaryVectorParameter.Hidden = true;268 }269 Operators.AddRange(shakingOperators.Except(Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()));270 #endregion271 } else {272 Operators.RemoveAll(x => x is IBinaryVectorCrossover273 || x is IBinaryVectorManipulator274 || x is IBinaryVectorMultiNeighborhoodShakingOperator);275 }276 #endregion277 #region Configure Operators for IntegerVectorEncoding278 var intCreator = newCreator as IIntegerVectorCreator;279 if (intCreator != null) {280 var paramName = intCreator.IntegerVectorParameter.ActualName;281 // do not replace an integer vector creator that was manually set282 if (!(SolutionCreator is IIntegerVectorCreator)283 || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intCreator.IntegerVectorParameter.ActualName) {284 Operators.Remove(SolutionCreator);285 SolutionCreator = newCreator;286 Operators.Add(SolutionCreator);287 }288 289 #region Wire IntegerVector Crossovers290 var crossovers = Operators.OfType<IIntegerVectorCrossover>()291 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>(), new TypeEqualityComparer<IIntegerVectorCrossover>())292 .ToList();293 foreach (var xo in crossovers) {294 xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;295 xo.ChildParameter.Hidden = true;296 xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;297 xo.ParentsParameter.Hidden = true;298 var bx = xo as IBoundedIntegerVectorOperator;299 if (bx != null) {300 bx.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;301 bx.BoundsParameter.Hidden = true;302 }303 }304 Operators.AddRange(crossovers.Except(Operators.OfType<IIntegerVectorCrossover>()));305 #endregion306 #region Wire IntegerVector Manipulators307 var manipulators = Operators.OfType<IIntegerVectorManipulator>()308 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>(), new TypeEqualityComparer<IIntegerVectorManipulator>())309 .ToList();310 foreach (var m in manipulators) {311 m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;312 m.IntegerVectorParameter.Hidden = true;313 var sm = m as ISelfAdaptiveManipulator;314 if (sm != null) {315 var p = sm.StrategyParameterParameter as ILookupParameter;316 if (p != null) {317 p.ActualName = paramName + "Strategy";318 p.Hidden = true;319 }320 }321 var bm = m as IBoundedIntegerVectorOperator;322 if (bm != null) {323 bm.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;324 bm.BoundsParameter.Hidden = true;325 }326 }327 Operators.AddRange(manipulators.Except(Operators.OfType<IIntegerVectorManipulator>()));328 #region Wire IntegerVector StrategyParameters for SelfAdaptiveManipulators329 var strategyOperators = Operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>()330 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IIntegerVectorStdDevStrategyParameterOperator>())331 .ToList();332 var problemSize = ((IntegerEncoding)encoding).Length;333 var b = ((IntegerEncoding)encoding).Bounds;334 var bounds = new DoubleMatrix(b.Rows, b.Columns);335 for (var i = 0; i < bounds.Rows; i++) {336 bounds[i, 1] = (int)Math.Ceiling(0.33 * (b[i, 1] - b[i, 0]));337 bounds[i, 0] = 0;338 if (bounds.Columns > 2) bounds[i, 2] = b[i, 2];339 }340 foreach (var s in strategyOperators) {341 var c = s as IIntegerVectorStdDevStrategyParameterCreator;342 if (c != null) {343 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();344 c.LengthParameter.ActualName = intCreator.LengthParameter.ActualName;345 c.StrategyParameterParameter.ActualName = paramName + "Strategy";346 }347 var m = s as IIntegerVectorStdDevStrategyParameterManipulator;348 if (m != null) {349 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();350 m.StrategyParameterParameter.ActualName = paramName + "Strategy";351 }352 var mm = s as Encodings.IntegerVectorEncoding.StdDevStrategyVectorManipulator;353 if (mm != null) {354 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize));355 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize)));356 }357 var x = s as IIntegerVectorStdDevStrategyParameterCrossover;358 if (x != null) {359 x.ParentsParameter.ActualName = paramName + "Strategy";360 x.StrategyParameterParameter.ActualName = paramName + "Strategy";361 }362 }363 Operators.AddRange(strategyOperators.Except(Operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>()));364 #endregion365 #endregion366 #region Wire IntegerVector ShakingOperators367 var shakingOperators = Operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>()368 .Union(ApplicationManager.Manager.GetInstances<IIntegerVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IIntegerVectorMultiNeighborhoodShakingOperator>())369 .ToList();370 foreach (var so in shakingOperators) {371 so.IntegerVectorParameter.ActualName = paramName;372 so.IntegerVectorParameter.Hidden = true;373 }374 Operators.AddRange(shakingOperators.Except(Operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>()));375 #endregion376 } else {377 Operators.RemoveAll(x => x is IIntegerVectorCrossover378 || x is IIntegerVectorManipulator379 || x is IIntegerVectorStdDevStrategyParameterOperator380 || x is IIntegerVectorMultiNeighborhoodShakingOperator);381 }382 #endregion383 #region Configure Operators for RealVectorEncoding384 var realCreator = newCreator as IRealVectorCreator;385 if (realCreator != null) {386 var paramName = realCreator.RealVectorParameter.ActualName;387 // do not replace a real vector creator that was manually set388 if (!(SolutionCreator is IRealVectorCreator)389 || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {390 Operators.Remove(SolutionCreator);391 SolutionCreator = newCreator;392 Operators.Add(SolutionCreator);393 }394 395 #region Wire RealVector Crossovers396 var crossovers = Operators.OfType<IRealVectorCrossover>()397 .Union(ApplicationManager.Manager.GetInstances<IRealVectorCrossover>(), new TypeEqualityComparer<IRealVectorCrossover>())398 .ToList();399 foreach (var xo in crossovers) {400 xo.ChildParameter.ActualName = paramName;401 xo.ChildParameter.Hidden = true;402 xo.ParentsParameter.ActualName = paramName;403 xo.ParentsParameter.Hidden = true;404 xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;405 xo.BoundsParameter.Hidden = true;406 }407 Operators.AddRange(crossovers.Except(Operators.OfType<IRealVectorCrossover>()));408 #endregion409 #region Wire RealVector Manipulators410 var manipulators = Operators.OfType<IRealVectorManipulator>()411 .Union(ApplicationManager.Manager.GetInstances<IRealVectorManipulator>(), new TypeEqualityComparer<IRealVectorManipulator>())412 .ToList();413 foreach (var m in manipulators) {414 m.RealVectorParameter.ActualName = paramName;415 m.RealVectorParameter.Hidden = true;416 m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;417 m.BoundsParameter.Hidden = true;418 var sm = m as ISelfAdaptiveManipulator;419 if (sm != null) {420 var p = sm.StrategyParameterParameter as ILookupParameter;421 if (p != null) {422 p.ActualName = paramName + "Strategy";423 p.Hidden = true;424 }425 }426 }427 Operators.AddRange(manipulators.Except(Operators.OfType<IRealVectorManipulator>()));428 #region Wire RealVector Strategy Parameters for SelfAdaptiveManipulators429 var strategyOperators = Operators.OfType<IRealVectorStdDevStrategyParameterOperator>()430 .Union(ApplicationManager.Manager.GetInstances<IRealVectorStdDevStrategyParameterOperator>(), new TypeEqualityComparer<IRealVectorStdDevStrategyParameterOperator>())431 .ToList();432 var problemSize = ((RealEncoding)encoding).Length;433 var bounds = (DoubleMatrix)((RealEncoding)encoding).Bounds.Clone();434 for (var i = 0; i < bounds.Rows; i++) {435 bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]);436 bounds[i, 0] = 0;437 }438 foreach (var s in strategyOperators) {439 var c = s as IRealVectorStdDevStrategyParameterCreator;440 if (c != null) {441 c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();442 c.LengthParameter.ActualName = realCreator.LengthParameter.ActualName;443 c.StrategyParameterParameter.ActualName = paramName + "Strategy";444 }445 var m = s as IRealVectorStdDevStrategyParameterManipulator;446 if (m != null) {447 m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();448 m.StrategyParameterParameter.ActualName = paramName + "Strategy";449 }450 var mm = s as Encodings.RealVectorEncoding.StdDevStrategyVectorManipulator;451 if (mm != null) {452 mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize));453 mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize)));454 }455 var x = s as IRealVectorStdDevStrategyParameterCrossover;456 if (x != null) {457 x.ParentsParameter.ActualName = paramName + "Strategy";458 x.StrategyParameterParameter.ActualName = paramName + "Strategy";459 }460 }461 Operators.AddRange(strategyOperators.Except(Operators.OfType<IRealVectorStdDevStrategyParameterOperator>()));462 #endregion463 #endregion464 #region Wire RealVector ParticleCreators465 var particleCreators = Operators.OfType<IRealVectorParticleCreator>()466 .Union(ApplicationManager.Manager.GetInstances<IRealVectorParticleCreator>(), new TypeEqualityComparer<IRealVectorParticleCreator>())467 .ToList();468 foreach (var pc in particleCreators) {469 pc.RealVectorParameter.ActualName = paramName;470 pc.RealVectorParameter.Hidden = true;471 pc.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;472 pc.BoundsParameter.Hidden = true;473 pc.ProblemSizeParameter.ActualName = realCreator.LengthParameter.ActualName;474 pc.ProblemSizeParameter.Hidden = true;475 }476 Operators.AddRange(particleCreators.Except(Operators.OfType<IRealVectorParticleCreator>()));477 #endregion478 #region Wire RealVector ParticleUpdaters479 var particleUpdaters = Operators.OfType<IRealVectorParticleUpdater>()480 .Union(ApplicationManager.Manager.GetInstances<IRealVectorParticleUpdater>(), new TypeEqualityComparer<IRealVectorParticleUpdater>())481 .ToList();482 foreach (var pu in particleUpdaters) {483 pu.RealVectorParameter.ActualName = paramName;484 pu.RealVectorParameter.Hidden = true;485 pu.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;486 pu.BoundsParameter.Hidden = true;487 }488 Operators.AddRange(particleUpdaters.Except(Operators.OfType<IRealVectorParticleUpdater>()));489 #endregion490 #region Wire RealVector SwarmUpdaters491 var swarmUpdaters = Operators.OfType<IRealVectorSwarmUpdater>()492 .Union(ApplicationManager.Manager.GetInstances<IRealVectorSwarmUpdater>(), new TypeEqualityComparer<IRealVectorSwarmUpdater>())493 .ToList();494 foreach (var su in swarmUpdaters) {495 su.RealVectorParameter.ActualName = paramName;496 su.RealVectorParameter.Hidden = true;497 su.MaximizationParameter.ActualName = MaximizationParameter.Name;498 su.MaximizationParameter.Hidden = true;499 }500 Operators.AddRange(swarmUpdaters.Except(Operators.OfType<IRealVectorSwarmUpdater>()));501 #endregion502 #region Wire RealVector ShakingOperators503 var shakingOperators = Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()504 .Union(ApplicationManager.Manager.GetInstances<IRealVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IRealVectorMultiNeighborhoodShakingOperator>())505 .ToList();506 foreach (var so in shakingOperators) {507 so.RealVectorParameter.ActualName = paramName;508 so.RealVectorParameter.Hidden = true;509 }510 Operators.AddRange(shakingOperators.Except(Operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>()));511 #endregion512 } else {513 Operators.RemoveAll(x => x is IRealVectorCrossover514 || x is IRealVectorManipulator515 || x is IRealVectorStdDevStrategyParameterOperator516 || x is IRealVectorParticleCreator517 || x is IRealVectorParticleUpdater518 || x is IRealVectorSwarmUpdater519 || x is IRealVectorMultiNeighborhoodShakingOperator);520 }521 #endregion522 #region Configure Operators for PermutationEncoding523 var permCreator = newCreator as IPermutationCreator;524 if (permCreator != null) {525 var paramName = permCreator.PermutationParameter.ActualName;526 // do not replace a permutation creator that was manually set527 if (!(SolutionCreator is IPermutationCreator)528 || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {529 Operators.Remove(SolutionCreator);530 SolutionCreator = newCreator;531 Operators.Add(SolutionCreator);532 }533 534 #region Wire Permutation Crossovers535 var crossovers = Operators.OfType<IPermutationCrossover>()536 .Union(ApplicationManager.Manager.GetInstances<IPermutationCrossover>(), new TypeEqualityComparer<IPermutationCrossover>())537 .ToList();538 foreach (var xo in crossovers) {539 xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;540 xo.ChildParameter.Hidden = true;541 xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;542 xo.ParentsParameter.Hidden = true;543 }544 Operators.AddRange(crossovers.Except(Operators.OfType<IPermutationCrossover>()));545 #endregion546 #region Wire Permutation Manipulators547 var manipulators = Operators.OfType<IPermutationManipulator>()548 .Union(ApplicationManager.Manager.GetInstances<IPermutationManipulator>(), new TypeEqualityComparer<IPermutationManipulator>())549 .ToList();550 foreach (var m in manipulators) {551 m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;552 m.PermutationParameter.Hidden = true;553 }554 Operators.AddRange(manipulators.Except(Operators.OfType<IPermutationManipulator>()));555 #endregion556 #region Wire Permutation ShakingOperators557 var shakingOperators = Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()558 .Union(ApplicationManager.Manager.GetInstances<IPermutationMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IPermutationMultiNeighborhoodShakingOperator>())559 .ToList();560 foreach (var op in shakingOperators) {561 op.PermutationParameter.ActualName = paramName;562 op.PermutationParameter.Hidden = true;563 }564 Operators.AddRange(shakingOperators.Except(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()));565 #endregion566 } else {567 Operators.RemoveAll(x => x is IPermutationCrossover568 || x is IPermutationManipulator569 || x is IPermutationMultiNeighborhoodShakingOperator);570 }571 #endregion572 }573 574 protected virtual void ConfigureMultiVectorEncodingOperators(MultiEncodingCreator newCreator, MultiEncoding encoding) {575 var newBinParams = new HashSet<string>(newCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName));576 var newIntParams = new HashSet<string>(newCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName));577 var newRealParams = new HashSet<string>(newCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName));578 var newPermParams = new HashSet<string>(newCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName));579 580 var oldCreator = SolutionCreator as MultiEncodingCreator;581 if (oldCreator == null) SolutionCreator = newCreator;582 else {583 #region Configure BinaryVector Creator584 var oldBinParams = new HashSet<string>(oldCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName));585 foreach (var toAdd in newBinParams.Except(oldBinParams)) {586 oldCreator.Operators.Add(newCreator.Operators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == toAdd));587 }588 foreach (var toRemove in oldBinParams.Except(newBinParams)) {589 var op = oldCreator.Operators.OfType<IBinaryVectorCreator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove);590 if (op != null) oldCreator.Operators.Remove(op);591 }592 #endregion593 594 #region Configure IntegerVector Creator595 var oldIntParams = new HashSet<string>(oldCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName));596 foreach (var toAdd in newIntParams.Except(oldIntParams)) {597 oldCreator.Operators.Add(newCreator.Operators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == toAdd));598 }599 foreach (var toRemove in oldIntParams.Except(newIntParams)) {600 var op = oldCreator.Operators.OfType<IIntegerVectorCreator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove);601 if (op != null) oldCreator.Operators.Remove(op);602 }603 #endregion604 605 #region Configure RealVector Creator606 var oldRealParams = new HashSet<string>(oldCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName));607 foreach (var toAdd in newRealParams.Except(oldRealParams)) {608 oldCreator.Operators.Add(newCreator.Operators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == toAdd));609 }610 foreach (var toRemove in oldRealParams.Except(newRealParams)) {611 var op = oldCreator.Operators.OfType<IRealVectorCreator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove);612 if (op != null) oldCreator.Operators.Remove(op);613 }614 #endregion615 616 #region Configure Permutation Creator617 var oldPermParams = new HashSet<string>(oldCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName));618 foreach (var toAdd in newPermParams.Except(oldPermParams)) {619 oldCreator.Operators.Add(newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == toAdd));620 }621 foreach (var toRemove in oldPermParams.Except(newPermParams)) {622 var op = oldCreator.Operators.OfType<IPermutationCreator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove);623 if (op != null) oldCreator.Operators.Remove(op);624 }625 // we also have to sync the permutation type (in case this changes, as it is a value parameter)626 foreach (var intersect in newPermParams.Intersect(oldPermParams)) {627 var oldPermCreator = oldCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect);628 var newPermCreator = newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect);629 oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value;630 }631 #endregion632 633 }634 635 // crossover and manipulator for multi-vector encoding636 // the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator)637 if (Operators.OfType<MultiEncodingCrossover>().Any() && Operators.OfType<MultiEncodingManipulator>().Any()) {638 #region Update existing multi-vector encoding639 #region Update ParameterVector Crossover ...640 foreach (var oldXo in Operators.OfType<MultiEncodingCrossover>()) {641 #region ... for binary parameters642 var oldBinParams = new HashSet<string>(oldXo.Operators.OfType<IBinaryVectorCrossover>().Select(x => x.ChildParameter.ActualName));643 foreach (var toAdd in newBinParams.Except(oldBinParams))644 oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, encoding));645 foreach (var toRemove in oldBinParams.Except(newBinParams)) {646 var op = oldXo.Operators.OfType<IBinaryVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);647 if (op != null) oldXo.Operators.Remove(op);648 }649 #endregion650 #region ... for integer parameters651 var oldIntParams = new HashSet<string>(oldXo.Operators.OfType<IIntegerVectorCrossover>().Select(x => x.ChildParameter.ActualName));652 foreach (var toAdd in newIntParams.Except(oldIntParams))653 oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, encoding));654 foreach (var toRemove in oldIntParams.Except(newIntParams)) {655 var op = oldXo.Operators.OfType<IIntegerVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);656 if (op != null) oldXo.Operators.Remove(op);657 }658 #endregion659 #region ... for real parameters660 var oldRealParams = new HashSet<string>(oldXo.Operators.OfType<IRealVectorCrossover>().Select(x => x.ChildParameter.ActualName));661 foreach (var toAdd in newRealParams.Except(oldRealParams))662 oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, encoding));663 foreach (var toRemove in oldRealParams.Except(newRealParams)) {664 var op = oldXo.Operators.OfType<IRealVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);665 if (op != null) oldXo.Operators.Remove(op);666 }667 #endregion668 #region ... for permutation parameters669 var oldPermParams = new HashSet<string>(oldXo.Operators.OfType<IPermutationCrossover>().Select(x => x.ChildParameter.ActualName));670 foreach (var toAdd in newPermParams.Except(oldPermParams))671 oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, encoding));672 foreach (var toRemove in oldPermParams.Except(newPermParams)) {673 var op = oldXo.Operators.OfType<IPermutationCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);674 if (op != null) oldXo.Operators.Remove(op);675 }676 #endregion677 }678 #endregion679 #region Update ParameterVector Manipulator ...680 foreach (var oldM in Operators.OfType<MultiEncodingManipulator>()) {681 #region ... for binary parameters682 var oldBinParams = new HashSet<string>(oldM.Operators.OfType<IBinaryVectorManipulator>().Select(x => x.BinaryVectorParameter.ActualName));683 foreach (var toAdd in newBinParams.Except(oldBinParams))684 oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, encoding));685 foreach (var toRemove in oldBinParams.Except(newBinParams)) {686 var op = oldM.Operators.OfType<IBinaryVectorManipulator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove);687 if (op != null) oldM.Operators.Remove(op);688 }689 #endregion690 #region ... for integer parameters691 var oldIntParams = new HashSet<string>(oldM.Operators.OfType<IIntegerVectorManipulator>().Select(x => x.IntegerVectorParameter.ActualName));692 foreach (var toAdd in newIntParams.Except(oldIntParams))693 oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, encoding));694 foreach (var toRemove in oldIntParams.Except(newIntParams)) {695 var op = oldM.Operators.OfType<IIntegerVectorManipulator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove);696 if (op != null) oldM.Operators.Remove(op);697 }698 #endregion699 #region ... for real parameters700 var oldRealParams = new HashSet<string>(oldM.Operators.OfType<IRealVectorManipulator>().Select(x => x.RealVectorParameter.ActualName));701 foreach (var toAdd in newRealParams.Except(oldRealParams))702 oldM.Operators.Add(GetDefaultRealManipulator(toAdd, encoding));703 foreach (var toRemove in oldRealParams.Except(newRealParams)) {704 var op = oldM.Operators.OfType<IRealVectorManipulator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove);705 if (op != null) oldM.Operators.Remove(op);706 }707 #endregion708 #region ... for permutation parameters709 var oldPermParams = new HashSet<string>(oldM.Operators.OfType<IPermutationManipulator>().Select(x => x.PermutationParameter.ActualName));710 foreach (var toAdd in newPermParams.Except(oldPermParams))711 oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, encoding));712 foreach (var toRemove in oldPermParams.Except(newPermParams)) {713 var op = oldM.Operators.OfType<IPermutationManipulator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove);714 if (op != null) oldM.Operators.Remove(op);715 }716 #endregion717 }718 #endregion719 #endregion720 } else {721 #region Handle transition from single-vector to multi-vector encoding722 Operators.RemoveAll(x => x is ICrossover);723 Operators.RemoveAll(x => x is IManipulator);724 Operators.RemoveAll(x => x is IStrategyParameterCreator || x is IStrategyParameterManipulator || x is IStrategyParameterCrossover);725 Operators.RemoveAll(x => x is IParticleCreator);726 Operators.RemoveAll(x => x is IParticleUpdater);727 Operators.RemoveAll(x => x is ISwarmUpdater);728 Operators.RemoveAll(x => x is IMultiNeighborhoodShakingOperator);729 730 var crossover = new MultiEncodingCrossover();731 var manipulator = new MultiEncodingManipulator();732 foreach (var enc in encoding.Encodings) {733 if (enc is BinaryEncoding) {734 crossover.Operators.Add(GetDefaultBinaryCrossover(enc.Name, encoding));735 manipulator.Operators.Add(GetDefaultBinaryManipulator(enc.Name, encoding));736 continue;737 }738 var intConfig = enc as IntegerEncoding;739 if (intConfig != null) {740 crossover.Operators.Add(GetDefaultIntegerCrossover(enc.Name, encoding));741 manipulator.Operators.Add(GetDefaultIntegerManipulator(enc.Name, encoding));742 continue;743 }744 var realConfig = enc as RealEncoding;745 if (realConfig != null) {746 crossover.Operators.Add(GetDefaultRealCrossover(enc.Name, encoding));747 manipulator.Operators.Add(GetDefaultRealManipulator(enc.Name, encoding));748 continue;749 }750 var permConfig = enc as PermutationEncoding;751 if (permConfig != null) {752 crossover.Operators.Add(GetDefaultPermutationCrossover(enc.Name, encoding));753 manipulator.Operators.Add(GetDefaultPermutationManipulator(enc.Name, encoding));754 continue;755 }756 throw new InvalidOperationException("Unknown type for parameter " + enc.Name);757 }758 Operators.Add(crossover);759 Operators.Add(manipulator);760 #endregion761 }762 }763 764 #region GetDefaultOperators for Crossovers and Manipulators765 // ReSharper disable RedundantNameQualifier766 protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, MultiEncoding config) {767 var binConfig = (BinaryEncoding)config.Encodings[paramName];768 IBinaryVectorCrossover binXo;769 if (binConfig.Length > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();770 else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();771 binXo.ChildParameter.ActualName = paramName;772 binXo.ParentsParameter.ActualName = paramName;773 return binXo;774 }775 776 protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, MultiEncoding config) {777 var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator();778 binM.BinaryVectorParameter.ActualName = paramName;779 binM.MutationProbabilityParameter.Value = new DoubleValue(0.1);780 return binM;781 }782 783 protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, MultiEncoding config) {784 var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaCrossover();785 intXo.ChildParameter.ActualName = paramName;786 intXo.ParentsParameter.ActualName = paramName;787 intXo.BoundsParameter.ActualName = paramName + "Bounds";788 return intXo;789 }790 791 protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, MultiEncoding configuration) {792 var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator();793 intM.IntegerVectorParameter.ActualName = paramName;794 intM.BoundsParameter.ActualName = paramName + "Bounds";795 intM.ProbabilityParameter.Value = new DoubleValue(0.1);796 return intM;797 }798 799 protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, MultiEncoding configuration) {800 var realXo = new Encodings.RealVectorEncoding.BlendAlphaCrossover();801 realXo.ChildParameter.ActualName = paramName;802 realXo.ParentsParameter.ActualName = paramName;803 realXo.BoundsParameter.ActualName = paramName + "Bounds";804 return realXo;805 }806 807 protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, MultiEncoding configuration) {808 var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator();809 realM.RealVectorParameter.ActualName = paramName;810 realM.BoundsParameter.ActualName = paramName + "Bounds";811 return realM;812 }813 814 protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, MultiEncoding configuration) {815 var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover();816 permXo.ChildParameter.ActualName = paramName;817 permXo.ParentsParameter.ActualName = paramName;818 return permXo;819 }820 821 protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, MultiEncoding configuration) {822 var permM = new Encodings.PermutationEncoding.Swap2Manipulator();823 permM.PermutationParameter.ActualName = paramName;824 return permM;825 }826 // ReSharper restore RedundantNameQualifier827 #endregion828 167 } 829 168 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingCreator.cs
r11484 r11587 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Operators;26 26 using HeuristicLab.Optimization; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Problems.Programmable.Operators; 28 29 29 30 namespace HeuristicLab.Problems.Programmable { 30 [Item("MultiEncodingCreator", "Contains solution creators that together create a multi encoding.")]31 [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")] 31 32 [StorableClass] 32 public class MultiEncodingCreator : MultiOperator<ISolutionCreator>, ISolutionCreator { 33 public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator { 34 [StorableConstructor] 35 private MultiEncodingCreator(bool deserializing) : base(deserializing) { } 33 36 34 [StorableConstructor] 35 protected MultiEncodingCreator(bool deserializing) : base(deserializing) { } 36 protected MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { } 37 private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { } 37 38 public MultiEncodingCreator() { } 38 39 … … 41 42 } 42 43 43 public override IOperation InstrumentedApply() { 44 var baseOperations = base.InstrumentedApply(); 45 return new OperationCollection(new[] { baseOperations }.Concat(Operators.Select(x => ExecutionContext.CreateOperation(x)))); 44 public override void AddEncoding(IEncoding encoding) { 45 base.AddEncoding(encoding); 46 var parameter = GetParameter(encoding); 47 parameter.Value = encoding.SolutionCreator; 48 encoding.SolutionCreatorChanged += Encoding_SolutionCreatorChanged; 49 } 50 51 public override bool RemoveEncoding(IEncoding encoding) { 52 var success = base.RemoveEncoding(encoding); 53 encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged; 54 return success; 55 } 56 57 private void Encoding_SolutionCreatorChanged(object sender, EventArgs e) { 58 var encoding = (IEncoding)sender; 59 var parameter = GetParameter(encoding); 60 61 var oldCreator = parameter.ValidValues.Single(creator => creator.GetType() == encoding.SolutionCreator.GetType()); 62 parameter.ValidValues.Remove(oldCreator); 63 parameter.ValidValues.Add(encoding.SolutionCreator); 64 parameter.Value = encoding.SolutionCreator; 46 65 } 47 66 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingCrossover.cs
r11484 r11587 20 20 #endregion 21 21 22 using System.Linq;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 using HeuristicLab.Operators;26 24 using HeuristicLab.Optimization; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.Programmable.Operators; 28 27 29 28 namespace HeuristicLab.Problems.Programmable { 30 [Item("MultiEncoding Crossover", "Applies different crossovers to cross a multi encoding.")]29 [Item("MultiEncoding Crossover", "Applies different crossovers to cross a multi-encoding.")] 31 30 [StorableClass] 32 public class MultiEncodingCrossover : MultiOperator<ICrossover>, ICrossover { 31 public sealed class MultiEncodingCrossover : MultiEncodingOperator<ICrossover>, ICrossover { 32 [StorableConstructor] 33 private MultiEncodingCrossover(bool deserializing) : base(deserializing) { } 34 private MultiEncodingCrossover(MultiEncodingCrossover original, Cloner cloner) : base(original, cloner) { } 35 public MultiEncodingCrossover() { } 33 36 34 [StorableConstructor] 35 protected MultiEncodingCrossover(bool deserializing) : base(deserializing) { } 36 protected MultiEncodingCrossover(MultiEncodingCrossover original, Cloner cloner) 37 : base(original, cloner) { } 38 public MultiEncodingCrossover() { 39 } 40 41 public override IDeepCloneable Clone(Cloner cloner) { 42 return new MultiEncodingCrossover(this, cloner); 43 } 44 45 public override IOperation InstrumentedApply() { 46 var baseOperations = base.InstrumentedApply(); 47 return new OperationCollection(new[] { baseOperations }.Concat(Operators.Select(x => ExecutionContext.CreateOperation(x)))); 48 } 37 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingCrossover(this, cloner); } 49 38 } 50 39 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingManipulator.cs
r11484 r11587 20 20 #endregion 21 21 22 using System.Linq;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 using HeuristicLab.Operators;26 24 using HeuristicLab.Optimization; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.Programmable.Operators; 28 27 29 28 namespace HeuristicLab.Problems.Programmable { 30 [Item("MultiEncoding Manipulator", "Applies different manipulators to change a multi encoding.")]29 [Item("MultiEncoding Manipulator", "Applies different manipulators to change a multi-encoding.")] 31 30 [StorableClass] 32 public class MultiEncodingManipulator : MultiOperator<IManipulator>, IManipulator { 31 public sealed class MultiEncodingManipulator : MultiEncodingOperator<IManipulator>, IManipulator { 32 [StorableConstructor] 33 private MultiEncodingManipulator(bool deserializing) : base(deserializing) { } 34 private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner) 35 : base(original, cloner) { } 36 public MultiEncodingManipulator() { } 33 37 34 [StorableConstructor] 35 protected MultiEncodingManipulator(bool deserializing) : base(deserializing) { } 36 protected MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner) 37 : base(original, cloner) { } 38 public MultiEncodingManipulator() { 39 } 38 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingManipulator(this, cloner); } 40 39 41 public override IDeepCloneable Clone(Cloner cloner) {42 return new MultiEncodingManipulator(this, cloner);43 }44 45 public override IOperation InstrumentedApply() {46 var baseOperations = base.InstrumentedApply();47 return new OperationCollection(new[] { baseOperations }.Concat(Operators.Select(x => ExecutionContext.CreateOperation(x))));48 }49 40 } 50 41 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r11582 r11587 27 27 using HeuristicLab.Common; 28 28 using HeuristicLab.Core; 29 using HeuristicLab.Data;30 using HeuristicLab.Encodings.BinaryVectorEncoding;31 using HeuristicLab.Encodings.IntegerVectorEncoding;32 using HeuristicLab.Encodings.PermutationEncoding;33 29 using HeuristicLab.Encodings.RealVectorEncoding; 34 30 using HeuristicLab.Optimization; … … 93 89 Operators.Add(new SingleObjectiveAnalyzer()); 94 90 Operators.Add(Evaluator); 95 Operators.Add(SolutionCreator);96 91 97 92 RegisterEventHandlers(); … … 148 143 149 144 IEncoding encoding = definition.Encoding; 145 SolutionCreator = encoding.SolutionCreator; 150 146 151 147 EncodingParameter.Value = encoding; … … 168 164 DynamicEncodingParameters.Clear(); 169 165 170 var creator = ConfigureCreator(encoding); 171 166 DynamicEncodingParameters.AddRange(encoding.Parameters); 172 167 foreach (var param in DynamicEncodingParameters) { 173 168 param.Hidden = true; … … 175 170 } 176 171 177 var multiEncoding = encoding as MultiEncoding; 178 if (multiEncoding != null) { 179 ConfigureMultiVectorEncodingOperators((MultiEncodingCreator)creator, multiEncoding); 180 } else { 181 ConfigureSingleEncodingOperators(creator, encoding); 182 } 172 ConfigureSingleEncodingOperators(); 183 173 UpdateImprovementOperators(); 184 174 UpdateMoveOperators(); 185 175 } 186 176 187 protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) { 188 #region Configure MultiEncoding Creator 189 var multiEncoding = encoding as MultiEncoding; 190 if (multiEncoding != null) { 191 var creator = new MultiEncodingCreator(); 192 foreach (var enc in multiEncoding.Encodings) { 193 if (enc is MultiEncoding) throw new InvalidOperationException("MultiEncoding within a MultiEncoding is not supported."); 194 creator.Operators.Add(ConfigureCreator(enc)); 195 } 196 return creator; 197 } 198 #endregion 199 200 #region Configure RealVector Creator 201 var realEnc = encoding as RealEncoding; 202 if (realEnc != null) { 203 DynamicEncodingParameters.AddRange(realEnc.Parameters); 204 return realEnc.SolutionCreator; 205 } 206 #endregion 207 #region Configure BinaryVector Creator 208 var binEnc = encoding as BinaryEncoding; 209 if (binEnc != null) { 210 DynamicEncodingParameters.AddRange(binEnc.Parameters); 211 return binEnc.SolutionCreator; 212 } 213 #endregion 214 #region Configure IntegerVector Creator 215 var intEnc = encoding as IntegerEncoding; 216 if (intEnc != null) { 217 DynamicEncodingParameters.AddRange(intEnc.Parameters); 218 return intEnc.SolutionCreator; 219 } 220 #endregion 221 #region Configure Permutation Creator 222 var permEnc = encoding as PermutationEncoding; 223 if (permEnc != null) { 224 DynamicEncodingParameters.AddRange(permEnc.Parameters); 225 return permEnc.SolutionCreator; 226 } 227 #endregion 228 throw new ArgumentException(string.Format("Encoding {0} is unknown.", encoding != null ? encoding.GetType().FullName : "(null)")); 229 } 230 231 protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) { 232 // remove all multiencoding operators 233 Operators.RemoveAll(x => x is MultiEncodingCrossover 234 || x is MultiEncodingManipulator 235 || x is MultiEncodingCreator); 236 237 #region Configure Operators for RealVectorEncoding 238 var realEncoding = encoding as RealEncoding; 239 if (realEncoding != null) { 240 // do not replace a real vector creator that was manually set 241 if (!(SolutionCreator is IRealVectorCreator) 242 || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realEncoding.Name) { 243 Operators.Remove(SolutionCreator); 244 SolutionCreator = newCreator; 245 } 246 247 foreach (var su in realEncoding.Operators.OfType<IRealVectorSwarmUpdater>()) { 248 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 249 } 250 } 251 #endregion 252 #region Configure Operators for BinaryVectorEncoding 253 var binEncoding = encoding as BinaryEncoding; 254 if (binEncoding != null) { 255 // do not replace a binary vector creator that was manually set 256 if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != binEncoding.Name) { 257 Operators.Remove(SolutionCreator); 258 SolutionCreator = newCreator; 259 } 260 } 261 #endregion 262 #region Configure Operators for IntegerVectorEncoding 263 var intEncoding = encoding as IntegerEncoding; 264 if (intEncoding != null) { 265 // do not replace an integer vector creator that was manually set 266 if (!(SolutionCreator is IIntegerVectorCreator) 267 || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intEncoding.Name) { 268 Operators.Remove(SolutionCreator); 269 SolutionCreator = newCreator; 270 } 271 } 272 #endregion 273 #region Configure Operators for PermutationEncoding 274 var permEncoding = encoding as PermutationEncoding; 275 if (permEncoding != null) { 276 // do not replace a permutation creator that was manually set 277 if (!(SolutionCreator is IPermutationCreator) 278 || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permEncoding.Name) { 279 Operators.Remove(SolutionCreator); 280 SolutionCreator = newCreator; 281 } 282 } else { 283 Operators.RemoveAll(x => x is IPermutationCrossover 284 || x is IPermutationManipulator 285 || x is IPermutationMultiNeighborhoodShakingOperator); 286 } 287 #endregion 288 } 289 290 protected virtual void ConfigureMultiVectorEncodingOperators(MultiEncodingCreator newCreator, MultiEncoding encoding) { 291 var newBinParams = new HashSet<string>(newCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName)); 292 var newIntParams = new HashSet<string>(newCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName)); 293 var newRealParams = new HashSet<string>(newCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName)); 294 var newPermParams = new HashSet<string>(newCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName)); 295 296 var oldCreator = SolutionCreator as MultiEncodingCreator; 297 if (oldCreator == null) SolutionCreator = newCreator; 298 else { 299 #region Configure BinaryVector Creator 300 var oldBinParams = new HashSet<string>(oldCreator.Operators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName)); 301 foreach (var toAdd in newBinParams.Except(oldBinParams)) { 302 oldCreator.Operators.Add(newCreator.Operators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == toAdd)); 303 } 304 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 305 var op = oldCreator.Operators.OfType<IBinaryVectorCreator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove); 306 if (op != null) oldCreator.Operators.Remove(op); 307 } 308 #endregion 309 310 #region Configure IntegerVector Creator 311 var oldIntParams = new HashSet<string>(oldCreator.Operators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName)); 312 foreach (var toAdd in newIntParams.Except(oldIntParams)) { 313 oldCreator.Operators.Add(newCreator.Operators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == toAdd)); 314 } 315 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 316 var op = oldCreator.Operators.OfType<IIntegerVectorCreator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove); 317 if (op != null) oldCreator.Operators.Remove(op); 318 } 319 #endregion 320 321 #region Configure RealVector Creator 322 var oldRealParams = new HashSet<string>(oldCreator.Operators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName)); 323 foreach (var toAdd in newRealParams.Except(oldRealParams)) { 324 oldCreator.Operators.Add(newCreator.Operators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == toAdd)); 325 } 326 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 327 var op = oldCreator.Operators.OfType<IRealVectorCreator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove); 328 if (op != null) oldCreator.Operators.Remove(op); 329 } 330 #endregion 331 332 #region Configure Permutation Creator 333 var oldPermParams = new HashSet<string>(oldCreator.Operators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName)); 334 foreach (var toAdd in newPermParams.Except(oldPermParams)) { 335 oldCreator.Operators.Add(newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == toAdd)); 336 } 337 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 338 var op = oldCreator.Operators.OfType<IPermutationCreator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove); 339 if (op != null) oldCreator.Operators.Remove(op); 340 } 341 // we also have to sync the permutation type (in case this changes, as it is a value parameter) 342 foreach (var intersect in newPermParams.Intersect(oldPermParams)) { 343 var oldPermCreator = oldCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect); 344 var newPermCreator = newCreator.Operators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == intersect); 345 oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value; 346 } 347 #endregion 348 349 } 350 351 // crossover and manipulator for multi-vector encoding 352 // the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator) 353 if (Operators.OfType<MultiEncodingCrossover>().Any() && Operators.OfType<MultiEncodingManipulator>().Any()) { 354 #region Update existing multi-vector encoding 355 #region Update ParameterVector Crossover ... 356 foreach (var oldXo in Operators.OfType<MultiEncodingCrossover>()) { 357 #region ... for binary parameters 358 var oldBinParams = new HashSet<string>(oldXo.Operators.OfType<IBinaryVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 359 foreach (var toAdd in newBinParams.Except(oldBinParams)) 360 oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, encoding)); 361 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 362 var op = oldXo.Operators.OfType<IBinaryVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 363 if (op != null) oldXo.Operators.Remove(op); 364 } 365 #endregion 366 #region ... for integer parameters 367 var oldIntParams = new HashSet<string>(oldXo.Operators.OfType<IIntegerVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 368 foreach (var toAdd in newIntParams.Except(oldIntParams)) 369 oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, encoding)); 370 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 371 var op = oldXo.Operators.OfType<IIntegerVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 372 if (op != null) oldXo.Operators.Remove(op); 373 } 374 #endregion 375 #region ... for real parameters 376 var oldRealParams = new HashSet<string>(oldXo.Operators.OfType<IRealVectorCrossover>().Select(x => x.ChildParameter.ActualName)); 377 foreach (var toAdd in newRealParams.Except(oldRealParams)) 378 oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, encoding)); 379 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 380 var op = oldXo.Operators.OfType<IRealVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 381 if (op != null) oldXo.Operators.Remove(op); 382 } 383 #endregion 384 #region ... for permutation parameters 385 var oldPermParams = new HashSet<string>(oldXo.Operators.OfType<IPermutationCrossover>().Select(x => x.ChildParameter.ActualName)); 386 foreach (var toAdd in newPermParams.Except(oldPermParams)) 387 oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, encoding)); 388 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 389 var op = oldXo.Operators.OfType<IPermutationCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove); 390 if (op != null) oldXo.Operators.Remove(op); 391 } 392 #endregion 393 } 394 #endregion 395 #region Update ParameterVector Manipulator ... 396 foreach (var oldM in Operators.OfType<MultiEncodingManipulator>()) { 397 #region ... for binary parameters 398 var oldBinParams = new HashSet<string>(oldM.Operators.OfType<IBinaryVectorManipulator>().Select(x => x.BinaryVectorParameter.ActualName)); 399 foreach (var toAdd in newBinParams.Except(oldBinParams)) 400 oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, encoding)); 401 foreach (var toRemove in oldBinParams.Except(newBinParams)) { 402 var op = oldM.Operators.OfType<IBinaryVectorManipulator>().SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove); 403 if (op != null) oldM.Operators.Remove(op); 404 } 405 #endregion 406 #region ... for integer parameters 407 var oldIntParams = new HashSet<string>(oldM.Operators.OfType<IIntegerVectorManipulator>().Select(x => x.IntegerVectorParameter.ActualName)); 408 foreach (var toAdd in newIntParams.Except(oldIntParams)) 409 oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, encoding)); 410 foreach (var toRemove in oldIntParams.Except(newIntParams)) { 411 var op = oldM.Operators.OfType<IIntegerVectorManipulator>().SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove); 412 if (op != null) oldM.Operators.Remove(op); 413 } 414 #endregion 415 #region ... for real parameters 416 var oldRealParams = new HashSet<string>(oldM.Operators.OfType<IRealVectorManipulator>().Select(x => x.RealVectorParameter.ActualName)); 417 foreach (var toAdd in newRealParams.Except(oldRealParams)) 418 oldM.Operators.Add(GetDefaultRealManipulator(toAdd, encoding)); 419 foreach (var toRemove in oldRealParams.Except(newRealParams)) { 420 var op = oldM.Operators.OfType<IRealVectorManipulator>().SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove); 421 if (op != null) oldM.Operators.Remove(op); 422 } 423 #endregion 424 #region ... for permutation parameters 425 var oldPermParams = new HashSet<string>(oldM.Operators.OfType<IPermutationManipulator>().Select(x => x.PermutationParameter.ActualName)); 426 foreach (var toAdd in newPermParams.Except(oldPermParams)) 427 oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, encoding)); 428 foreach (var toRemove in oldPermParams.Except(newPermParams)) { 429 var op = oldM.Operators.OfType<IPermutationManipulator>().SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove); 430 if (op != null) oldM.Operators.Remove(op); 431 } 432 #endregion 433 } 434 #endregion 435 #endregion 436 } else { 437 #region Handle transition from single-vector to multi-vector encoding 438 Operators.RemoveAll(x => x is ICrossover); 439 Operators.RemoveAll(x => x is IManipulator); 440 Operators.RemoveAll(x => x is IStrategyParameterCreator || x is IStrategyParameterManipulator || x is IStrategyParameterCrossover); 441 Operators.RemoveAll(x => x is IParticleCreator); 442 Operators.RemoveAll(x => x is IParticleUpdater); 443 Operators.RemoveAll(x => x is ISwarmUpdater); 444 Operators.RemoveAll(x => x is IMultiNeighborhoodShakingOperator); 445 446 var crossover = new MultiEncodingCrossover(); 447 var manipulator = new MultiEncodingManipulator(); 448 foreach (var enc in encoding.Encodings) { 449 if (enc is BinaryEncoding) { 450 crossover.Operators.Add(GetDefaultBinaryCrossover(enc.Name, encoding)); 451 manipulator.Operators.Add(GetDefaultBinaryManipulator(enc.Name, encoding)); 452 continue; 453 } 454 var intConfig = enc as IntegerEncoding; 455 if (intConfig != null) { 456 crossover.Operators.Add(GetDefaultIntegerCrossover(enc.Name, encoding)); 457 manipulator.Operators.Add(GetDefaultIntegerManipulator(enc.Name, encoding)); 458 continue; 459 } 460 var realConfig = enc as RealEncoding; 461 if (realConfig != null) { 462 crossover.Operators.Add(GetDefaultRealCrossover(enc.Name, encoding)); 463 manipulator.Operators.Add(GetDefaultRealManipulator(enc.Name, encoding)); 464 continue; 465 } 466 var permConfig = enc as PermutationEncoding; 467 if (permConfig != null) { 468 crossover.Operators.Add(GetDefaultPermutationCrossover(enc.Name, encoding)); 469 manipulator.Operators.Add(GetDefaultPermutationManipulator(enc.Name, encoding)); 470 continue; 471 } 472 throw new InvalidOperationException("Unknown type for parameter " + enc.Name); 473 } 474 Operators.Add(crossover); 475 Operators.Add(manipulator); 476 #endregion 177 178 protected virtual void ConfigureSingleEncodingOperators() { 179 foreach (var su in GetOperators().OfType<IRealVectorSwarmUpdater>()) { 180 su.MaximizationParameter.ActualName = MaximizationParameter.Name; 477 181 } 478 182 } … … 512 216 } 513 217 514 #region GetDefaultOperators for Crossovers and Manipulators 515 // ReSharper disable RedundantNameQualifier 516 protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, MultiEncoding config) { 517 var binConfig = (BinaryEncoding)config.Encodings[paramName]; 518 IBinaryVectorCrossover binXo; 519 if (binConfig.Length > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover(); 520 else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover(); 521 binXo.ChildParameter.ActualName = paramName; 522 binXo.ParentsParameter.ActualName = paramName; 523 return binXo; 524 } 525 526 protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, MultiEncoding config) { 527 var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator(); 528 binM.BinaryVectorParameter.ActualName = paramName; 529 binM.MutationProbabilityParameter.Value = new DoubleValue(0.1); 530 return binM; 531 } 532 533 protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, MultiEncoding config) { 534 var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaBetaCrossover(); 535 intXo.ChildParameter.ActualName = paramName; 536 intXo.ParentsParameter.ActualName = paramName; 537 intXo.BoundsParameter.ActualName = paramName + "Bounds"; 538 intXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 539 intXo.MaximizationParameter.ActualName = MaximizationParameter.Name; 540 return intXo; 541 } 542 543 protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, MultiEncoding configuration) { 544 var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator(); 545 intM.IntegerVectorParameter.ActualName = paramName; 546 intM.BoundsParameter.ActualName = paramName + "Bounds"; 547 intM.ProbabilityParameter.Value = new DoubleValue(0.1); 548 return intM; 549 } 550 551 protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, MultiEncoding configuration) { 552 var realXo = new Encodings.RealVectorEncoding.BlendAlphaBetaCrossover(); 553 realXo.ChildParameter.ActualName = paramName; 554 realXo.ParentsParameter.ActualName = paramName; 555 realXo.BoundsParameter.ActualName = paramName + "Bounds"; 556 realXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 557 realXo.MaximizationParameter.ActualName = MaximizationParameter.Name; 558 return realXo; 559 } 560 561 protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, MultiEncoding configuration) { 562 var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator(); 563 realM.RealVectorParameter.ActualName = paramName; 564 realM.BoundsParameter.ActualName = paramName + "Bounds"; 565 return realM; 566 } 567 568 protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, MultiEncoding configuration) { 569 var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover(); 570 permXo.ChildParameter.ActualName = paramName; 571 permXo.ParentsParameter.ActualName = paramName; 572 return permXo; 573 } 574 575 protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, MultiEncoding configuration) { 576 var permM = new Encodings.PermutationEncoding.Swap2Manipulator(); 577 permM.PermutationParameter.ActualName = paramName; 578 return permM; 579 } 580 // ReSharper restore RedundantNameQualifier 581 #endregion 218 582 219 } 583 220 }
Note: See TracChangeset
for help on using the changeset viewer.