Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Knapsack.cs @ 3070

Last change on this file since 3070 was 3070, checked in by svonolfe, 14 years ago

Added the Knapsack problem to test the BinaryVector operators (#917)

File size: 9.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.Knapsack {
36  [Item("Knapsack", "Represents a Knapsack Problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class Knapsack : 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<DoubleValue> PenaltyParameter {
55      get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }
56    }
57    public ValueParameter<IntArray> WeightsParameter {
58      get { return (ValueParameter<IntArray>)Parameters["Weights"]; }
59    }
60    public ValueParameter<IntArray> ValuesParameter {
61      get { return (ValueParameter<IntArray>)Parameters["Values"]; }
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    #endregion
76
77    #region Properties
78    public IBinaryVectorCreator SolutionCreator {
79      get { return SolutionCreatorParameter.Value; }
80      set { SolutionCreatorParameter.Value = value; }
81    }
82    ISolutionCreator IProblem.SolutionCreator {
83      get { return SolutionCreatorParameter.Value; }
84    }
85    public IKnapsackEvaluator Evaluator {
86      get { return EvaluatorParameter.Value; }
87      set { EvaluatorParameter.Value = value; }
88    }
89    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
90      get { return EvaluatorParameter.Value; }
91    }
92    IEvaluator IProblem.Evaluator {
93      get { return EvaluatorParameter.Value; }
94    }
95    private List<IBinaryVectorOperator> operators;
96    public IEnumerable<IOperator> Operators {
97      get { return operators.Cast<IOperator>(); }
98    }
99    #endregion
100
101    public Knapsack()
102      : base() {
103      RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
104      KnapsackEvaluator evaluator = new KnapsackEvaluator();
105
106      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Knapsack Problem is a maximization problem.", new BoolValue(true)));
107      Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
108      Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
109      WeightsParameter.ActualValue.ToStringChanged += new EventHandler(WeightsActualValue_ToStringChanged);
110      Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
111      ValuesParameter.ActualValue.ToStringChanged += new EventHandler(ValuesActualValue_ToStringChanged);
112      Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1)));
113      Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new Knapsack solutions.", creator));
114      Parameters.Add(new ValueParameter<IKnapsackEvaluator>("Evaluator", "The operator which should be used to evaluate Knapsack solutions.", evaluator));
115
116      creator.BinaryVectorParameter.ActualName = "KnapsackSolution";
117      evaluator.QualityParameter.ActualName = "NumberOfOnes";
118      ParameterizeSolutionCreator();
119      ParameterizeEvaluator();
120
121      Initialize();
122    }
123
124    [StorableConstructor]
125    private Knapsack(bool deserializing) : base() { }
126
127    public override IDeepCloneable Clone(Cloner cloner) {
128      Knapsack clone = (Knapsack)base.Clone(cloner);
129      clone.Initialize();
130      return clone;
131    }
132
133    #region Events
134    public event EventHandler SolutionCreatorChanged;
135    private void OnSolutionCreatorChanged() {
136      if (SolutionCreatorChanged != null)
137        SolutionCreatorChanged(this, EventArgs.Empty);
138    }
139    public event EventHandler EvaluatorChanged;
140    private void OnEvaluatorChanged() {
141      if (EvaluatorChanged != null)
142        EvaluatorChanged(this, EventArgs.Empty);
143    }
144    public event EventHandler OperatorsChanged;
145    private void OnOperatorsChanged() {
146      if (OperatorsChanged != null)
147        OperatorsChanged(this, EventArgs.Empty);
148    }
149
150    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
151      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
152      ParameterizeSolutionCreator();
153      ParameterizeEvaluator();
154      ParameterizeOperators();
155      OnSolutionCreatorChanged();
156    }
157    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
158      ParameterizeEvaluator();
159      ParameterizeOperators();
160    }
161    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
162      ParameterizeEvaluator();
163      OnEvaluatorChanged();
164    }
165
166    void ValuesActualValue_ToStringChanged(object sender, EventArgs e) {
167      ParameterizeSolutionCreator();
168    }
169
170    void WeightsActualValue_ToStringChanged(object sender, EventArgs e) {
171      ParameterizeSolutionCreator();
172    }
173    #endregion
174
175    #region Helpers
176    [StorableHook(HookType.AfterDeserialization)]
177    private void Initialize() {
178      InitializeOperators();
179      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
180      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
181      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
182    }
183    private void ParameterizeSolutionCreator() {
184      if(SolutionCreator.LengthParameter.Value == null ||
185        SolutionCreator.LengthParameter.Value.Value != WeightsParameter.Value.Length)
186        SolutionCreator.LengthParameter.Value = new IntValue(WeightsParameter.Value.Length);
187    }
188    private void ParameterizeEvaluator() {
189      if (Evaluator is KnapsackEvaluator) {
190        KnapsackEvaluator knapsackEvaluator =
191          (KnapsackEvaluator)Evaluator;
192        knapsackEvaluator.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
193        knapsackEvaluator.WeightsParameter.ActualName = WeightsParameter.Name;
194        knapsackEvaluator.ValuesParameter.ActualName = ValuesParameter.Name;
195        knapsackEvaluator.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
196        knapsackEvaluator.PenaltyParameter.ActualName = PenaltyParameter.Name;
197      }
198    }
199    private void InitializeOperators() {
200      operators = new List<IBinaryVectorOperator>();
201      if (ApplicationManager.Manager != null) {
202        operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>());
203        ParameterizeOperators();
204      }
205    }
206    private void ParameterizeOperators() {
207      foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
208        op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
209        op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
210      }
211      foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
212        op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
213      }
214    }
215    #endregion
216  }
217}
Note: See TracBrowser for help on using the repository browser.