Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKLandscape.cs @ 7766

Last change on this file since 7766 was 7202, checked in by epitzer, 13 years ago

#1696: Add static item image according to #1651

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