[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;
|
---|
[11984] | 23 | using HeuristicLab.Analysis;
|
---|
[11739] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[11996] | 26 | using HeuristicLab.Data;
|
---|
[11739] | 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[11797] | 31 | namespace HeuristicLab.Problems.Programmable {
|
---|
[11814] | 32 | [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")]
|
---|
[12504] | 33 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
|
---|
[11739] | 34 | [StorableClass]
|
---|
[12616] | 35 | public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveBasicProblem<IEncoding>, IProgrammableItem {
|
---|
[11739] | 36 |
|
---|
[13656] | 37 |
|
---|
| 38 | private FixedValueParameter<SingleObjectiveProblemDefinitionScript> SingleObjectiveProblemScriptParameter
|
---|
| 39 | {
|
---|
[11739] | 40 | get { return (FixedValueParameter<SingleObjectiveProblemDefinitionScript>)Parameters["ProblemScript"]; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[13656] | 43 | public SingleObjectiveProblemDefinitionScript ProblemScript
|
---|
| 44 | {
|
---|
[11739] | 45 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[13656] | 48 | public ISingleObjectiveProblemDefinition ProblemDefinition
|
---|
| 49 | {
|
---|
[11739] | 50 | get { return SingleObjectiveProblemScriptParameter.Value; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[11814] | 53 | private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
|
---|
[11739] | 54 | : base(original, cloner) {
|
---|
| 55 | RegisterEvents();
|
---|
| 56 | }
|
---|
[11814] | 57 | public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveProgrammableProblem(this, cloner); }
|
---|
[11739] | 58 |
|
---|
| 59 | [StorableConstructor]
|
---|
[11814] | 60 | private SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
|
---|
| 61 | public SingleObjectiveProgrammableProblem()
|
---|
[11739] | 62 | : base() {
|
---|
| 63 | Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
|
---|
[11984] | 64 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
[11739] | 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();
|
---|
[13626] | 75 | ProblemScript.NameChanged += (o, e) => OnProblemScriptNameChanged();
|
---|
[11739] | 76 | }
|
---|
| 77 |
|
---|
| 78 | private void OnProblemDefinitionChanged() {
|
---|
[12002] | 79 | Parameters.Remove("Maximization");
|
---|
[11996] | 80 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
| 81 |
|
---|
[11753] | 82 | Encoding = ProblemDefinition.Encoding;
|
---|
[11996] | 83 | OnOperatorsChanged();
|
---|
| 84 | OnReset();
|
---|
[11739] | 85 | }
|
---|
[13626] | 86 | protected override void OnNameChanged() {
|
---|
| 87 | base.OnNameChanged();
|
---|
| 88 | ProblemScript.Name = Name;
|
---|
| 89 | }
|
---|
| 90 | private void OnProblemScriptNameChanged() {
|
---|
| 91 | Name = ProblemScript.Name;
|
---|
| 92 | }
|
---|
[11739] | 93 |
|
---|
[13656] | 94 | public override bool Maximization
|
---|
| 95 | {
|
---|
[11998] | 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 | }
|
---|