Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveScriptableProblem.cs @ 11767

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

#2174: Several bug fixes in programmable problem that were discovered during testing.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.Programmable {
30  [Item("Scriptable Problem (multi-objective)", "Represents a multi-objective problem that can be scripted.")]
31  [Creatable("1 Test")]
32  [StorableClass]
33  public sealed class MultiObjectiveScriptableProblem : MultiObjectiveProgrammableProblem<IEncoding> {
34
35    private FixedValueParameter<MultiObjectiveProblemDefinitionScript> MultiObjectiveProblemScriptParameter {
36      get { return (FixedValueParameter<MultiObjectiveProblemDefinitionScript>)Parameters["ProblemScript"]; }
37    }
38
39    public MultiObjectiveProblemDefinitionScript ProblemScript {
40      get { return MultiObjectiveProblemScriptParameter.Value; }
41    }
42
43    public IMultiObjectiveProblemDefinition ProblemDefinition {
44      get { return MultiObjectiveProblemScriptParameter.Value; }
45    }
46
47    private MultiObjectiveScriptableProblem(MultiObjectiveScriptableProblem original, Cloner cloner)
48      : base(original, cloner) {
49      RegisterEvents();
50      try {
51        ProblemScript.Compile();
52      }
53      catch (InvalidOperationException) {
54        //Compilation error
55      }
56    }
57    public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveScriptableProblem(this, cloner); }
58
59
60    [StorableConstructor]
61    private MultiObjectiveScriptableProblem(bool deserializing) : base(deserializing) { }
62
63    public MultiObjectiveScriptableProblem()
64      : base() {
65      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
66      RegisterEvents();
67    }
68
69    [StorableHook(HookType.AfterDeserialization)]
70    private void AfterDeserialization() {
71      RegisterEvents();
72      try {
73        ProblemScript.Compile();
74      }
75      catch (InvalidOperationException) {
76        //Compilation error
77      }
78    }
79
80    private void RegisterEvents() {
81      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
82    }
83
84    private void OnProblemDefinitionChanged() {
85      Encoding = ProblemDefinition.Encoding;
86      OnOperatorsChanged();
87      OnReset();
88    }
89
90    public override bool[] Maximization {
91      get { return ProblemDefinition.Maximization; }
92    }
93
94    public override double[] Evaluate(Individual individual, IRandom random) {
95      return ProblemDefinition.Evaluate(individual, random);
96    }
97
98    public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
99      ProblemDefinition.Analyze(individuals, qualities, results);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.