Changeset 8086 for branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/Improvers/KnapsackImprovementOperator.cs
- Timestamp:
- 06/22/12 11:11:38 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)
- Property svn:ignore
-
old new 20 20 bin 21 21 protoc.exe 22 _ReSharper.HeuristicLab 3.3 Tests
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Knapsack/3.3/Improvers/KnapsackImprovementOperator.cs
r7789 r8086 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 26 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 27 28 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization .Operators;29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Parameters; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 70 71 set { PenaltyParameter.ActualValue = value; } 71 72 } 72 private IItem Target {73 get { return TargetParameter.ActualValue; }74 }75 73 private IntArray Values { 76 74 get { return ValuesParameter.ActualValue; } … … 89 87 : base() { 90 88 #region Create parameters 91 Parameters.Add(new ScopeParameter("CurrentScope" ));92 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity" ));93 Parameters.Add(new LookupParameter<DoubleValue>("Penalty" ));94 Parameters.Add(new ValueLookupParameter<IItem>("Target" ));95 Parameters.Add(new LookupParameter<IntArray>("Values" ));96 Parameters.Add(new LookupParameter<IntArray>("Weights" ));89 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved.")); 90 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.")); 91 Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.")); 92 Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only.")); 93 Parameters.Add(new LookupParameter<IntArray>("Values", "The values of the items.")); 94 Parameters.Add(new LookupParameter<IntArray>("Weights", "The weights of the items.")); 97 95 #endregion 98 96 } … … 104 102 public override IOperation Apply() { 105 103 BinaryVector sol = CurrentScope.Variables[TargetParameter.ActualName].Value as BinaryVector; 104 if (sol == null) 105 throw new ArgumentException("Cannot improve solution because it has the wrong type."); 106 106 107 107 // calculate value-to-weight ratio … … 113 113 // calculate order for ratio 114 114 int[] order = new int[ratio.Length]; 115 ratio.Select((x, index) => new { Value = x, ValueIndex = index }) 116 .OrderBy(x => x.Value) 117 .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index }).ToList() 118 .ForEach(x => order[x.ItemIndex] = x.ValueIndex); 115 foreach (var x in ratio.Select((x, index) => new { Value = x, ValueIndex = index }) 116 .OrderBy(x => x.Value) 117 .Select((x, index) => new { ValueIndex = x.ValueIndex, ItemIndex = index })) { 118 order[x.ItemIndex] = x.ValueIndex; 119 } 119 120 121 int evaluatedSolutions = 0; 120 122 int j = sol.Length - 1; 121 123 while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values) 122 .SumWeights.Value > KnapsackCapacity.Value && j >= 0) 124 .SumWeights.Value > KnapsackCapacity.Value && j >= 0) { 123 125 sol[order[j--]] = false; 126 evaluatedSolutions++; 127 } 124 128 125 129 // calculate weight … … 136 140 weight += Weights[order[j]]; 137 141 } else feasible = false; 142 evaluatedSolutions++; 138 143 } 139 144 140 145 CurrentScope.Variables[TargetParameter.ActualName].Value = sol; 146 CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(evaluatedSolutions))); 141 147 142 148 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.