Changeset 14073


Ignore:
Timestamp:
07/14/16 17:40:54 (15 months ago)
Author:
mkommend
Message:

#1087: Worked on Multi-objective test function problem and adapted plugin dependencies.

Location:
branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/MultiObjectiveTestFunctionProblem.cs

    r14068 r14073  
    3333namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
    3434  [StorableClass]
     35  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
     36  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
    3537  public class MultiObjectiveTestFunctionProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IProblemInstanceConsumer<MOTFData> {
    3638
    3739    #region Parameter Properties
    38 
    39     /// <summary>
    40     /// Whether an objective is to be maximized or minimized
    41     /// </summary>
    42     private IValueParameter<BoolArray> MaximizationParameter {
    43       get {
    44         return (IValueParameter<BoolArray>)Parameters["Maximization"];
    45       }
    46       set {
    47         Parameters["Maximization"].ActualValue = value;
    48       }
    49     }
    50 
    51     /// <summary>
    52     /// The dimensionality of the solution candidates
    53     /// </summary>
    54     private IFixedValueParameter<IntValue> ProblemSizeParameter {
     40    public IValueParameter<BoolArray> MaximizationParameter {
     41      get { return (IValueParameter<BoolArray>)Parameters["Maximization"]; }
     42    }
     43    public IFixedValueParameter<IntValue> ProblemSizeParameter {
    5544      get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
    5645    }
    57 
    58     /// <summary>
    59     /// The number of objectives that are to be optimized
    60     /// </summary>
    61     private IFixedValueParameter<IntValue> ObjectivesParameter {
     46    public IFixedValueParameter<IntValue> ObjectivesParameter {
    6247      get { return (IFixedValueParameter<IntValue>)Parameters["Objectives"]; }
    6348    }
    64 
    65     /// <summary>
    66     /// The bounds for the entries of the solution candidate
    67     /// </summary>
    68     private IValueParameter<DoubleMatrix> BoundsParameter {
     49    public IValueParameter<DoubleMatrix> BoundsParameter {
    6950      get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
    7051    }
    71 
    72     /// <summary>
    73     /// The testfunction
    74     /// </summary>
    7552    public IValueParameter<IMultiObjectiveTestFunction> TestFunctionParameter {
    7653      get { return (IValueParameter<IMultiObjectiveTestFunction>)Parameters["TestFunction"]; }
    7754    }
    78 
    79     /// <summary>
    80     /// The testfunction
    81     /// </summary>
    8255    public IValueParameter<DoubleArray> ReferencePointParameter {
    8356      get { return (IValueParameter<DoubleArray>)Parameters["ReferencePoint"]; }
    8457    }
    85 
    8658    public IValueParameter<DoubleMatrix> BestKnownFrontParameter {
     59      get { return (IValueParameter<DoubleMatrix>)Parameters["BestKnownFront"]; }
     60    }
     61
     62    #endregion
     63
     64    #region Properties
     65    public override bool[] Maximization {
    8766      get {
    88         return (IValueParameter<DoubleMatrix>)Parameters["BestKnownFront"];
    89       }
    90     }
    91 
    92     #endregion
    93 
    94     #region Properties
    95     private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
    96       get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
     67        //necessary because of virtual member call in base ctor to this property
     68        if (!Parameters.ContainsKey("TestFunction")) return new bool[2];
     69        return TestFunction.Maximization(Objectives);
     70      }
    9771    }
    9872
     
    11387      set { TestFunctionParameter.Value = value; }
    11488    }
     89    public IEnumerable<double[]> BestKnownFront {
     90      get { return Parameters.ContainsKey("BestKnownFront") ? TestFunction.OptimalParetoFront(Objectives) : null; }
     91    }
    11592    #endregion
    11693
    11794    [StorableConstructor]
    11895    protected MultiObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
     96    [StorableHook(HookType.AfterDeserialization)]
     97    private void AfterDeserialization() {
     98      RegisterEventHandlers();
     99    }
     100
    119101    protected MultiObjectiveTestFunctionProblem(MultiObjectiveTestFunctionProblem original, Cloner cloner)
    120102      : base(original, cloner) {
    121103      RegisterEventHandlers();
    122104    }
     105    public override IDeepCloneable Clone(Cloner cloner) {
     106      return new MultiObjectiveTestFunctionProblem(this, cloner);
     107    }
     108
    123109    public MultiObjectiveTestFunctionProblem()
    124110      : base() {
    125       Parameters.Remove("Maximization");
    126       Parameters.Add(new ValueParameter<BoolArray>("Maximization", "", new BoolArray(new bool[] { false, false })));
    127111      Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
    128112      Parameters.Add(new FixedValueParameter<IntValue>("Objectives", "The dimensionality of the solution vector (number of objectives).", new IntValue(2)));
     
    135119      BestKnownFrontParameter.Hidden = true;
    136120
     121      UpdateParameterValues();
    137122      InitializeOperators();
    138123      RegisterEventHandlers();
    139124    }
    140     public override IDeepCloneable Clone(Cloner cloner) {
    141       return new MultiObjectiveTestFunctionProblem(this, cloner);
    142     }
    143     [StorableHook(HookType.AfterDeserialization)]
    144     private void AfterDeserialization() {
    145       RegisterEventHandlers();
    146     }
     125
     126    private void RegisterEventHandlers() {
     127      TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
     128      ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
     129      ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
     130    }
     131
    147132
    148133    public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
     
    150135      if (results.ContainsKey("Pareto Front")) {
    151136        ((DoubleMatrix)results["Pareto Front"].Value).SortableView = true;
    152       }
    153     }
    154 
    155     public override bool[] Maximization {
    156       get {
    157         return Parameters.ContainsKey("TestFunction") ? TestFunction.Maximization(Objectives) : new bool[2];
    158       }
    159     }
    160 
    161     public IEnumerable<double[]> BestKnownFront {
    162       get {
    163         return Parameters.ContainsKey("BestKnownFront") ? TestFunction.OptimalParetoFront(Objectives) : null;
    164137      }
    165138    }
     
    178151    }
    179152
    180     public double[] Evaluate(RealVector individual, IRandom random) {
     153    public double[] Evaluate(RealVector individual) {
    181154      return TestFunction.Evaluate(individual, Objectives);
    182155    }
    183156
    184157    public override double[] Evaluate(Individual individual, IRandom random) {
    185       return Evaluate(individual.RealVector(), random);
     158      return Evaluate(individual.RealVector());
    186159    }
    187160
     
    190163    }
    191164
    192     private void RegisterEventHandlers() {
    193       TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
    194       ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
    195       ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;
    196       BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
    197     }
    198 
    199165    #region Events
     166    private void UpdateParameterValues() {
     167      MaximizationParameter.ActualValue = (BoolArray)new BoolArray(Maximization).AsReadOnly();
     168      var front = BestKnownFront;
     169      if (front != null) { BestKnownFrontParameter.Value = (DoubleMatrix)new DoubleMatrix(To2D(front.ToArray())).AsReadOnly(); }
     170
     171    }
     172
    200173    protected override void OnEncodingChanged() {
    201174      base.OnEncodingChanged();
    202       Parameterize();
     175      UpdateParameterValues();
    203176      ParameterizeAnalyzers();
    204177    }
    205178    protected override void OnEvaluatorChanged() {
    206179      base.OnEvaluatorChanged();
    207       Parameterize();
     180      UpdateParameterValues();
    208181      ParameterizeAnalyzers();
    209182    }
    210183
    211184    private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
    212       var problemSizeChange = ProblemSize < TestFunction.MinimumSolutionLength
    213                               || ProblemSize > TestFunction.MaximumSolutionLength;
    214       if (problemSizeChange) {
    215         ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
    216       }
    217 
    218       var solutionSizeChange = Objectives < TestFunction.MinimumObjectives
    219                               || Objectives > TestFunction.MaximumObjectives;
    220       if (solutionSizeChange) {
    221         ProblemSize = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
    222       }
     185      ProblemSize = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(ProblemSize, TestFunction.MaximumSolutionLength));
     186      Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives));
    223187
    224188      Bounds = (DoubleMatrix)new DoubleMatrix(TestFunction.Bounds(Objectives)).Clone();
    225189      ParameterizeAnalyzers();
    226       Parameterize();
     190      UpdateParameterValues();
    227191      OnReset();
    228192    }
    229193
    230194    private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
    231       if (ProblemSize < TestFunction.MinimumSolutionLength
    232         || ProblemSize > TestFunction.MaximumSolutionLength)
    233         ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
    234       if (Objectives < TestFunction.MinimumObjectives
    235         || Objectives > TestFunction.MaximumObjectives)
    236         Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
    237       Parameterize();
     195      ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
     196      UpdateParameterValues();
    238197    }
    239198
    240199    private void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {
    241       if (Objectives < TestFunction.MinimumObjectives
    242         || Objectives > TestFunction.MaximumObjectives)
    243         Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
    244       if (ProblemSize < TestFunction.MinimumSolutionLength
    245         || ProblemSize > TestFunction.MaximumSolutionLength)
    246         ProblemSize = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, ProblemSize));
    247 
    248 
    249       Parameterize();
    250     }
    251 
    252     private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
    253       Parameterize();
    254     }
     200      Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives));
     201      UpdateParameterValues();
     202    }
     203
    255204    #endregion
    256205
     
    264213      Operators.Add(new ScatterPlotAnalyzer());
    265214      Operators.Add(new NormalizedHypervolumeAnalyzer());
    266       ParameterizeAnalyzers();
    267       Parameterize();
    268     }
    269 
    270     private void Parameterize() {
    271       MaximizationParameter.ActualValue = new BoolArray(Maximization);
    272       var front = BestKnownFront;
    273       if (front != null) { BestKnownFrontParameter.ActualValue = new DoubleMatrix(To2D(front.ToArray<double[]>())); }
    274 
     215
     216      ParameterizeAnalyzers();
     217    }
     218
     219    private IEnumerable<IMultiObjectiveTestFunctionAnalyzer> Analyzers {
     220      get { return Operators.OfType<IMultiObjectiveTestFunctionAnalyzer>(); }
    275221    }
    276222
     
    282228        analyzer.BestKnownFrontParameter.ActualName = BestKnownFrontParameter.Name;
    283229
    284 
    285         var front = BestKnownFront;
    286         if (front != null) { BestKnownFrontParameter.ActualValue = new DoubleMatrix(To2D(front.ToArray<double[]>())); }
    287 
    288 
    289230        var hyperVolumeAnalyzer = analyzer as HypervolumeAnalyzer;
    290231        if (hyperVolumeAnalyzer != null) {
     
    295236        var normalizedHyperVolumeAnalyzer = analyzer as NormalizedHypervolumeAnalyzer;
    296237        if (normalizedHyperVolumeAnalyzer != null) {
    297           normalizedHyperVolumeAnalyzer.OptimalFrontParameter.ActualValue = (DoubleMatrix)BestKnownFrontParameter.ActualValue;
     238          normalizedHyperVolumeAnalyzer.OptimalFrontParameter.Value = (DoubleMatrix)BestKnownFrontParameter.ActualValue;
    298239        }
    299240
     
    302243          scatterPlotAnalyzer.IndividualsParameter.ActualName = Encoding.Name;
    303244        }
    304 
    305245      }
    306246    }
     
    308248    public static T[,] To2D<T>(T[][] source) {
    309249      try {
    310         int FirstDim = source.Length;
    311         int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular
    312 
    313         var result = new T[FirstDim, SecondDim];
    314         for (int i = 0; i < FirstDim; ++i)
    315           for (int j = 0; j < SecondDim; ++j)
     250        int firstDimension = source.Length;
     251        int secondDimension = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular
     252
     253        var result = new T[firstDimension, secondDimension];
     254        for (int i = 0; i < firstDimension; ++i)
     255          for (int j = 0; j < secondDimension; ++j)
    316256            result[i, j] = source[i][j];
    317257
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Plugin.cs.frame

    r13394 r14073  
    2727  [PluginDependency("HeuristicLab.Collections", "3.3")]
    2828  [PluginDependency("HeuristicLab.Common", "3.3")]
    29   [PluginDependency("HeuristicLab.Core", "3.3")]
     29  [PluginDependency("HeuristicLab.Common.Resources", "3.3")]
     30  [PluginDependency("HeuristicLab.Core", "3.3")] 
     31  [PluginDependency("HeuristicLab.Data", "3.3")]
     32  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
     33  [PluginDependency("HeuristicLab.Operators", "3.3")]
     34  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     35  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    3036  [PluginDependency("HeuristicLab.Persistence", "3.3")]
    31   public class HeuristicLabScriptingPlugin : PluginBase {
     37  [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]
     38
     39  [PluginDependency("HeuristicLab.Core.Views", "3.3")]
     40  [PluginDependency("HeuristicLab.MainForm", "3.3")]
     41  [PluginDependency("HeuristicLab.MainForm.WindowsForms", "3.3")]
     42  [PluginDependency("HeuristicLab.Visualization.ChartControlsExtensions", "3.3")]
     43  public class Plugin : PluginBase {
    3244  }
    3345}
Note: See TracChangeset for help on using the changeset viewer.