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("OneMax Problem", "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 ValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
67 | get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
68 | }
|
---|
69 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
70 | get { return BestKnownQualityParameter; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Properties
|
---|
75 | public IntValue Length {
|
---|
76 | get { return LengthParameter.Value; }
|
---|
77 | set { LengthParameter.Value = value; }
|
---|
78 | }
|
---|
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 | }
|
---|
96 | public DoubleValue BestKnownQuality {
|
---|
97 | get { return BestKnownQualityParameter.Value; }
|
---|
98 | }
|
---|
99 | public IEnumerable<IOperator> Operators {
|
---|
100 | get { return operators.Cast<IOperator>(); }
|
---|
101 | }
|
---|
102 | private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
|
---|
103 | get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | [Storable]
|
---|
108 | private List<IOperator> operators;
|
---|
109 |
|
---|
110 | [StorableConstructor]
|
---|
111 | private OneMaxProblem(bool deserializing) : base() { }
|
---|
112 | public OneMaxProblem()
|
---|
113 | : base() {
|
---|
114 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
115 | OneMaxEvaluator evaluator = new OneMaxEvaluator();
|
---|
116 |
|
---|
117 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
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));
|
---|
121 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
|
---|
122 |
|
---|
123 | creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
124 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
125 | ParameterizeSolutionCreator();
|
---|
126 | ParameterizeEvaluator();
|
---|
127 |
|
---|
128 | InitializeOperators();
|
---|
129 | AttachEventHandlers();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
133 | OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
|
---|
134 | clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
135 | clone.AttachEventHandlers();
|
---|
136 | return clone;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #region Events
|
---|
140 | public event EventHandler SolutionCreatorChanged;
|
---|
141 | private void OnSolutionCreatorChanged() {
|
---|
142 | EventHandler handler = SolutionCreatorChanged;
|
---|
143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 | public event EventHandler EvaluatorChanged;
|
---|
146 | private void OnEvaluatorChanged() {
|
---|
147 | EventHandler handler = EvaluatorChanged;
|
---|
148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
149 | }
|
---|
150 | public event EventHandler OperatorsChanged;
|
---|
151 | private void OnOperatorsChanged() {
|
---|
152 | EventHandler handler = OperatorsChanged;
|
---|
153 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
154 | }
|
---|
155 | public event EventHandler Reset;
|
---|
156 | private void OnReset() {
|
---|
157 | EventHandler handler = Reset;
|
---|
158 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
162 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
163 | ParameterizeSolutionCreator();
|
---|
164 | ParameterizeEvaluator();
|
---|
165 | ParameterizeAnalyzer();
|
---|
166 | ParameterizeOperators();
|
---|
167 | OnSolutionCreatorChanged();
|
---|
168 | }
|
---|
169 | private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
170 | ParameterizeEvaluator();
|
---|
171 | ParameterizeAnalyzer();
|
---|
172 | ParameterizeOperators();
|
---|
173 | }
|
---|
174 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
175 | ParameterizeEvaluator();
|
---|
176 | ParameterizeAnalyzer();
|
---|
177 | OnEvaluatorChanged();
|
---|
178 | }
|
---|
179 | void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
180 | ParameterizeSolutionCreator();
|
---|
181 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
182 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
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 | }
|
---|
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 | }
|
---|
196 | #endregion
|
---|
197 |
|
---|
198 | #region Helpers
|
---|
199 | [StorableHook(HookType.AfterDeserialization)]
|
---|
200 | private void AttachEventHandlers() {
|
---|
201 | // Start BackwardsCompatibility3.3 (remove with 3.4)
|
---|
202 | if (operators == null) InitializeOperators();
|
---|
203 | // End BackwardsCompatibility3.3
|
---|
204 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
205 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
206 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
207 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
208 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
209 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
210 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
211 | }
|
---|
212 | private void ParameterizeSolutionCreator() {
|
---|
213 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
214 | }
|
---|
215 | private void ParameterizeEvaluator() {
|
---|
216 | if (Evaluator is OneMaxEvaluator)
|
---|
217 | ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
218 | }
|
---|
219 | private void ParameterizeAnalyzer() {
|
---|
220 | BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
221 | BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
222 | BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
223 | BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
224 | }
|
---|
225 | private void InitializeOperators() {
|
---|
226 | operators = new List<IOperator>();
|
---|
227 | operators.Add(new BestOneMaxSolutionAnalyzer());
|
---|
228 | ParameterizeAnalyzer();
|
---|
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 | }
|
---|