Free cookie consent management tool by TermsFeed Policy Generator

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