Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs @ 17317

Last change on this file since 17317 was 17317, checked in by abeham, 4 years ago

#2521: refactored multi-objective problems' maximization

  • Add ForceValue method to IValueParameter to perform changes even when it is read-only
  • Add MaximizationChanged event handler
File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Drawing;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Scripting;
31
32namespace HeuristicLab.Problems.Programmable {
33  [Item("Programmable Problem (multi-objective)", "Represents a multi-objective problem that can be programmed with a script.")]
34  [StorableType("1AA24077-4E1E-4FAE-8EC8-B6008DFD30B9")]
35  public abstract class MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> : MultiObjectiveProblem<TEncoding, TEncodedSolution>, IProgrammableItem, IProgrammableProblem
36    where TEncoding : class, IEncoding<TEncodedSolution>
37    where TEncodedSolution : class, IEncodedSolution {
38    protected static readonly string ENCODING_NAMESPACE = "ENCODING_NAMESPACE";
39    protected static readonly string ENCODING_CLASS = "ENCODING_CLASS";
40    protected static readonly string SOLUTION_CLASS = "SOLUTION_CLASS";
41
42    public static new Image StaticItemImage {
43      get { return VSImageLibrary.Script; }
44    }
45
46    private FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>> MultiObjectiveProblemScriptParameter {
47      get { return (FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>)Parameters["ProblemScript"]; }
48    }
49
50    Script IProgrammableProblem.ProblemScript {
51      get { return ProblemScript; }
52    }
53    public MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution> ProblemScript {
54      get { return MultiObjectiveProblemScriptParameter.Value; }
55    }
56
57    public IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> ProblemDefinition {
58      get { return MultiObjectiveProblemScriptParameter.Value; }
59    }
60
61
62    [StorableConstructor]
63    protected MultiObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
64    protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
65      : base(original, cloner) {
66      RegisterEvents();
67    }
68
69    public MultiObjectiveProgrammableProblem(TEncoding encoding)
70      : base(encoding) {
71      Parameters.Add(new FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>("ProblemScript", "Defines the problem.",
72        new MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>() {Name = Name}));
73      ProblemScript.Encoding = (TEncoding)encoding.Clone();
74
75      var codeTemplate = ScriptTemplates.MultiObjectiveProblem_Template;
76      codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace);
77      codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name);
78      codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TEncodedSolution).Name);
79      ProblemScript.Code = codeTemplate;
80
81      RegisterEvents();
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      RegisterEvents();
87    }
88
89    private void RegisterEvents() {
90      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
91      ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
92    }
93
94    private void OnProblemDefinitionChanged() {
95      Maximization = ProblemDefinition.Maximization;
96      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
97
98      OnOperatorsChanged();
99      OnReset();
100    }
101    protected override void OnNameChanged() {
102      base.OnNameChanged();
103      ProblemScript.Name = Name;
104    }
105    private void OnProblemScriptNameChanged() {
106      Name = ProblemScript.Name;
107    }
108
109    public override IReadOnlyList<double[]> BestKnownFront {
110      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.BestKnownFront : null; }
111    }
112
113    public override double[] ReferencePoint {
114      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.ReferencePoint : null; }
115    }
116
117    public override double[] Evaluate(TEncodedSolution individual, IRandom random) {
118      return ProblemDefinition.Evaluate(individual, random);
119    }
120
121    public override void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
122      ProblemDefinition.Analyze(individuals, qualities, results, random);
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.