- Timestamp:
- 01/24/12 17:23:21 (13 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3
- Files:
-
- 16 added
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPEvaluator.cs
r7319 r7407 87 87 } 88 88 89 public static DoubleValue Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, 90 DoubleMatrix installCosts, double transportCosts, DoubleArray demands, DoubleArray capacities, 91 out double infeasibility) { 92 double quality = 0; 93 int len = assignment.Length; 94 var slack = (DoubleArray)capacities.Clone(); 95 for (int i = 0; i < len; i++) { 96 quality += installCosts[i, assignment[i]]; 97 for (int j = 0; j < len; j++) { 98 quality += transportCosts * weights[i, j] * distances[assignment[i], assignment[j]]; 99 } 100 slack[assignment[i]] -= demands[i]; 101 } 102 103 infeasibility = -slack.Where(x => x < 0).Sum(); 104 return new DoubleValue(quality); 105 } 106 89 107 public override IOperation Apply() { 90 108 IntegerVector assignment = AssignmentParameter.ActualValue; … … 96 114 DoubleArray capacities = (DoubleArray)CapacitiesParameter.ActualValue.Clone(); 97 115 double penalty = PenaltyParameter.ActualValue.Value; 98 double quality = 0;99 double infeasibility = 0;100 116 101 117 if (weights.Rows != weights.Columns || distances.Rows != distances.Columns … … 104 120 throw new InvalidOperationException("ERROR: The problem configuration is not valid! Check the sizes of the weights (NxN), distances (MxM) and installation costs (NxM) matrices as well as the length of the demand (N) and capacities (M) vectors."); 105 121 106 int len = assignment.Length; 107 for (int i = 0; i < len; i++) { 108 quality += installCosts[i, assignment[i]]; 109 for (int j = 0; j < len; j++) { 110 quality += transportCosts * weights[i, j] * distances[assignment[i], assignment[j]]; 111 } 112 capacities[assignment[i]] -= demands[i]; 113 } 114 115 infeasibility = -capacities.Where(x => x < 0).Sum(); 122 double infeasibility; 123 var quality = Evaluate(assignment, weights, distances, installCosts, transportCosts, demands, capacities, out infeasibility); 116 124 117 125 InfeasibilityParameter.ActualValue = new DoubleValue(infeasibility); 118 QualityParameter.ActualValue = new DoubleValue(quality + penalty * infeasibility);126 QualityParameter.ActualValue = new DoubleValue(quality.Value + penalty * infeasibility); 119 127 return base.Apply(); 120 128 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs
r7373 r7407 170 170 base.OnSolutionCreatorChanged(); 171 171 Parameterize(); 172 SolutionCreator. IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);172 SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged); 173 173 } 174 174 … … 212 212 SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 213 213 214 foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) { 215 op.DemandsParameter.ActualName = DemandsParameter.Name; 216 } 217 foreach (var op in Operators.OfType<IGQAPCrossover>()) { 218 op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 219 op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 220 } 221 foreach (var op in Operators.OfType<IGQAPEvaluationOperator>()) { 222 op.WeightsParameter.ActualName = WeightsParameter.Name; 223 op.DistancesParameter.ActualName = DistancesParameter.Name; 224 op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name; 225 op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name; 226 op.DemandsParameter.ActualName = DemandsParameter.Name; 227 op.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 228 op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 229 } 230 foreach (var op in Operators.OfType<IGQAPLocalImprovementOperator>()) { 231 op.WeightsParameter.ActualName = WeightsParameter.Name; 232 op.DistancesParameter.ActualName = DistancesParameter.Name; 233 op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name; 234 op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name; 235 op.DemandsParameter.ActualName = DemandsParameter.Name; 236 op.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 237 op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 238 op.InfeasibilityParameter.ActualName = Evaluator.InfeasibilityParameter.ActualName; 239 } 240 foreach (var op in Operators.OfType<IGQAPManipulator>()) { 241 op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 242 } 243 foreach (var op in Operators.OfType<IGQAPMoveOperator>()) { 244 op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 245 } 246 foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) { 247 op.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 248 } 249 214 250 foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) { 215 251 op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; … … 218 254 foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) { 219 255 op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 220 }221 foreach (var op in Operators.OfType<IGQAPCrossover>()) {222 op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;223 op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;224 }225 foreach (var op in Operators.OfType<IGQAPManipulator>()) {226 op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;227 }228 foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {229 op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;230 }231 foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {232 op.DemandsParameter.ActualName = DemandsParameter.Name;233 256 } 234 257 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment-3.3.csproj
r7363 r7407 89 89 <ItemGroup> 90 90 <Compile Include="Analyzers\BestGQAPSolutionAnalyzer.cs" /> 91 <Compile Include="E xtensionMethods.cs" />91 <Compile Include="Evaluators\GQAPNMoveEvaluator.cs" /> 92 92 <Compile Include="GQAPAssignment.cs" /> 93 93 <None Include="Plugin.cs.frame" /> 94 94 <Compile Include="Evaluators\GQAPEvaluator.cs" /> 95 95 <Compile Include="GeneralizedQuadraticAssignmentProblem.cs" /> 96 <Compile Include="Interfaces\IGQAPNMoveEvaluator.cs" /> 96 97 <Compile Include="Interfaces\IEquipmentAwareGQAPOperator.cs" /> 97 98 <Compile Include="Interfaces\IGQAPCrossover.cs" /> 99 <Compile Include="Interfaces\IGQAPEvaluationOperator.cs" /> 98 100 <Compile Include="Interfaces\IGQAPEvaluator.cs" /> 101 <Compile Include="Interfaces\IGQAPLocalImprovementOperator.cs" /> 99 102 <Compile Include="Interfaces\IGQAPManipulator.cs" /> 103 <Compile Include="Interfaces\IGQAPMoveEvaluator.cs" /> 104 <Compile Include="Interfaces\IGQAPMoveOperator.cs" /> 105 <Compile Include="Interfaces\IGQAPNMoveOperator.cs" /> 100 106 <Compile Include="Interfaces\IGQAPOperator.cs" /> 107 <Compile Include="Interfaces\IGQAPSolutionCreator.cs" /> 101 108 <Compile Include="Interfaces\ILocationAwareGQAPOperator.cs" /> 109 <Compile Include="Moves\NMove.cs" /> 110 <Compile Include="Moves\GQAPMoveGenerator.cs" /> 111 <Compile Include="Moves\GQAPNMoveGenerator.cs" /> 112 <Compile Include="Moves\NMoveMaker.cs" /> 113 <Compile Include="Moves\StochasticNMoveMultiMoveGenerator.cs" /> 114 <Compile Include="Moves\StochasticNMoveSingleMoveGenerator.cs" /> 115 <Compile Include="Operators\ApproximateLocalSearch.cs" /> 116 <Compile Include="Operators\GQAPStochasticSolutionCreator.cs" /> 102 117 <Compile Include="Operators\DemandEquivalentSwapEquipmentManipluator.cs" /> 103 118 <Compile Include="Operators\GQAPCrossover.cs" /> 104 119 <Compile Include="Operators\DiscreteLocationCrossover.cs" /> 120 <Compile Include="Operators\GQAPSolutionCreator.cs" /> 121 <Compile Include="Operators\GreedyRandomizedSolutionCreator.cs" /> 122 <Compile Include="Operators\RandomSolutionCreator.cs" /> 105 123 <Compile Include="Operators\RelocateEquipmentManipluator.cs" /> 106 124 <Compile Include="Operators\GQAPManipulator.cs" /> … … 120 138 </ProjectReference> 121 139 </ItemGroup> 140 <ItemGroup /> 122 141 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 123 142 <PropertyGroup> -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Interfaces/IGQAPEvaluator.cs
r7319 r7407 22 22 using HeuristicLab.Core; 23 23 using HeuristicLab.Data; 24 using HeuristicLab.Encodings.IntegerVectorEncoding;25 24 using HeuristicLab.Optimization; 26 25 27 26 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 28 public interface IGQAPEvaluator : ISingleObjectiveEvaluator { 29 ILookupParameter<DoubleMatrix> WeightsParameter { get; } 30 ILookupParameter<DoubleMatrix> DistancesParameter { get; } 31 ILookupParameter<DoubleMatrix> InstallationCostsParameter { get; } 32 ILookupParameter<DoubleValue> TransportationCostsParameter { get; } 33 ILookupParameter<DoubleArray> DemandsParameter { get; } 34 ILookupParameter<DoubleArray> CapacitiesParameter { get; } 35 ILookupParameter<IntegerVector> AssignmentParameter { get; } 27 public interface IGQAPEvaluator : IGQAPEvaluationOperator, ISingleObjectiveEvaluator { 28 ILookupParameter<DoubleValue> InfeasibilityParameter { get; } 36 29 } 37 30 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DemandEquivalentSwapEquipmentManipluator.cs
r7373 r7407 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 31 32 32 33 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { … … 79 80 groupedLocations[location2].Remove(equipment2); 80 81 bool selected; 81 equipment2 = groupedLocations[location2]. SelectRandomOrDefault(random, out selected);82 equipment2 = groupedLocations[location2].ChooseRandomOrDefault(random, out selected); 82 83 if (!selected) break; 83 84 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DiscreteLocationCrossover.cs
r7319 r7407 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 30 31 31 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment .Operators{32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 32 33 [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")] 33 34 [StorableClass] … … 52 53 53 54 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) { 54 55 55 56 var groupedLocations = parents 56 57 .Select(p => p.Select((value, index) => new { Equipment = index, Location = value }) -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs
r7373 r7407 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 26 27 using HeuristicLab.Data; 27 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 using HeuristicLab.Optimization;29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 31 32 32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment .Operators{33 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 33 34 [Item("GreedyRandomizedSolutionCreator", "Creates a solution according to the procedure described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")] 34 35 [StorableClass] 35 public class GreedyRandomizedSolutionCreator : GQAPS olutionCreator, IStochasticOperator {36 public class GreedyRandomizedSolutionCreator : GQAPStochasticSolutionCreator { 36 37 37 public ILookupParameter<IRandom> RandomParameter {38 get { return (ILookupParameter<IRandom>)Parameters["Random"]; }39 }40 38 public IValueLookupParameter<IntValue> MaximumTriesParameter { 41 39 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; } … … 48 46 public GreedyRandomizedSolutionCreator() 49 47 : base() { 50 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 51 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.")); 48 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.", new IntValue(10000))); 52 49 } 53 50 … … 56 53 } 57 54 58 protected override IntegerVector Create Solution(DoubleArray demands, DoubleArray capacities) {59 int k = 0, t= MaximumTriesParameter.ActualValue.Value;60 HashSet<int> F = new HashSet<int>(Enumerable.Range(0, demands.Length));61 while (k < t && F.Any()) {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>(); 62 59 60 while (tries < maxTries) { 61 assignment.Clear(); 62 demands.Select((v, i) => slack[i] = v).Enumerate(); 63 HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments 64 T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL) 65 CL = new HashSet<int>(), // set of chosen locations 66 F = new HashSet<int>(Enumerable.Range(0, demands.Length)), // set of (initially) all facilities / equipments 67 L = new HashSet<int>(Enumerable.Range(0, capacities.Length)); // set of (initially) all locations 68 69 double threshold = 1.0; 70 do { 71 if (L.Any() && random.NextDouble() < threshold) { 72 int l = L.ChooseRandom(random); 73 L.Remove(l); 74 CL.Add(l); 75 T = new HashSet<int>(WithDemandEqualOrLess(F, GetMaximumSlack(slack, CL), demands)); 76 } 77 if (T.Any()) { 78 int f = T.ChooseRandom(random); 79 T.Remove(f); 80 F.Remove(f); 81 CF.Add(f); 82 int l = WithSlackGreaterOrEqual(CL, demands[f], slack).ChooseRandom(random); 83 assignment.Add(f, l); 84 slack[l] -= demands[f]; 85 T = new HashSet<int>(WithDemandEqualOrLess(F, GetMaximumSlack(slack, CL), demands)); 86 threshold = 1.0 - (double)T.Count / Math.Max(F.Count, 1.0); 87 } 88 } while (T.Any() || L.Any()); 89 tries++; 90 if (!F.Any()) break; 63 91 } 64 92 65 // TODO: return actual solution 66 return UniformRandomIntegerVectorCreator.Apply(RandomParameter.ActualValue, demands.Length, 0, capacities.Length); 93 if (assignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maxTries)); 94 95 return new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray()); 96 } 97 98 private static IEnumerable<int> WithDemandEqualOrLess(IEnumerable<int> facilities, double maximum, DoubleArray demands) { 99 foreach (int f in facilities) { 100 if (demands[f] <= maximum) yield return f; 101 } 102 } 103 104 private static double GetMaximumSlack(Dictionary<int, double> slack, HashSet<int> CL) { 105 return slack.Where(x => CL.Contains(x.Key)).Select(x => x.Value).Max(); 106 } 107 108 private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, Dictionary<int, double> slack) { 109 foreach (int l in locations) { 110 if (slack[l] >= minimum) yield return l; 111 } 67 112 } 68 113 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/RandomSolutionCreator.cs
r7373 r7407 24 24 using HeuristicLab.Data; 25 25 using HeuristicLab.Encodings.IntegerVectorEncoding; 26 using HeuristicLab.Optimization;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 … … 31 29 [Item("RandomSolutionCreator", "Creates a completely random solution to the Generalized Quadratic Assignment Problem.")] 32 30 [StorableClass] 33 public class RandomSolutionCreator : GQAPSolutionCreator, IStochasticOperator { 34 35 public ILookupParameter<IRandom> RandomParameter { 36 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 37 } 31 public class RandomSolutionCreator : GQAPStochasticSolutionCreator { 38 32 39 33 [StorableConstructor] 40 34 protected RandomSolutionCreator(bool deserializing) : base(deserializing) { } 41 protected RandomSolutionCreator(RandomSolutionCreator original, Cloner cloner) 42 : base(original, cloner) { } 43 public RandomSolutionCreator() 44 : base() { 45 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 46 } 35 protected RandomSolutionCreator(RandomSolutionCreator original, Cloner cloner) : base(original, cloner) { } 36 public RandomSolutionCreator() : base() { } 47 37 48 38 public override IDeepCloneable Clone(Cloner cloner) { … … 50 40 } 51 41 52 protected override IntegerVector Create Solution(DoubleArray demands, DoubleArray capacities) {53 return UniformRandomIntegerVectorCreator.Apply( RandomParameter.ActualValue, demands.Length, 0, capacities.Length);42 protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) { 43 return UniformRandomIntegerVectorCreator.Apply(random, demands.Length, 0, capacities.Length); 54 44 } 55 45 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/RelocateEquipmentManipluator.cs
r7319 r7407 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 31 32 32 33 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { … … 34 35 [Item("RelocateEquipmentManipluator", "Relocates a random equipment from an overstuffed location to a random one with space or a random equipment if constraints are satisfied.")] 35 36 [StorableClass] 36 public class RelocateEquipmentManipluator : GQAPManipulator, ILocationAwareGQAPOperator, IEquipmentAwareGQAP Manipulator {37 public class RelocateEquipmentManipluator : GQAPManipulator, ILocationAwareGQAPOperator, IEquipmentAwareGQAPOperator { 37 38 38 39 public ILookupParameter<DoubleArray> CapacitiesParameter { … … 60 61 if (locations < 2) throw new InvalidOperationException("There are not enough locations for relocation operations."); 61 62 62 Dictionary<int, double> usedCapacities = new Dictionary<int, double>();63 Dictionary<int, double> usedCapacities = new Dictionary<int, double>(); 63 64 Dictionary<int, List<int>> groupedLocations = new Dictionary<int, List<int>>(); 64 65 for (int i = 0; i < locations; i++) { … … 72 73 73 74 bool selected; 74 var overstuffedLocation = usedCapacities.Where(x => capacities[x.Key] < x.Value). SelectRandomOrDefault(random, out selected);75 var overstuffedLocation = usedCapacities.Where(x => capacities[x.Key] < x.Value).ChooseRandomOrDefault(random, out selected); 75 76 if (selected) { 76 int equipment = groupedLocations[overstuffedLocation.Key]. SelectRandomOrDefault(random, out selected);77 int equipment = groupedLocations[overstuffedLocation.Key].ChooseRandomOrDefault(random, out selected); 77 78 var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value).ToList(); 78 var location = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipment]). SelectRandomOrDefault(random, out selected);79 var location = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipment]).ChooseRandomOrDefault(random, out selected); 79 80 if (selected) { 80 81 assignment[equipment] = location.Key; 81 82 } else { 82 location = freeLocations. SelectRandomOrDefault(random, out selected);83 location = freeLocations.ChooseRandomOrDefault(random, out selected); 83 84 if (selected) assignment[equipment] = location.Key; 84 85 else assignment[equipment] = random.Next(locations);
Note: See TracChangeset
for help on using the changeset viewer.