Changeset 17625


Ignore:
Timestamp:
06/23/20 04:23:45 (3 weeks ago)
Author:
abeham
Message:

#2521:

  • Changed IParticleCreator to not derive from ISolutionCreator (it calls the solution creator itself, thus leading to an infinite loop)
  • Remove ForceValue method and added method to set value in constrained value parameters from valid values
  • Fix ScopeTreeLookupParameter not caching the actual value
  • Fixed single-objective test functions and removed lots of parameterize code
Location:
branches/2521_ProblemRefactoring
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IParticleCreator.cs

    r17226 r17625  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Core;
    23 using HEAL.Attic;
    2424
    2525namespace HeuristicLab.Optimization {
     
    2828  /// Interface to represent an operator that generates a particle.
    2929  /// </summary>
    30   public interface IParticleCreator : ISolutionCreator {
     30  public interface IParticleCreator : IOperator {
    3131    ILookupParameter<ISolutionCreator> SolutionCreatorParameter { get; }
    3232  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs

    r17614 r17625  
    5555      set {
    5656        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
    57         DoSetValue(value);
    58       }
    59     }
    60     public virtual void ForceValue(T value) {
    61       DoSetValue(value);
    62     }
    63     private void DoSetValue(T value) {
    64       if (value != this.value) {
    65         if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
    66         DeregisterValueEvents();
    67         this.value = value;
    68         RegisterValueEvents();
    69         OnValueChanged();
    70       }
    71     }
     57        if (value != this.value) {
     58          if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
     59          DeregisterValueEvents();
     60          this.value = value;
     61          RegisterValueEvents();
     62          OnValueChanged();
     63        }
     64      }
     65    }
     66
    7267    IItem IValueParameter.Value {
    7368      get { return Value; }
     
    170165    #endregion
    171166
    172     public void Populate(IEnumerable<IItem> items) {
     167    public virtual void Populate(IEnumerable<IItem> items) {
    173168      ValidValues.Clear();
    174169      ValidValues.UnionWith(items.OfType<T>());
     
    187182        if (item != null) Value = item;
    188183      }
     184    }
     185
     186    public virtual IItem SetValueToFirstOf(Type itemType) {
     187      var item = ValidValues.Where(i => itemType.IsAssignableFrom(i.GetType())).FirstOrDefault();
     188      if (item != null) Value = item;
     189      return item;
    189190    }
    190191
  • branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ScopeTreeLookupParameter.cs

    r17257 r17625  
    133133        j++;
    134134      }
     135      CachedActualValue = values;
    135136    }
    136137
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r17614 r17625  
    2525using System.Threading;
    2626using HEAL.Attic;
    27 using HeuristicLab.Analysis;
    2827using HeuristicLab.Common;
    2928using HeuristicLab.Core;
     
    7170      BestKnownQuality = TestFunction.BestKnownQuality;
    7271      Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
    73       Dimension = TestFunction.MinimumProblemSize;
     72      Dimension = Math.Min(Math.Max(2, TestFunction.MinimumProblemSize), TestFunction.MaximumProblemSize);
    7473      BestKnownSolutionParameter.Value = TestFunction.GetBestKnownSolution(Dimension);
    75       InitializeOperators();
     74
     75
     76      Operators.AddRange(new IItem[] {
     77        new SingleObjectiveTestFunctionImprovementOperator(),
     78        new SingleObjectiveTestFunctionPathRelinker(),
     79        new SingleObjectiveTestFunctionSimilarityCalculator(),
     80        new EuclideanSimilarityCalculator(),
     81        new AdditiveMoveEvaluator() });
     82
     83      SolutionCreatorParameter.SetValueToFirstOf(typeof(UniformRandomRealVectorCreator));
     84
     85      Parameterize();
    7686      RegisterEventHandlers();
    7787    }
     
    8797
    8898    private void RegisterEventHandlers() {
    89       Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
    9099      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
    91100    }
     
    109118
    110119      SingleObjectiveTestFunctionSolution solution = null;
    111       if (results.TryGetValue("Best Solution", out var res)) {
     120      if (results.TryGetValue("Best Solution", out var res) && res.Value != null) {
    112121        solution = (SingleObjectiveTestFunctionSolution)res.Value;
    113122        if (IsBetter(best.Item2, solution.BestQuality.Value)) {
     
    122131          Bounds = BoundsRefParameter.Value
    123132        };
    124         results.Add(new Result("Best Solution", solution));
     133        results.AddOrUpdateResult("Best Solution", solution);
    125134      }
    126135      if (best.Item1.Length == 2) solution.Population = new ItemArray<RealVector>(realVectors.Select(x => (RealVector)x.Clone()));
     
    129138
    130139    #region Events
    131     protected override void OnEncodingChanged() {
    132       base.OnEncodingChanged();
    133       Parameterize();
    134     }
    135     protected override void OnEvaluatorChanged() {
    136       base.OnEvaluatorChanged();
    137       Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     140    protected override void ParameterizeOperators() {
     141      base.ParameterizeOperators();
    138142      Parameterize();
    139143    }
     
    145149    protected override void BoundsOnChanged() {
    146150      base.BoundsOnChanged();
    147       Parameterize();
    148     }
    149     private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    150151      Parameterize();
    151152    }
     
    167168
    168169    #region Helpers
    169     private void InitializeOperators() {
    170       Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
    171       Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
    172       Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
    173       Operators.Add(new EuclideanSimilarityCalculator());
    174       Operators.Add(new AdditiveMoveEvaluator());
    175 
    176       Parameterize();
    177     }
    178 
    179170    private void Parameterize() {
    180171      var operators = new List<IItem>();
    181172
    182       //TODO correct wiring code, because most of the parameters are wired in the encoding
    183       foreach (var op in Operators.OfType<PopulationSimilarityAnalyzer>()) {
    184         var calcs = Operators.OfType<ISolutionSimilarityCalculator>().ToArray();
    185         op.SimilarityCalculatorParameter.ValidValues.Clear();
    186         foreach (var c in calcs) {
    187           // TODO: unified encoding parameters
    188           c.SolutionVariableName = Encoding.Name;
    189           c.QualityVariableName = Evaluator.QualityParameter.ActualName;
    190           op.SimilarityCalculatorParameter.ValidValues.Add(c);
    191         }
    192       }
    193       foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
    194         operators.Add(op);
    195         op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    196         op.QualityParameter.Hidden = true;
    197         foreach (var movOp in Encoding.Operators.OfType<IRealVectorAdditiveMoveQualityOperator>())
    198           movOp.MoveQualityParameter.ActualName = op.MoveQualityParameter.ActualName;
    199       }
    200       foreach (var op in Operators.OfType<IRealVectorParticleCreator>()) {
    201         // TODO: unified encoding parameters
    202         op.RealVectorParameter.ActualName = Encoding.Name;
    203         op.RealVectorParameter.Hidden = true;
    204         op.BoundsParameter.ActualName = BoundsRefParameter.Name;
    205         op.BoundsParameter.Hidden = true;
    206       }
    207       foreach (var op in Operators.OfType<IRealVectorParticleUpdater>()) {
    208         // TODO: unified encoding parameters
    209         op.RealVectorParameter.ActualName = Encoding.Name;
    210         op.RealVectorParameter.Hidden = true;
    211         op.BoundsParameter.ActualName = BoundsRefParameter.Name;
    212         op.BoundsParameter.Hidden = true;
    213       }
    214       foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
    215         op.MaximizationParameter.ActualName = MaximizationParameter.Name;
    216         op.MaximizationParameter.Hidden = true;
    217       }
    218       foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
    219         operators.Add(op);
    220         op.SolutionParameter.ActualName = Encoding.Name;
    221         op.SolutionParameter.Hidden = true;
    222       }
    223173      foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
    224174        operators.Add(op);
    225         op.SolutionVariableName = Encoding.Name;
    226         op.QualityVariableName = Evaluator.QualityParameter.ActualName;
    227175        op.Bounds = Bounds;
    228176      }
Note: See TracChangeset for help on using the changeset viewer.