Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs @ 13348

Last change on this file since 13348 was 13348, checked in by mkommend, 8 years ago

#2521: Refactored single-objective programmable problem.

File size: 4.8 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.Collections.Generic;
23using System.Drawing;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
35  [StorableClass]
36  public class SingleObjectiveProgrammableProblem<TEncoding, TSolution> : SingleObjectiveProblem<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<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>> SingleObjectiveProblemScriptParameter {
48      get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>)Parameters["ProblemScript"]; }
49    }
50
51    public SingleObjectiveProblemDefinitionScript<TEncoding, TSolution> ProblemScript {
52      get { return SingleObjectiveProblemScriptParameter.Value; }
53    }
54
55    public ISingleObjectiveProblemDefinition<TEncoding, TSolution> ProblemDefinition {
56      get { return SingleObjectiveProblemScriptParameter.Value; }
57    }
58
59    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding, TSolution> original, Cloner cloner)
60      : base(original, cloner) {
61      RegisterEvents();
62    }
63    public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner); }
64
65    [StorableConstructor]
66    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
67    public SingleObjectiveProgrammableProblem()
68      : base() {
69      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name, Encoding = Encoding }));
70      Operators.Add(new BestScopeSolutionAnalyzer());
71      RegisterEvents();
72    }
73
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() {
76      RegisterEvents();
77    }
78
79    private void RegisterEvents() {
80      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
81    }
82
83    private void OnProblemDefinitionChanged() {
84      Parameters.Remove("Maximization");
85      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
86      ProblemScript.Initialize();
87
88      OnOperatorsChanged();
89      OnReset();
90    }
91
92    public override bool Maximization {
93      get { return Parameters.ContainsKey("ProblemScript") && ProblemDefinition.Maximization; }
94    }
95
96    public override double Evaluate(TSolution individual, IRandom random) {
97      return ProblemDefinition.Evaluate(individual, random);
98    }
99
100    public override void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
101      ProblemDefinition.Analyze(individuals, qualities, results, random);
102    }
103    public override IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
104      return ProblemDefinition.GetNeighbors(individual, random);
105    }
106
107    #region IProgrammableProblem Members
108    Scripting.Script IProgrammableProblem.ProblemScript {
109      get { return ProblemScript; }
110    }
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.