1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.Knapsack;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.ScatterSearch.Knapsack {
|
---|
35 | /// <summary>
|
---|
36 | /// An operator that improves knapsack solutions.
|
---|
37 | /// </summary>
|
---|
38 | [Item("KnapsackImprovementOperator", "An operator that improves knapsack solutions.")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class KnapsackImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor {
|
---|
41 | #region Problem properties
|
---|
42 | public Type ProblemType {
|
---|
43 | get { return typeof(KnapsackProblem); }
|
---|
44 | }
|
---|
45 | [Storable]
|
---|
46 | private KnapsackProblem problem;
|
---|
47 | public IProblem Problem {
|
---|
48 | get { return problem; }
|
---|
49 | set { problem = (KnapsackProblem)value; }
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | #region Parameter properties
|
---|
54 | public ScopeParameter CurrentScopeParameter {
|
---|
55 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
56 | }
|
---|
57 | public IValueLookupParameter<IntArray> ValuesParameter {
|
---|
58 | get { return (IValueLookupParameter<IntArray>)Parameters["Values"]; }
|
---|
59 | }
|
---|
60 | public IValueLookupParameter<IntArray> WeightsParameter {
|
---|
61 | get { return (IValueLookupParameter<IntArray>)Parameters["Weights"]; }
|
---|
62 | }
|
---|
63 | public IValueLookupParameter<IntValue> KnapsackCapacityParameter {
|
---|
64 | get { return (IValueLookupParameter<IntValue>)Parameters["KnapsackCapacity"]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<IItem> TargetParameter {
|
---|
67 | get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
|
---|
68 | }
|
---|
69 | #region ILocalImprovementOperator Parameters
|
---|
70 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
71 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
72 | }
|
---|
73 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
74 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
75 | }
|
---|
76 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
77 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Properties
|
---|
83 | private IScope CurrentScope {
|
---|
84 | get { return CurrentScopeParameter.ActualValue; }
|
---|
85 | }
|
---|
86 | private IntArray Values {
|
---|
87 | get { return ValuesParameter.ActualValue; }
|
---|
88 | set { ValuesParameter.ActualValue = value; }
|
---|
89 | }
|
---|
90 | private IntArray Weights {
|
---|
91 | get { return WeightsParameter.ActualValue; }
|
---|
92 | set { WeightsParameter.ActualValue = value; }
|
---|
93 | }
|
---|
94 | private IntValue KnapsackCapacity {
|
---|
95 | get { return KnapsackCapacityParameter.ActualValue; }
|
---|
96 | set { KnapsackCapacityParameter.ActualValue = value; }
|
---|
97 | }
|
---|
98 | private IItem Target {
|
---|
99 | get { return TargetParameter.ActualValue; }
|
---|
100 | }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 | [StorableConstructor]
|
---|
104 | private KnapsackImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
105 | private KnapsackImprovementOperator(KnapsackImprovementOperator original, Cloner cloner)
|
---|
106 | : base(original, cloner) {
|
---|
107 | this.problem = cloner.Clone(original.problem);
|
---|
108 | }
|
---|
109 | public KnapsackImprovementOperator()
|
---|
110 | : base() {
|
---|
111 | #region Create parameters
|
---|
112 | Parameters.Add(new ScopeParameter("CurrentScope"));
|
---|
113 | Parameters.Add(new ValueLookupParameter<IntArray>("Values"));
|
---|
114 | Parameters.Add(new ValueLookupParameter<IntArray>("Weights"));
|
---|
115 | Parameters.Add(new ValueLookupParameter<IntValue>("KnapsackCapacity"));
|
---|
116 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
117 | #endregion
|
---|
118 | TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
122 | return new KnapsackImprovementOperator(this, cloner);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public override IOperation Apply() {
|
---|
126 | var sol = CurrentScope.Variables[TargetParameter.ActualName].Value as BinaryVector;
|
---|
127 |
|
---|
128 | // calculate value-to-weight ratio
|
---|
129 | double[] ratio = new double[Values.Length];
|
---|
130 | for (int i = 0; i < ratio.Length; i++) {
|
---|
131 | ratio[i] = (double)Values[i] / (double)Weights[i];
|
---|
132 | }
|
---|
133 |
|
---|
134 | // calculate order for ratio
|
---|
135 | int[] order = new int[ratio.Length];
|
---|
136 | ratio.Select((x, index) => new { Value = x, ValueIndex = index })
|
---|
137 | .OrderBy(x => x.Value)
|
---|
138 | .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index }).ToList()
|
---|
139 | .ForEach(x => order[x.ItemIndex] = x.ValueIndex);
|
---|
140 |
|
---|
141 | int j = sol.Length - 1;
|
---|
142 | while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, new DoubleValue(1.0), Weights, Values).SumWeights.Value > KnapsackCapacity.Value && j >= 0) {
|
---|
143 | sol[order[j--]] = false;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // calculate weight
|
---|
147 | int lhs = 0;
|
---|
148 | for (int i = 0; i < sol.Length; i++) {
|
---|
149 | if (sol[i]) lhs += Weights[i];
|
---|
150 | }
|
---|
151 |
|
---|
152 | // improve solution
|
---|
153 | bool feasible = true; j = 0;
|
---|
154 | while (feasible && j < sol.Length) {
|
---|
155 | while (sol[order[j]]) j++;
|
---|
156 | if (lhs + Weights[order[j]] <= KnapsackCapacity.Value) {
|
---|
157 | sol[order[j]] = true;
|
---|
158 | lhs += Weights[order[j]];
|
---|
159 | } else {
|
---|
160 | feasible = false;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | CurrentScope.Variables[TargetParameter.ActualName].Value = sol;
|
---|
165 |
|
---|
166 | return base.Apply();
|
---|
167 | }
|
---|
168 |
|
---|
169 | private bool IsFeasiblse(BinaryVector sol) {
|
---|
170 | int sum = 0;
|
---|
171 | for (int i = 0; i < sol.Length; i++)
|
---|
172 | if (sol[i]) sum += Weights[i];
|
---|
173 | return sum <= KnapsackCapacity.Value;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|