1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.Knapsack {
|
---|
36 | [Item("KnapsackProblem", "Represents a Knapsack Problem.")]
|
---|
37 | [Creatable("Problems")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class KnapsackProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
40 | public override Image ItemImage {
|
---|
41 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region Parameter Properties
|
---|
45 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
46 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
47 | }
|
---|
48 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
49 | get { return MaximizationParameter; }
|
---|
50 | }
|
---|
51 | public ValueParameter<IntValue> KnapsackCapacityParameter {
|
---|
52 | get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
53 | }
|
---|
54 | public ValueParameter<IntArray> WeightsParameter {
|
---|
55 | get { return (ValueParameter<IntArray>)Parameters["Weights"]; }
|
---|
56 | }
|
---|
57 | public ValueParameter<IntArray> ValuesParameter {
|
---|
58 | get { return (ValueParameter<IntArray>)Parameters["Values"]; }
|
---|
59 | }
|
---|
60 | public ValueParameter<DoubleValue> PenaltyParameter {
|
---|
61 | get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }
|
---|
62 | }
|
---|
63 | public ValueParameter<IBinaryVectorCreator> SolutionCreatorParameter {
|
---|
64 | get { return (ValueParameter<IBinaryVectorCreator>)Parameters["SolutionCreator"]; }
|
---|
65 | }
|
---|
66 | IParameter IProblem.SolutionCreatorParameter {
|
---|
67 | get { return SolutionCreatorParameter; }
|
---|
68 | }
|
---|
69 | public ValueParameter<IKnapsackEvaluator> EvaluatorParameter {
|
---|
70 | get { return (ValueParameter<IKnapsackEvaluator>)Parameters["Evaluator"]; }
|
---|
71 | }
|
---|
72 | IParameter IProblem.EvaluatorParameter {
|
---|
73 | get { return EvaluatorParameter; }
|
---|
74 | }
|
---|
75 | public OptionalValueParameter<IKnapsackSolutionsVisualizer> VisualizerParameter {
|
---|
76 | get { return (OptionalValueParameter<IKnapsackSolutionsVisualizer>)Parameters["Visualizer"]; }
|
---|
77 | }
|
---|
78 | IParameter IProblem.VisualizerParameter {
|
---|
79 | get { return VisualizerParameter; }
|
---|
80 | }
|
---|
81 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
82 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
83 | }
|
---|
84 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
85 | get { return BestKnownQualityParameter; }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | #region Properties
|
---|
90 | public IntValue KnapsackCapacity {
|
---|
91 | get { return KnapsackCapacityParameter.Value; }
|
---|
92 | set { KnapsackCapacityParameter.Value = value; }
|
---|
93 | }
|
---|
94 | public IntArray Weights {
|
---|
95 | get { return WeightsParameter.Value; }
|
---|
96 | set { WeightsParameter.Value = value; }
|
---|
97 | }
|
---|
98 | public IntArray Values {
|
---|
99 | get { return ValuesParameter.Value; }
|
---|
100 | set { ValuesParameter.Value = value; }
|
---|
101 | }
|
---|
102 | public DoubleValue Penalty {
|
---|
103 | get { return PenaltyParameter.Value; }
|
---|
104 | set { PenaltyParameter.Value = value; }
|
---|
105 | }
|
---|
106 | public IBinaryVectorCreator SolutionCreator {
|
---|
107 | get { return SolutionCreatorParameter.Value; }
|
---|
108 | set { SolutionCreatorParameter.Value = value; }
|
---|
109 | }
|
---|
110 | ISolutionCreator IProblem.SolutionCreator {
|
---|
111 | get { return SolutionCreatorParameter.Value; }
|
---|
112 | }
|
---|
113 | public IKnapsackEvaluator Evaluator {
|
---|
114 | get { return EvaluatorParameter.Value; }
|
---|
115 | set { EvaluatorParameter.Value = value; }
|
---|
116 | }
|
---|
117 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
118 | get { return EvaluatorParameter.Value; }
|
---|
119 | }
|
---|
120 | IEvaluator IProblem.Evaluator {
|
---|
121 | get { return EvaluatorParameter.Value; }
|
---|
122 | }
|
---|
123 | public IKnapsackSolutionsVisualizer Visualizer {
|
---|
124 | get { return VisualizerParameter.Value; }
|
---|
125 | set { VisualizerParameter.Value = value; }
|
---|
126 | }
|
---|
127 | ISolutionsVisualizer IProblem.Visualizer {
|
---|
128 | get { return VisualizerParameter.Value; }
|
---|
129 | }
|
---|
130 | public DoubleValue BestKnownQuality {
|
---|
131 | get { return BestKnownQualityParameter.Value; }
|
---|
132 | set { BestKnownQualityParameter.Value = value; }
|
---|
133 | }
|
---|
134 | private List<IBinaryVectorOperator> operators;
|
---|
135 | public IEnumerable<IOperator> Operators {
|
---|
136 | get { return operators.Cast<IOperator>(); }
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 |
|
---|
140 | private void InitializeRandomKnapsackInstance() {
|
---|
141 | System.Random rand = new System.Random();
|
---|
142 |
|
---|
143 | int itemCount = rand.Next(10, 100);
|
---|
144 | int capacity = itemCount * 5;
|
---|
145 |
|
---|
146 | KnapsackCapacity = new IntValue(capacity);
|
---|
147 | Weights = new IntArray(itemCount);
|
---|
148 | Values = new IntArray(itemCount);
|
---|
149 |
|
---|
150 | for (int i = 0; i < itemCount; i++ ) {
|
---|
151 | int value = rand.Next(1, 10);
|
---|
152 | int weight = rand.Next(1, 10);
|
---|
153 |
|
---|
154 | Values[i] = value;
|
---|
155 | Weights[i] = weight;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public KnapsackProblem()
|
---|
160 | : base() {
|
---|
161 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
162 | KnapsackEvaluator evaluator = new KnapsackEvaluator();
|
---|
163 |
|
---|
164 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Knapsack Problem is a maximization problem.", new BoolValue(true)));
|
---|
165 | Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
|
---|
166 | Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
|
---|
167 | Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
|
---|
168 | Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1)));
|
---|
169 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new Knapsack solutions.", creator));
|
---|
170 | Parameters.Add(new ValueParameter<IKnapsackEvaluator>("Evaluator", "The operator which should be used to evaluate Knapsack solutions.", evaluator));
|
---|
171 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Knapsack instance."));
|
---|
172 | Parameters.Add(new ValueParameter<IKnapsackSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize Knapsack solutions.", null));
|
---|
173 |
|
---|
174 | creator.BinaryVectorParameter.ActualName = "KnapsackSolution";
|
---|
175 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
176 |
|
---|
177 | InitializeRandomKnapsackInstance();
|
---|
178 |
|
---|
179 | ParameterizeSolutionCreator();
|
---|
180 | ParameterizeEvaluator();
|
---|
181 |
|
---|
182 | Initialize();
|
---|
183 | }
|
---|
184 |
|
---|
185 | [StorableConstructor]
|
---|
186 | private KnapsackProblem(bool deserializing) : base() { }
|
---|
187 |
|
---|
188 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
189 | KnapsackProblem clone = (KnapsackProblem)base.Clone(cloner);
|
---|
190 | clone.Initialize();
|
---|
191 | return clone;
|
---|
192 | }
|
---|
193 |
|
---|
194 | #region Events
|
---|
195 | public event EventHandler SolutionCreatorChanged;
|
---|
196 | private void OnSolutionCreatorChanged() {
|
---|
197 | if (SolutionCreatorChanged != null)
|
---|
198 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
199 | }
|
---|
200 | public event EventHandler EvaluatorChanged;
|
---|
201 | private void OnEvaluatorChanged() {
|
---|
202 | if (EvaluatorChanged != null)
|
---|
203 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
204 | }
|
---|
205 | public event EventHandler VisualizerChanged;
|
---|
206 | private void OnVisualizerChanged() {
|
---|
207 | if (VisualizerChanged != null)
|
---|
208 | VisualizerChanged(this, EventArgs.Empty);
|
---|
209 | }
|
---|
210 |
|
---|
211 | public event EventHandler OperatorsChanged;
|
---|
212 | private void OnOperatorsChanged() {
|
---|
213 | if (OperatorsChanged != null)
|
---|
214 | OperatorsChanged(this, EventArgs.Empty);
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
218 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
219 | ParameterizeSolutionCreator();
|
---|
220 | ParameterizeEvaluator();
|
---|
221 | ParameterizeOperators();
|
---|
222 | OnSolutionCreatorChanged();
|
---|
223 | }
|
---|
224 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
225 | ParameterizeEvaluator();
|
---|
226 | ParameterizeOperators();
|
---|
227 | }
|
---|
228 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
229 | ParameterizeEvaluator();
|
---|
230 | OnEvaluatorChanged();
|
---|
231 | }
|
---|
232 | void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
233 | ParameterizeEvaluator();
|
---|
234 | }
|
---|
235 | void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
236 | ParameterizeEvaluator();
|
---|
237 | ParameterizeSolutionCreator();
|
---|
238 |
|
---|
239 | WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset);
|
---|
240 | }
|
---|
241 | void WeightsValue_Reset(object sender, EventArgs e) {
|
---|
242 | ParameterizeSolutionCreator();
|
---|
243 | }
|
---|
244 | void ValuesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
245 | ParameterizeEvaluator();
|
---|
246 | ParameterizeSolutionCreator();
|
---|
247 |
|
---|
248 | ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset);
|
---|
249 | }
|
---|
250 | void ValuesValue_Reset(object sender, EventArgs e) {
|
---|
251 | ParameterizeSolutionCreator();
|
---|
252 | }
|
---|
253 | void PenaltyParameter_ValueChanged(object sender, EventArgs e) {
|
---|
254 | ParameterizeEvaluator();
|
---|
255 | }
|
---|
256 | void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
257 | OnVisualizerChanged();
|
---|
258 | }
|
---|
259 | void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
260 | string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName;
|
---|
261 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
262 | op.OneBitflipMoveParameter.ActualName = name;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | #region Helpers
|
---|
268 | [StorableHook(HookType.AfterDeserialization)]
|
---|
269 | private void Initialize() {
|
---|
270 | InitializeOperators();
|
---|
271 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
272 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
273 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
274 | KnapsackCapacityParameter.ValueChanged += new EventHandler(KnapsackCapacityParameter_ValueChanged);
|
---|
275 | WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
276 | WeightsParameter.Value.Reset += new EventHandler(WeightsValue_Reset);
|
---|
277 | ValuesParameter.ValueChanged += new EventHandler(ValuesParameter_ValueChanged);
|
---|
278 | ValuesParameter.Value.Reset += new EventHandler(ValuesValue_Reset);
|
---|
279 | PenaltyParameter.ValueChanged += new EventHandler(PenaltyParameter_ValueChanged);
|
---|
280 | VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
|
---|
281 | }
|
---|
282 | private void ParameterizeSolutionCreator() {
|
---|
283 | if(SolutionCreator.LengthParameter.Value == null ||
|
---|
284 | SolutionCreator.LengthParameter.Value.Value != WeightsParameter.Value.Length)
|
---|
285 | SolutionCreator.LengthParameter.Value = new IntValue(WeightsParameter.Value.Length);
|
---|
286 | }
|
---|
287 | private void ParameterizeEvaluator() {
|
---|
288 | if (Evaluator is KnapsackEvaluator) {
|
---|
289 | KnapsackEvaluator knapsackEvaluator =
|
---|
290 | (KnapsackEvaluator)Evaluator;
|
---|
291 | knapsackEvaluator.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
292 | knapsackEvaluator.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
|
---|
293 | knapsackEvaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
294 | knapsackEvaluator.ValuesParameter.ActualName = ValuesParameter.Name;
|
---|
295 | knapsackEvaluator.PenaltyParameter.ActualName = PenaltyParameter.Name;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | private void InitializeOperators() {
|
---|
299 | operators = new List<IBinaryVectorOperator>();
|
---|
300 | foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) {
|
---|
301 | if (!(op is ISingleObjectiveMoveEvaluator) || (op is IKnapsackMoveEvaluator)) {
|
---|
302 | operators.Add(op);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | ParameterizeOperators();
|
---|
306 | InitializeMoveGenerators();
|
---|
307 | }
|
---|
308 | private void InitializeMoveGenerators() {
|
---|
309 | foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) {
|
---|
310 | if (op is IMoveGenerator) {
|
---|
311 | op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | private void ParameterizeOperators() {
|
---|
316 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
317 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
318 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
319 | }
|
---|
320 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
321 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
322 | }
|
---|
323 | foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) {
|
---|
324 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
325 | }
|
---|
326 | foreach (IKnapsackMoveEvaluator op in Operators.OfType<IKnapsackMoveEvaluator>()) {
|
---|
327 | op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
|
---|
328 | op.PenaltyParameter.ActualName = PenaltyParameter.Name;
|
---|
329 | op.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
330 | op.ValuesParameter.ActualName = ValuesParameter.Name;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | #endregion
|
---|
334 | }
|
---|
335 | }
|
---|