Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16782


Ignore:
Timestamp:
04/15/19 14:13:13 (6 years ago)
Author:
abeham
Message:

#2740: Fixed problem with local random number generator and multi-encoding in ALPS

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs

    r16565 r16782  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Analysis;
    2627using HeuristicLab.Collections;
     
    3233using HeuristicLab.Optimization.Operators;
    3334using HeuristicLab.Parameters;
    34 using HEAL.Attic;
    3535using HeuristicLab.PluginInfrastructure;
    3636using HeuristicLab.Random;
     
    501501    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
    502502      base.Problem_OperatorsChanged(sender, e);
     503      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
    503504      ParameterizeIterationBasedOperators();
    504505      UpdateCrossovers();
  • trunk/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs

    r16565 r16782  
    2222using System;
    2323using System.Linq;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    26 using HEAL.Attic;
     27using HeuristicLab.Parameters;
    2728
    2829namespace HeuristicLab.Optimization {
    2930  [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")]
    3031  [StorableType("E261B506-6F74-4BC4-8164-5ACE20FBC319")]
    31   public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator {
     32  public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator, IStochasticOperator {
     33    public ILookupParameter<IRandom> RandomParameter {
     34      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     35    }
     36
     37    public override string OperatorPrefix => "Creator";
     38
    3239    [StorableConstructor]
    3340    private MultiEncodingCreator(StorableConstructorFlag _) : base(_) { }
    3441
    3542    private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { }
    36     public MultiEncodingCreator() { }
     43    public MultiEncodingCreator() {
     44      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     45    }
    3746
    3847    public override IDeepCloneable Clone(Cloner cloner) {
    3948      return new MultiEncodingCreator(this, cloner);
     49    }
     50
     51    [StorableHook(HookType.AfterDeserialization)]
     52    private void AfterDeserialization() {
     53      if (!Parameters.ContainsKey("Random")) {
     54        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     55      }
    4056    }
    4157
  • trunk/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCrossover.cs

    r16565 r16782  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    24 using HEAL.Attic;
     25using HeuristicLab.Parameters;
    2526
    2627namespace HeuristicLab.Optimization {
    2728  [Item("MultiEncoding Crossover", "Applies different crossovers to cross a multi-encoding.")]
    2829  [StorableType("BB0A04E2-899D-460C-82A2-5E4CEEDE8996")]
    29   public sealed class MultiEncodingCrossover : MultiEncodingOperator<ICrossover>, ICrossover {
     30  public sealed class MultiEncodingCrossover : MultiEncodingOperator<ICrossover>, ICrossover, IStochasticOperator {
     31    public ILookupParameter<IRandom> RandomParameter {
     32      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     33    }
     34
     35    public override string OperatorPrefix => "Crossover";
     36
    3037    [StorableConstructor]
    3138    private MultiEncodingCrossover(StorableConstructorFlag _) : base(_) { }
    3239    private MultiEncodingCrossover(MultiEncodingCrossover original, Cloner cloner) : base(original, cloner) { }
    33     public MultiEncodingCrossover() { }
     40    public MultiEncodingCrossover() {
     41      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     42    }
    3443
    3544    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingCrossover(this, cloner); }
     45
     46    [StorableHook(HookType.AfterDeserialization)]
     47    private void AfterDeserialization() {
     48      if (!Parameters.ContainsKey("Random")) {
     49        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     50      }
     51    }
    3652  }
    3753}
  • trunk/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingManipulator.cs

    r16565 r16782  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    24 using HEAL.Attic;
     25using HeuristicLab.Parameters;
    2526
    2627namespace HeuristicLab.Optimization {
    2728  [Item("MultiEncoding Manipulator", "Applies different manipulators to change a multi-encoding.")]
    2829  [StorableType("574D0530-47E8-4FD9-8AC8-B8EA2DE3C203")]
    29   public sealed class MultiEncodingManipulator : MultiEncodingOperator<IManipulator>, IManipulator {
     30  public sealed class MultiEncodingManipulator : MultiEncodingOperator<IManipulator>, IManipulator, IStochasticOperator {
     31    public ILookupParameter<IRandom> RandomParameter {
     32      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     33    }
     34
     35    public override string OperatorPrefix => "Manipulator";
     36
    3037    [StorableConstructor]
    3138    private MultiEncodingManipulator(StorableConstructorFlag _) : base(_) { }
    3239    private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner)
    3340      : base(original, cloner) { }
    34     public MultiEncodingManipulator() { }
     41    public MultiEncodingManipulator() {
     42      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     43    }
    3544
    3645    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingManipulator(this, cloner); }
    3746
     47    [StorableHook(HookType.AfterDeserialization)]
     48    private void AfterDeserialization() {
     49      if (!Parameters.ContainsKey("Random")) {
     50        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator used by the individual operators."));
     51      }
     52    }
     53
    3854  }
    3955}
  • trunk/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

    r16565 r16782  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Operators;
    2829using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Optimization {
     
    3939    }
    4040
     41    public abstract string OperatorPrefix { get; }
     42
    4143    [StorableConstructor]
    42     protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) {
    43     }
    44 
     44    protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) { }
    4545    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
    4646      : base(original, cloner) {
     
    4949        encoding.OperatorsChanged += Encoding_OperatorsChanged;
    5050    }
    51 
    5251    protected MultiEncodingOperator() : base() { }
    5352
    5453    [StorableHook(HookType.AfterDeserialization)]
    5554    private void AfterDeserialization() {
    56       foreach (var encoding in encodings)
     55      foreach (var encoding in encodings) {
     56        // BackwardsCompatibility3.3
     57        #region Backwards compatible code, remove with 3.4
     58        if (Parameters.ContainsKey(encoding.Name) && !Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) {
     59          var oldParam = (IConstrainedValueParameter<T>)Parameters[encoding.Name];
     60          var selected = oldParam.Value;
     61          Parameters.Remove(oldParam);
     62          var newParam = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(oldParam.ValidValues));
     63          newParam.Value = selected;
     64          Parameters.Add(newParam);
     65          oldParam.ValidValues.Clear();
     66        }
     67        #endregion
    5768        encoding.OperatorsChanged += Encoding_OperatorsChanged;
     69      }
    5870    }
    5971
    60 
    6172    public override IOperation Apply() {
    62       var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
     73      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateChildOperation(op));
    6374      return new OperationCollection(operations);
    6475    }
    6576
    6677    public virtual void AddEncoding(IEncoding encoding) {
    67       if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
     78      if (Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
    6879
    6980      encodings.Add(encoding);
    7081      encoding.OperatorsChanged += Encoding_OperatorsChanged;
    7182
    72       var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
     83      var param = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
    7384      param.Value = param.ValidValues.First();
    7485      Parameters.Add(param);
     
    7889      if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
    7990      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
    80       return Parameters.Remove(encoding.Name);
     91      return Parameters.Remove(OperatorPrefix + "." + encoding.Name);
    8192    }
    8293
    8394    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
    84       if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
     95      if (!Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
    8596
    86       return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
     97      return (IConstrainedValueParameter<T>)Parameters[OperatorPrefix + "." + encoding.Name];
    8798    }
    8899
Note: See TracChangeset for help on using the changeset viewer.