Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16773 was 16751, checked in by mkommend, 6 years ago

#2521: Renamed Solution to EncodedSolution.

File size: 5.4 KB
RevLine 
[11740]1#region License Information
2/* HeuristicLab
[16723]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11740]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
[11961]22using System.Drawing;
[16725]23using HEAL.Attic;
[11740]24using HeuristicLab.Common;
[11961]25using HeuristicLab.Common.Resources;
[11740]26using HeuristicLab.Core;
[11996]27using HeuristicLab.Data;
[11740]28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
[13350]30using HeuristicLab.Scripting;
[11740]31
[11753]32namespace HeuristicLab.Problems.Programmable {
[11814]33  [Item("Programmable Problem (multi-objective)", "Represents a multi-objective problem that can be programmed with a script.")]
[12504]34  [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)]
[16723]35  [StorableType("1AA24077-4E1E-4FAE-8EC8-B6008DFD30B9")]
[16751]36  public class MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> : MultiObjectiveProblem<TEncoding, TEncodedSolution>, IProgrammableItem, IProgrammableProblem
37    where TEncoding : class, IEncoding<TEncodedSolution>
38    where TEncodedSolution : class, IEncodedSolution {
[13350]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
[11961]43    public static new Image StaticItemImage {
44      get { return VSImageLibrary.Script; }
45    }
[11740]46
[16751]47    private FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>> MultiObjectiveProblemScriptParameter {
48      get { return (FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>)Parameters["ProblemScript"]; }
[11740]49    }
50
[13350]51    Script IProgrammableProblem.ProblemScript {
52      get { return ProblemScript; }
53    }
[16751]54    public MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution> ProblemScript {
[11740]55      get { return MultiObjectiveProblemScriptParameter.Value; }
56    }
57
[16751]58    public IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> ProblemDefinition {
[11740]59      get { return MultiObjectiveProblemScriptParameter.Value; }
60    }
61
[13390]62    [StorableConstructor]
[16725]63    protected MultiObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
[16751]64    protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
[11740]65      : base(original, cloner) {
66      RegisterEvents();
67    }
[13422]68
69    public override IDeepCloneable Clone(Cloner cloner) {
[16751]70      return new MultiObjectiveProgrammableProblem<TEncoding, TEncodedSolution>(this, cloner);
[13422]71    }
72
[11814]73    public MultiObjectiveProgrammableProblem()
[11740]74      : base() {
[16751]75      Parameters.Add(new FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>("ProblemScript", "Defines the problem.",
76        new MultiObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>() { Name = Name }));
[13382]77      ProblemScript.Encoding = (TEncoding)Encoding.Clone();
[13422]78
79      var codeTemplate = ScriptTemplates.MultiObjectiveProblem_Template;
80      codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace);
81      codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name);
[16751]82      codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TEncodedSolution).Name);
[13422]83      ProblemScript.Code = codeTemplate;
84
[11740]85      RegisterEvents();
86    }
87
88    [StorableHook(HookType.AfterDeserialization)]
89    private void AfterDeserialization() {
90      RegisterEvents();
91    }
92
93    private void RegisterEvents() {
94      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
[16692]95      ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
[11740]96    }
97
98    private void OnProblemDefinitionChanged() {
[12002]99      Parameters.Remove("Maximization");
100      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) { Hidden = true });
[13365]101      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
[11996]102
[11753]103      OnOperatorsChanged();
104      OnReset();
[11740]105    }
[16692]106    protected override void OnNameChanged() {
107      base.OnNameChanged();
108      ProblemScript.Name = Name;
109    }
110    private void OnProblemScriptNameChanged() {
111      Name = ProblemScript.Name;
112    }
[11740]113
114    public override bool[] Maximization {
[12002]115      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] { false }; }
[11740]116    }
117
[16751]118    public override double[] Evaluate(TEncodedSolution individual, IRandom random) {
[11740]119      return ProblemDefinition.Evaluate(individual, random);
120    }
121
[16751]122    public override void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
[11880]123      ProblemDefinition.Analyze(individuals, qualities, results, random);
[11740]124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.