[10753] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[10850] | 23 | using System.Collections.Generic;
|
---|
[10753] | 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 31 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 32 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 33 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 34 | using HeuristicLab.Optimization;
|
---|
| 35 | using HeuristicLab.Parameters;
|
---|
| 36 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[10850] | 37 | using HeuristicLab.PluginInfrastructure;
|
---|
[10753] | 38 |
|
---|
| 39 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 40 | [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed.")]
|
---|
| 41 | [Creatable("Problems")]
|
---|
| 42 | [StorableClass]
|
---|
[10850] | 43 | public class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem<ISingleObjectiveProgrammableProblemEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent {
|
---|
[10753] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
| 46 | public static new Image StaticItemImage {
|
---|
[10856] | 47 | get { return Common.Resources.VSImageLibrary.Script; }
|
---|
[10753] | 48 | }
|
---|
| 49 |
|
---|
[10850] | 50 | public new ParameterCollection Parameters {
|
---|
| 51 | get { return base.Parameters; }
|
---|
[10753] | 52 | }
|
---|
[10850] | 53 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
| 54 | get { return Parameters; }
|
---|
[10753] | 55 | }
|
---|
| 56 |
|
---|
[10850] | 57 | public IValueParameter<SingleObjectiveScript> ScriptParameter {
|
---|
| 58 | get { return (IValueParameter<SingleObjectiveScript>)Parameters["Script"]; }
|
---|
[10753] | 59 | }
|
---|
| 60 |
|
---|
[10850] | 61 | public IValueParameter<Configuration> ConfigurationParameter {
|
---|
| 62 | get { return (IValueParameter<Configuration>)Parameters["Configuration"]; }
|
---|
[10753] | 63 | }
|
---|
| 64 |
|
---|
| 65 | [Storable]
|
---|
[10850] | 66 | protected List<IParameter> DynamicConfigurationParameters;
|
---|
[10753] | 67 |
|
---|
| 68 | [StorableConstructor]
|
---|
[10850] | 69 | protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
|
---|
[10753] | 70 |
|
---|
[10850] | 71 | protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
|
---|
[10753] | 72 | : base(original, cloner) {
|
---|
[10850] | 73 | DynamicConfigurationParameters = original.DynamicConfigurationParameters.Select(cloner.Clone).ToList();
|
---|
[10753] | 74 | RegisterEventHandlers();
|
---|
| 75 | }
|
---|
| 76 | public SingleObjectiveProgrammableProblem()
|
---|
[10754] | 77 | : base(new SingleObjectiveEvaluator(), new ParameterVectorCreater()) {
|
---|
[10856] | 78 | Parameters.Add(new ValueParameter<SingleObjectiveScript>("Script", "Defines the problem.", new SingleObjectiveScript() { Name = Name }));
|
---|
[10850] | 79 | Parameters.Add(new ValueParameter<Configuration>("Configuration", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
|
---|
[10753] | 80 |
|
---|
[10850] | 81 | DynamicConfigurationParameters = new List<IParameter>();
|
---|
[10753] | 82 |
|
---|
| 83 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
| 84 | Operators.Add(Evaluator);
|
---|
| 85 | Operators.Add(SolutionCreator);
|
---|
| 86 |
|
---|
| 87 | RegisterEventHandlers();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 91 | return new SingleObjectiveProgrammableProblem(this, cloner);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[10856] | 95 | // ReSharper disable UnusedMember.Local
|
---|
[10753] | 96 | private void AfterDeserialization() {
|
---|
| 97 | RegisterEventHandlers();
|
---|
| 98 | }
|
---|
[10856] | 99 | // ReSharper restore UnusedMember.Local
|
---|
[10753] | 100 |
|
---|
| 101 | private void RegisterEventHandlers() {
|
---|
| 102 | ScriptParameter.ValueChanged += ScriptParameterOnValueChanged;
|
---|
| 103 | RegisterScriptInstanceChanges();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | private void ScriptParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
| 107 | RegisterScriptInstanceChanges();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void RegisterScriptInstanceChanges() {
|
---|
| 111 | ScriptParameter.Value.InstanceChanged += ScriptOnInstanceChanged;
|
---|
| 112 | ScriptParameter.Value.NameChanged += ScriptOnNameChanged;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private void ScriptOnNameChanged(object sender, EventArgs eventArgs) {
|
---|
| 116 | if (sender != ScriptParameter.Value) return;
|
---|
[10856] | 117 | Name = ScriptParameter.Value.Name;
|
---|
[10753] | 118 | }
|
---|
| 119 |
|
---|
| 120 | protected override void OnNameChanged() {
|
---|
| 121 | base.OnNameChanged();
|
---|
[10856] | 122 | ScriptParameter.Value.Name = Name;
|
---|
[10753] | 123 | }
|
---|
| 124 |
|
---|
[10850] | 125 | protected virtual void ScriptOnInstanceChanged(object sender, EventArgs eventArgs) {
|
---|
[10753] | 126 | var instance = ScriptParameter.Value.Instance;
|
---|
| 127 | if (instance == null) return;
|
---|
| 128 |
|
---|
[10850] | 129 | var configuration = instance.GetConfiguration();
|
---|
| 130 | ConfigurationParameter.Value = configuration;
|
---|
[10856] | 131 | Maximization.Value = instance.IsMaximizationProblem;
|
---|
[10753] | 132 |
|
---|
[10856] | 133 | var solutionCreators = UpdateDynamicConfigurationParameters(configuration);
|
---|
| 134 |
|
---|
| 135 | if (solutionCreators.Count == 1) {
|
---|
| 136 | UpdateSingleVectorEncodingOperators(solutionCreators);
|
---|
| 137 | } else {
|
---|
| 138 | UpdateMultiVectorEncodingOperators(solutionCreators, configuration);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | protected virtual List<ISolutionCreator> UpdateDynamicConfigurationParameters(Configuration configuration) {
|
---|
[10850] | 143 | foreach (var param in DynamicConfigurationParameters)
|
---|
| 144 | if (Parameters.Contains(param)) Parameters.Remove(param);
|
---|
| 145 | DynamicConfigurationParameters.Clear();
|
---|
[10753] | 146 |
|
---|
[10850] | 147 | var solutionCreators = new List<ISolutionCreator>();
|
---|
[10855] | 148 | foreach (var param in configuration.Parameters) {
|
---|
[10856] | 149 | /*
|
---|
| 150 | * Binary Vectors
|
---|
| 151 | */
|
---|
[10855] | 152 | var binConfig = param.Value as BinaryParameterConfiguration;
|
---|
[10850] | 153 | if (binConfig != null) {
|
---|
[10855] | 154 | var p = new ValueParameter<IntValue>(param.Key + "Length", binConfig.Length);
|
---|
[10850] | 155 | DynamicConfigurationParameters.Add(p);
|
---|
[10753] | 156 |
|
---|
[10850] | 157 | var creator = new RandomBinaryVectorCreator();
|
---|
[10855] | 158 | creator.BinaryVectorParameter.ActualName = param.Key;
|
---|
[10850] | 159 | creator.LengthParameter.ActualName = p.Name;
|
---|
| 160 | solutionCreators.Add(creator);
|
---|
| 161 | }
|
---|
[10856] | 162 | /*
|
---|
| 163 | * Integer Vectors
|
---|
| 164 | */
|
---|
[10855] | 165 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
[10850] | 166 | if (intConfig != null) {
|
---|
[10855] | 167 | var l = new ValueParameter<IntValue>(param.Key + "Length", intConfig.Length);
|
---|
| 168 | var b = new ValueParameter<IntMatrix>(param.Key + "Bounds", intConfig.Bounds);
|
---|
[10850] | 169 | DynamicConfigurationParameters.Add(l);
|
---|
| 170 | DynamicConfigurationParameters.Add(b);
|
---|
[10753] | 171 |
|
---|
[10850] | 172 | var creator = new UniformRandomIntegerVectorCreator();
|
---|
[10855] | 173 | creator.IntegerVectorParameter.ActualName = param.Key;
|
---|
[10850] | 174 | creator.LengthParameter.ActualName = l.Name;
|
---|
| 175 | creator.BoundsParameter.ActualName = b.Name;
|
---|
| 176 | solutionCreators.Add(creator);
|
---|
| 177 | }
|
---|
[10856] | 178 | /*
|
---|
| 179 | * Real Vectors
|
---|
| 180 | */
|
---|
[10855] | 181 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
[10850] | 182 | if (realConfig != null) {
|
---|
[10855] | 183 | var l = new ValueParameter<IntValue>(param.Key + "Length", realConfig.Length);
|
---|
| 184 | var b = new ValueParameter<DoubleMatrix>(param.Key + "Bounds", realConfig.Bounds);
|
---|
[10850] | 185 | DynamicConfigurationParameters.Add(l);
|
---|
| 186 | DynamicConfigurationParameters.Add(b);
|
---|
| 187 |
|
---|
| 188 | var creator = new UniformRandomRealVectorCreator();
|
---|
[10855] | 189 | creator.RealVectorParameter.ActualName = param.Key;
|
---|
[10850] | 190 | creator.LengthParameter.ActualName = l.Name;
|
---|
| 191 | creator.BoundsParameter.ActualName = b.Name;
|
---|
| 192 | solutionCreators.Add(creator);
|
---|
| 193 | }
|
---|
[10856] | 194 | /*
|
---|
| 195 | * Permutations
|
---|
| 196 | */
|
---|
[10855] | 197 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
[10850] | 198 | if (permConfig != null) {
|
---|
[10855] | 199 | var l = new ValueParameter<IntValue>(param.Key + "Length", permConfig.Length);
|
---|
[10850] | 200 | DynamicConfigurationParameters.Add(l);
|
---|
| 201 |
|
---|
| 202 | var creator = new RandomPermutationCreator();
|
---|
[10855] | 203 | creator.PermutationParameter.ActualName = param.Key;
|
---|
[10850] | 204 | creator.LengthParameter.ActualName = l.Name;
|
---|
| 205 | creator.PermutationTypeParameter.Value = permConfig.Type;
|
---|
| 206 | solutionCreators.Add(creator);
|
---|
| 207 | }
|
---|
[10753] | 208 | }
|
---|
| 209 |
|
---|
[10850] | 210 | foreach (var param in DynamicConfigurationParameters) {
|
---|
| 211 | param.Hidden = true;
|
---|
| 212 | Parameters.Add(param);
|
---|
[10753] | 213 | }
|
---|
[10856] | 214 | return solutionCreators;
|
---|
| 215 | }
|
---|
[10753] | 216 |
|
---|
[10856] | 217 | protected virtual void UpdateSingleVectorEncodingOperators(List<ISolutionCreator> solutionCreators) {
|
---|
| 218 | var newCreator = solutionCreators.Single();
|
---|
| 219 | var binCreator = newCreator as IBinaryVectorCreator;
|
---|
| 220 | if (binCreator != null) {
|
---|
| 221 | var paramName = binCreator.BinaryVectorParameter.ActualName;
|
---|
| 222 | // do not replace a binary vector creator that was manually set
|
---|
| 223 | if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != paramName) {
|
---|
| 224 | Operators.Remove(SolutionCreator);
|
---|
| 225 | SolutionCreator = newCreator;
|
---|
| 226 | Operators.Add(SolutionCreator);
|
---|
[10850] | 227 | }
|
---|
[10856] | 228 | Operators.RemoveAll(x => x is ICrossover && !(x is IBinaryVectorCrossover) || x is IBinaryVectorCrossover && ((IBinaryVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
| 229 | Operators.RemoveAll(x => x is IManipulator && !(x is IBinaryVectorManipulator) || x is IBinaryVectorManipulator && ((IBinaryVectorManipulator)x).BinaryVectorParameter.ActualName != paramName);
|
---|
| 230 | var crossovers = ApplicationManager.Manager.GetInstances<IBinaryVectorCrossover>().ToList();
|
---|
| 231 | var manipulators = ApplicationManager.Manager.GetInstances<IBinaryVectorManipulator>().ToList();
|
---|
| 232 | foreach (var xo in crossovers) {
|
---|
| 233 | xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
| 234 | xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
[10850] | 235 | }
|
---|
[10856] | 236 | foreach (var m in manipulators)
|
---|
| 237 | m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
| 238 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 239 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 240 | }
|
---|
| 241 | var intCreator = newCreator as IIntegerVectorCreator;
|
---|
| 242 | if (intCreator != null) {
|
---|
| 243 | var paramName = intCreator.IntegerVectorParameter.ActualName;
|
---|
| 244 | // do not replace an integer vector creator that was manually set
|
---|
| 245 | if (!(SolutionCreator is IIntegerVectorCreator)
|
---|
| 246 | || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intCreator.IntegerVectorParameter.ActualName) {
|
---|
| 247 | Operators.Remove(SolutionCreator);
|
---|
| 248 | SolutionCreator = newCreator;
|
---|
| 249 | Operators.Add(SolutionCreator);
|
---|
| 250 | }
|
---|
| 251 | Operators.RemoveAll(x => x is ICrossover && !(x is IIntegerVectorCrossover) || x is IIntegerVectorCrossover && ((IIntegerVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
| 252 | Operators.RemoveAll(x => x is IManipulator && !(x is IIntegerVectorManipulator) || x is IIntegerVectorManipulator && ((IIntegerVectorManipulator)x).IntegerVectorParameter.ActualName != paramName);
|
---|
| 253 | var crossovers = ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>().ToList();
|
---|
| 254 | var manipulators = ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>().ToList();
|
---|
| 255 | foreach (var xo in crossovers) {
|
---|
| 256 | xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
| 257 | xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
| 258 | }
|
---|
| 259 | foreach (var m in manipulators)
|
---|
| 260 | m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
| 261 | foreach (var bo in crossovers.OfType<IBoundedIntegerVectorOperator>()
|
---|
| 262 | .Concat(manipulators.OfType<IBoundedIntegerVectorOperator>())
|
---|
| 263 | .ToList()) {
|
---|
| 264 | bo.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;
|
---|
| 265 | }
|
---|
| 266 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 267 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 268 | }
|
---|
| 269 | var realCreator = newCreator as IRealVectorCreator;
|
---|
| 270 | if (realCreator != null) {
|
---|
| 271 | var paramName = realCreator.RealVectorParameter.ActualName;
|
---|
| 272 | // do not replace a real vector creator that was manually set
|
---|
| 273 | if (!(SolutionCreator is IRealVectorCreator)
|
---|
| 274 | || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {
|
---|
| 275 | Operators.Remove(SolutionCreator);
|
---|
| 276 | SolutionCreator = newCreator;
|
---|
| 277 | Operators.Add(SolutionCreator);
|
---|
| 278 | }
|
---|
| 279 | Operators.RemoveAll(x => x is ICrossover && !(x is IRealVectorCrossover) || x is IRealVectorCrossover && ((IRealVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
| 280 | Operators.RemoveAll(x => x is IManipulator && !(x is IRealVectorManipulator) || x is IRealVectorManipulator && ((IRealVectorManipulator)x).RealVectorParameter.ActualName != paramName);
|
---|
| 281 | var crossovers = ApplicationManager.Manager.GetInstances<IRealVectorCrossover>().ToList();
|
---|
| 282 | var manipulators = ApplicationManager.Manager.GetInstances<IRealVectorManipulator>().ToList();
|
---|
| 283 | foreach (var xo in crossovers) {
|
---|
| 284 | xo.ChildParameter.ActualName = realCreator.RealVectorParameter.ActualName;
|
---|
| 285 | xo.ParentsParameter.ActualName = realCreator.RealVectorParameter.ActualName;
|
---|
| 286 | xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
| 287 | }
|
---|
| 288 | foreach (var m in manipulators) {
|
---|
| 289 | m.RealVectorParameter.ActualName = realCreator.RealVectorParameter.ActualName;
|
---|
| 290 | m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
| 291 | }
|
---|
| 292 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 293 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 294 | }
|
---|
| 295 | var permCreator = newCreator as IPermutationCreator;
|
---|
| 296 | if (permCreator != null) {
|
---|
| 297 | var paramName = permCreator.PermutationParameter.ActualName;
|
---|
| 298 | // do not replace a permutation creator that was manually set
|
---|
| 299 | if (!(SolutionCreator is IPermutationCreator)
|
---|
| 300 | || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {
|
---|
| 301 | Operators.Remove(SolutionCreator);
|
---|
| 302 | SolutionCreator = newCreator;
|
---|
| 303 | Operators.Add(SolutionCreator);
|
---|
| 304 | }
|
---|
| 305 | Operators.RemoveAll(x => x is ICrossover && !(x is IPermutationCrossover) || x is IPermutationCrossover && ((IPermutationCrossover)x).ChildParameter.ActualName != paramName);
|
---|
| 306 | Operators.RemoveAll(x => x is IManipulator && !(x is IPermutationManipulator) || x is IPermutationManipulator && ((IPermutationManipulator)x).PermutationParameter.ActualName != paramName);
|
---|
| 307 | var crossovers = ApplicationManager.Manager.GetInstances<IPermutationCrossover>().ToList();
|
---|
| 308 | var manipulators = ApplicationManager.Manager.GetInstances<IPermutationManipulator>().ToList();
|
---|
| 309 | foreach (var xo in crossovers) {
|
---|
| 310 | xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
| 311 | xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
| 312 | }
|
---|
| 313 | foreach (var m in manipulators)
|
---|
| 314 | m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
| 315 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 316 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | protected virtual void UpdateMultiVectorEncodingOperators(List<ISolutionCreator> solutionCreators, Configuration configuration) {
|
---|
| 321 | var oldCreator = SolutionCreator as ParameterVectorCreater;
|
---|
| 322 | var newCreator = new ParameterVectorCreater();
|
---|
| 323 |
|
---|
| 324 | /*
|
---|
| 325 | * Binary Vectors
|
---|
| 326 | */
|
---|
| 327 | var newBinParams = new HashSet<string>(solutionCreators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName));
|
---|
| 328 | if (oldCreator != null) {
|
---|
| 329 | // we want to reuse the old creator
|
---|
| 330 | var oldParams = new HashSet<string>(oldCreator.Operators.OfType<IBinaryVectorCreator>()
|
---|
| 331 | .Select(x => x.BinaryVectorParameter.ActualName));
|
---|
| 332 | foreach (var toAdd in newBinParams.Except(oldParams)) {
|
---|
| 333 | var paramName = toAdd;
|
---|
| 334 | oldCreator.Operators.Add(
|
---|
| 335 | solutionCreators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == paramName));
|
---|
| 336 | }
|
---|
| 337 | foreach (var toRemove in oldParams.Except(newBinParams)) {
|
---|
| 338 | var paramName = toRemove;
|
---|
| 339 | var op =
|
---|
| 340 | oldCreator.Operators.OfType<IBinaryVectorCreator>()
|
---|
| 341 | .SingleOrDefault(x => x.BinaryVectorParameter.ActualName == paramName);
|
---|
| 342 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
| 343 | }
|
---|
| 344 | } else {
|
---|
| 345 | // we will use the new creator
|
---|
| 346 | foreach (var binCreator in solutionCreators.OfType<IBinaryVectorCreator>()) {
|
---|
| 347 | newCreator.Operators.Add(binCreator);
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /*
|
---|
| 352 | * Integer Vectors
|
---|
| 353 | */
|
---|
| 354 | var newIntParams = new HashSet<string>(solutionCreators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName));
|
---|
| 355 | if (oldCreator != null) {
|
---|
| 356 | // we want to reuse the old creator
|
---|
| 357 | var oldParams = new HashSet<string>(oldCreator
|
---|
| 358 | .Operators.OfType<IIntegerVectorCreator>()
|
---|
| 359 | .Select(x => x.IntegerVectorParameter.ActualName));
|
---|
| 360 | foreach (var toAdd in newIntParams.Except(oldParams)) {
|
---|
| 361 | var paramName = toAdd;
|
---|
| 362 | oldCreator.Operators.Add(
|
---|
| 363 | solutionCreators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == paramName));
|
---|
| 364 | }
|
---|
| 365 | foreach (var toRemove in oldParams.Except(newIntParams)) {
|
---|
| 366 | var paramName = toRemove;
|
---|
| 367 | var op =
|
---|
| 368 | oldCreator.Operators.OfType<IIntegerVectorCreator>()
|
---|
| 369 | .SingleOrDefault(x => x.IntegerVectorParameter.ActualName == paramName);
|
---|
| 370 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
| 371 | }
|
---|
| 372 | } else {
|
---|
| 373 | // we will use the new creator
|
---|
| 374 | foreach (var intCreator in solutionCreators.OfType<IIntegerVectorCreator>()) {
|
---|
| 375 | newCreator.Operators.Add(intCreator);
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /*
|
---|
| 380 | * Real Vectors
|
---|
| 381 | */
|
---|
| 382 | var newRealParams = new HashSet<string>(solutionCreators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName));
|
---|
| 383 | if (oldCreator != null) {
|
---|
| 384 | // we want to reuse the old creator
|
---|
| 385 | var oldParams = new HashSet<string>(oldCreator
|
---|
| 386 | .Operators.OfType<IRealVectorCreator>()
|
---|
| 387 | .Select(x => x.RealVectorParameter.ActualName));
|
---|
| 388 | foreach (var toAdd in newRealParams.Except(oldParams)) {
|
---|
| 389 | var paramName = toAdd;
|
---|
| 390 | oldCreator.Operators.Add(
|
---|
| 391 | solutionCreators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == paramName));
|
---|
| 392 | }
|
---|
| 393 | foreach (var toRemove in oldParams.Except(newRealParams)) {
|
---|
| 394 | var paramName = toRemove;
|
---|
| 395 | var op =
|
---|
| 396 | oldCreator.Operators.OfType<IRealVectorCreator>()
|
---|
| 397 | .SingleOrDefault(x => x.RealVectorParameter.ActualName == paramName);
|
---|
| 398 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
| 399 | }
|
---|
| 400 | } else {
|
---|
| 401 | // we will use the new creator
|
---|
| 402 | foreach (var realCreator in solutionCreators.OfType<IRealVectorCreator>()) {
|
---|
| 403 | newCreator.Operators.Add(realCreator);
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /*
|
---|
| 408 | * Permutations
|
---|
| 409 | */
|
---|
| 410 | var newPermParams = new HashSet<string>(solutionCreators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName));
|
---|
| 411 | if (oldCreator != null) {
|
---|
| 412 | // we want to reuse the old creator
|
---|
| 413 | var oldParams = new HashSet<string>(oldCreator
|
---|
| 414 | .Operators.OfType<IPermutationCreator>()
|
---|
| 415 | .Select(x => x.PermutationParameter.ActualName));
|
---|
| 416 | foreach (var toAdd in newPermParams.Except(oldParams)) {
|
---|
| 417 | var paramName = toAdd;
|
---|
| 418 | oldCreator.Operators.Add(
|
---|
| 419 | solutionCreators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == paramName));
|
---|
| 420 | }
|
---|
| 421 | foreach (var toRemove in oldParams.Except(newPermParams)) {
|
---|
| 422 | var paramName = toRemove;
|
---|
| 423 | var op =
|
---|
| 424 | oldCreator.Operators.OfType<IPermutationCreator>()
|
---|
| 425 | .SingleOrDefault(x => x.PermutationParameter.ActualName == paramName);
|
---|
| 426 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
| 427 | }
|
---|
| 428 | // we also have to sync the permutation type (in case this changes, as it is a value parameter)
|
---|
| 429 | foreach (var intersect in newPermParams.Intersect(oldParams)) {
|
---|
| 430 | var paramName = intersect;
|
---|
| 431 | var oldPermCreator = oldCreator.Operators.OfType<IPermutationCreator>()
|
---|
| 432 | .Single(x => x.PermutationParameter.ActualName == paramName);
|
---|
| 433 | var newPermCreator = solutionCreators.OfType<IPermutationCreator>()
|
---|
| 434 | .Single(x => x.PermutationParameter.ActualName == paramName);
|
---|
| 435 | oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value;
|
---|
| 436 | }
|
---|
| 437 | } else {
|
---|
| 438 | // we will use the new creator
|
---|
| 439 | foreach (var permCreator in solutionCreators.OfType<IPermutationCreator>()) {
|
---|
| 440 | newCreator.Operators.Add(permCreator);
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | if (oldCreator == null) SolutionCreator = newCreator;
|
---|
| 445 |
|
---|
| 446 | // crossover and manipulator for multi-vector encoding
|
---|
| 447 | // the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator)
|
---|
| 448 | if (Operators.OfType<ParameterVectorCrossover>().Any() && Operators.OfType<ParameterVectorManipulator>().Any()) {
|
---|
| 449 | // Update the crossovers
|
---|
| 450 | foreach (var oldXo in Operators.OfType<ParameterVectorCrossover>()) {
|
---|
| 451 | /*
|
---|
| 452 | * Binary Vectors
|
---|
| 453 | */
|
---|
| 454 | var oldBinParams = new HashSet<string>(oldXo.Operators.OfType<IBinaryVectorCrossover>()
|
---|
| 455 | .Select(x => x.ChildParameter.ActualName));
|
---|
| 456 | foreach (var toAdd in newBinParams.Except(oldBinParams))
|
---|
| 457 | oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, configuration));
|
---|
| 458 | foreach (var toRemove in oldBinParams.Except(newBinParams)) {
|
---|
| 459 | var op =
|
---|
| 460 | oldXo.Operators.OfType<IBinaryVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
| 461 | if (op != null) oldXo.Operators.Remove(op);
|
---|
[10850] | 462 | }
|
---|
[10856] | 463 |
|
---|
| 464 | /*
|
---|
| 465 | * Integer Vectors
|
---|
| 466 | */
|
---|
| 467 | var oldIntParams = new HashSet<string>(oldXo.Operators.OfType<IIntegerVectorCrossover>()
|
---|
| 468 | .Select(x => x.ChildParameter.ActualName));
|
---|
| 469 | foreach (var toAdd in newIntParams.Except(oldIntParams))
|
---|
| 470 | oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, configuration));
|
---|
| 471 | foreach (var toRemove in oldIntParams.Except(newIntParams)) {
|
---|
| 472 | var op =
|
---|
| 473 | oldXo.Operators.OfType<IIntegerVectorCrossover>()
|
---|
| 474 | .SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
| 475 | if (op != null) oldXo.Operators.Remove(op);
|
---|
[10850] | 476 | }
|
---|
[10856] | 477 |
|
---|
| 478 | /*
|
---|
| 479 | * Real Vectors
|
---|
| 480 | */
|
---|
| 481 | var oldRealParams = new HashSet<string>(oldXo.Operators.OfType<IRealVectorCrossover>()
|
---|
| 482 | .Select(x => x.ChildParameter.ActualName));
|
---|
| 483 | foreach (var toAdd in newRealParams.Except(oldRealParams))
|
---|
| 484 | oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, configuration));
|
---|
| 485 | foreach (var toRemove in oldRealParams.Except(newRealParams)) {
|
---|
| 486 | var op =
|
---|
| 487 | oldXo.Operators.OfType<IRealVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
| 488 | if (op != null) oldXo.Operators.Remove(op);
|
---|
[10850] | 489 | }
|
---|
[10856] | 490 |
|
---|
| 491 | /*
|
---|
| 492 | * Permutations
|
---|
| 493 | */
|
---|
| 494 | var oldPermParams = new HashSet<string>(oldXo.Operators.OfType<IPermutationCrossover>()
|
---|
| 495 | .Select(x => x.ChildParameter.ActualName));
|
---|
| 496 | foreach (var toAdd in newPermParams.Except(oldPermParams))
|
---|
| 497 | oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, configuration));
|
---|
| 498 | foreach (var toRemove in oldPermParams.Except(newPermParams)) {
|
---|
| 499 | var op =
|
---|
| 500 | oldXo.Operators.OfType<IPermutationCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
| 501 | if (op != null) oldXo.Operators.Remove(op);
|
---|
[10850] | 502 | }
|
---|
| 503 | }
|
---|
[10856] | 504 | // Update the manipulators
|
---|
| 505 | foreach (var oldM in Operators.OfType<ParameterVectorManipulator>()) {
|
---|
| 506 | /*
|
---|
| 507 | * Binary Vectors
|
---|
| 508 | */
|
---|
| 509 | var oldBinParams = new HashSet<string>(oldM.Operators.OfType<IBinaryVectorManipulator>()
|
---|
[10850] | 510 | .Select(x => x.BinaryVectorParameter.ActualName));
|
---|
[10856] | 511 | foreach (var toAdd in newBinParams.Except(oldBinParams))
|
---|
| 512 | oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, configuration));
|
---|
| 513 | foreach (var toRemove in oldBinParams.Except(newBinParams)) {
|
---|
| 514 | var op =
|
---|
| 515 | oldM.Operators.OfType<IBinaryVectorManipulator>()
|
---|
| 516 | .SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove);
|
---|
| 517 | if (op != null) oldM.Operators.Remove(op);
|
---|
[10850] | 518 | }
|
---|
[10753] | 519 |
|
---|
[10856] | 520 | /*
|
---|
| 521 | * Integer Vectors
|
---|
| 522 | */
|
---|
| 523 | var oldIntParams = new HashSet<string>(oldM.Operators.OfType<IIntegerVectorManipulator>()
|
---|
[10850] | 524 | .Select(x => x.IntegerVectorParameter.ActualName));
|
---|
[10856] | 525 | foreach (var toAdd in newIntParams.Except(oldIntParams))
|
---|
| 526 | oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, configuration));
|
---|
| 527 | foreach (var toRemove in oldIntParams.Except(newIntParams)) {
|
---|
| 528 | var op =
|
---|
| 529 | oldM.Operators.OfType<IIntegerVectorManipulator>()
|
---|
| 530 | .SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove);
|
---|
| 531 | if (op != null) oldM.Operators.Remove(op);
|
---|
[10850] | 532 | }
|
---|
[10753] | 533 |
|
---|
[10856] | 534 | /*
|
---|
| 535 | * Real Vectors
|
---|
| 536 | */
|
---|
| 537 | var oldRealParams = new HashSet<string>(oldM.Operators.OfType<IRealVectorManipulator>()
|
---|
[10850] | 538 | .Select(x => x.RealVectorParameter.ActualName));
|
---|
[10856] | 539 | foreach (var toAdd in newRealParams.Except(oldRealParams))
|
---|
| 540 | oldM.Operators.Add(GetDefaultRealManipulator(toAdd, configuration));
|
---|
| 541 | foreach (var toRemove in oldRealParams.Except(newRealParams)) {
|
---|
| 542 | var op =
|
---|
| 543 | oldM.Operators.OfType<IRealVectorManipulator>()
|
---|
| 544 | .SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove);
|
---|
| 545 | if (op != null) oldM.Operators.Remove(op);
|
---|
[10850] | 546 | }
|
---|
[10753] | 547 |
|
---|
[10856] | 548 | /*
|
---|
| 549 | * Permutations
|
---|
| 550 | */
|
---|
| 551 | var oldPermParams = new HashSet<string>(oldM.Operators.OfType<IPermutationManipulator>()
|
---|
[10850] | 552 | .Select(x => x.PermutationParameter.ActualName));
|
---|
[10856] | 553 | foreach (var toAdd in newPermParams.Except(oldPermParams))
|
---|
| 554 | oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, configuration));
|
---|
| 555 | foreach (var toRemove in oldPermParams.Except(newPermParams)) {
|
---|
| 556 | var op =
|
---|
| 557 | oldM.Operators.OfType<IPermutationManipulator>()
|
---|
| 558 | .SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove);
|
---|
| 559 | if (op != null) oldM.Operators.Remove(op);
|
---|
[10850] | 560 | }
|
---|
[10856] | 561 | }
|
---|
| 562 | } else {
|
---|
| 563 | // change from single-vector encoding to multi-vector encoding
|
---|
| 564 | Operators.RemoveAll(x => x is ICrossover);
|
---|
| 565 | Operators.RemoveAll(x => x is IManipulator);
|
---|
| 566 | var crossover = new ParameterVectorCrossover();
|
---|
| 567 | var manipulator = new ParameterVectorManipulator();
|
---|
| 568 | foreach (var param in configuration.Parameters) {
|
---|
| 569 | if (param.Value is BinaryParameterConfiguration) {
|
---|
| 570 | crossover.Operators.Add(GetDefaultBinaryCrossover(param.Key, configuration));
|
---|
| 571 | manipulator.Operators.Add(GetDefaultBinaryManipulator(param.Key, configuration));
|
---|
| 572 | continue;
|
---|
[10850] | 573 | }
|
---|
[10856] | 574 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
| 575 | if (intConfig != null) {
|
---|
| 576 | crossover.Operators.Add(GetDefaultIntegerCrossover(param.Key, configuration));
|
---|
| 577 | manipulator.Operators.Add(GetDefaultIntegerManipulator(param.Key, configuration));
|
---|
| 578 | continue;
|
---|
[10850] | 579 | }
|
---|
[10856] | 580 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
| 581 | if (realConfig != null) {
|
---|
| 582 | crossover.Operators.Add(GetDefaultRealCrossover(param.Key, configuration));
|
---|
| 583 | manipulator.Operators.Add(GetDefaultRealManipulator(param.Key, configuration));
|
---|
| 584 | continue;
|
---|
[10850] | 585 | }
|
---|
[10856] | 586 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
| 587 | if (permConfig != null) {
|
---|
| 588 | crossover.Operators.Add(GetDefaultPermutationCrossover(param.Key, configuration));
|
---|
| 589 | manipulator.Operators.Add(GetDefaultPermutationManipulator(param.Key, configuration));
|
---|
| 590 | continue;
|
---|
[10855] | 591 | }
|
---|
[10856] | 592 | throw new InvalidOperationException("Unknown type for parameter " + param.Key);
|
---|
[10855] | 593 | }
|
---|
[10856] | 594 | Operators.Add(crossover);
|
---|
| 595 | Operators.Add(manipulator);
|
---|
[10753] | 596 | }
|
---|
| 597 | }
|
---|
[10856] | 598 |
|
---|
| 599 | // ReSharper disable RedundantNameQualifier
|
---|
| 600 | protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, Configuration config) {
|
---|
| 601 | var binConfig = (BinaryParameterConfiguration)config.Parameters[paramName];
|
---|
| 602 | IBinaryVectorCrossover binXo;
|
---|
| 603 | if (binConfig.Length.Value > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
|
---|
| 604 | else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();
|
---|
| 605 | binXo.ChildParameter.ActualName = paramName;
|
---|
| 606 | binXo.ParentsParameter.ActualName = paramName;
|
---|
| 607 | return binXo;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, Configuration config) {
|
---|
| 611 | var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator();
|
---|
| 612 | binM.BinaryVectorParameter.ActualName = paramName;
|
---|
| 613 | binM.MutationProbabilityParameter.Value = new DoubleValue(0.1);
|
---|
| 614 | return binM;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, Configuration config) {
|
---|
| 618 | var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaBetaCrossover();
|
---|
| 619 | intXo.ChildParameter.ActualName = paramName;
|
---|
| 620 | intXo.ParentsParameter.ActualName = paramName;
|
---|
| 621 | intXo.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
| 622 | intXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 623 | intXo.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 624 | return intXo;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, Configuration configuration) {
|
---|
| 628 | var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator();
|
---|
| 629 | intM.IntegerVectorParameter.ActualName = paramName;
|
---|
| 630 | intM.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
| 631 | intM.ProbabilityParameter.Value = new DoubleValue(0.1);
|
---|
| 632 | return intM;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, Configuration configuration) {
|
---|
| 636 | var realXo = new Encodings.RealVectorEncoding.BlendAlphaBetaCrossover();
|
---|
| 637 | realXo.ChildParameter.ActualName = paramName;
|
---|
| 638 | realXo.ParentsParameter.ActualName = paramName;
|
---|
| 639 | realXo.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
| 640 | realXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 641 | realXo.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 642 | return realXo;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, Configuration configuration) {
|
---|
| 646 | var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator();
|
---|
| 647 | realM.RealVectorParameter.ActualName = paramName;
|
---|
| 648 | realM.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
| 649 | return realM;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, Configuration configuration) {
|
---|
| 653 | var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover();
|
---|
| 654 | permXo.ChildParameter.ActualName = paramName;
|
---|
| 655 | permXo.ParentsParameter.ActualName = paramName;
|
---|
| 656 | return permXo;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, Configuration configuration) {
|
---|
| 660 | var permM = new Encodings.PermutationEncoding.Swap2Manipulator();
|
---|
| 661 | permM.PermutationParameter.ActualName = paramName;
|
---|
| 662 | return permM;
|
---|
| 663 | }
|
---|
| 664 | // ReSharper restore RedundantNameQualifier
|
---|
[10753] | 665 | }
|
---|
| 666 | }
|
---|