source: branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs @ 13350

Last change on this file since 13350 was 13350, checked in by mkommend, 7 years ago

#2521: Adapted multi-objective programmable problem.

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
23using HeuristicLab.Common;
24using HeuristicLab.Common.Resources;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)]
35  [StorableClass]
36  public class MultiObjectiveProgrammableProblem<TEncoding, TSolution> : MultiObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem
37    where TEncoding : class, IEncoding<TSolution>
38    where TSolution : class, ISolution {
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, TSolution>> MultiObjectiveProblemScriptParameter {
48      get { return (FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>>)Parameters["ProblemScript"]; }
49    }
50
51    Script IProgrammableProblem.ProblemScript {
52      get { return ProblemScript; }
53    }
54    public MultiObjectiveProblemDefinitionScript<TEncoding, TSolution> ProblemScript {
55      get { return MultiObjectiveProblemScriptParameter.Value; }
56    }
57
58    public IMultiObjectiveProblemDefinition<TEncoding, TSolution> ProblemDefinition {
59      get { return MultiObjectiveProblemScriptParameter.Value; }
60    }
61
62    protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem<TEncoding, TSolution> original, Cloner cloner)
63      : base(original, cloner) {
64      RegisterEvents();
65    }
66    public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner); }
67
68    [StorableConstructor]
69    protected MultiObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
70    public MultiObjectiveProgrammableProblem()
71      : base() {
72      Parameters.Add(new FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.",
73        new MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name, Encoding = Encoding }));
74      RegisterEvents();
75    }
76
77    [StorableHook(HookType.AfterDeserialization)]
78    private void AfterDeserialization() {
79      RegisterEvents();
80    }
81
82    private void RegisterEvents() {
83      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
84    }
85
86    private void OnProblemDefinitionChanged() {
87      Parameters.Remove("Maximization");
88      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) { Hidden = true });
89
90      ProblemScript.Initialize();
91      OnOperatorsChanged();
92      OnReset();
93    }
94
95    public override bool[] Maximization {
96      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] { false }; }
97    }
98
99    public override double[] Evaluate(TSolution individual, IRandom random) {
100      return ProblemDefinition.Evaluate(individual, random);
101    }
102
103    public override void Analyze(TSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
104      ProblemDefinition.Analyze(individuals, qualities, results, random);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.