[3070] | 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("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 | }
|
---|
[3071] | 75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 77 | }
|
---|
[3070] | 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region Properties
|
---|
| 81 | public IBinaryVectorCreator SolutionCreator {
|
---|
| 82 | get { return SolutionCreatorParameter.Value; }
|
---|
| 83 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 84 | }
|
---|
| 85 | ISolutionCreator IProblem.SolutionCreator {
|
---|
| 86 | get { return SolutionCreatorParameter.Value; }
|
---|
| 87 | }
|
---|
| 88 | public IKnapsackEvaluator Evaluator {
|
---|
| 89 | get { return EvaluatorParameter.Value; }
|
---|
| 90 | set { EvaluatorParameter.Value = value; }
|
---|
| 91 | }
|
---|
| 92 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
| 93 | get { return EvaluatorParameter.Value; }
|
---|
| 94 | }
|
---|
| 95 | IEvaluator IProblem.Evaluator {
|
---|
| 96 | get { return EvaluatorParameter.Value; }
|
---|
| 97 | }
|
---|
[3071] | 98 | public DoubleValue BestKnownQuality {
|
---|
| 99 | get { return BestKnownQualityParameter.Value; }
|
---|
| 100 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 101 | }
|
---|
[3070] | 102 | private List<IBinaryVectorOperator> operators;
|
---|
| 103 | public IEnumerable<IOperator> Operators {
|
---|
| 104 | get { return operators.Cast<IOperator>(); }
|
---|
| 105 | }
|
---|
| 106 | #endregion
|
---|
| 107 |
|
---|
| 108 | public Knapsack()
|
---|
| 109 | : base() {
|
---|
| 110 | RandomBinaryVectorCreator creator = new RandomBinaryVectorCreator();
|
---|
| 111 | KnapsackEvaluator evaluator = new KnapsackEvaluator();
|
---|
| 112 |
|
---|
| 113 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Knapsack Problem is a maximization problem.", new BoolValue(true)));
|
---|
| 114 | Parameters.Add(new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(0)));
|
---|
| 115 | Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
|
---|
| 116 | WeightsParameter.ActualValue.ToStringChanged += new EventHandler(WeightsActualValue_ToStringChanged);
|
---|
| 117 | Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
|
---|
| 118 | ValuesParameter.ActualValue.ToStringChanged += new EventHandler(ValuesActualValue_ToStringChanged);
|
---|
| 119 | Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1)));
|
---|
| 120 | Parameters.Add(new ValueParameter<IBinaryVectorCreator>("SolutionCreator", "The operator which should be used to create new Knapsack solutions.", creator));
|
---|
| 121 | Parameters.Add(new ValueParameter<IKnapsackEvaluator>("Evaluator", "The operator which should be used to evaluate Knapsack solutions.", evaluator));
|
---|
[3071] | 122 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Knapsack instance."));
|
---|
[3070] | 123 |
|
---|
| 124 | creator.BinaryVectorParameter.ActualName = "KnapsackSolution";
|
---|
| 125 | evaluator.QualityParameter.ActualName = "NumberOfOnes";
|
---|
| 126 | ParameterizeSolutionCreator();
|
---|
| 127 | ParameterizeEvaluator();
|
---|
| 128 |
|
---|
| 129 | Initialize();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | [StorableConstructor]
|
---|
| 133 | private Knapsack(bool deserializing) : base() { }
|
---|
| 134 |
|
---|
| 135 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 136 | Knapsack clone = (Knapsack)base.Clone(cloner);
|
---|
| 137 | clone.Initialize();
|
---|
| 138 | return clone;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | #region Events
|
---|
| 142 | public event EventHandler SolutionCreatorChanged;
|
---|
| 143 | private void OnSolutionCreatorChanged() {
|
---|
| 144 | if (SolutionCreatorChanged != null)
|
---|
| 145 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
| 146 | }
|
---|
| 147 | public event EventHandler EvaluatorChanged;
|
---|
| 148 | private void OnEvaluatorChanged() {
|
---|
| 149 | if (EvaluatorChanged != null)
|
---|
| 150 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
| 151 | }
|
---|
| 152 | public event EventHandler OperatorsChanged;
|
---|
| 153 | private void OnOperatorsChanged() {
|
---|
| 154 | if (OperatorsChanged != null)
|
---|
| 155 | OperatorsChanged(this, EventArgs.Empty);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 159 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 160 | ParameterizeSolutionCreator();
|
---|
| 161 | ParameterizeEvaluator();
|
---|
| 162 | ParameterizeOperators();
|
---|
| 163 | OnSolutionCreatorChanged();
|
---|
| 164 | }
|
---|
| 165 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 166 | ParameterizeEvaluator();
|
---|
| 167 | ParameterizeOperators();
|
---|
| 168 | }
|
---|
| 169 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 170 | ParameterizeEvaluator();
|
---|
| 171 | OnEvaluatorChanged();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void ValuesActualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
| 175 | ParameterizeSolutionCreator();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void WeightsActualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
| 179 | ParameterizeSolutionCreator();
|
---|
| 180 | }
|
---|
| 181 | #endregion
|
---|
| 182 |
|
---|
| 183 | #region Helpers
|
---|
| 184 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 185 | private void Initialize() {
|
---|
| 186 | InitializeOperators();
|
---|
| 187 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 188 | SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 189 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 190 | }
|
---|
| 191 | private void ParameterizeSolutionCreator() {
|
---|
| 192 | if(SolutionCreator.LengthParameter.Value == null ||
|
---|
| 193 | SolutionCreator.LengthParameter.Value.Value != WeightsParameter.Value.Length)
|
---|
| 194 | SolutionCreator.LengthParameter.Value = new IntValue(WeightsParameter.Value.Length);
|
---|
| 195 | }
|
---|
| 196 | private void ParameterizeEvaluator() {
|
---|
| 197 | if (Evaluator is KnapsackEvaluator) {
|
---|
| 198 | KnapsackEvaluator knapsackEvaluator =
|
---|
| 199 | (KnapsackEvaluator)Evaluator;
|
---|
| 200 | knapsackEvaluator.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 201 | knapsackEvaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 202 | knapsackEvaluator.ValuesParameter.ActualName = ValuesParameter.Name;
|
---|
| 203 | knapsackEvaluator.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;
|
---|
| 204 | knapsackEvaluator.PenaltyParameter.ActualName = PenaltyParameter.Name;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | private void InitializeOperators() {
|
---|
| 208 | operators = new List<IBinaryVectorOperator>();
|
---|
| 209 | if (ApplicationManager.Manager != null) {
|
---|
| 210 | operators.AddRange(ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>());
|
---|
| 211 | ParameterizeOperators();
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | private void ParameterizeOperators() {
|
---|
| 215 | foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) {
|
---|
| 216 | op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 217 | op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 218 | }
|
---|
| 219 | foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) {
|
---|
| 220 | op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | #endregion
|
---|
| 224 | }
|
---|
| 225 | }
|
---|