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