- Timestamp:
- 01/26/12 14:50:56 (13 years ago)
- Location:
- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs
r7412 r7415 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 { … … 51 52 get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; } 52 53 } 54 public LookupParameter<DoubleMatrix> InstallationCostsParameter { 55 get { return (LookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; } 56 } 57 public LookupParameter<DoubleArray> DemandsParameter { 58 get { return (LookupParameter<DoubleArray>)Parameters["Demands"]; } 59 } 60 public LookupParameter<DoubleArray> CapacitiesParameter { 61 get { return (LookupParameter<DoubleArray>)Parameters["Capacities"]; } 62 } 63 public LookupParameter<DoubleValue> TransportationCostsParameter { 64 get { return (LookupParameter<DoubleValue>)Parameters["TransportationCosts"]; } 65 } 66 public LookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter { 67 get { return (LookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; } 68 } 53 69 public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter { 54 70 get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; } … … 56 72 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 57 73 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 74 } 75 public ScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter { 76 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; } 77 } 78 public ScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter { 79 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; } 80 } 81 public ScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter { 82 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; } 58 83 } 59 84 public LookupParameter<GQAPAssignment> BestSolutionParameter { … … 86 111 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem.")); 87 112 Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations.")); 88 Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities.")); 113 Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the equipments.")); 114 Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The cost of installing equipment x at location y.")); 115 Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands of the equipments.")); 116 Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities at the locations.")); 117 Parameters.Add(new LookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already.")); 118 Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality.")); 89 119 Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed.")); 90 120 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed.")); 121 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", "The flow-distance qualities of the GQAP solutions which should be analyzed.")); 122 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", "The installation qualities of the GQAP solutions which should be analyzed.")); 123 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", "The overbooked capacities of the GQAP solutions which should be analyzed.")); 91 124 Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution.")); 92 125 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored.")); … … 109 142 110 143 public override IOperation Apply() { 111 DoubleMatrix distances = DistancesParameter.ActualValue; 112 DoubleMatrix weights = WeightsParameter.ActualValue; 113 ItemArray<IntegerVector> assignments = AssignmentParameter.ActualValue; 114 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 115 ResultCollection results = ResultsParameter.ActualValue; 116 bool max = MaximizationParameter.ActualValue.Value; 117 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 118 119 var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray(); 120 if (max) sorted = sorted.Reverse().ToArray(); 121 int i = sorted.First().index; 122 123 if (bestKnownQuality == null 124 || max && qualities[i].Value > bestKnownQuality.Value 125 || !max && qualities[i].Value < bestKnownQuality.Value) { 126 // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known 127 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 128 BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[i].Clone(); 144 var assignments = AssignmentParameter.ActualValue; 145 var qualities = QualityParameter.ActualValue; 146 var equipmentNames = EquipmentNamesParameter.ActualValue; 147 var locationNames = LocationNamesParameter.ActualValue; 148 var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue; 149 var installationQualities = InstallationQualityParameter.ActualValue; 150 var overbookedCapacities = OverbookedCapacityParameter.ActualValue; 151 var distances = DistancesParameter.ActualValue; 152 var weights = WeightsParameter.ActualValue; 153 var installationCosts = InstallationCostsParameter.ActualValue; 154 var demands = DemandsParameter.ActualValue; 155 var capacities = CapacitiesParameter.ActualValue; 156 var transportationCosts = TransportationCostsParameter.ActualValue; 157 var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue; 158 var results = ResultsParameter.ActualValue; 159 var maximization = MaximizationParameter.ActualValue.Value; 160 var bestKnownQuality = BestKnownQualityParameter.ActualValue; 161 162 int bestIndex; 163 var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value }); 164 if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index; 165 else bestIndex = tmp.SelectMin(x => x.Value).Index; 166 167 if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) { 168 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value); 169 BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone(); 129 170 } 130 171 131 172 GQAPAssignment assignment = BestSolutionParameter.ActualValue; 132 173 if (assignment == null) { 133 StringArray equipmentNames = EquipmentNamesParameter.ActualValue;134 StringArray locationNames = LocationNamesParameter.ActualValue;135 assignment = new GQAPAssignment(weights, (IntegerVector)assignments[i].Clone(), new DoubleValue(qualities[i].Value), equipmentNames, locationNames);174 assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), 175 equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, 176 overbookedCapacityPenalty, flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]); 136 177 assignment.Distances = distances; 137 178 BestSolutionParameter.ActualValue = assignment; 138 179 results.Add(new Result("Best GQAP Solution", assignment)); 139 180 } else { 140 if (max && assignment.Quality.Value < qualities[i].Value || 141 !max && assignment.Quality.Value > qualities[i].Value) { 181 if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) { 182 assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone(); 183 assignment.Quality = (DoubleValue)qualities[bestIndex].Clone(); 184 assignment.EquipmentNames = equipmentNames; 185 assignment.LocationNames = locationNames; 142 186 assignment.Distances = distances; 143 187 assignment.Weights = weights; 144 assignment.Assignment = (IntegerVector)assignments[i].Clone(); 145 assignment.Quality.Value = qualities[i].Value; 188 assignment.InstallationCosts = installationCosts; 189 assignment.Demands = demands; 190 assignment.Capacities = capacities; 191 assignment.TransportationCosts = transportationCosts; 192 assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty; 193 assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone(); 194 assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone(); 195 assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone(); 146 196 } 147 197 } … … 149 199 return base.Apply(); 150 200 } 201 202 private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) { 203 return maximization && oldQuality < quality || !maximization && oldQuality > quality; 204 } 151 205 } 152 206 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GQAPAssignment.cs
r7319 r7415 33 33 34 34 [Storable] 35 private IntegerVector assignment; 36 public IntegerVector Assignment { 37 get { return assignment; } 38 set { 39 bool changed = (assignment != value); 40 assignment = value; 41 if (changed) OnPropertyChanged("Assignment"); 42 } 43 } 44 45 [Storable] 46 private DoubleValue quality; 47 public DoubleValue Quality { 48 get { return quality; } 49 set { 50 bool changed = (quality != value); 51 quality = value; 52 if (changed) OnPropertyChanged("Quality"); 53 } 54 } 55 56 [Storable] 57 private StringArray equipmentNames; 58 public StringArray EquipmentNames { 59 get { return equipmentNames; } 60 set { 61 bool changed = (equipmentNames != value); 62 equipmentNames = value; 63 if (changed) OnPropertyChanged("EquipmentNames"); 64 } 65 } 66 67 [Storable] 68 private StringArray locationNames; 69 public StringArray LocationNames { 70 get { return locationNames; } 71 set { 72 bool changed = (locationNames != value); 73 locationNames = value; 74 if (changed) OnPropertyChanged("LocationNames"); 75 } 76 } 77 78 [Storable] 35 79 private DoubleMatrix distances; 36 80 public DoubleMatrix Distances { … … 55 99 56 100 [Storable] 57 private IntegerVector assignment; 58 public IntegerVector Assignment { 59 get { return assignment; } 60 set { 61 bool changed = (assignment != value); 62 assignment = value; 63 if (changed) OnPropertyChanged("Assignment"); 64 } 65 } 66 67 [Storable] 68 private DoubleValue quality; 69 public DoubleValue Quality { 70 get { return quality; } 71 set { 72 bool changed = (quality != value); 73 quality = value; 74 if (changed) OnPropertyChanged("Quality"); 75 } 76 } 77 78 [Storable] 79 private StringArray equipmentNames; 80 public StringArray EquipmentNames { 81 get { return equipmentNames; } 82 set { 83 bool changed = (equipmentNames != value); 84 equipmentNames = value; 85 if (changed) OnPropertyChanged("EquipmentNames"); 86 } 87 } 88 89 [Storable] 90 private StringArray locationNames; 91 public StringArray LocationNames { 92 get { return locationNames; } 93 set { 94 bool changed = (locationNames != value); 95 locationNames = value; 96 if (changed) OnPropertyChanged("LocationNames"); 101 private DoubleMatrix installationCosts; 102 public DoubleMatrix InstallationCosts { 103 get { return installationCosts; } 104 set { 105 bool changed = (installationCosts != value); 106 installationCosts = value; 107 if (changed) OnPropertyChanged("InstallationCosts"); 108 } 109 } 110 111 [Storable] 112 private DoubleArray demands; 113 public DoubleArray Demands { 114 get { return demands; } 115 set { 116 bool changed = (demands != value); 117 demands = value; 118 if (changed) OnPropertyChanged("Demands"); 119 } 120 } 121 122 [Storable] 123 private DoubleArray capacities; 124 public DoubleArray Capacities { 125 get { return capacities; } 126 set { 127 bool changed = (capacities != value); 128 capacities = value; 129 if (changed) OnPropertyChanged("Capacities"); 130 } 131 } 132 133 [Storable] 134 private DoubleValue transportationCosts; 135 public DoubleValue TransportationCosts { 136 get { return transportationCosts; } 137 set { 138 bool changed = (transportationCosts != value); 139 transportationCosts = value; 140 if (changed) OnPropertyChanged("TransportationCosts"); 141 } 142 } 143 144 [Storable] 145 private DoubleValue overbookedCapacityPenalty; 146 public DoubleValue OverbookedCapacityPenalty { 147 get { return overbookedCapacityPenalty; } 148 set { 149 bool changed = (overbookedCapacityPenalty != value); 150 overbookedCapacityPenalty = value; 151 if (changed) OnPropertyChanged("OverbookedCapacityPenalty"); 152 } 153 } 154 155 [Storable] 156 private DoubleValue flowDistanceQuality; 157 public DoubleValue FlowDistanceQuality { 158 get { return flowDistanceQuality; } 159 set { 160 bool changed = (flowDistanceQuality != value); 161 flowDistanceQuality = value; 162 if (changed) OnPropertyChanged("FlowDistanceQuality"); 163 } 164 } 165 166 [Storable] 167 private DoubleValue installationQuality; 168 public DoubleValue InstallationQuality { 169 get { return installationQuality; } 170 set { 171 bool changed = (installationQuality != value); 172 installationQuality = value; 173 if (changed) OnPropertyChanged("InstallationQuality"); 174 } 175 } 176 177 [Storable] 178 private DoubleValue overbookedCapacity; 179 public DoubleValue OverbookedCapacity { 180 get { return overbookedCapacity; } 181 set { 182 bool changed = (overbookedCapacity != value); 183 overbookedCapacity = value; 184 if (changed) OnPropertyChanged("OverbookedCapacity"); 97 185 } 98 186 } … … 102 190 private GQAPAssignment(GQAPAssignment original, Cloner cloner) 103 191 : base(original, cloner) { 104 distances = cloner.Clone(original.distances);105 weights = cloner.Clone(original.weights);106 192 assignment = cloner.Clone(original.assignment); 107 193 quality = cloner.Clone(original.quality); 108 194 equipmentNames = cloner.Clone(original.equipmentNames); 109 195 locationNames = cloner.Clone(original.locationNames); 110 } 111 public GQAPAssignment(DoubleMatrix weights, IntegerVector assignment) { 112 this.weights = weights; 196 distances = cloner.Clone(original.distances); 197 weights = cloner.Clone(original.weights); 198 installationCosts = cloner.Clone(original.installationCosts); 199 demands = cloner.Clone(original.demands); 200 capacities = cloner.Clone(original.capacities); 201 transportationCosts = cloner.Clone(original.transportationCosts); 202 overbookedCapacityPenalty = cloner.Clone(original.overbookedCapacityPenalty); 203 flowDistanceQuality = cloner.Clone(original.flowDistanceQuality); 204 installationQuality = cloner.Clone(original.installationQuality); 205 overbookedCapacity = cloner.Clone(original.overbookedCapacity); 206 } 207 public GQAPAssignment(IntegerVector assignment, DoubleValue quality) 208 : base() { 113 209 this.assignment = assignment; 114 }115 public GQAPAssignment(DoubleMatrix weights, IntegerVector assignment, DoubleValue quality)116 : this(weights, assignment) {117 210 this.quality = quality; 118 211 } 119 public GQAPAssignment( DoubleMatrix weights,IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames)120 : this( weights,assignment, quality) {212 public GQAPAssignment(IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames) 213 : this(assignment, quality) { 121 214 this.equipmentNames = equipmentNames; 122 215 this.locationNames = locationNames; 216 } 217 public GQAPAssignment(IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames, 218 DoubleMatrix distances, DoubleMatrix weights, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities, 219 DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty, DoubleValue flowDistanceQuality, 220 DoubleValue installationQuality, DoubleValue overbookedCapacity) 221 : this(assignment, quality, equipmentNames, locationNames) { 222 this.distances = distances; 223 this.weights = weights; 224 this.installationCosts = installationCosts; 225 this.demands = demands; 226 this.capacities = capacities; 227 this.transportationCosts = transportationCosts; 228 this.overbookedCapacityPenalty = overbookedCapacityPenalty; 229 this.flowDistanceQuality = flowDistanceQuality; 230 this.installationQuality = installationQuality; 231 this.overbookedCapacity = overbookedCapacity; 123 232 } 124 233 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs
r7413 r7415 68 68 get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; } 69 69 } 70 public OptionalValueParameter<StringArray> EquipmentNamesParameter { 71 get { return (OptionalValueParameter<StringArray>)Parameters["EquipmentNames"]; } 72 } 73 public OptionalValueParameter<StringArray> LocationNamesParameter { 74 get { return (OptionalValueParameter<StringArray>)Parameters["LocationNames"]; } 75 } 70 76 #endregion 71 77 … … 99 105 set { TransportationCostsParameter.Value.Value = value; } 100 106 } 107 public StringArray EquipmentNames { 108 get { return EquipmentNamesParameter.Value; } 109 set { EquipmentNamesParameter.Value = value; } 110 } 111 public StringArray LocationNames { 112 get { return LocationNamesParameter.Value; } 113 set { LocationNamesParameter.Value = value; } 114 } 101 115 #endregion 102 116 103 [Storable]104 private BestGQAPSolutionAnalyzer bestSolutionAnalyzer;105 117 public BestGQAPSolutionAnalyzer BestSolutionAnalyzer { 106 get { return bestSolutionAnalyzer; } 107 set { bestSolutionAnalyzer = value; } 118 get { return Operators.OfType<BestGQAPSolutionAnalyzer>().First(); } 108 119 } 109 120 … … 112 123 private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner) 113 124 : base(original, cloner) { 114 bestSolutionAnalyzer = cloner.Clone(original.bestSolutionAnalyzer);115 125 AttachEventHandlers(); 116 126 } … … 201 211 202 212 private void InitializeOperators() { 213 Operators.Add(new BestGQAPSolutionAnalyzer()); 214 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>()); 203 215 Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>()); 204 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());205 216 Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator); 206 217 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>()); 207 Operators.Add(new BestGQAPSolutionAnalyzer());208 218 Parameterize(); 209 219 } … … 276 286 277 287 if (BestSolutionAnalyzer != null) { 278 BestSolutionAnalyzer. QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;288 BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 279 289 BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name; 280 290 BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name; 291 BestSolutionAnalyzer.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name; 292 BestSolutionAnalyzer.DemandsParameter.ActualName = DemandsParameter.Name; 293 BestSolutionAnalyzer.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 294 BestSolutionAnalyzer.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name; 295 BestSolutionAnalyzer.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name; 281 296 BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 297 BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 298 BestSolutionAnalyzer.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName; 299 BestSolutionAnalyzer.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName; 300 BestSolutionAnalyzer.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName; 282 301 BestSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 283 302 BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 284 BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 303 BestSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 304 BestSolutionAnalyzer.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name; 305 BestSolutionAnalyzer.LocationNamesParameter.ActualName = LocationNamesParameter.Name; 285 306 } 286 307 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs
r7412 r7415 60 60 while (tries < maxTries) { 61 61 assignment.Clear(); 62 capacities.Select((v, i) => slack[i] = v).Enumerate();62 slack = capacities.Select((v, i) => new { Index = i, Value = v }).ToDictionary(x => x.Index, y => y.Value); 63 63 HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments 64 64 T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
Note: See TracChangeset
for help on using the changeset viewer.