Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs @ 11984

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

#2174: Added best scope solution analyzer to single-objective programmable problem.

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