Changeset 15564 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators
- Timestamp:
- 01/01/18 23:52:39 (7 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/DiscreteLocationCrossover.cs
r15504 r15564 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 46 47 } 47 48 48 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) { 49 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray demands, DoubleArray capacities) { 50 var locations = capacities.Length; 51 var cap = Math.Max(parents[0].Length / locations, 1); 52 var lookup = new List<int>[parents.Length][]; 53 for (var p = 0; p < parents.Length; p++) { 54 lookup[p] = new List<int>[locations]; 55 var assign = parents[p]; 56 for (var e = 0; e < parents[p].Length; e++) { 57 var loc = assign[e]; 58 if (lookup[p][loc] == null) lookup[p][loc] = new List<int>(cap); 59 lookup[p][loc].Add(e); 60 } 61 } 49 62 50 var groupedLocations = parents 51 .Select(p => p.Select((value, index) => new { Equipment = index, Location = value }) 52 .GroupBy(x => x.Location) 53 .ToDictionary(x => x.Key, y => y.AsEnumerable())).ToArray(); 54 63 var slack = capacities.ToArray(); 55 64 IntegerVector child = new IntegerVector(parents[0].Length); 56 HashSet<int> remainingEquipment = new HashSet<int>(Enumerable.Range(0, child.Length)); 57 int locations = capacities.Length; 58 foreach (var i in Enumerable.Range(0, locations).Shuffle(random)) { 65 var takenEquip = new bool[child.Length]; 66 foreach (var loc in Enumerable.Range(0, locations).Shuffle(random)) { 59 67 int parent = random.Next(parents.Length); 60 if ( !groupedLocations[parent].ContainsKey(i)) {68 if (lookup[parent][loc] == null) { 61 69 int tmp = parent; 62 70 do { 63 71 tmp = (tmp + 1) % parents.Length; 64 } while (tmp != parent && !groupedLocations[tmp].ContainsKey(i));72 } while (tmp != parent && lookup[tmp][loc] == null); 65 73 if (parent == tmp) continue; 66 74 else parent = tmp; 67 75 } 68 foreach (var item in groupedLocations[parent][i]) { 69 if (remainingEquipment.Contains(item.Equipment)) { 70 child[item.Equipment] = i; 71 remainingEquipment.Remove(item.Equipment); 76 foreach (var equip in lookup[parent][loc]) { 77 if (!takenEquip[equip]) { 78 child[equip] = loc; 79 takenEquip[equip] = true; 80 slack[loc] -= demands[equip]; 72 81 } 73 82 } 74 83 } 75 84 76 foreach (var equipment in remainingEquipment) { 77 int parent = random.Next(parents.Length); 78 child[equipment] = parents[parent][equipment]; 85 var order = Enumerable.Range(0, takenEquip.Length).Shuffle(random); // avoid bias 86 foreach (var e in order) { 87 if (takenEquip[e]) continue; 88 var assigned = false; 89 // try 1: find a parent where equipment can be assigned feasibly 90 foreach (var p in Enumerable.Range(0, parents.Length).Shuffle(random)) { 91 if (slack[parents[p][e]] >= demands[e]) { 92 child[e] = parents[p][e]; 93 slack[child[e]] -= demands[e]; 94 assigned = true; 95 break; 96 } 97 } 98 // try 2: find a random feasible location 99 if (!assigned) { 100 var possible = Enumerable.Range(0, locations).Where(x => slack[x] >= demands[e]).ToList(); 101 if (possible.Count > 0) { 102 var loc = possible.SampleRandom(random); 103 child[e] = loc; 104 slack[loc] -= demands[e]; 105 assigned = true; 106 } 107 } 108 // try 3: insert in a random location (no feasible insert found) 109 if (!assigned) { 110 child[e] = random.Next(locations); 111 slack[child[e]] -= demands[e]; 112 } 79 113 } 80 114 … … 84 118 protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents, 85 119 GQAPInstance problemInstance) { 86 return Apply(random, parents, problemInstance. Capacities);120 return Apply(random, parents, problemInstance.Demands, problemInstance.Capacities); 87 121 } 88 122 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/RelocateEquipmentManipluator.cs
r15504 r15564 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 26 using HeuristicLab.Encodings.IntegerVectorEncoding; 29 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 47 45 } 48 46 49 public static void Apply(IRandom random, IntegerVector assignment, DoubleArray capacities, DoubleArray demands) { 50 int locations = capacities.Length; 47 public static void Apply(IRandom random, IntegerVector assignment, int locations, double stopProb) { 51 48 if (locations < 2) throw new InvalidOperationException("There are not enough locations for relocation operations."); 52 49 53 Dictionary<int, double> usedCapacities = new Dictionary<int, double>(); 54 Dictionary<int, List<int>> groupedLocations = new Dictionary<int, List<int>>(); 55 for (int i = 0; i < locations; i++) { 56 usedCapacities[i] = 0.0; 57 groupedLocations.Add(i, new List<int>()); 58 } 59 for (int i = 0; i < assignment.Length; i++) { 60 usedCapacities[assignment[i]] += demands[i]; 61 groupedLocations[assignment[i]].Add(i); 62 } 63 64 var overbookedLocations = usedCapacities.Where(x => capacities[x.Key] < x.Value); 65 var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value); 66 if (!overbookedLocations.Any() || !freeLocations.Any()) { // perform a random relocation 67 assignment[random.Next(assignment.Length)] = random.Next(locations); 68 return; 69 } 70 71 var sourceLocation = overbookedLocations.SampleRandom(random); 72 int equipmentToRelocate = groupedLocations[sourceLocation.Key].SampleRandom(random); 73 var bestFreeLocations = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipmentToRelocate]); 74 75 if (bestFreeLocations.Any()) { // a feasible solution will still be feasible, an infeasible solution might become feasible 76 var selected = bestFreeLocations.SampleRandom(random); 77 assignment[equipmentToRelocate] = sourceLocation.Key; 78 } else { // the solution will become infeasible 79 sourceLocation = freeLocations.SampleRandom(random); 80 assignment[equipmentToRelocate] = sourceLocation.Key; 50 var equipments = Enumerable.Range(0, assignment.Length).Shuffle(random); 51 foreach (var equipment in equipments) { 52 var oldLoc = assignment[equipment]; 53 var newLoc =random.Next(locations - 1); 54 if (newLoc >= oldLoc) newLoc++; // don't reassign to current loc 55 assignment[equipment] = newLoc; 56 if (random.NextDouble() < stopProb) break; 81 57 } 82 58 } 83 59 84 60 protected override void Manipulate(IRandom random, IntegerVector vector, GQAPInstance problemInstance) { 85 Apply(random, vector, problemInstance.Capacities , problemInstance.Demands);61 Apply(random, vector, problemInstance.Capacities.Length, 4.0 / problemInstance.Demands.Length); 86 62 } 87 63 }
Note: See TracChangeset
for help on using the changeset viewer.