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