[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 {
|
---|
[3528] | 36 | [Item("OneMax Problem", "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 | }
|
---|
[3080] | 66 | public ValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 67 | get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 68 | }
|
---|
| 69 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
| 70 | get { return BestKnownQualityParameter; }
|
---|
| 71 | }
|
---|
[3065] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region Properties
|
---|
[3108] | 75 | public IntValue Length {
|
---|
| 76 | get { return LengthParameter.Value; }
|
---|
| 77 | set { LengthParameter.Value = value; }
|
---|
| 78 | }
|
---|
[3065] | 79 | public IBinaryVectorCreator SolutionCreator {
|
---|
| 80 | get { return SolutionCreatorParameter.Value; }
|
---|
| 81 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 82 | }
|
---|
| 83 | ISolutionCreator IProblem.SolutionCreator {
|
---|
| 84 | get { return SolutionCreatorParameter.Value; }
|
---|
| 85 | }
|
---|
| 86 | public IOneMaxEvaluator Evaluator {
|
---|
| 87 | get { return EvaluatorParameter.Value; }
|
---|
| 88 | set { EvaluatorParameter.Value = value; }
|
---|
| 89 | }
|
---|
| 90 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
| 91 | get { return EvaluatorParameter.Value; }
|
---|
| 92 | }
|
---|
| 93 | IEvaluator IProblem.Evaluator {
|
---|
| 94 | get { return EvaluatorParameter.Value; }
|
---|
| 95 | }
|
---|
[3080] | 96 | public DoubleValue BestKnownQuality {
|
---|
| 97 | get { return BestKnownQualityParameter.Value; }
|
---|
| 98 | }
|
---|
[3065] | 99 | public IEnumerable<IOperator> Operators {
|
---|
| 100 | get { return operators.Cast<IOperator>(); }
|
---|
| 101 | }
|
---|
[3667] | 102 | private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
|
---|
| 103 | get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3642] | 104 | }
|
---|
[3065] | 105 | #endregion
|
---|
| 106 |
|
---|
[4098] | 107 | [Storable]
|
---|
| 108 | private List<IOperator> operators;
|
---|
| 109 |
|
---|
| 110 | [StorableConstructor]
|
---|
[4118] | 111 | private OneMaxProblem(bool deserializing) : base(deserializing) { }
|
---|
[3164] | 112 | public OneMaxProblem()
|
---|
[3065] | 113 | : base() {
|
---|
| 114 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
| 115 | OneMaxEvaluator evaluator = new OneMaxEvaluator();
|
---|
| 116 |
|
---|
[3067] | 117 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
[3065] | 118 | Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
|
---|
| 119 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
|
---|
| 120 | Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
|
---|
[3080] | 121 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
|
---|
[3065] | 122 |
|
---|
| 123 | creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
| 124 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
| 125 | ParameterizeSolutionCreator();
|
---|
| 126 | ParameterizeEvaluator();
|
---|
| 127 |
|
---|
[4098] | 128 | InitializeOperators();
|
---|
| 129 | AttachEventHandlers();
|
---|
[3065] | 130 | }
|
---|
| 131 |
|
---|
| 132 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3164] | 133 | OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
|
---|
[4098] | 134 | clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
| 135 | clone.AttachEventHandlers();
|
---|
[3065] | 136 | return clone;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | #region Events
|
---|
| 140 | public event EventHandler SolutionCreatorChanged;
|
---|
| 141 | private void OnSolutionCreatorChanged() {
|
---|
[3739] | 142 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3065] | 144 | }
|
---|
| 145 | public event EventHandler EvaluatorChanged;
|
---|
| 146 | private void OnEvaluatorChanged() {
|
---|
[3739] | 147 | EventHandler handler = EvaluatorChanged;
|
---|
| 148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3065] | 149 | }
|
---|
| 150 | public event EventHandler OperatorsChanged;
|
---|
| 151 | private void OnOperatorsChanged() {
|
---|
[3739] | 152 | EventHandler handler = OperatorsChanged;
|
---|
| 153 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3065] | 154 | }
|
---|
[3739] | 155 | public event EventHandler Reset;
|
---|
| 156 | private void OnReset() {
|
---|
| 157 | EventHandler handler = Reset;
|
---|
| 158 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 159 | }
|
---|
[3065] | 160 |
|
---|
| 161 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[3642] | 162 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
[3065] | 163 | ParameterizeSolutionCreator();
|
---|
| 164 | ParameterizeEvaluator();
|
---|
[3667] | 165 | ParameterizeAnalyzer();
|
---|
[3065] | 166 | ParameterizeOperators();
|
---|
| 167 | OnSolutionCreatorChanged();
|
---|
| 168 | }
|
---|
[3642] | 169 | private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[3065] | 170 | ParameterizeEvaluator();
|
---|
[3667] | 171 | ParameterizeAnalyzer();
|
---|
[3065] | 172 | ParameterizeOperators();
|
---|
| 173 | }
|
---|
| 174 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 175 | ParameterizeEvaluator();
|
---|
[3667] | 176 | ParameterizeAnalyzer();
|
---|
[3065] | 177 | OnEvaluatorChanged();
|
---|
| 178 | }
|
---|
[3108] | 179 | void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 180 | ParameterizeSolutionCreator();
|
---|
| 181 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
[3115] | 182 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
[3108] | 183 | }
|
---|
| 184 | void Length_ValueChanged(object sender, EventArgs e) {
|
---|
| 185 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
| 186 | }
|
---|
| 187 | void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 188 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
| 189 | }
|
---|
[3121] | 190 | void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 191 | string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
|
---|
| 192 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
| 193 | op.OneBitflipMoveParameter.ActualName = name;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
[3065] | 196 | #endregion
|
---|
| 197 |
|
---|
| 198 | #region Helpers
|
---|
| 199 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4118] | 200 | private void AfterDeserializationHook() {
|
---|
| 201 | // BackwardsCompatibility3.3
|
---|
| 202 | #region Backwards compatible code (remove with 3.4)
|
---|
| 203 | if (operators == null) InitializeOperators();
|
---|
| 204 | #endregion
|
---|
| 205 | AttachEventHandlers();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[4098] | 208 | private void AttachEventHandlers() {
|
---|
[3065] | 209 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
[3642] | 210 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
[3065] | 211 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
[3108] | 212 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
[3115] | 213 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
[3108] | 214 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
[3065] | 215 | }
|
---|
[4118] | 216 |
|
---|
[3065] | 217 | private void ParameterizeSolutionCreator() {
|
---|
[3108] | 218 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
[3065] | 219 | }
|
---|
| 220 | private void ParameterizeEvaluator() {
|
---|
| 221 | if (Evaluator is OneMaxEvaluator)
|
---|
| 222 | ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 223 | }
|
---|
[3667] | 224 | private void ParameterizeAnalyzer() {
|
---|
[3789] | 225 | BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 226 | BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
[3667] | 227 | BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 228 | BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[3642] | 229 | }
|
---|
[3065] | 230 | private void InitializeOperators() {
|
---|
[3642] | 231 | operators = new List<IOperator>();
|
---|
| 232 | operators.Add(new BestOneMaxSolutionAnalyzer());
|
---|
[3667] | 233 | ParameterizeAnalyzer();
|
---|
[3303] | 234 | foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
| 235 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
|
---|
| 236 | operators.Add(op);
|
---|
[3126] | 237 | }
|
---|
[3065] | 238 | }
|
---|
[3303] | 239 | ParameterizeOperators();
|
---|
[3121] | 240 | InitializeMoveGenerators();
|
---|
[3065] | 241 | }
|
---|
[3121] | 242 | private void InitializeMoveGenerators() {
|
---|
| 243 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
| 244 | if (op is IMoveGenerator) {
|
---|
| 245 | op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
[3065] | 249 | private void ParameterizeOperators() {
|
---|
| 250 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
| 251 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 252 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 253 | }
|
---|
| 254 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
| 255 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 256 | }
|
---|
[3121] | 257 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
| 258 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 259 | }
|
---|
[3065] | 260 | }
|
---|
| 261 | #endregion
|
---|
| 262 | }
|
---|
| 263 | }
|
---|