Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14728 was 13422, checked in by mkommend, 9 years ago

#2521: Adapted type discovery and type selector to allow the creation of generic programmable problems.

File size: 5.3 KB
RevLine 
[11739]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11739]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;
[11961]23using System.Drawing;
[11984]24using HeuristicLab.Analysis;
[11739]25using HeuristicLab.Common;
[11961]26using HeuristicLab.Common.Resources;
[11739]27using HeuristicLab.Core;
[11996]28using HeuristicLab.Data;
[11739]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13350]32using HeuristicLab.Scripting;
[11739]33
[11797]34namespace HeuristicLab.Problems.Programmable {
[11814]35  [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
[13422]36  [Creatable(CreatableAttribute.Categories.Problems, Priority = 110)]
[11739]37  [StorableClass]
[13422]38  public class SingleObjectiveProgrammableProblem<TEncoding, TSolution> : SingleObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem
[13345]39    where TEncoding : class, IEncoding<TSolution>
40    where TSolution : class, ISolution {
[13348]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
[11961]45    public static new Image StaticItemImage {
46      get { return VSImageLibrary.Script; }
47    }
[11739]48
[13345]49    private FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>> SingleObjectiveProblemScriptParameter {
50      get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>)Parameters["ProblemScript"]; }
[11739]51    }
52
[13350]53    Script IProgrammableProblem.ProblemScript {
54      get { return ProblemScript; }
55    }
[13345]56    public SingleObjectiveProblemDefinitionScript<TEncoding, TSolution> ProblemScript {
[11739]57      get { return SingleObjectiveProblemScriptParameter.Value; }
58    }
59
[13345]60    public ISingleObjectiveProblemDefinition<TEncoding, TSolution> ProblemDefinition {
[11739]61      get { return SingleObjectiveProblemScriptParameter.Value; }
62    }
63
[13390]64    [StorableConstructor]
65    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
[13345]66    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding, TSolution> original, Cloner cloner)
[11739]67      : base(original, cloner) {
68      RegisterEvents();
69    }
[13422]70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new SingleObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner);
73    }
74
[13348]75    public SingleObjectiveProgrammableProblem()
[11739]76      : base() {
[13422]77      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name }));
[13382]78      ProblemScript.Encoding = (TEncoding)Encoding.Clone();
[13422]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(TSolution).Name);
84      ProblemScript.Code = codeTemplate;
85
[11984]86      Operators.Add(new BestScopeSolutionAnalyzer());
[11739]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    }
98
99    private void OnProblemDefinitionChanged() {
[12002]100      Parameters.Remove("Maximization");
[11996]101      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
[13365]102      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
[11996]103
104      OnOperatorsChanged();
105      OnReset();
[11739]106    }
107
108    public override bool Maximization {
[13345]109      get { return Parameters.ContainsKey("ProblemScript") && ProblemDefinition.Maximization; }
[11739]110    }
111
[13345]112    public override double Evaluate(TSolution individual, IRandom random) {
[11739]113      return ProblemDefinition.Evaluate(individual, random);
114    }
115
[13345]116    public override void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
[11880]117      ProblemDefinition.Analyze(individuals, qualities, results, random);
[11739]118    }
[13345]119    public override IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
[11739]120      return ProblemDefinition.GetNeighbors(individual, random);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.