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", "Represents a OneMax Problem.")]
|
---|
37 | [Creatable("Problems")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class OneMax : 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 IBinaryVectorCreator SolutionCreator {
|
---|
76 | get { return SolutionCreatorParameter.Value; }
|
---|
77 | set { SolutionCreatorParameter.Value = value; }
|
---|
78 | }
|
---|
79 | ISolutionCreator IProblem.SolutionCreator {
|
---|
80 | get { return SolutionCreatorParameter.Value; }
|
---|
81 | }
|
---|
82 | public IOneMaxEvaluator Evaluator {
|
---|
83 | get { return EvaluatorParameter.Value; }
|
---|
84 | set { EvaluatorParameter.Value = value; }
|
---|
85 | }
|
---|
86 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
87 | get { return EvaluatorParameter.Value; }
|
---|
88 | }
|
---|
89 | IEvaluator IProblem.Evaluator {
|
---|
90 | get { return EvaluatorParameter.Value; }
|
---|
91 | }
|
---|
92 | public DoubleValue BestKnownQuality {
|
---|
93 | get { return BestKnownQualityParameter.Value; }
|
---|
94 | }
|
---|
95 | private List<IBinaryVectorOperator> operators;
|
---|
96 | public IEnumerable<IOperator> Operators {
|
---|
97 | get { return operators.Cast<IOperator>(); }
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | public OneMax()
|
---|
102 | : base() {
|
---|
103 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
104 | OneMaxEvaluator evaluator = new OneMaxEvaluator();
|
---|
105 |
|
---|
106 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
107 | Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
|
---|
108 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
|
---|
109 | Parameters.Add(new ValueParameter<IOneMaxEvaluator>("Evaluator", "The operator which should be used to evaluate OneMax solutions.", evaluator));
|
---|
110 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(5)));
|
---|
111 |
|
---|
112 | creator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
113 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
114 | ParameterizeSolutionCreator();
|
---|
115 | ParameterizeEvaluator();
|
---|
116 |
|
---|
117 | Initialize();
|
---|
118 | }
|
---|
119 |
|
---|
120 | [StorableConstructor]
|
---|
121 | private OneMax(bool deserializing) : base() { }
|
---|
122 |
|
---|
123 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
124 | OneMax clone = (OneMax)base.Clone(cloner);
|
---|
125 | clone.Initialize();
|
---|
126 | return clone;
|
---|
127 | }
|
---|
128 |
|
---|
129 | #region Events
|
---|
130 | public event EventHandler SolutionCreatorChanged;
|
---|
131 | private void OnSolutionCreatorChanged() {
|
---|
132 | if (SolutionCreatorChanged != null)
|
---|
133 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
134 | }
|
---|
135 | public event EventHandler EvaluatorChanged;
|
---|
136 | private void OnEvaluatorChanged() {
|
---|
137 | if (EvaluatorChanged != null)
|
---|
138 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
139 | }
|
---|
140 | public event EventHandler OperatorsChanged;
|
---|
141 | private void OnOperatorsChanged() {
|
---|
142 | if (OperatorsChanged != null)
|
---|
143 | OperatorsChanged(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
147 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
148 | ParameterizeSolutionCreator();
|
---|
149 | ParameterizeEvaluator();
|
---|
150 | ParameterizeOperators();
|
---|
151 | OnSolutionCreatorChanged();
|
---|
152 | }
|
---|
153 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
154 | ParameterizeEvaluator();
|
---|
155 | ParameterizeOperators();
|
---|
156 | }
|
---|
157 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
158 | ParameterizeEvaluator();
|
---|
159 | OnEvaluatorChanged();
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 |
|
---|
163 | #region Helpers
|
---|
164 | [StorableHook(HookType.AfterDeserialization)]
|
---|
165 | private void Initialize() {
|
---|
166 | InitializeOperators();
|
---|
167 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
168 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
169 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
170 | }
|
---|
171 | private void ParameterizeSolutionCreator() {
|
---|
172 | SolutionCreator.LengthParameter.Value = LengthParameter.Value;
|
---|
173 | }
|
---|
174 | private void ParameterizeEvaluator() {
|
---|
175 | if (Evaluator is OneMaxEvaluator)
|
---|
176 | ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
177 | }
|
---|
178 | private void InitializeOperators() {
|
---|
179 | operators = new List<IBinaryVectorOperator>();
|
---|
180 | if (ApplicationManager.Manager != null) {
|
---|
181 | operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>());
|
---|
182 | ParameterizeOperators();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | private void ParameterizeOperators() {
|
---|
186 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
187 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
188 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
189 | }
|
---|
190 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
191 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|