Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: reverted r17309

File size: 5.6 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.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Scripting;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("Programmable Problem (multi-objective)", "Represents a multi-objective problem that can be programmed with a script.")]
35  [StorableType("1AA24077-4E1E-4FAE-8EC8-B6008DFD30B9")]
36  public abstract class MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> : MultiObjectiveProblem<TEncoding, TEncodedSolution>, IProgrammableItem, IProgrammableProblem
37    where TEncoding : class, IEncoding<TEncodedSolution>
38    where TEncodedSolution : class, IEncodedSolution {
39    protected static readonly string ENCODING_NAMESPACE = "ENCODING_NAMESPACE";
40    protected static readonly string ENCODING_CLASS = "ENCODING_CLASS";
41    protected static readonly string SOLUTION_CLASS = "SOLUTION_CLASS";
42
43    public static new Image StaticItemImage {
44      get { return VSImageLibrary.Script; }
45    }
46
47    private FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>> MultiObjectiveProblemScriptParameter {
48      get { return (FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>)Parameters["ProblemScript"]; }
49    }
50
51    Script IProgrammableProblem.ProblemScript {
52      get { return ProblemScript; }
53    }
54    public MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution> ProblemScript {
55      get { return MultiObjectiveProblemScriptParameter.Value; }
56    }
57
58    public IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> ProblemDefinition {
59      get { return MultiObjectiveProblemScriptParameter.Value; }
60    }
61
62
63    [StorableConstructor]
64    protected MultiObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
65    protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
66      : base(original, cloner) {
67      RegisterEvents();
68    }
69
70    public MultiObjectiveProgrammableProblem(TEncoding encoding)
71      : base(encoding) {
72      Parameters.Add(new FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>("ProblemScript", "Defines the problem.",
73        new MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>() {Name = Name}));
74      ProblemScript.Encoding = (TEncoding)encoding.Clone();
75
76      var codeTemplate = ScriptTemplates.MultiObjectiveProblem_Template;
77      codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace);
78      codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name);
79      codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TEncodedSolution).Name);
80      ProblemScript.Code = codeTemplate;
81
82      RegisterEvents();
83    }
84
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      RegisterEvents();
88    }
89
90    private void RegisterEvents() {
91      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
92      ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
93    }
94
95    private void OnProblemDefinitionChanged() {
96      Parameters.Remove("Maximization");
97      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) {Hidden = true});
98      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
99
100      OnOperatorsChanged();
101      OnReset();
102    }
103    protected override void OnNameChanged() {
104      base.OnNameChanged();
105      ProblemScript.Name = Name;
106    }
107    private void OnProblemScriptNameChanged() {
108      Name = ProblemScript.Name;
109    }
110
111    public override bool[] Maximization {
112      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] {false}; }
113    }
114
115    public override IReadOnlyList<double[]> BestKnownFront {
116      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.BestKnownFront : null; }
117    }
118
119    public override double[] ReferencePoint {
120      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.ReferencePoint : null; }
121    }
122
123    public override double[] Evaluate(TEncodedSolution individual, IRandom random) {
124      return ProblemDefinition.Evaluate(individual, random);
125    }
126
127    public override void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
128      ProblemDefinition.Analyze(individuals, qualities, results, random);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.