Changeset 3124
- Timestamp:
- 03/19/10 16:05:27 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.Knapsack/3.3
- Files:
-
- 4 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Evaluators/KnapsackEvaluator.cs
r3070 r3124 66 66 } 67 67 68 public sealed override IOperation Apply() { 69 BinaryVector v = BinaryVectorParameter.ActualValue; 70 int capacity = KnapsackCapacityParameter.ActualValue.Value; 71 double penalty = PenaltyParameter.ActualValue.Value; 72 73 if (WeightsParameter.ActualValue.Length != ValuesParameter.ActualValue.Length) 68 public static DoubleValue Apply(BinaryVector v, IntValue capacity, DoubleValue penalty, IntArray weights, IntArray values) { 69 if (weights.Length != values.Length) 74 70 throw new InvalidOperationException("The weights and values parameters of the Knapsack problem have different sizes"); 71 72 double quality = 0; 75 73 76 74 int weight = 0; 77 75 int value = 0; 78 double quality = 0;79 76 80 77 for (int i = 0; i < v.Length; i++) { 81 78 if (v[i]) { 82 weight += WeightsParameter.ActualValue[i];83 value += ValuesParameter.ActualValue[i];79 weight += weights[i]; 80 value += values[i]; 84 81 } 85 82 } 86 83 87 if (weight > capacity ) {84 if (weight > capacity.Value) { 88 85 quality = 89 value - penalty * (weight - capacity);86 value - penalty.Value * (weight - capacity.Value); 90 87 } else { 91 88 quality = value; 92 89 } 93 90 94 QualityParameter.ActualValue = new DoubleValue(quality); 91 return new DoubleValue(quality); 92 } 93 94 public sealed override IOperation Apply() { 95 BinaryVector v = BinaryVectorParameter.ActualValue; 96 97 DoubleValue quality = Apply(BinaryVectorParameter.ActualValue, 98 KnapsackCapacityParameter.ActualValue, 99 PenaltyParameter.ActualValue, 100 WeightsParameter.ActualValue, 101 ValuesParameter.ActualValue); 102 103 QualityParameter.ActualValue = quality; 95 104 96 105 return base.Apply(); -
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/HeuristicLab.Problems.Knapsack-3.3.csproj
r3116 r3124 85 85 <Compile Include="Interfaces\IKnapsackEvaluator.cs" /> 86 86 <Compile Include="Interfaces\IKnapsackSolutionsVisualizer.cs" /> 87 <Compile Include="Interfaces\IKnapsackMoveEvaluator.cs" /> 87 88 <Compile Include="Knapsack.cs" /> 89 <Compile Include="MoveEvaluators\KnapsackMoveEvaluator.cs" /> 90 <Compile Include="MoveEvaluators\KnapsackOneBitflipMoveEvaluator.cs" /> 88 91 <Compile Include="Properties\AssemblyInfo.cs" /> 89 92 </ItemGroup> -
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Knapsack.cs
r3116 r3124 235 235 OnVisualizerChanged(); 236 236 } 237 void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) { 238 string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName; 239 foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) { 240 op.OneBitflipMoveParameter.ActualName = name; 241 } 242 } 237 243 #endregion 238 244 … … 274 280 ParameterizeOperators(); 275 281 } 282 283 InitializeMoveGenerators(); 284 } 285 private void InitializeMoveGenerators() { 286 foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) { 287 if (op is IMoveGenerator) { 288 op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged); 289 } 290 } 276 291 } 277 292 private void ParameterizeOperators() { … … 283 298 op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 284 299 } 300 foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) { 301 op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 302 } 303 foreach (IKnapsackMoveEvaluator op in Operators.OfType<IKnapsackMoveEvaluator>()) { 304 op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name; 305 op.PenaltyParameter.ActualName = PenaltyParameter.Name; 306 op.WeightsParameter.ActualName = WeightsParameter.Name; 307 op.ValuesParameter.ActualName = ValuesParameter.Name; 308 } 285 309 } 286 310 #endregion
Note: See TracChangeset
for help on using the changeset viewer.