[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[11961] | 23 | using System.Drawing;
|
---|
[17320] | 24 | using System.Threading;
|
---|
[16725] | 25 | using HEAL.Attic;
|
---|
[11984] | 26 | using HeuristicLab.Analysis;
|
---|
[11739] | 27 | using HeuristicLab.Common;
|
---|
[11961] | 28 | using HeuristicLab.Common.Resources;
|
---|
[11739] | 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
[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.")]
|
---|
[16723] | 36 | [StorableType("44944E6B-E95E-4805-8F0A-0C0F7D761DB9")]
|
---|
[16815] | 37 | public abstract class SingleObjectiveProgrammableProblem<TEncoding, TEncodedSolution> : SingleObjectiveProblem<TEncoding, TEncodedSolution>, IProgrammableItem, IProgrammableProblem
|
---|
[16751] | 38 | where TEncoding : class, IEncoding<TEncodedSolution>
|
---|
| 39 | where TEncodedSolution : class, IEncodedSolution {
|
---|
[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 |
|
---|
[16751] | 48 | private FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>> SingleObjectiveProblemScriptParameter {
|
---|
| 49 | get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>)Parameters["ProblemScript"]; }
|
---|
[11739] | 50 | }
|
---|
| 51 |
|
---|
[13350] | 52 | Script IProgrammableProblem.ProblemScript {
|
---|
| 53 | get { return ProblemScript; }
|
---|
| 54 | }
|
---|
[16751] | 55 | public SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution> ProblemScript {
|
---|
[11739] | 56 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[16751] | 59 | public ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> ProblemDefinition {
|
---|
[11739] | 60 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[16751] | 63 | protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
|
---|
[11739] | 64 | : base(original, cloner) {
|
---|
| 65 | RegisterEvents();
|
---|
| 66 | }
|
---|
[13422] | 67 |
|
---|
[16723] | 68 | [StorableConstructor]
|
---|
[16725] | 69 | protected SingleObjectiveProgrammableProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[16811] | 70 | public SingleObjectiveProgrammableProblem(TEncoding encoding)
|
---|
| 71 | : base(encoding) {
|
---|
[16751] | 72 | Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript<TEncoding, TEncodedSolution>() { Name = Name }));
|
---|
[16811] | 73 | ProblemScript.Encoding = (TEncoding)encoding.Clone();
|
---|
[13422] | 74 |
|
---|
| 75 | var codeTemplate = ScriptTemplates.SingleObjectiveProblem_Template;
|
---|
| 76 | codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace);
|
---|
| 77 | codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name);
|
---|
[16751] | 78 | codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TEncodedSolution).Name);
|
---|
[13422] | 79 | ProblemScript.Code = codeTemplate;
|
---|
| 80 |
|
---|
[11984] | 81 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
[11739] | 82 | RegisterEvents();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 86 | private void AfterDeserialization() {
|
---|
| 87 | RegisterEvents();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void RegisterEvents() {
|
---|
| 91 | ProblemScript.ProblemDefinitionChanged += (o, e) => OnProblemDefinitionChanged();
|
---|
[16692] | 92 | ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
|
---|
[11739] | 93 | }
|
---|
| 94 |
|
---|
| 95 | private void OnProblemDefinitionChanged() {
|
---|
[17270] | 96 | Maximization = ProblemDefinition.Maximization;
|
---|
[13365] | 97 | Encoding = (TEncoding)ProblemScript.Encoding.Clone();
|
---|
[11996] | 98 |
|
---|
| 99 | OnOperatorsChanged();
|
---|
| 100 | OnReset();
|
---|
[11739] | 101 | }
|
---|
[16692] | 102 | protected override void OnNameChanged() {
|
---|
| 103 | base.OnNameChanged();
|
---|
| 104 | ProblemScript.Name = Name;
|
---|
| 105 | }
|
---|
| 106 | private void OnProblemScriptNameChanged() {
|
---|
| 107 | Name = ProblemScript.Name;
|
---|
| 108 | }
|
---|
[11739] | 109 |
|
---|
[17363] | 110 | public override void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
|
---|
| 111 | ProblemDefinition.Evaluate(solutionContext, random, cancellationToken);
|
---|
| 112 | }
|
---|
[17320] | 113 | public override double Evaluate(TEncodedSolution individual, IRandom random, CancellationToken cancellationToken) {
|
---|
| 114 | return ProblemDefinition.Evaluate(individual, random, cancellationToken);
|
---|
[11739] | 115 | }
|
---|
| 116 |
|
---|
[17363] | 117 | public override void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
|
---|
| 118 | ProblemDefinition.Analyze(solutionContexts, results, random);
|
---|
| 119 | }
|
---|
[16751] | 120 | public override void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
[11880] | 121 | ProblemDefinition.Analyze(individuals, qualities, results, random);
|
---|
[11739] | 122 | }
|
---|
[17363] | 123 |
|
---|
| 124 | public override IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
|
---|
| 125 | return ProblemDefinition.GetNeighbors(solutionContext, random);
|
---|
| 126 | }
|
---|
[16751] | 127 | public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random) {
|
---|
[11739] | 128 | return ProblemDefinition.GetNeighbors(individual, random);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|