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 {
|
---|
36 | [Item("OneMaxProblem", "Represents a OneMax Problem.")]
|
---|
37 | [Creatable("Problems")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class OneMaxProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
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 | }
|
---|
66 | public OptionalValueParameter<IOneMaxSolutionsVisualizer> VisualizerParameter {
|
---|
67 | get { return (OptionalValueParameter<IOneMaxSolutionsVisualizer>)Parameters["Visualizer"]; }
|
---|
68 | }
|
---|
69 | IParameter IProblem.VisualizerParameter {
|
---|
70 | get { return VisualizerParameter; }
|
---|
71 | }
|
---|
72 | public ValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
73 | get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
74 | }
|
---|
75 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
76 | get { return BestKnownQualityParameter; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | #region Properties
|
---|
81 | public IntValue Length {
|
---|
82 | get { return LengthParameter.Value; }
|
---|
83 | set { LengthParameter.Value = value; }
|
---|
84 | }
|
---|
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 | }
|
---|
102 | public IOneMaxSolutionsVisualizer Visualizer {
|
---|
103 | get { return VisualizerParameter.Value; }
|
---|
104 | set { VisualizerParameter.Value = value; }
|
---|
105 | }
|
---|
106 | ISolutionsVisualizer IProblem.Visualizer {
|
---|
107 | get { return VisualizerParameter.Value; }
|
---|
108 | }
|
---|
109 | public DoubleValue BestKnownQuality {
|
---|
110 | get { return BestKnownQualityParameter.Value; }
|
---|
111 | }
|
---|
112 | private List<IBinaryVectorOperator> operators;
|
---|
113 | public IEnumerable<IOperator> Operators {
|
---|
114 | get { return operators.Cast<IOperator>(); }
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | public OneMaxProblem()
|
---|
119 | : base() {
|
---|
120 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
121 | OneMaxEvaluator evaluator = new OneMaxEvaluator();
|
---|
122 |
|
---|
123 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
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));
|
---|
127 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
|
---|
128 | Parameters.Add(new ValueParameter<IOneMaxSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize OneMax solutions.", null));
|
---|
129 |
|
---|
130 | creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
131 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
132 | ParameterizeSolutionCreator();
|
---|
133 | ParameterizeEvaluator();
|
---|
134 |
|
---|
135 | Initialize();
|
---|
136 | }
|
---|
137 |
|
---|
138 | [StorableConstructor]
|
---|
139 | private OneMaxProblem(bool deserializing) : base() { }
|
---|
140 |
|
---|
141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
142 | OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
|
---|
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 | }
|
---|
158 | public event EventHandler VisualizerChanged;
|
---|
159 | private void OnVisualizerChanged() {
|
---|
160 | if (VisualizerChanged != null)
|
---|
161 | VisualizerChanged(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 |
|
---|
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 | }
|
---|
185 | void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
186 | ParameterizeSolutionCreator();
|
---|
187 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
188 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
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 | }
|
---|
196 | void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
197 | OnVisualizerChanged();
|
---|
198 | }
|
---|
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 | }
|
---|
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);
|
---|
214 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
215 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
216 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
217 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
218 | VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
|
---|
219 | }
|
---|
220 | private void ParameterizeSolutionCreator() {
|
---|
221 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
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>();
|
---|
229 | foreach(IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
230 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
|
---|
231 | operators.Add(op);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | ParameterizeOperators();
|
---|
235 | InitializeMoveGenerators();
|
---|
236 | }
|
---|
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 | }
|
---|
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 | }
|
---|
252 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
253 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | #endregion
|
---|
257 | }
|
---|
258 | }
|
---|