Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11140 was 8172, checked in by gkronber, 12 years ago

#1797: adapted return type of ConstrainedValueParameter properties in FLA branch.

File size: 11.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.BinaryVectorEncoding;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.PluginInfrastructure;
12using HeuristicLab.Problems.NK.WeightInitializers;
13using HeuristicLab.Random;
14
15namespace HeuristicLab.Problems.NK {
16
17  [Item("NK Landscape", "Represents an NK landscape optimization problem.")]
18  [Creatable("Problems")]
19  [StorableClass]
20  public sealed class NKLandscape : SingleObjectiveHeuristicOptimizationProblem<INKEvaluator, IBinaryVectorCreator>, IStorableContent {
21
22    public string Filename { get; set; }
23
24    #region Parameter Properties
25    public ValueParameter<IntValue> LengthParameter {
26      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
27    }
28    public ValueParameter<BoolMatrix> GeneInteractionsParameter {
29      get { return (ValueParameter<BoolMatrix>)Parameters["GeneInteractions"]; }
30    }
31    public ValueParameter<IntValue> InteractionSeedParameter {
32      get { return (ValueParameter<IntValue>)Parameters["InteractionSeed"]; }
33    }
34    public ValueParameter<IntValue> NrOfInteractionsParameter {
35      get { return (ValueParameter<IntValue>)Parameters["NrOfInteractions"]; }
36    }
37    public ValueParameter<IntValue> NrOfFitnessComponentsParameter {
38      get { return (ValueParameter<IntValue>)Parameters["NrOfFitnessComponents"]; }
39    }
40    public ValueParameter<DoubleArray> WeightsParameter {
41      get { return (ValueParameter<DoubleArray>)Parameters["Weights"]; }
42    }
43    public IConstrainedValueParameter<IInteractionInitializer> InteractionInitializerParameter {
44      get { return (IConstrainedValueParameter<IInteractionInitializer>)Parameters["InteractionInitializer"]; }
45    }
46    public IConstrainedValueParameter<IWeightsInitializer> WeightsInitializerParameter {
47      get { return (IConstrainedValueParameter<IWeightsInitializer>)Parameters["WeightsInitializer"]; }
48    }
49    #endregion
50
51    #region Properties
52    public IntValue Length {
53      get { return LengthParameter.Value; }
54      set { LengthParameter.Value = value; }
55    }
56    public IInteractionInitializer InteractionInitializer {
57      get { return InteractionInitializerParameter.Value; }
58    }
59    [Storable(AllowOneWay = true)]
60    private List<IOperator> operators {
61      set {
62        Operators.AddRange(value);
63      }
64    }
65    #endregion
66
67    private static MersenneTwister random = new MersenneTwister();
68
69    [StorableConstructor]
70    private NKLandscape(bool deserializing) : base(deserializing) { }
71    private NKLandscape(NKLandscape original, Cloner cloner)
72      : base(original, cloner) {
73      AttachEventHandlers();
74    }
75    public NKLandscape()
76      : base() {
77      Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10)));
78      Parameters.Add(new ValueParameter<BoolMatrix>("GeneInteractions", "Every column gives the participating genes for each fitness component"));
79      Parameters.Add(new ValueParameter<IntValue>("InteractionSeed", "The seed used for the hash function to generate interaction tables.", new IntValue(random.Next())));
80      Parameters.Add(new ValueParameter<IntValue>("NrOfFitnessComponents", "Number of fitness component functions. (nr of columns in the interaction column)", new IntValue(10)));
81      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)));
82      Parameters.Add(new ValueParameter<DoubleArray>("Weights", "The weights for the component functions. If shorted, will be repeated.", new DoubleArray(new[] { 1.0 })));
83      Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialize interactions within the component functions."));
84      Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution"));
85
86      SolutionCreator = new RandomBinaryVectorCreator();
87      Evaluator = new NKEvaluator();
88
89      InitializeInteractionInitializerParameter();
90      InitializeWeightsInitializerParameter();
91
92      ParameterizeSolutionCreator();
93      ParameterizeEvaluator();
94
95      InitializeOperators();
96      AttachEventHandlers();
97      InitializeInteractions();
98    }
99
100    private void InitializeInteractionInitializerParameter() {
101      foreach (var initializer in ApplicationManager.Manager.GetInstances<IInteractionInitializer>())
102        InteractionInitializerParameter.ValidValues.Add(initializer);
103      InteractionInitializerParameter.Value = InteractionInitializerParameter.ValidValues.First(v => v is RandomInteractionsInitializer);
104    }
105
106    private void InitializeWeightsInitializerParameter() {
107      foreach (var initializer in ApplicationManager.Manager.GetInstances<IWeightsInitializer>())
108        WeightsInitializerParameter.ValidValues.Add(initializer);
109      WeightsInitializerParameter.Value = WeightsInitializerParameter.ValidValues.First(v => v is EqualWeightsInitializer);
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new NKLandscape(this, cloner);
114    }
115
116    #region Events
117    protected override void OnSolutionCreatorChanged() {
118      SolutionCreator.BinaryVectorParameter.ActualNameChanged += SolutionCreator_BinaryVectorParameter_ActualNameChanged;
119      ParameterizeSolutionCreator();
120      ParameterizeEvaluator();
121      ParameterizeAnalyzer();
122      ParameterizeOperators();
123      base.OnSolutionCreatorChanged();
124    }
125    private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
126      ParameterizeEvaluator();
127      ParameterizeAnalyzer();
128      ParameterizeOperators();
129    }
130    protected override void OnEvaluatorChanged() {
131      ParameterizeEvaluator();
132      ParameterizeAnalyzer();
133      base.OnEvaluatorChanged();
134    }
135    void LengthParameter_ValueChanged(object sender, EventArgs e) {
136      ParameterizeSolutionCreator();
137      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
138      BestKnownQualityParameter.Value.Value = Length.Value;
139    }
140    void Length_ValueChanged(object sender, EventArgs e) {
141      NrOfFitnessComponentsParameter.Value = new IntValue(Length.Value);
142    }
143    void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
144      string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
145      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
146        op.OneBitflipMoveParameter.ActualName = name;
147      }
148    }
149    #endregion
150
151    #region Helpers
152    [StorableHook(HookType.AfterDeserialization)]
153    private void AfterDeserialization() {
154      if (!Parameters.ContainsKey("InteractionInitializer")) {
155        Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialized the interaction between bits and fitness components"));
156        InitializeInteractionInitializerParameter();
157      }
158      if (!Parameters.ContainsKey("WeightsInitializer")) {
159        Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution"));
160        InitializeWeightsInitializerParameter();
161      }
162      AttachEventHandlers();
163    }
164
165    private void AttachEventHandlers() {
166      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
167      LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
168      LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
169      NrOfInteractionsParameter.ValueChanged += InteractionParameterChanged;
170      NrOfInteractionsParameter.Value.ValueChanged += InteractionParameterChanged;
171      NrOfFitnessComponentsParameter.ValueChanged += InteractionParameterChanged;
172      NrOfFitnessComponentsParameter.Value.ValueChanged += InteractionParameterChanged;
173      InteractionInitializerParameter.ValueChanged += new EventHandler(InteractionInitializerParameter_ValueChanged);
174      WeightsInitializerParameter.ValueChanged += new EventHandler(WeightsInitializerParameter_ValueChanged);
175    }
176
177    void WeightsInitializerParameter_ValueChanged(object sender, EventArgs e) {
178      InitializeWeights();
179    }
180
181    void InteractionInitializerParameter_ValueChanged(object sender, EventArgs e) {
182      InitializeInteractions();
183    }
184
185    private void InteractionParameterChanged(object sender, EventArgs e) {
186      InitializeInteractions();
187    }
188
189    private void ParameterizeSolutionCreator() {
190      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
191    }
192
193    private void ParameterizeEvaluator() {
194      if (Evaluator is NKEvaluator)
195        ((NKEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
196    }
197    private void ParameterizeAnalyzer() {
198      //BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
199      //BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
200      //BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
201      //BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
202    }
203
204    private void InitializeOperators() {
205      ParameterizeAnalyzer();
206      foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
207        if (!(op is ISingleObjectiveMoveEvaluator) || (op is INKMoveEvaluator)) {
208          Operators.Add(op);
209        }
210      }
211      ParameterizeOperators();
212      InitializeMoveGenerators();
213    }
214    private void InitializeMoveGenerators() {
215      foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
216        if (op is IMoveGenerator) {
217          op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
218        }
219      }
220    }
221    private void ParameterizeOperators() {
222      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
223        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
224        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
225      }
226      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
227        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
228      }
229      foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
230        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
231      }
232    }
233
234    private void InitializeInteractions() {
235      if (InteractionInitializer != null)
236        GeneInteractionsParameter.Value = InteractionInitializer.InitializeInterations(
237          Length.Value,
238          NrOfFitnessComponentsParameter.Value.Value,
239          NrOfInteractionsParameter.Value.Value,
240          random);
241    }
242
243    private void InitializeWeights() {
244      if (WeightsInitializerParameter.Value != null)
245        WeightsParameter.Value = new DoubleArray(
246          WeightsInitializerParameter.Value.GetWeights(NrOfFitnessComponentsParameter.Value.Value)
247          .ToArray());
248    }
249    #endregion
250
251  }
252}
Note: See TracBrowser for help on using the repository browser.