- Timestamp:
- 05/02/12 19:09:48 (13 years ago)
- Location:
- branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/BinaryVectorDiversityCalculator.cs
r7756 r7775 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 41 42 } 42 43 43 protected override int CalculateDiversity(IItem left, IItem right) { 44 protected override double CalculateDiversity(IItem left, IItem right) { 45 if (!(left is BinaryVector) || !(right is BinaryVector)) 46 throw new ArgumentException("Cannot calculate diversity because one of the provided objects or both have the wrong type."); 47 44 48 var v1 = left as BinaryVector; 45 49 var v2 = right as BinaryVector; 46 int diversity = 0; 47 if (v1 != null && v2 != null) 48 for (int i = 0; i < v1.Length; i++) 49 if (v1[i] != v2[i]) diversity++; 50 double diversity = 0; 51 for (int i = 0; i < v1.Length; i++) 52 if (v1[i] != v2[i]) diversity++; 50 53 return diversity; 51 54 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackImprovementOperator.cs
r7756 r7775 67 67 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 68 68 } 69 public IValueLookupParameter<DoubleValue> PenaltyParameter { 70 get { return (IValueLookupParameter<DoubleValue>)Parameters["Penalty"]; } 71 } 69 72 #region ILocalImprovementOperator Parameters 70 73 public IValueLookupParameter<IntValue> MaximumIterationsParameter { … … 99 102 get { return TargetParameter.ActualValue; } 100 103 } 104 private DoubleValue Penalty { 105 get { return PenaltyParameter.ActualValue; } 106 set { PenaltyParameter.ActualValue = value; } 107 } 101 108 #endregion 102 109 … … 115 122 Parameters.Add(new ValueLookupParameter<IntValue>("KnapsackCapacity")); 116 123 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 124 Parameters.Add(new ValueLookupParameter<DoubleValue>("Penalty")); 117 125 #endregion 118 126 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem … … 128 136 // calculate value-to-weight ratio 129 137 double[] ratio = new double[Values.Length]; 130 for (int i = 0; i < ratio.Length; i++) {138 for (int i = 0; i < ratio.Length; i++) 131 139 ratio[i] = (double)Values[i] / (double)Weights[i]; 132 } 140 133 141 134 142 // calculate order for ratio … … 140 148 141 149 int j = sol.Length - 1; 142 while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, new DoubleValue(1.0), Weights, Values).SumWeights.Value > KnapsackCapacity.Value && j >= 0) { 150 while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values) 151 .SumWeights.Value > KnapsackCapacity.Value && j >= 0) 143 152 sol[order[j--]] = false; 144 }145 153 146 154 // calculate weight 147 int lhs = 0; 148 for (int i = 0; i < sol.Length; i++) { 149 if (sol[i]) lhs += Weights[i]; 150 } 155 int weight = 0; 156 for (int i = 0; i < sol.Length; i++) 157 if (sol[i]) weight += Weights[i]; 151 158 152 159 // improve solution … … 154 161 while (feasible && j < sol.Length) { 155 162 while (sol[order[j]]) j++; 156 if ( lhs+ Weights[order[j]] <= KnapsackCapacity.Value) {163 if (weight + Weights[order[j]] <= KnapsackCapacity.Value) { 157 164 sol[order[j]] = true; 158 lhs += Weights[order[j]]; 159 } else { 160 feasible = false; 161 } 165 weight += Weights[order[j]]; 166 } else feasible = false; 162 167 } 163 168 … … 166 171 return base.Apply(); 167 172 } 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 173 } 176 174 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackPathRelinker.cs
r7756 r7775 49 49 50 50 public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) { 51 if (!(initiator is BinaryVector) || !(guide is BinaryVector)) 52 throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type."); 53 if (n.Value <= 0.0) 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 51 56 var v1 = initiator.Clone() as BinaryVector; 52 57 var v2 = guide as BinaryVector; 53 58 54 59 if (v1.Length != v2.Length) 55 throw new ArgumentException("KnapsackPathRelinker: The solutions are of different length."); 56 if (n.Value == 0.0) 57 throw new ArgumentException("KnapsackPathRelinker: RelinkingAccuracy is 0."); 60 throw new ArgumentException("The solutions are of different length."); 58 61 59 62 var solutions = new List<BinaryVector>(); 60 for (int i = 0; i < v1.Length; i++) {63 for (int i = 0; i < v1.Length; i++) 61 64 if (v1[i] != v2[i]) { 62 65 v1[i] = v2[i]; 63 solutions.Add( (BinaryVector)v1.Clone());66 solutions.Add(v1.Clone() as BinaryVector); 64 67 } 65 }66 68 67 var stepSize = 1 / n.Value;68 69 var selection = new List<IItem>(); 69 for (int i = 0; i < solutions.Count; i = (int)Math.Round(i + stepSize)) { 70 selection.Add(solutions.ElementAt(i)); 70 if (solutions.Count > 0) { 71 var noSol = (int)Math.Round(solutions.Count * n.Value); 72 if (noSol <= 0) noSol++; 73 var stepSize = (double)solutions.Count / (double)noSol; 74 for (int i = 0; i < noSol; i++) 75 selection.Add(solutions.ElementAt((int)Math.Round((i + 1) * stepSize - stepSize * 0.5))); 71 76 } 72 77 … … 75 80 76 81 protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) { 77 if (parents.Length != 2) throw new ArgumentException("KnapsackPathRelinker: The number of parents is not equal to 2."); 82 if (parents.Length != 2) 83 throw new ArgumentException("The number of parents is not equal to 2."); 78 84 return Apply(parents[0], parents[1], n); 79 85 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NBinaryVectorCrossover.cs
r7756 r7775 72 72 public sealed override IOperation Apply() { 73 73 var offspringSolutions = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue); 74 for (int i = 0; i < offspringSolutions.Length; i++) {74 for (int i = 0; i < offspringSolutions.Length; i++) 75 75 CurrentScope.Variables.Add(new Variable(string.Format("Child {0}", i), offspringSolutions[i])); 76 }77 76 return base.Apply(); 78 77 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NChildCrossover.cs
r7756 r7775 58 58 if (parent1.Length != parent2.Length) 59 59 throw new ArgumentException("NChildCrossover: The parents are of different length."); 60 61 60 if (n.Value > Math.Pow(2, parent1.Length) - 2) 62 61 throw new ArgumentException("NChildCrossover: There cannot be more children than 2^size of parents - 2."); 63 64 62 if (n.Value < 1) 65 63 throw new ArgumentException("NChildCrossover: N cannot be < 1."); … … 68 66 for (int i = 0; i < n.Value; i++) { 69 67 var solution = new BinaryVector(parent1.Length); 70 for (int j = 0; j < solution.Length; j++) {68 for (int j = 0; j < solution.Length; j++) 71 69 solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j]; 72 }73 70 solutions[i] = solution; 74 71 } … … 78 75 79 76 protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) { 80 if (parents.Length != 2) throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");81 82 if (NParameter.ActualValue == null) throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");83 77 if (parents.Length != 2) 78 throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2."); 79 if (NParameter.ActualValue == null) 80 throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found."); 84 81 return Apply(random, parents[0], parents[1], NParameter.Value); 85 82 }
Note: See TracChangeset
for help on using the changeset viewer.