[3065] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.OneMax {
|
---|
[3164] | 36 | [Item("OneMaxProblem", "Represents a OneMax Problem.")]
|
---|
[3065] | 37 | [Creatable("Problems")]
|
---|
| 38 | [StorableClass]
|
---|
[3164] | 39 | public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
[3065] | 40 | public override Image ItemImage {
|
---|
| 41 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | #region Parameter Properties
|
---|
| 45 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 46 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 47 | }
|
---|
| 48 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
| 49 | get { return MaximizationParameter; }
|
---|
| 50 | }
|
---|
| 51 | public ValueParameter<IntValue> LengthParameter {
|
---|
| 52 | get { return (ValueParameter<IntValue>)Parameters["Length"]; }
|
---|
| 53 | }
|
---|
| 54 | public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
|
---|
| 55 | get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
|
---|
| 56 | }
|
---|
| 57 | IParameter IProblem.SolutionCreatorParameter {
|
---|
| 58 | get { return SolutionCreatorParameter; }
|
---|
| 59 | }
|
---|
| 60 | public ValueParameter<IOneMaxEvaluator> EvaluatorParameter {
|
---|
| 61 | get { return (ValueParameter<IOneMaxEvaluator>)Parameters["Evaluator"]; }
|
---|
| 62 | }
|
---|
| 63 | IParameter IProblem.EvaluatorParameter {
|
---|
| 64 | get { return EvaluatorParameter; }
|
---|
| 65 | }
|
---|
[3115] | 66 | public OptionalValueParameter<IOneMaxSolutionsVisualizer> VisualizerParameter {
|
---|
| 67 | get { return (OptionalValueParameter<IOneMaxSolutionsVisualizer>)Parameters["Visualizer"]; }
|
---|
| 68 | }
|
---|
[3107] | 69 | IParameter IProblem.VisualizerParameter {
|
---|
[3115] | 70 | get { return VisualizerParameter; }
|
---|
[3108] | 71 | }
|
---|
[3080] | 72 | public ValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 73 | get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 74 | }
|
---|
| 75 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
| 76 | get { return BestKnownQualityParameter; }
|
---|
| 77 | }
|
---|
[3065] | 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region Properties
|
---|
[3108] | 81 | public IntValue Length {
|
---|
| 82 | get { return LengthParameter.Value; }
|
---|
| 83 | set { LengthParameter.Value = value; }
|
---|
| 84 | }
|
---|
[3065] | 85 | public IBinaryVectorCreator SolutionCreator {
|
---|
| 86 | get { return SolutionCreatorParameter.Value; }
|
---|
| 87 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 88 | }
|
---|
| 89 | ISolutionCreator IProblem.SolutionCreator {
|
---|
| 90 | get { return SolutionCreatorParameter.Value; }
|
---|
| 91 | }
|
---|
| 92 | public IOneMaxEvaluator Evaluator {
|
---|
| 93 | get { return EvaluatorParameter.Value; }
|
---|
| 94 | set { EvaluatorParameter.Value = value; }
|
---|
| 95 | }
|
---|
| 96 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
| 97 | get { return EvaluatorParameter.Value; }
|
---|
| 98 | }
|
---|
| 99 | IEvaluator IProblem.Evaluator {
|
---|
| 100 | get { return EvaluatorParameter.Value; }
|
---|
| 101 | }
|
---|
[3115] | 102 | public IOneMaxSolutionsVisualizer Visualizer {
|
---|
| 103 | get { return VisualizerParameter.Value; }
|
---|
| 104 | set { VisualizerParameter.Value = value; }
|
---|
| 105 | }
|
---|
[3107] | 106 | ISolutionsVisualizer IProblem.Visualizer {
|
---|
[3115] | 107 | get { return VisualizerParameter.Value; }
|
---|
[3108] | 108 | }
|
---|
[3080] | 109 | public DoubleValue BestKnownQuality {
|
---|
| 110 | get { return BestKnownQualityParameter.Value; }
|
---|
| 111 | }
|
---|
[3065] | 112 | private List<IBinaryVectorOperator> operators;
|
---|
| 113 | public IEnumerable<IOperator> Operators {
|
---|
| 114 | get { return operators.Cast<IOperator>(); }
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
| 117 |
|
---|
[3164] | 118 | public OneMaxProblem()
|
---|
[3065] | 119 | : base() {
|
---|
| 120 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
| 121 | OneMaxEvaluator evaluator = new OneMaxEvaluator();
|
---|
| 122 |
|
---|
[3067] | 123 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
[3065] | 124 | Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
|
---|
| 125 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
|
---|
| 126 | Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
|
---|
[3080] | 127 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
|
---|
[3115] | 128 | Parameters.Add(new ValueParameter<IOneMaxSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize OneMax solutions.", null));
|
---|
[3065] | 129 |
|
---|
| 130 | creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
| 131 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
| 132 | ParameterizeSolutionCreator();
|
---|
| 133 | ParameterizeEvaluator();
|
---|
| 134 |
|
---|
| 135 | Initialize();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | [StorableConstructor]
|
---|
[3164] | 139 | private OneMaxProblem(bool deserializing) : base() { }
|
---|
[3065] | 140 |
|
---|
| 141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3164] | 142 | OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
|
---|
[3065] | 143 | clone.Initialize();
|
---|
| 144 | return clone;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | #region Events
|
---|
| 148 | public event EventHandler SolutionCreatorChanged;
|
---|
| 149 | private void OnSolutionCreatorChanged() {
|
---|
| 150 | if (SolutionCreatorChanged != null)
|
---|
| 151 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
| 152 | }
|
---|
| 153 | public event EventHandler EvaluatorChanged;
|
---|
| 154 | private void OnEvaluatorChanged() {
|
---|
| 155 | if (EvaluatorChanged != null)
|
---|
| 156 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
| 157 | }
|
---|
[3107] | 158 | public event EventHandler VisualizerChanged;
|
---|
[3108] | 159 | private void OnVisualizerChanged() {
|
---|
| 160 | if (VisualizerChanged != null)
|
---|
| 161 | VisualizerChanged(this, EventArgs.Empty);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[3065] | 164 | public event EventHandler OperatorsChanged;
|
---|
| 165 | private void OnOperatorsChanged() {
|
---|
| 166 | if (OperatorsChanged != null)
|
---|
| 167 | OperatorsChanged(this, EventArgs.Empty);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 171 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 172 | ParameterizeSolutionCreator();
|
---|
| 173 | ParameterizeEvaluator();
|
---|
| 174 | ParameterizeOperators();
|
---|
| 175 | OnSolutionCreatorChanged();
|
---|
| 176 | }
|
---|
| 177 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 178 | ParameterizeEvaluator();
|
---|
| 179 | ParameterizeOperators();
|
---|
| 180 | }
|
---|
| 181 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 182 | ParameterizeEvaluator();
|
---|
| 183 | OnEvaluatorChanged();
|
---|
| 184 | }
|
---|
[3108] | 185 | void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 186 | ParameterizeSolutionCreator();
|
---|
| 187 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
[3115] | 188 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
[3108] | 189 | }
|
---|
| 190 | void Length_ValueChanged(object sender, EventArgs e) {
|
---|
| 191 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
| 192 | }
|
---|
| 193 | void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 194 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
| 195 | }
|
---|
[3115] | 196 | void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 197 | OnVisualizerChanged();
|
---|
| 198 | }
|
---|
[3121] | 199 | void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 200 | string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
|
---|
| 201 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
| 202 | op.OneBitflipMoveParameter.ActualName = name;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
[3065] | 205 | #endregion
|
---|
| 206 |
|
---|
| 207 | #region Helpers
|
---|
| 208 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 209 | private void Initialize() {
|
---|
| 210 | InitializeOperators();
|
---|
| 211 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 212 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 213 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
[3108] | 214 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
[3115] | 215 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
[3108] | 216 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
| 217 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
[3115] | 218 | VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
|
---|
[3065] | 219 | }
|
---|
| 220 | private void ParameterizeSolutionCreator() {
|
---|
[3108] | 221 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
[3065] | 222 | }
|
---|
| 223 | private void ParameterizeEvaluator() {
|
---|
| 224 | if (Evaluator is OneMaxEvaluator)
|
---|
| 225 | ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 226 | }
|
---|
| 227 | private void InitializeOperators() {
|
---|
| 228 | operators = new List<IBinaryVectorOperator>();
|
---|
[3303] | 229 | foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
| 230 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
|
---|
| 231 | operators.Add(op);
|
---|
[3126] | 232 | }
|
---|
[3065] | 233 | }
|
---|
[3303] | 234 | ParameterizeOperators();
|
---|
[3121] | 235 | InitializeMoveGenerators();
|
---|
[3065] | 236 | }
|
---|
[3121] | 237 | private void InitializeMoveGenerators() {
|
---|
| 238 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
| 239 | if (op is IMoveGenerator) {
|
---|
| 240 | op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
[3065] | 244 | private void ParameterizeOperators() {
|
---|
| 245 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
| 246 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 247 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 248 | }
|
---|
| 249 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
| 250 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 251 | }
|
---|
[3121] | 252 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
| 253 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 254 | }
|
---|
[3065] | 255 | }
|
---|
| 256 | #endregion
|
---|
| 257 | }
|
---|
| 258 | }
|
---|