1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.OneMax {
|
---|
35 | [Item("OneMax Problem", "Represents a OneMax Problem.")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class OneMaxProblem : SingleObjectiveHeuristicOptimizationProblem<IOneMaxEvaluator, IBinaryVectorCreator>, IStorableContent {
|
---|
38 | public string Filename { get; set; }
|
---|
39 |
|
---|
40 | #region Parameter Properties
|
---|
41 | public ValueParameter<IntValue> LengthParameter {
|
---|
42 | get { return (ValueParameter<IntValue>)Parameters["Length"]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region Properties
|
---|
47 | public IntValue Length {
|
---|
48 | get { return LengthParameter.Value; }
|
---|
49 | set { LengthParameter.Value = value; }
|
---|
50 | }
|
---|
51 | private BestOneMaxSolutionAnalyzer BestOneMaxSolutionAnalyzer {
|
---|
52 | get { return Operators.OfType<BestOneMaxSolutionAnalyzer>().FirstOrDefault(); }
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | // BackwardsCompatibility3.3
|
---|
57 | #region Backwards compatible code, remove with 3.4
|
---|
58 | [Obsolete]
|
---|
59 | [Storable(Name = "operators")]
|
---|
60 | private IEnumerable<IOperator> oldOperators {
|
---|
61 | get { return null; }
|
---|
62 | set {
|
---|
63 | if (value != null && value.Any())
|
---|
64 | Operators.AddRange(value);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | private OneMaxProblem(bool deserializing) : base(deserializing) { }
|
---|
71 | private OneMaxProblem(OneMaxProblem original, Cloner cloner)
|
---|
72 | : base(original, cloner) {
|
---|
73 | RegisterEventHandlers();
|
---|
74 | }
|
---|
75 | public OneMaxProblem()
|
---|
76 | : base(new OneMaxEvaluator(), new RandomBinaryVectorCreator()) {
|
---|
77 | Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(5)));
|
---|
78 |
|
---|
79 | Maximization.Value = true;
|
---|
80 | MaximizationParameter.Hidden = true;
|
---|
81 | BestKnownQuality = new DoubleValue(5);
|
---|
82 |
|
---|
83 | SolutionCreator.BinaryVectorParameter.ActualName = "OneMaxSolution";
|
---|
84 | Evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
85 | ParameterizeSolutionCreator();
|
---|
86 | ParameterizeEvaluator();
|
---|
87 |
|
---|
88 | InitializeOperators();
|
---|
89 | RegisterEventHandlers();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
93 | return new OneMaxProblem(this, cloner);
|
---|
94 | }
|
---|
95 |
|
---|
96 | #region Events
|
---|
97 | protected override void OnSolutionCreatorChanged() {
|
---|
98 | base.OnSolutionCreatorChanged();
|
---|
99 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
100 | ParameterizeSolutionCreator();
|
---|
101 | ParameterizeEvaluator();
|
---|
102 | ParameterizeAnalyzer();
|
---|
103 | ParameterizeOperators();
|
---|
104 | }
|
---|
105 | protected override void OnEvaluatorChanged() {
|
---|
106 | base.OnEvaluatorChanged();
|
---|
107 | ParameterizeEvaluator();
|
---|
108 | ParameterizeAnalyzer();
|
---|
109 | }
|
---|
110 | private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
111 | ParameterizeEvaluator();
|
---|
112 | ParameterizeAnalyzer();
|
---|
113 | ParameterizeOperators();
|
---|
114 | }
|
---|
115 | private void LengthParameter_ValueChanged(object sender, EventArgs e) {
|
---|
116 | ParameterizeSolutionCreator();
|
---|
117 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
118 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
119 | }
|
---|
120 | private void Length_ValueChanged(object sender, EventArgs e) {
|
---|
121 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
122 | }
|
---|
123 | private void BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
124 | BestKnownQualityParameter.Value.Value = Length.Value;
|
---|
125 | }
|
---|
126 | private void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
127 | string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
|
---|
128 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
129 | op.OneBitflipMoveParameter.ActualName = name;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | #region Helpers
|
---|
135 | [StorableHook(HookType.AfterDeserialization)]
|
---|
136 | private void AfterDeserialization() {
|
---|
137 | // BackwardsCompatibility3.3
|
---|
138 | #region Backwards compatible code (remove with 3.4)
|
---|
139 | if (Operators.Count == 0) InitializeOperators();
|
---|
140 | #endregion
|
---|
141 | RegisterEventHandlers();
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void RegisterEventHandlers() {
|
---|
145 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
|
---|
146 | LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged);
|
---|
147 | LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged);
|
---|
148 | BestKnownQualityParameter.ValueChanged += new EventHandler(BestKnownQualityParameter_ValueChanged);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void ParameterizeSolutionCreator() {
|
---|
152 | SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
153 | }
|
---|
154 | private void ParameterizeEvaluator() {
|
---|
155 | if (Evaluator is OneMaxEvaluator)
|
---|
156 | ((OneMaxEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
157 | }
|
---|
158 | private void ParameterizeAnalyzer() {
|
---|
159 | BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
160 | BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
161 | BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
162 | BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
163 | }
|
---|
164 | private void InitializeOperators() {
|
---|
165 | Operators.Add(new BestOneMaxSolutionAnalyzer());
|
---|
166 | ParameterizeAnalyzer();
|
---|
167 | foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
168 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is IOneMaxMoveEvaluator)) {
|
---|
169 | Operators.Add(op);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | ParameterizeOperators();
|
---|
173 | InitializeMoveGenerators();
|
---|
174 | }
|
---|
175 | private void InitializeMoveGenerators() {
|
---|
176 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
177 | if (op is IMoveGenerator) {
|
---|
178 | op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | private void ParameterizeOperators() {
|
---|
183 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
184 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
185 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
186 | }
|
---|
187 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
188 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
189 | }
|
---|
190 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
191 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
192 | }
|
---|
193 | foreach (var op in Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>())
|
---|
194 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
195 | }
|
---|
196 | #endregion
|
---|
197 | }
|
---|
198 | }
|
---|