1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Common.Resources;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.Programmable {
|
---|
34 | [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
|
---|
35 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
|
---|
36 | [StorableType("8121ADB4-318F-41CD-930E-5F7C031AE54D")]
|
---|
37 | public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveBasicProblem<IEncoding>, IProgrammableItem {
|
---|
38 | public static new Image StaticItemImage {
|
---|
39 | get { return VSImageLibrary.Script; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private FixedValueParameter<SingleObjectiveProblemDefinitionScript> SingleObjectiveProblemScriptParameter {
|
---|
43 | get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript>)Parameters["ProblemScript"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public SingleObjectiveProblemDefinitionScript ProblemScript {
|
---|
47 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public ISingleObjectiveProblemDefinition ProblemDefinition {
|
---|
51 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | RegisterEvents();
|
---|
57 | }
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProgrammableProblem(this, cloner); }
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | private SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
|
---|
62 | public SingleObjectiveProgrammableProblem()
|
---|
63 | : base() {
|
---|
64 | Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
|
---|
65 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
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();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void OnProblemDefinitionChanged() {
|
---|
79 | Parameters.Remove("Maximization");
|
---|
80 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
81 |
|
---|
82 | Encoding = ProblemDefinition.Encoding;
|
---|
83 | OnOperatorsChanged();
|
---|
84 | OnReset();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public override bool Maximization {
|
---|
88 | get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : false; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
92 | return ProblemDefinition.Evaluate(individual, random);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
96 | ProblemDefinition.Analyze(individuals, qualities, results, random);
|
---|
97 | }
|
---|
98 | public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
99 | return ProblemDefinition.GetNeighbors(individual, random);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|