source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs @ 16751

Last change on this file since 16751 was 16751, checked in by mkommend, 7 months ago

#2521: Renamed Solution to EncodedSolution.

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Scripting;
33
34namespace HeuristicLab.Problems.Programmable {
35  [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
36  [Creatable(CreatableAttribute.Categories.Problems, Priority = 110)]
37  [StorableType("44944E6B-E95E-4805-8F0A-0C0F7D761DB9")]
38  public class SingleObjectiveProgrammableProblem<TEncoding, TEncodedSolution> : SingleObjectiveProblem<TEncoding, TEncodedSolution>, IProgrammableItem, IProgrammableProblem
39    where TEncoding : class, IEncoding<TEncodedSolution>
40    where TEncodedSolution : class, IEncodedSolution {
41    protected static readonly string ENCODING_NAMESPACE = "ENCODING_NAMESPACE";
42    protected static readonly string ENCODING_CLASS = "ENCODING_CLASS";
43    protected static readonly string SOLUTION_CLASS = "SOLUTION_CLASS";
44
45    public static new Image StaticItemImage {
46      get { return VSImageLibrary.Script; }
47    }
48
49    private FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>> SingleObjectiveProblemScriptParameter {
50      get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>)Parameters["ProblemScript"]; }
51    }
52
53    Script IProgrammableProblem.ProblemScript {
54      get { return ProblemScript; }
55    }
56    public SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution> ProblemScript {
57      get { return SingleObjectiveProblemScriptParameter.Value; }
58    }
59
60    public ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> ProblemDefinition {
61      get { return SingleObjectiveProblemScriptParameter.Value; }
62    }
63
64    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
65      : base(original, cloner) {
66      RegisterEvents();
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new SingleObjectiveProgrammableProblem<TEncoding, TEncodedSolution>(this, cloner);
71    }
72
73    [StorableConstructor]
74    protected SingleObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
75    public SingleObjectiveProgrammableProblem()
76      : base() {
77      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>() { Name = Name }));
78      ProblemScript.Encoding = (TEncoding)Encoding.Clone();
79
80      var codeTemplate = ScriptTemplates.SingleObjectiveProblem_Template;
81      codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace);
82      codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name);
83      codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TEncodedSolution).Name);
84      ProblemScript.Code = codeTemplate;
85
86      Operators.Add(new BestScopeSolutionAnalyzer());
87      RegisterEvents();
88    }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    private void AfterDeserialization() {
92      RegisterEvents();
93    }
94
95    private void RegisterEvents() {
96      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
97      ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
98    }
99
100    private void OnProblemDefinitionChanged() {
101      Parameters.Remove("Maximization");
102      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
103      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
104
105      OnOperatorsChanged();
106      OnReset();
107    }
108    protected override void OnNameChanged() {
109      base.OnNameChanged();
110      ProblemScript.Name = Name;
111    }
112    private void OnProblemScriptNameChanged() {
113      Name = ProblemScript.Name;
114    }
115
116    public override bool Maximization {
117      get { return Parameters.ContainsKey("ProblemScript") && ProblemDefinition.Maximization; }
118    }
119
120    public override double Evaluate(TEncodedSolution individual, IRandom random) {
121      return ProblemDefinition.Evaluate(individual, random);
122    }
123
124    public override void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
125      ProblemDefinition.Analyze(individuals, qualities, results, random);
126    }
127    public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random) {
128      return ProblemDefinition.GetNeighbors(individual, random);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.