Changeset 11587


Ignore:
Timestamp:
11/26/14 13:24:20 (8 years ago)
Author:
mkommend
Message:

#2174: Implemented multi-encoding operators and adapated wiring of operators in the programmable problems.

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  
    4848    }
    4949    public override IEnumerable<IValueParameter> Parameters {
    50       get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter }); }
     50      get { return new IValueParameter[] { LengthParameter }; }
    5151    }
    5252    #endregion
     
    7373      : base(name) {
    7474      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
     75      SolutionCreator = new RandomBinaryVectorCreator();
    7576      RegisterParameterEvents();
    7677      DiscoverOperators();
    77       SolutionCreator = Operators.OfType<RandomBinaryVectorCreator>().Single();
    7878    }
    7979
     
    106106      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
    107107      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();
    109109
    110110      ConfigureOperators(newOperators);
    111       encodingOperators.AddRange(newOperators);
     111      foreach (var @operator in newOperators)
     112        encodingOperators.Add(@operator);
    112113    }
    113114    #endregion
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11582 r11587  
    3737    }
    3838
    39     public virtual IEnumerable<IValueParameter> Parameters {
    40       get { return Enumerable.Empty<IValueParameter>(); }
    41     }
     39    public abstract IEnumerable<IValueParameter> Parameters { get; }
    4240
    43     protected List<IOperator> encodingOperators = new List<IOperator>();
     41    protected HashSet<IOperator> encodingOperators = new HashSet<IOperator>(new TypeEqualityComparer<IOperator>());
    4442    [Storable]
    4543    public IEnumerable<IOperator> Operators {
    4644      get { return encodingOperators; }
    47       protected set { encodingOperators = new List<IOperator>(value); }
     45      private set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); }
    4846    }
    49 
    5047
    5148    ISolutionCreator IEncoding.SolutionCreator {
    5249      get { return SolutionCreator; }
    5350      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()));
    5552        SolutionCreator = (T)value;
    5653      }
     
    6461        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
    6562        if (solutionCreator == value) return;
     63        encodingOperators.Remove(solutionCreator);
     64        encodingOperators.Add(value);
    6665        solutionCreator = value;
    6766        OnSolutionCreatorChanged();
     
    7473    protected Encoding(Encoding<T> original, Cloner cloner)
    7574      : base(original, cloner) {
    76       encodingOperators = original.Operators.Select(cloner.Clone).ToList();
     75      encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone));
    7776      solutionCreator = cloner.Clone(original.solutionCreator);
    7877    }
     
    8079
    8180
     81    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
     82    public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
    8283
    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;
    9085    protected virtual void OnSolutionCreatorChanged() {
    9186      ConfigureOperator(SolutionCreator);
     87      var handler = SolutionCreatorChanged;
     88      if (handler != null) handler(this, EventArgs.Empty);
    9289    }
    9390
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs

    r11582 r11587  
    6161
    6262    public override IEnumerable<IValueParameter> Parameters {
    63       get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }
     63      get { return new IValueParameter[] { LengthParameter, BoundsParameter }; }
    6464    }
    6565    #endregion
     
    9494      if (min >= max) throw new ArgumentException("min must be less than max", "min");
    9595      if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step");
     96
    9697      Length = length;
    9798      Bounds = new IntMatrix(1, step.HasValue ? 3 : 2);
     
    99100      Bounds[0, 1] = max;
    100101      if (step.HasValue) Bounds[0, 2] = step.Value;
     102
     103      SolutionCreator = new UniformRandomIntegerVectorCreator();
    101104      RegisterParameterEvents();
    102105      DiscoverOperators();
    103       SolutionCreator = Operators.OfType<UniformRandomIntegerVectorCreator>().Single();
    104106    }
    105107    public IntegerEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)
     
    109111      if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step");
    110112      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
    111114      Length = length;
    112115      Bounds = new IntMatrix(min.Count, step != null ? 3 : 2);
     
    116119        if (step != null) Bounds[i, 2] = step[i];
    117120      }
     121
     122      SolutionCreator = new UniformRandomIntegerVectorCreator();
    118123      RegisterParameterEvents();
    119124      DiscoverOperators();
    120       SolutionCreator = Operators.OfType<UniformRandomIntegerVectorCreator>().Single();
    121125    }
    122126
     
    159163      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
    160164      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();
    162166
    163167      ConfigureOperators(newOperators);
    164       encodingOperators.AddRange(newOperators);
     168      foreach (var @operator in newOperators)
     169        encodingOperators.Add(@operator);
    165170    }
    166171    #endregion
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs

    r11559 r11587  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    26 using HeuristicLab.Encodings.PermutationEncoding;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.PluginInfrastructure;
     29using HeuristicLab.Problems.Programmable.Interfaces;
    2830
    2931namespace HeuristicLab.Problems.Programmable {
     
    3133  [StorableClass]
    3234  public class MultiEncoding : Encoding<MultiEncodingCreator> {
     35
     36    private readonly List<IEncoding> encodings;
     37
    3338    [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    }
    3547
    3648    [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); }
    3855    protected MultiEncoding(MultiEncoding original, Cloner cloner)
    3956      : base(original, cloner) {
    40       Encodings = cloner.Clone(original.Encodings);
     57      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
    4158    }
    4259    public MultiEncoding()
    4360      : 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);
    4566    }
    4667
    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);
    5072
    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      }
    5476      return this;
    5577    }
    5678
    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;
    6185    }
    6286
    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) {
    6888
    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;
    8589    }
    8690  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs

    r11582 r11587  
    6060
    6161    public override IEnumerable<IValueParameter> Parameters {
    62       get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, PermutationTypeParameter }); }
     62      get { return new IValueParameter[] { LengthParameter, PermutationTypeParameter }; }
    6363    }
    6464    #endregion
     
    8989      RegisterParameterEvents();
    9090    }
     91
    9192    public PermutationEncoding(string name, int length, PermutationTypes type)
    9293      : base(name) {
    9394      Length = length;
    9495      Type = type;
     96
     97      SolutionCreator = new RandomPermutationCreator();
    9598      RegisterParameterEvents();
    9699      DiscoverOperators();
    97       SolutionCreator = Operators.OfType<RandomPermutationCreator>().Single();
    98100    }
    99101
     
    138140      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
    139141      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();
    141143
    142144      ConfigureOperators(newOperators);
    143       encodingOperators.AddRange(newOperators);
     145      foreach (var @operator in newOperators)
     146        encodingOperators.Add(@operator);
    144147    }
    145148    #endregion
    146149
    147150    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    148       base.ConfigureOperators(operators);
    149151      ConfigureCreators(Operators.OfType<IPermutationCreator>());
    150152      ConfigureCrossovers(Operators.OfType<IPermutationCrossover>());
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11582 r11587  
    6262
    6363    public override IEnumerable<IValueParameter> Parameters {
    64       get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }
     64      get { return new IValueParameter[] { LengthParameter, BoundsParameter }; }
    6565    }
    6666    #endregion
     
    101101      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
    102102      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
     103
     104      SolutionCreator = new UniformRandomRealVectorCreator();
    103105      RegisterParameterEvents();
    104106      DiscoverOperators();
    105       SolutionCreator = Operators.OfType<UniformRandomRealVectorCreator>().Single();
    106107    }
    107108
     
    119120      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
    120121      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
     122
     123      SolutionCreator = new UniformRandomRealVectorCreator();
    121124      RegisterParameterEvents();
    122125      DiscoverOperators();
    123       SolutionCreator = Operators.OfType<UniformRandomRealVectorCreator>().Single();
    124126    }
    125127
     
    167169      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
    168170      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();
    170172
    171173      ConfigureOperators(newOperators);
    172       encodingOperators.AddRange(newOperators);
     174      foreach (var @operator in newOperators)
     175        encodingOperators.Add(@operator);
    173176
    174177      foreach (var strategyVectorCreator in Operators.OfType<IRealVectorStdDevStrategyParameterCreator>())
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11574 r11587  
    136136    <Compile Include="Helper.cs" />
    137137    <Compile Include="Interfaces\IEncoding.cs" />
     138    <Compile Include="Interfaces\IMultiEncodingOperator.cs" />
    138139    <Compile Include="Interfaces\ISingleObjectiveMoveOperator.cs" />
    139140    <Compile Include="Interfaces\IMultiObjectiveProblemDefinition.cs" />
     
    144145    <Compile Include="Operators\MultiEncodingManipulator.cs" />
    145146    <Compile Include="Operators\MultiEncodingCrossover.cs" />
     147    <Compile Include="Operators\MultiEncodingOperator.cs" />
    146148    <Compile Include="Operators\SingleObjectiveMoveEvaluator.cs" />
    147149    <Compile Include="Operators\SingleObjectiveMoveGenerator.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

    r11582 r11587  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Core;
     
    3435    void ConfigureOperator(IOperator @operator);
    3536    void ConfigureOperators(IEnumerable<IOperator> operators);
     37
     38    event EventHandler SolutionCreatorChanged;
     39    //event EventHandler OperatorsChanged;
    3640  }
    3741}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11582 r11587  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.BinaryVectorEncoding;
    30 using HeuristicLab.Encodings.IntegerVectorEncoding;
    31 using HeuristicLab.Encodings.PermutationEncoding;
    32 using HeuristicLab.Encodings.RealVectorEncoding;
    3329using HeuristicLab.Optimization;
    3430using HeuristicLab.Parameters;
    3531using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    36 using HeuristicLab.PluginInfrastructure;
    3732
    3833namespace HeuristicLab.Problems.Programmable {
     
    8681      Operators.Add(new MultiObjectiveAnalyzer());
    8782      Operators.Add(Evaluator);
    88       Operators.Add(SolutionCreator);
    8983
    9084      RegisterEventHandlers();
     
    156150      }
    157151
     152      SolutionCreator = encoding.SolutionCreator;
     153
    158154      foreach (var param in DynamicEncodingParameters)
    159155        if (Parameters.Contains(param)) Parameters.Remove(param);
    160156      DynamicEncodingParameters.Clear();
    161157
    162       var creator = ConfigureCreator(encoding);
     158      DynamicEncodingParameters.AddRange(encoding.Parameters);
     159
    163160
    164161      foreach (var param in DynamicEncodingParameters) {
     
    167164      }
    168165
    169       var multiEncoding = encoding as MultiEncoding;
    170       if (multiEncoding != null) {
    171         ConfigureMultiVectorEncodingOperators((MultiEncodingCreator)creator, multiEncoding);
    172       } else {
    173         ConfigureSingleEncodingOperators(creator, encoding);
    174       }
    175166    }
    176 
    177     protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) {
    178       #region Configure MultiEncoding Creator
    179       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       #endregion
    189       #region Configure RealVector Creator
    190       var realEnc = encoding as RealEncoding;
    191       if (realEnc != null) {
    192         DynamicEncodingParameters.AddRange(realEnc.Parameters);
    193         return realEnc.SolutionCreator;
    194       }
    195       #endregion
    196       #region Configure BinaryVector Creator
    197       var binEnc = encoding as BinaryEncoding;
    198       if (binEnc != null) {
    199         DynamicEncodingParameters.AddRange(binEnc.Parameters);
    200         return binEnc.SolutionCreator;
    201       }
    202       #endregion
    203       #region Configure IntegerVector Creator
    204       var intEnc = encoding as IntegerEncoding;
    205       if (intEnc != null) {
    206         DynamicEncodingParameters.AddRange(intEnc.Parameters);
    207         return intEnc.SolutionCreator;
    208       }
    209       #endregion
    210       #region Configure Permutation Creator
    211       var permEnc = encoding as PermutationEncoding;
    212       if (permEnc != null) {
    213         DynamicEncodingParameters.AddRange(permEnc.Parameters);
    214         return permEnc.SolutionCreator;
    215       }
    216       #endregion
    217 
    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 operators
    224       Operators.RemoveAll(x => x is MultiEncodingCrossover
    225                             || x is MultiEncodingManipulator
    226                             || x is MultiEncodingCreator);
    227 
    228       #region Configure Operators for BinaryVectorEncoding
    229       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 set
    233         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 Crossovers
    240         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         #endregion
    251         #region Wire BinaryVector Manipulators
    252         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         #endregion
    261         #region Wire BinaryVector ShakingOperators
    262         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         #endregion
    271       } else {
    272         Operators.RemoveAll(x => x is IBinaryVectorCrossover
    273           || x is IBinaryVectorManipulator
    274           || x is IBinaryVectorMultiNeighborhoodShakingOperator);
    275       }
    276       #endregion
    277       #region Configure Operators for IntegerVectorEncoding
    278       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 set
    282         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 Crossovers
    290         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         #endregion
    306         #region Wire IntegerVector Manipulators
    307         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 SelfAdaptiveManipulators
    329         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         #endregion
    365         #endregion
    366         #region Wire IntegerVector ShakingOperators
    367         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         #endregion
    376       } else {
    377         Operators.RemoveAll(x => x is IIntegerVectorCrossover
    378           || x is IIntegerVectorManipulator
    379           || x is IIntegerVectorStdDevStrategyParameterOperator
    380           || x is IIntegerVectorMultiNeighborhoodShakingOperator);
    381       }
    382       #endregion
    383       #region Configure Operators for RealVectorEncoding
    384       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 set
    388         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 Crossovers
    396         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         #endregion
    409         #region Wire RealVector Manipulators
    410         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 SelfAdaptiveManipulators
    429         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         #endregion
    463         #endregion
    464         #region Wire RealVector ParticleCreators
    465         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         #endregion
    478         #region Wire RealVector ParticleUpdaters
    479         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         #endregion
    490         #region Wire RealVector SwarmUpdaters
    491         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         #endregion
    502         #region Wire RealVector ShakingOperators
    503         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         #endregion
    512       } else {
    513         Operators.RemoveAll(x => x is IRealVectorCrossover
    514           || x is IRealVectorManipulator
    515           || x is IRealVectorStdDevStrategyParameterOperator
    516           || x is IRealVectorParticleCreator
    517           || x is IRealVectorParticleUpdater
    518           || x is IRealVectorSwarmUpdater
    519           || x is IRealVectorMultiNeighborhoodShakingOperator);
    520       }
    521       #endregion
    522       #region Configure Operators for PermutationEncoding
    523       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 set
    527         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 Crossovers
    535         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         #endregion
    546         #region Wire Permutation Manipulators
    547         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         #endregion
    556         #region Wire Permutation ShakingOperators
    557         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         #endregion
    566       } else {
    567         Operators.RemoveAll(x => x is IPermutationCrossover
    568           || x is IPermutationManipulator
    569           || x is IPermutationMultiNeighborhoodShakingOperator);
    570       }
    571       #endregion
    572     }
    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 Creator
    584         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         #endregion
    593 
    594         #region Configure IntegerVector Creator
    595         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         #endregion
    604 
    605         #region Configure RealVector Creator
    606         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         #endregion
    615 
    616         #region Configure Permutation Creator
    617         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         #endregion
    632 
    633       }
    634 
    635       // crossover and manipulator for multi-vector encoding
    636       // 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 encoding
    639         #region Update ParameterVector Crossover ...
    640         foreach (var oldXo in Operators.OfType<MultiEncodingCrossover>()) {
    641           #region ... for binary parameters
    642           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           #endregion
    650           #region ... for integer parameters
    651           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           #endregion
    659           #region ... for real parameters
    660           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           #endregion
    668           #region ... for permutation parameters
    669           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           #endregion
    677         }
    678         #endregion
    679         #region Update ParameterVector Manipulator ...
    680         foreach (var oldM in Operators.OfType<MultiEncodingManipulator>()) {
    681           #region ... for binary parameters
    682           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           #endregion
    690           #region ... for integer parameters
    691           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           #endregion
    699           #region ... for real parameters
    700           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           #endregion
    708           #region ... for permutation parameters
    709           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           #endregion
    717         }
    718         #endregion
    719         #endregion
    720       } else {
    721         #region Handle transition from single-vector to multi-vector encoding
    722         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         #endregion
    761       }
    762     }
    763 
    764     #region GetDefaultOperators for Crossovers and Manipulators
    765     // ReSharper disable RedundantNameQualifier
    766     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 RedundantNameQualifier
    827     #endregion
    828167  }
    829168}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingCreator.cs

    r11484 r11587  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Operators;
    2626using HeuristicLab.Optimization;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Problems.Programmable.Operators;
    2829
    2930namespace HeuristicLab.Problems.Programmable {
    30   [Item("MultiEncodingCreator", "Contains solution creators that together create a multiencoding.")]
     31  [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")]
    3132  [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) { }
    3336
    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) { }
    3738    public MultiEncodingCreator() { }
    3839
     
    4142    }
    4243
    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;
    4665    }
    4766  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingCrossover.cs

    r11484 r11587  
    2020#endregion
    2121
    22 using System.Linq;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    25 using HeuristicLab.Operators;
    2624using HeuristicLab.Optimization;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Problems.Programmable.Operators;
    2827
    2928namespace HeuristicLab.Problems.Programmable {
    30   [Item("MultiEncoding Crossover", "Applies different crossovers to cross a multiencoding.")]
     29  [Item("MultiEncoding Crossover", "Applies different crossovers to cross a multi-encoding.")]
    3130  [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() { }
    3336
    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); }
    4938  }
    5039}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingManipulator.cs

    r11484 r11587  
    2020#endregion
    2121
    22 using System.Linq;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    25 using HeuristicLab.Operators;
    2624using HeuristicLab.Optimization;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Problems.Programmable.Operators;
    2827
    2928namespace HeuristicLab.Problems.Programmable {
    30   [Item("MultiEncoding Manipulator", "Applies different manipulators to change a multiencoding.")]
     29  [Item("MultiEncoding Manipulator", "Applies different manipulators to change a multi-encoding.")]
    3130  [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() { }
    3337
    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); }
    4039
    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     }
    4940  }
    5041}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11582 r11587  
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Core;
    29 using HeuristicLab.Data;
    30 using HeuristicLab.Encodings.BinaryVectorEncoding;
    31 using HeuristicLab.Encodings.IntegerVectorEncoding;
    32 using HeuristicLab.Encodings.PermutationEncoding;
    3329using HeuristicLab.Encodings.RealVectorEncoding;
    3430using HeuristicLab.Optimization;
     
    9389      Operators.Add(new SingleObjectiveAnalyzer());
    9490      Operators.Add(Evaluator);
    95       Operators.Add(SolutionCreator);
    9691
    9792      RegisterEventHandlers();
     
    148143
    149144      IEncoding encoding = definition.Encoding;
     145      SolutionCreator = encoding.SolutionCreator;
    150146
    151147      EncodingParameter.Value = encoding;
     
    168164      DynamicEncodingParameters.Clear();
    169165
    170       var creator = ConfigureCreator(encoding);
    171 
     166      DynamicEncodingParameters.AddRange(encoding.Parameters);
    172167      foreach (var param in DynamicEncodingParameters) {
    173168        param.Hidden = true;
     
    175170      }
    176171
    177       var multiEncoding = encoding as MultiEncoding;
    178       if (multiEncoding != null) {
    179         ConfigureMultiVectorEncodingOperators((MultiEncodingCreator)creator, multiEncoding);
    180       } else {
    181         ConfigureSingleEncodingOperators(creator, encoding);
    182       }
     172      ConfigureSingleEncodingOperators();
    183173      UpdateImprovementOperators();
    184174      UpdateMoveOperators();
    185175    }
    186176
    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;
    477181      }
    478182    }
     
    512216    }
    513217
    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
    582219  }
    583220}
Note: See TracChangeset for help on using the changeset viewer.