Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs @ 17860

Last change on this file since 17860 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.5 KB
RevLine 
[11739]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[12005]24using HeuristicLab.Analysis;
[11739]25using HeuristicLab.Common;
[11961]26using HeuristicLab.Common.Resources;
[11739]27using HeuristicLab.Core;
[12005]28using HeuristicLab.Data;
[11739]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
[17097]31using HEAL.Attic;
[11739]32
[11797]33namespace HeuristicLab.Problems.Programmable {
[11814]34  [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
[12708]35  [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
[17097]36  [StorableType("44944E6B-E95E-4805-8F0A-0C0F7D761DB9")]
[12686]37  public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveBasicProblem<IEncoding>, IProgrammableItem {
[11961]38    public static new Image StaticItemImage {
39      get { return VSImageLibrary.Script; }
40    }
[11739]41
42    private FixedValueParameter<SingleObjectiveProblemDefinitionScript> SingleObjectiveProblemScriptParameter {
43      get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript>)Parameters["ProblemScript"]; }
44    }
45
[11753]46    public SingleObjectiveProblemDefinitionScript ProblemScript {
[11739]47      get { return SingleObjectiveProblemScriptParameter.Value; }
48    }
49
[11753]50    public ISingleObjectiveProblemDefinition ProblemDefinition {
[11739]51      get { return SingleObjectiveProblemScriptParameter.Value; }
52    }
53
[11814]54    private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
[11739]55      : base(original, cloner) {
56      RegisterEvents();
57    }
[11814]58    public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProgrammableProblem(this, cloner); }
[11739]59
60    [StorableConstructor]
[17097]61    private SingleObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
[11814]62    public SingleObjectiveProgrammableProblem()
[11739]63      : base() {
64      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
[12005]65      Operators.Add(new BestScopeSolutionAnalyzer());
[11739]66      RegisterEvents();
67    }
68
69    [StorableHook(HookType.AfterDeserialization)]
70    private void AfterDeserialization() {
71      RegisterEvents();
72    }
73
74    private void RegisterEvents() {
75      ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
[14172]76      ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
[11739]77    }
78
79    private void OnProblemDefinitionChanged() {
[12005]80      Parameters.Remove("Maximization");
81      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
82
[11753]83      Encoding = ProblemDefinition.Encoding;
[12005]84      OnOperatorsChanged();
85      OnReset();
[11739]86    }
[14172]87    protected override void OnNameChanged() {
88      base.OnNameChanged();
89      ProblemScript.Name = Name;
90    }
91    private void OnProblemScriptNameChanged() {
92      Name = ProblemScript.Name;
93    }
[11739]94
95    public override bool Maximization {
[12005]96      get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : false; }
[11739]97    }
98
99    public override double Evaluate(Individual individual, IRandom random) {
100      return ProblemDefinition.Evaluate(individual, random);
101    }
102
[11880]103    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
104      ProblemDefinition.Analyze(individuals, qualities, results, random);
[11739]105    }
106    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
107      return ProblemDefinition.GetNeighbors(individual, random);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.