[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[11961] | 23 | using System.Drawing;
|
---|
[11984] | 24 | using HeuristicLab.Analysis;
|
---|
[11739] | 25 | using HeuristicLab.Common;
|
---|
[11961] | 26 | using HeuristicLab.Common.Resources;
|
---|
[11739] | 27 | using HeuristicLab.Core;
|
---|
[11996] | 28 | using HeuristicLab.Data;
|
---|
[11739] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[13350] | 32 | using HeuristicLab.Scripting;
|
---|
[11739] | 33 |
|
---|
[11797] | 34 | namespace HeuristicLab.Problems.Programmable {
|
---|
[11814] | 35 | [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
|
---|
[11739] | 36 | [StorableClass]
|
---|
[13345] | 37 | public class SingleObjectiveProgrammableProblem<TEncoding, TSolution> : SingleObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem
|
---|
| 38 | where TEncoding : class, IEncoding<TSolution>
|
---|
| 39 | where TSolution : class, ISolution {
|
---|
[13348] | 40 | protected static readonly string ENCODING_NAMESPACE = "ENCODING_NAMESPACE";
|
---|
| 41 | protected static readonly string ENCODING_CLASS = "ENCODING_CLASS";
|
---|
| 42 | protected static readonly string SOLUTION_CLASS = "SOLUTION_CLASS";
|
---|
| 43 |
|
---|
[11961] | 44 | public static new Image StaticItemImage {
|
---|
| 45 | get { return VSImageLibrary.Script; }
|
---|
| 46 | }
|
---|
[11739] | 47 |
|
---|
[13345] | 48 | private FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>> SingleObjectiveProblemScriptParameter {
|
---|
| 49 | get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>)Parameters["ProblemScript"]; }
|
---|
[11739] | 50 | }
|
---|
| 51 |
|
---|
[13350] | 52 |
|
---|
| 53 | Script IProgrammableProblem.ProblemScript {
|
---|
| 54 | get { return ProblemScript; }
|
---|
| 55 | }
|
---|
[13345] | 56 | public SingleObjectiveProblemDefinitionScript<TEncoding, TSolution> ProblemScript {
|
---|
[11739] | 57 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[13345] | 60 | public ISingleObjectiveProblemDefinition<TEncoding, TSolution> ProblemDefinition {
|
---|
[11739] | 61 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[13345] | 64 | protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding, TSolution> original, Cloner cloner)
|
---|
[11739] | 65 | : base(original, cloner) {
|
---|
| 66 | RegisterEvents();
|
---|
| 67 | }
|
---|
[13345] | 68 | public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner); }
|
---|
[11739] | 69 |
|
---|
| 70 | [StorableConstructor]
|
---|
[13345] | 71 | protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
|
---|
[13348] | 72 | public SingleObjectiveProgrammableProblem()
|
---|
[11739] | 73 | : base() {
|
---|
[13350] | 74 | Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.",
|
---|
[13365] | 75 | new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name }));
|
---|
[13382] | 76 | ProblemScript.Encoding = (TEncoding)Encoding.Clone();
|
---|
[11984] | 77 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
[11739] | 78 | RegisterEvents();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 82 | private void AfterDeserialization() {
|
---|
| 83 | RegisterEvents();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private void RegisterEvents() {
|
---|
| 87 | ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void OnProblemDefinitionChanged() {
|
---|
[12002] | 91 | Parameters.Remove("Maximization");
|
---|
[11996] | 92 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
[13376] | 93 | var multiEnc = ProblemScript.Encoding as CombinedEncoding;
|
---|
[13365] | 94 | if (multiEnc != null) multiEnc.Clear();
|
---|
| 95 | Encoding = (TEncoding)ProblemScript.Encoding.Clone();
|
---|
[11996] | 96 |
|
---|
| 97 | OnOperatorsChanged();
|
---|
| 98 | OnReset();
|
---|
[11739] | 99 | }
|
---|
| 100 |
|
---|
| 101 | public override bool Maximization {
|
---|
[13345] | 102 | get { return Parameters.ContainsKey("ProblemScript") && ProblemDefinition.Maximization; }
|
---|
[11739] | 103 | }
|
---|
| 104 |
|
---|
[13345] | 105 | public override double Evaluate(TSolution individual, IRandom random) {
|
---|
[11739] | 106 | return ProblemDefinition.Evaluate(individual, random);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[13345] | 109 | public override void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
[11880] | 110 | ProblemDefinition.Analyze(individuals, qualities, results, random);
|
---|
[11739] | 111 | }
|
---|
[13345] | 112 | public override IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
|
---|
[11739] | 113 | return ProblemDefinition.GetNeighbors(individual, random);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|