1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Common.Resources;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.Parameters;
|
---|
12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
13 | using HeuristicLab.PluginInfrastructure;
|
---|
14 | using HeuristicLab.Random;
|
---|
15 | using HeuristicLab.Problems.NK.WeightInitializers;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.Problems.NK {
|
---|
18 |
|
---|
19 | [Item("NK Landscape", "Represents an NK landscape optimization problem.")]
|
---|
20 | [Creatable("Problems")]
|
---|
21 | [StorableClass]
|
---|
22 | public sealed class NKLandscape : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
23 | public string Filename { get; set; }
|
---|
24 |
|
---|
25 | public override Image ItemImage {
|
---|
26 | get { return VSImageLibrary.Type; }
|
---|
27 | }
|
---|
28 |
|
---|
29 | #region Parameter Properties
|
---|
30 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
31 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
32 | }
|
---|
33 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
34 | get { return MaximizationParameter; }
|
---|
35 | }
|
---|
36 | public ValueParameter<IntValue> LengthParameter {
|
---|
37 | get { return (ValueParameter<IntValue>)Parameters["Length"]; }
|
---|
38 | }
|
---|
39 | public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
|
---|
40 | get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
|
---|
41 | }
|
---|
42 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
43 | get { return SolutionCreatorParameter; }
|
---|
44 | }
|
---|
45 | public ValueParameter<INKEvaluator> EvaluatorParameter {
|
---|
46 | get { return (ValueParameter<INKEvaluator>)Parameters["Evaluator"]; }
|
---|
47 | }
|
---|
48 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
49 | get { return EvaluatorParameter; }
|
---|
50 | }
|
---|
51 | public ValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
52 | get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
53 | }
|
---|
54 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
55 | get { return BestKnownQualityParameter; }
|
---|
56 | }
|
---|
57 | public ValueParameter<BoolMatrix> GeneInteractionsParameter {
|
---|
58 | get { return (ValueParameter<BoolMatrix>)Parameters["GeneInteractions"]; }
|
---|
59 | }
|
---|
60 | public ValueParameter<IntValue> InteractionSeedParameter {
|
---|
61 | get { return (ValueParameter<IntValue>)Parameters["InteractionSeed"]; }
|
---|
62 | }
|
---|
63 | public ValueParameter<IntValue> NrOfInteractionsParameter {
|
---|
64 | get { return (ValueParameter<IntValue>)Parameters["NrOfInteractions"]; }
|
---|
65 | }
|
---|
66 | public ValueParameter<IntValue> NrOfFitnessComponentsParameter {
|
---|
67 | get { return (ValueParameter<IntValue>)Parameters["NrOfFitnessComponents"]; }
|
---|
68 | }
|
---|
69 | public ValueParameter<DoubleArray> WeightsParameter {
|
---|
70 | get { return (ValueParameter<DoubleArray>)Parameters["Weights"]; }
|
---|
71 | }
|
---|
72 | public OptionalConstrainedValueParameter<IInteractionInitializer> InteractionInitializerParameter {
|
---|
73 | get { return (OptionalConstrainedValueParameter<IInteractionInitializer>)Parameters["InteractionInitializer"]; }
|
---|
74 | }
|
---|
75 | public OptionalConstrainedValueParameter<IWeightsInitializer> WeightsInitializerParameter {
|
---|
76 | get { return (OptionalConstrainedValueParameter<IWeightsInitializer>)Parameters["WeightsInitializer"]; }
|
---|
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 IHeuristicOptimizationProblem.SolutionCreator {
|
---|
90 | get { return SolutionCreatorParameter.Value; }
|
---|
91 | }
|
---|
92 | public INKEvaluator Evaluator {
|
---|
93 | get { return EvaluatorParameter.Value; }
|
---|
94 | set { EvaluatorParameter.Value = value; }
|
---|
95 | }
|
---|
96 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
97 | get { return EvaluatorParameter.Value; }
|
---|
98 | }
|
---|
99 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
100 | get { return EvaluatorParameter.Value; }
|
---|
101 | }
|
---|
102 | public DoubleValue BestKnownQuality {
|
---|
103 | get { return BestKnownQualityParameter.Value; }
|
---|
104 | }
|
---|
105 | public IEnumerable<IOperator> Operators {
|
---|
106 | get { return operators.Cast<IOperator>(); }
|
---|
107 | }
|
---|
108 | public IInteractionInitializer InteractionInitializer {
|
---|
109 | get { return InteractionInitializerParameter.Value; }
|
---|
110 | }
|
---|
111 | //private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
|
---|
112 | //get { return operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
|
---|
113 | //}
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | private static MersenneTwister random = new MersenneTwister();
|
---|
117 |
|
---|
118 | [Storable]
|
---|
119 | private List<IOperator> operators;
|
---|
120 |
|
---|
121 | [StorableConstructor]
|
---|
122 | private NKLandscape(bool deserializing) : base(deserializing) { }
|
---|
123 | private NKLandscape(NKLandscape original, Cloner cloner)
|
---|
124 | : base(original, cloner) {
|
---|
125 | operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
126 | AttachEventHandlers();
|
---|
127 | }
|
---|
128 | public NKLandscape()
|
---|
129 | : base() {
|
---|
130 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
131 | INKEvaluator evaluator = new NKEvaluator();
|
---|
132 |
|
---|
133 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the OneMax Problem is a maximization problem.", new BoolValue(true)));
|
---|
134 | Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10)));
|
---|
135 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new OneMax solutions.", creator));
|
---|
136 | Parameters.Add(new ValueParameter<INKEvaluator>("Evaluator", "The operator which should be used to evaluate NK landscape solutions.", evaluator));
|
---|
137 | Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this OneMax instance.", new DoubleValue(0)));
|
---|
138 | Parameters.Add(new ValueParameter<BoolMatrix>("GeneInteractions", "Every column gives the participating genes for each fitness component"));
|
---|
139 | Parameters.Add(new ValueParameter<IntValue>("InteractionSeed", "The seed used for the hash function to generate interaction tables.", new IntValue(random.Next())));
|
---|
140 | Parameters.Add(new ValueParameter<IntValue>("NrOfFitnessComponents", "Number of fitness component functions. (nr of columns in the interaction column)", new IntValue(10)));
|
---|
141 | Parameters.Add(new ValueParameter<IntValue>("NrOfInteractions", "Number of genes interacting with each other. (nr of True values per column in the interaction matrix)", new IntValue(3)));
|
---|
142 | Parameters.Add(new ValueParameter<DoubleArray>("Weights", "The weights for the component functions. If shorted, will be repeated.", new DoubleArray(new[] { 1.0 })));
|
---|
143 | Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialize interactions within the component functions."));
|
---|
144 | Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution"));
|
---|
145 |
|
---|
146 | InitializeInteractionInitializerParameter();
|
---|
147 | InitializeWeightsInitializerParameter();
|
---|
148 |
|
---|
149 | ParameterizeSolutionCreator();
|
---|
150 | ParameterizeEvaluator();
|
---|
151 |
|
---|
152 | InitializeOperators();
|
---|
153 | AttachEventHandlers();
|
---|
154 | InitializeInteractions();
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void InitializeInteractionInitializerParameter() {
|
---|
158 | foreach (var initializer in ApplicationManager.Manager.GetInstances<IInteractionInitializer>())
|
---|
159 | InteractionInitializerParameter.ValidValues.Add(initializer);
|
---|
160 | InteractionInitializerParameter.Value = InteractionInitializerParameter.ValidValues.First(v => v is RandomInteractionsInitializer);
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void InitializeWeightsInitializerParameter() {
|
---|
164 | foreach (var initializer in ApplicationManager.Manager.GetInstances<IWeightsInitializer>())
|
---|
165 | WeightsInitializerParameter.ValidValues.Add(initializer);
|
---|
166 | WeightsInitializerParameter.Value = WeightsInitializerParameter.ValidValues.First(v => v is EqualWeightsInitializer);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
170 | return new NKLandscape(this, cloner);
|
---|
171 | }
|
---|
172 |
|
---|
173 | #region Events
|
---|
174 | public event EventHandler SolutionCreatorChanged;
|
---|
175 | private void OnSolutionCreatorChanged() {
|
---|
176 | EventHandler handler = SolutionCreatorChanged;
|
---|
177 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
178 | }
|
---|
179 | public event EventHandler EvaluatorChanged;
|
---|
180 | private void OnEvaluatorChanged() {
|
---|
181 | EventHandler handler = EvaluatorChanged;
|
---|
182 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
183 | }
|
---|
184 | public event EventHandler OperatorsChanged;
|
---|
185 | private void OnOperatorsChanged() {
|
---|
186 | EventHandler handler = OperatorsChanged;
|
---|
187 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
188 | }
|
---|
189 | public event EventHandler Reset;
|
---|
190 | private void OnReset() {
|
---|
191 | EventHandler handler = Reset;
|
---|
192 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
193 | }
|
---|
194 |
|
---|
195 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
196 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
197 | ParameterizeSolutionCreator();
|
---|
198 | ParameterizeEvaluator();
|
---|
199 | ParameterizeAnalyzer();
|
---|
200 | ParameterizeOperators();
|
---|
201 | OnSolutionCreatorChanged();
|
---|
202 | }
|
---|
203 | private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
204 | ParameterizeEvaluator();
|
---|
205 | ParameterizeAnalyzer();
|
---|
206 | ParameterizeOperators();
|
---|
207 | }
|
---|
208 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
209 | ParameterizeEvaluator();
|
---|
210 | ParameterizeAnalyzer();
|
---|
211 | OnEvaluatorChanged();
|
---|
212 | }
|
---|
213 | void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
214 | ParameterizeSolutionCreator();
|
---|
215 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
216 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
217 | }
|
---|
218 | void Length_ValueChanged(object sender, EventArgs e) {
|
---|
219 | NrOfFitnessComponentsParameter.Value = new IntValue(Length.Value);
|
---|
220 | }
|
---|
221 | void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
222 | //BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
223 | }
|
---|
224 | void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
225 | string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
|
---|
226 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
227 | op.OneBitflipMoveParameter.ActualName = name;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | #endregion
|
---|
232 |
|
---|
233 | #region Helpers
|
---|
234 | [StorableHook(HookType.AfterDeserialization)]
|
---|
235 | private void AfterDeserialization() {
|
---|
236 | if (!Parameters.ContainsKey("InteractionInitializer")) {
|
---|
237 | Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialized the interaction between bits and fitness components"));
|
---|
238 | InitializeInteractionInitializerParameter();
|
---|
239 | }
|
---|
240 | if (!Parameters.ContainsKey("WeightsInitializer")) {
|
---|
241 | Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution"));
|
---|
242 | InitializeWeightsInitializerParameter();
|
---|
243 | }
|
---|
244 | AttachEventHandlers();
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void AttachEventHandlers() {
|
---|
248 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
249 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
250 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
251 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
252 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
253 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
254 | NrOfInteractionsParameter.ValueChanged += InteractionParameterChanged;
|
---|
255 | NrOfInteractionsParameter.Value.ValueChanged += InteractionParameterChanged;
|
---|
256 | NrOfFitnessComponentsParameter.ValueChanged += InteractionParameterChanged;
|
---|
257 | NrOfFitnessComponentsParameter.Value.ValueChanged += InteractionParameterChanged;
|
---|
258 | InteractionInitializerParameter.ValueChanged += new EventHandler(InteractionInitializerParameter_ValueChanged);
|
---|
259 | WeightsInitializerParameter.ValueChanged += new EventHandler(WeightsInitializerParameter_ValueChanged);
|
---|
260 | }
|
---|
261 |
|
---|
262 | void WeightsInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
263 | InitializeWeights();
|
---|
264 | }
|
---|
265 |
|
---|
266 | void InteractionInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
267 | InitializeInteractions();
|
---|
268 | }
|
---|
269 |
|
---|
270 | private void InteractionParameterChanged(object sender, EventArgs e) {
|
---|
271 | InitializeInteractions();
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void ParameterizeSolutionCreator() {
|
---|
275 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void ParameterizeEvaluator() {
|
---|
279 | if (Evaluator is NKEvaluator)
|
---|
280 | ((NKEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
281 | }
|
---|
282 | private void ParameterizeAnalyzer() {
|
---|
283 | //BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
284 | //BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
285 | //BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
286 | //BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
287 | }
|
---|
288 |
|
---|
289 | private void InitializeOperators() {
|
---|
290 | operators = new List<IOperator>();
|
---|
291 | //operators.Add(new BestOneMaxSolutionAnalyzer());
|
---|
292 | ParameterizeAnalyzer();
|
---|
293 | foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
294 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is INKMoveEvaluator)) {
|
---|
295 | operators.Add(op);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | ParameterizeOperators();
|
---|
299 | InitializeMoveGenerators();
|
---|
300 | }
|
---|
301 | private void InitializeMoveGenerators() {
|
---|
302 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
303 | if (op is IMoveGenerator) {
|
---|
304 | op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 | private void ParameterizeOperators() {
|
---|
309 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
310 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
311 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
312 | }
|
---|
313 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
314 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
315 | }
|
---|
316 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
317 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | private void InitializeInteractions() {
|
---|
322 | if (InteractionInitializer != null)
|
---|
323 | GeneInteractionsParameter.Value = InteractionInitializer.InitializeInterations(
|
---|
324 | Length.Value,
|
---|
325 | NrOfFitnessComponentsParameter.Value.Value,
|
---|
326 | NrOfInteractionsParameter.Value.Value,
|
---|
327 | random);
|
---|
328 | }
|
---|
329 |
|
---|
330 | private void InitializeWeights() {
|
---|
331 | if (WeightsInitializerParameter.Value != null)
|
---|
332 | WeightsParameter.Value = new DoubleArray(
|
---|
333 | WeightsInitializerParameter.Value.GetWeights(NrOfFitnessComponentsParameter.Value.Value)
|
---|
334 | .ToArray());
|
---|
335 | }
|
---|
336 | #endregion
|
---|
337 | }
|
---|
338 | }
|
---|