Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/23/20 16:21:55 (4 years ago)
Author:
abeham
Message:

#2521:

  • Moving solution creator parameter from problems to algorithms (breaking wiring in some HeuristicOptimizationProblems)
  • Disallowing evaluator or encoding changes in encoding-specific base problems (to avoid confusion in derived problems whether this needs to be handled or not)
  • Added private set to ReferenceParameter property (serialization)
Location:
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationEngineAlgorithm.cs

    r17226 r17695  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HEAL.Attic;
     26using HeuristicLab.Parameters;
    2627
    2728namespace HeuristicLab.Optimization {
     
    3738    }
    3839
    39     protected HeuristicOptimizationEngineAlgorithm() : base() { }
    40     protected HeuristicOptimizationEngineAlgorithm(string name) : base(name) { }
    41     protected HeuristicOptimizationEngineAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
    42     protected HeuristicOptimizationEngineAlgorithm(string name, string description) : base(name, description) { }
    43     protected HeuristicOptimizationEngineAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
     40    [Storable] public IConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; }
     41    public ISolutionCreator SolutionCreator {
     42      get => SolutionCreatorParameter.Value;
     43      set {
     44        if (!SolutionCreatorParameter.ValidValues.Contains(value))
     45          SolutionCreatorParameter.ValidValues.Add(value);
     46        SolutionCreatorParameter.Value = value;
     47      }
     48    }
     49
     50    protected HeuristicOptimizationEngineAlgorithm() : base() {
     51      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
     52
     53      RegisterEventHandlers();
     54    }
     55    protected HeuristicOptimizationEngineAlgorithm(string name) : base(name) {
     56      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
     57
     58      RegisterEventHandlers();
     59    }
     60    protected HeuristicOptimizationEngineAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) {
     61      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
     62
     63      RegisterEventHandlers();
     64    }
     65    protected HeuristicOptimizationEngineAlgorithm(string name, string description) : base(name, description) {
     66      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
     67
     68      RegisterEventHandlers();
     69    }
     70    protected HeuristicOptimizationEngineAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
     71      Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem."));
     72
     73      RegisterEventHandlers();
     74    }
    4475
    4576    [StorableConstructor]
    4677    protected HeuristicOptimizationEngineAlgorithm(StorableConstructorFlag _) : base(_) { }
    47     protected HeuristicOptimizationEngineAlgorithm(HeuristicOptimizationEngineAlgorithm original, Cloner cloner) : base(original, cloner) { }
     78    protected HeuristicOptimizationEngineAlgorithm(HeuristicOptimizationEngineAlgorithm original, Cloner cloner) : base(original, cloner) {
     79      SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter);
     80
     81      RegisterEventHandlers();
     82    }
     83
     84    [StorableHook(HookType.AfterDeserialization)]
     85    private void AfterDeserialization() {
     86      RegisterEventHandlers();
     87    }
    4888
    4989    #region Events
     90    private void RegisterEventHandlers() {
     91      ParameterChangeHandler<ISolutionCreator>.Create(SolutionCreatorParameter, SolutionCreatorOnChanged);
     92    }
    5093    protected override void DeregisterProblemEvents() {
    51       Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
    52       Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
     94      Problem.EvaluatorChanged -= Problem_EvaluatorChanged;
    5395      base.DeregisterProblemEvents();
    5496    }
    5597    protected override void RegisterProblemEvents() {
    5698      base.RegisterProblemEvents();
    57       Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
    58       Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
     99      Problem.EvaluatorChanged += Problem_EvaluatorChanged;
     100    }
     101    protected override void OnProblemChanged() {
     102      base.OnProblemChanged();
     103      SolutionCreatorParameter.Repopulate(Problem.Operators);
    59104    }
    60105
    61     protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
     106    protected virtual void SolutionCreatorOnChanged() { }
    62107    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
     108
     109    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
     110      base.Problem_OperatorsChanged(sender, e);
     111      SolutionCreatorParameter.Repopulate(Problem.Operators);
     112    }
    63113    #endregion
    64114  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs

    r17614 r17695  
    3737    where TEvaluator : class, IEvaluator {
    3838
    39     [Storable] protected ConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; }
    40 
    4139    //TODO remove parameter for encoding?
    4240    protected IValueParameter<TEncoding> EncodingParameter {
     
    5048        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
    5149        EncodingParameter.Value = value;
    52       }
    53     }
    54 
    55     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get => SolutionCreatorParameter.Value; }
    56     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get => SolutionCreatorParameter; }
    57 
    58     event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged {
    59       add {
    60         SolutionCreatorParameter.ValueChanged += value;
    61       }
    62       remove {
    63         SolutionCreatorParameter.ValueChanged -= value;
    6450      }
    6551    }
     
    10389      Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.", encoding) { Hidden = true });
    10490      Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true });
    105       Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution."));
    106 
     91     
    10792      oldEncoding = Encoding;
    10893      Parameterize();
     
    11499      : base(original, cloner) {
    115100      oldEncoding = cloner.Clone(original.oldEncoding);
    116       SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter);
    117101      RegisterEvents();
    118102    }
     
    123107    private void AfterDeserialization() {
    124108      oldEncoding = Encoding;
    125       // TODO: remove below
    126       if (SolutionCreatorParameter == null) Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution."));
    127 
    128109      RegisterEvents();
    129110    }
     
    160141      Encoding.ConfigureOperators(Operators);
    161142
    162       SolutionCreatorParameter.Repopulate(GetOperators());
    163143      //var multiEncoding = Encoding as MultiEncoding;
    164144      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r17594 r17695  
    203203    <Compile Include="Algorithms\HeuristicOptimizationEngineAlgorithm.cs" />
    204204    <Compile Include="Interfaces\ILocalImprovementOperator.cs" />
    205     <Compile Include="Algorithms\HeuristicOptimizationAlgorithm.cs" />
    206205    <Compile Include="Interfaces\IMultiNeighborhoodShakingOperator.cs" />
    207206    <Compile Include="MoveHelper.cs" />
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IHeuristicOptimizationProblem.cs

    r17513 r17695  
    3030  /// </summary>
    3131  public interface IHeuristicOptimizationProblem : IEncodedProblem {
    32     IParameter SolutionCreatorParameter { get; }
    33     ISolutionCreator SolutionCreator { get; }
    3432    IParameter EvaluatorParameter { get; }
    3533    IEvaluator Evaluator { get; }
    3634
    37     event EventHandler SolutionCreatorChanged;
    3835    event EventHandler EvaluatorChanged;
    3936  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/HeuristicOptimizationProblem.cs

    r17513 r17695  
    2929  [Item("Heuristic Optimization Problem", "Represents the base class for a heuristic optimization problem.")]
    3030  [StorableType("DE0478BA-3797-4AC3-9A89-3734D2643823")]
    31   public abstract class HeuristicOptimizationProblem<T, U> : EncodedProblem, IHeuristicOptimizationProblem
    32     where T : class, IEvaluator
    33     where U : class, ISolutionCreator {
     31  public abstract class HeuristicOptimizationProblem<T> : EncodedProblem, IHeuristicOptimizationProblem
     32    where T : class, IEvaluator {
    3433    private const string EvaluatorParameterName = "Evaluator";
    35     private const string SolutionCreateParameterName = "SolutionCreator";
    3634
    3735    [StorableConstructor]
    3836    protected HeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
    39     protected HeuristicOptimizationProblem(HeuristicOptimizationProblem<T, U> original, Cloner cloner)
     37    protected HeuristicOptimizationProblem(HeuristicOptimizationProblem<T> original, Cloner cloner)
    4038      : base(original, cloner) {
    4139      RegisterEventHandlers();
     
    4543      : base() {
    4644      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution."));
    47       Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution."));
    4845      RegisterEventHandlers();
    4946    }
    5047
    51     protected HeuristicOptimizationProblem(T evaluator, U solutionCreator)
     48    protected HeuristicOptimizationProblem(T evaluator)
    5249      : base() {
    5350      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution.", evaluator));
    54       Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution.", solutionCreator));
    5551      RegisterEventHandlers();
    5652    }
     
    6359    private void RegisterEventHandlers() {
    6460      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    65       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    6661    }
    6762
     
    7671    IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } }
    7772    IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
    78 
    79     public U SolutionCreator {
    80       get { return (U)SolutionCreatorParameter.Value; }
    81       protected set { SolutionCreatorParameter.Value = value; }
    82     }
    83     public IValueParameter SolutionCreatorParameter {
    84       get { return (IValueParameter)Parameters[SolutionCreateParameterName]; }
    85     }
    86     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get { return SolutionCreator; } }
    87     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get { return SolutionCreatorParameter; } }
    8873    #endregion
    8974
     
    9883        handler(this, EventArgs.Empty);
    9984    }
    100 
    101     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    102       OnSolutionCreatorChanged();
    103     }
    104     public event EventHandler SolutionCreatorChanged;
    105     protected virtual void OnSolutionCreatorChanged() {
    106       EventHandler handler = SolutionCreatorChanged;
    107       if (handler != null)
    108         handler(this, EventArgs.Empty);
    109     }
    11085    #endregion
    11186  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/MultiObjectiveHeuristicOptimizationProblem.cs

    r17317 r17695  
    3030  [Item("Multi-Objective Heuristic Optimization Problem", "A base class for multi-objective heuristic optimization problems.")]
    3131  [StorableType("C46643E3-7144-4884-A30A-5329BD80DC4E")]
    32   public abstract class MultiObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, IMultiObjectiveHeuristicOptimizationProblem
    33     where T : class, IMultiObjectiveEvaluator
    34     where U : class, ISolutionCreator {
     32  public abstract class MultiObjectiveHeuristicOptimizationProblem<T> : HeuristicOptimizationProblem<T>, IMultiObjectiveHeuristicOptimizationProblem
     33    where T : class, IMultiObjectiveEvaluator {
    3534    private const string MaximizationParameterName = "Maximization";
    3635
    3736    [StorableConstructor]
    3837    protected MultiObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
    39     protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
     38    protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T> original, Cloner cloner)
    4039      : base(original, cloner) {
    4140      RegisterEventHandlers();
     
    4847    }
    4948
    50     protected MultiObjectiveHeuristicOptimizationProblem(T evaluator, U solutionCreator)
    51       : base(evaluator, solutionCreator) {
     49    protected MultiObjectiveHeuristicOptimizationProblem(T evaluator)
     50      : base(evaluator) {
    5251      Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Determines for each objective whether it should be maximized or minimized."));
    5352
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs

    r17317 r17695  
    3030  [Item("Single-Objective Heuristic OptimizationProblem", "A base class for single-objective heuristic optimization problems.")]
    3131  [StorableType("DFD5588E-6AB2-4712-9083-A405EF21226F")]
    32   public abstract class SingleObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, ISingleObjectiveHeuristicOptimizationProblem
    33     where T : class, ISingleObjectiveEvaluator
    34     where U : class, ISolutionCreator {
     32  public abstract class SingleObjectiveHeuristicOptimizationProblem<T> : HeuristicOptimizationProblem<T>, ISingleObjectiveHeuristicOptimizationProblem
     33    where T : class, ISingleObjectiveEvaluator {
    3534    private const string MaximizationParameterName = "Maximization";
    3635    private const string BestKnownQualityParameterName = "BestKnownQuality";
     
    3837    [StorableConstructor]
    3938    protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
    40     protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
     39    protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T> original, Cloner cloner)
    4140      : base(original, cloner) {
    4241      RegisterEventHandlers();
     
    5049    }
    5150
    52     protected SingleObjectiveHeuristicOptimizationProblem(T evaluator, U solutionCreator)
    53       : base(evaluator, solutionCreator) {
     51    protected SingleObjectiveHeuristicOptimizationProblem(T evaluator)
     52      : base(evaluator) {
    5453      Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
    5554      Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/UserDefinedProblem.cs

    r17612 r17695  
    6262      get { return MaximizationParameter; }
    6363    }
    64     public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
    65       get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
    66     }
    67     IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
    68       get { return SolutionCreatorParameter; }
    69     }
    7064    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
    7165      get { return EvaluatorParameter; }
     
    8983      get { return MaximizationParameter.Value; }
    9084      set { MaximizationParameter.Value = value; }
    91     }
    92     public ISolutionCreator SolutionCreator {
    93       get { return SolutionCreatorParameter.Value; }
    94       set { SolutionCreatorParameter.Value = value; }
    95     }
    96     ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
    97       get { return SolutionCreatorParameter.Value; }
    9885    }
    9986    public ISingleObjectiveEvaluator Evaluator {
     
    138125      : base() {
    139126      Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
    140       Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
    141127      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
    142128      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
     
    156142
    157143    #region Events
    158     public event EventHandler SolutionCreatorChanged;
    159     private void OnSolutionCreatorChanged() {
    160       EventHandler handler = SolutionCreatorChanged;
    161       if (handler != null) handler(this, EventArgs.Empty);
    162     }
    163144    public event EventHandler EvaluatorChanged;
    164145    private void OnEvaluatorChanged() {
     
    174155
    175156    #region Event handlers
    176     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    177       OnSolutionCreatorChanged();
    178     }
    179157    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
    180158      if (Evaluator != null)
     
    202180    #region Helpers
    203181    private void RegisterEventHandlers() {
    204       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    205182      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    206183      if (Evaluator != null)
Note: See TracChangeset for help on using the changeset viewer.