Changeset 14429 for branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
 Timestamp:
 11/29/16 15:46:48 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r13469 r14429 91 91 92 92 InitializeRandomKnapsackInstance(); 93 Encoding.Length = Weights.Length; 93 94 94 95 InitializeOperators(); … … 97 98 98 99 public override double Evaluate(BinaryVector solution, IRandom random) { 100 var weights = Weights; 101 var values = Values; 99 102 var totalWeight = 0.0; 100 103 var totalValue = 0.0; 101 104 for (var i = 0; i < solution.Length; i++) { 102 105 if (!solution[i]) continue; 103 totalWeight += Weights[i];104 totalValue += Values[i];106 totalWeight += weights[i]; 107 totalValue += values[i]; 105 108 } 106 109 return totalWeight > KnapsackCapacity ? KnapsackCapacity  totalWeight : totalValue; … … 253 256 var sysrand = new System.Random(); 254 257 255 var itemCount = sysrand.Next(10, 100); 258 var power = sysrand.Next(5, 11); 259 var itemCount = (int)Math.Pow(2, power); 256 260 Weights = new IntArray(itemCount); 257 261 Values = new IntArray(itemCount); … … 260 264 261 265 for (int i = 0; i < itemCount; i++) { 262 var value = sysrand.Next(1, 10);263 var weight = sysrand.Next(1, 10);266 var value = sysrand.Next(1, 30); 267 var weight = sysrand.Next(1, 30); 264 268 265 269 Values[i] = value; … … 268 272 } 269 273 270 KnapsackCapacity = (int)Math.Round(0. 7* totalWeight);274 KnapsackCapacity = (int)Math.Round(0.5 * totalWeight); 271 275 } 272 276 }
Note: See TracChangeset
for help on using the changeset viewer.