- Timestamp:
- 03/11/12 10:54:36 (13 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPEvaluator.cs
r7419 r7593 132 132 slack[assignment[i]] -= demands[i]; 133 133 } 134 overbookedCapacity = slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum(); 134 overbookedCapacity = EvaluateOverbooking(slack, capacities); 135 } 136 137 public static double EvaluateOverbooking(DoubleArray slack, DoubleArray capacities) { 138 return slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum(); 135 139 } 136 140 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment-3.3.csproj
r7561 r7593 104 104 <Compile Include="GQAPAssignment.cs" /> 105 105 <Compile Include="Operators\Crossovers\CordeauCrossover.cs" /> 106 <Compile Include="SolutionCreators\SlackMinimizationSolutionCreator.cs" /> 107 <Compile Include="SolutionCreators\RandomButFeasibleSolutionCreator.cs" /> 106 108 <Compile Include="SolutionCreators\GQAPSolutionCreator.cs" /> 107 109 <Compile Include="SolutionCreators\GQAPStochasticSolutionCreator.cs" /> -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs
r7523 r7593 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Threading; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 39 40 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; } 40 41 } 42 public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter { 43 get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; } 44 } 41 45 42 46 [StorableConstructor] … … 46 50 public GreedyRandomizedSolutionCreator() 47 51 : base() { 48 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.", new IntValue(10000))); 52 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution after which an exception is thrown. If it is set to 0 or a negative value there will be an infinite number of attempts.", new IntValue(100000))); 53 Parameters.Add(new ValueLookupParameter<BoolValue>("CreateMostFeasibleSolution", "If this is set to true the operator will always succeed, and outputs the solution with the least violation instead of throwing an exception.", new BoolValue(false))); 49 54 } 50 55 … … 53 58 } 54 59 55 protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) { 56 int tries = 0, maxTries = MaximumTriesParameter.ActualValue.Value; 57 var assignment = new Dictionary<int, int>(); 58 var slack = new Dictionary<int, double>(); 59 60 while (tries < maxTries) { 61 assignment.Clear(); 62 slack = capacities.Select((v, i) => new { Index = i, Value = v }).ToDictionary(x => x.Index, y => y.Value); 63 HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments 60 public static IntegerVector CreateSolution(IRandom random, DoubleArray demands, DoubleArray capacities, int maximumTries, bool createMostFeasibleSolution, CancellationToken cancelToken) { 61 int tries = 0; 62 var assignment = new Dictionary<int, int>(demands.Length); 63 DoubleArray slack = new DoubleArray(capacities.Length); 64 double minViolation = double.MaxValue; 65 Dictionary<int, int> bestAssignment = null; 66 HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments 64 67 T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL) 65 68 CL = new HashSet<int>(), // set of chosen locations 66 69 F = new HashSet<int>(Enumerable.Range(0, demands.Length)), // set of (initially) all facilities / equipments 67 70 L = new HashSet<int>(Enumerable.Range(0, capacities.Length)); // set of (initially) all locations 71 72 while (maximumTries <= 0 || tries < maximumTries) { 73 cancelToken.ThrowIfCancellationRequested(); 74 75 assignment.Clear(); 76 for (int i = 0; i < capacities.Length; i++) slack[i] = capacities[i]; 77 CF.Clear(); 78 T.Clear(); 79 CL.Clear(); 80 F.Clear(); F.UnionWith(Enumerable.Range(0, demands.Length)); 81 L.Clear(); L.UnionWith(Enumerable.Range(0, capacities.Length)); 68 82 69 83 double threshold = 1.0; … … 87 101 } 88 102 } while (T.Any() || L.Any()); 89 tries++; 90 if (!F.Any()) break; 103 if (maximumTries > 0) tries++; 104 if (!F.Any()) { 105 bestAssignment = assignment; 106 break; 107 } else if (createMostFeasibleSolution) { 108 // complete the solution and remember the one with least violation 109 while (F.Any()) { 110 var f = F.ChooseMax(x => demands[x]); 111 var l = L.ChooseMax(x => slack[x]); 112 F.Remove(f); 113 assignment.Add(f, l); 114 slack[l] -= demands[f]; 115 } 116 double violation = GQAPEvaluator.EvaluateOverbooking(slack, capacities); 117 if (violation < minViolation) { 118 bestAssignment = assignment; 119 assignment = new Dictionary<int, int>(demands.Length); 120 minViolation = violation; 121 } 122 } 91 123 } 92 124 93 if ( assignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maxTries));125 if (bestAssignment == null || bestAssignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maximumTries)); 94 126 95 return new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray()); 127 return new IntegerVector(bestAssignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray()); 128 } 129 130 protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) { 131 return CreateSolution(random, demands, capacities, 132 MaximumTriesParameter.ActualValue.Value, 133 CreateMostFeasibleSolutionParameter.ActualValue.Value, 134 CancellationToken); 96 135 } 97 136 … … 102 141 } 103 142 104 private static double GetMaximumSlack(D ictionary<int, double>slack, HashSet<int> CL) {105 return slack. Where(x => CL.Contains(x.Key)).Select(x => x.Value).Max();143 private static double GetMaximumSlack(DoubleArray slack, HashSet<int> CL) { 144 return slack.Select((val, idx) => new { idx, val }).Where(x => CL.Contains(x.idx)).Select(x => x.val).Max(); 106 145 } 107 146 108 private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, D ictionary<int, double>slack) {147 private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, DoubleArray slack) { 109 148 foreach (int l in locations) { 110 149 if (slack[l] >= minimum) yield return l; -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/RandomSolutionCreator.cs
r7523 r7593 40 40 } 41 41 42 public static IntegerVector CreateSolution(IRandom random, DoubleArray demands, DoubleArray capacities) { 43 return UniformRandomIntegerVectorCreator.Apply(random, demands.Length, 0, capacities.Length); 44 } 45 42 46 protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) { 43 return UniformRandomIntegerVectorCreator.Apply(random, demands.Length, 0, capacities.Length);47 return CreateSolution(random, demands, capacities); 44 48 } 45 49 }
Note: See TracChangeset
for help on using the changeset viewer.