Changeset 5598 for branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
- Timestamp:
- 03/02/11 22:33:30 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
r5583 r5598 59 59 get { return (ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; } 60 60 } 61 public ValueParameter<BoolValue> UseDistanceMatrixParameter {62 get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }63 }64 65 61 #endregion 66 62 … … 81 77 get { return DistanceMatrixParameter.Value; } 82 78 set { DistanceMatrixParameter.Value = value; } 83 }84 public BoolValue UseDistanceMatrix {85 get { return UseDistanceMatrixParameter.Value; }86 set { UseDistanceMatrixParameter.Value = value; }87 79 } 88 80 … … 112 104 : base() { 113 105 Parameters.Add(new ValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null)); 114 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations. "));106 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations. If this is changed the distance matrix is calculated automatically using the euclidean distance.")); 115 107 Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5))); 116 108 Parameters.Add(new ValueParameter<DoubleMatrix>("DistanceMatrix", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates.", new DoubleMatrix(5, 5))); 117 Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "Defaults to true, set to false only when the number of facilities is very high and a distance matrix would become too large (>1000 facilities).", new BoolValue(true)));118 109 119 110 Maximization = new BoolValue(false); … … 196 187 ParameterizeOperators(); 197 188 } 189 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) { 190 Coordinates.Reset += new EventHandler(Coordinates_Reset); 191 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 192 UpdateDistanceMatrix(); 193 } 194 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { 195 UpdateDistanceMatrix(); 196 } 197 private void Coordinates_Reset(object sender, EventArgs e) { 198 UpdateDistanceMatrix(); 199 } 198 200 #endregion 199 201 … … 205 207 206 208 private void AttachEventHandlers() { 209 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 210 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 207 211 WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged); 208 212 Weights.RowsChanged += new EventHandler(Weights_RowsChanged); 209 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 210 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 213 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 214 Coordinates.Reset += new EventHandler(Coordinates_Reset); 215 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 211 216 } 212 217 … … 226 231 if (Evaluator != null) { 227 232 Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 228 Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;229 233 Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 230 234 Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name; … … 265 269 } 266 270 } 271 272 private void UpdateDistanceMatrix() { 273 if (Coordinates != null && Coordinates.Columns == 2 && Coordinates.Rows > 1) { 274 DistanceMatrix = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows); 275 for (int i = 0; i < Coordinates.Rows - 1; i++) { 276 for (int j = i + 1; j < Coordinates.Rows; j++) { 277 double dx = Coordinates[i, 0] - Coordinates[j, 0]; 278 double dy = Coordinates[i, 1] - Coordinates[j, 1]; 279 DistanceMatrix[i, j] = Math.Sqrt(dx * dx + dy * dy); 280 DistanceMatrix[j, i] = DistanceMatrix[i, j]; 281 } 282 } 283 } 284 } 267 285 #endregion 268 286 … … 273 291 DistanceMatrix = new DoubleMatrix(parser.Distances); 274 292 Weights = new DoubleMatrix(parser.Weights); 275 UseDistanceMatrix.Value = true;276 293 Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")"; 277 294 Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + "."; 295 BestKnownQuality = null; 296 BestKnownSolution = null; 278 297 OnReset(); 279 298 } … … 287 306 DistanceMatrix = new DoubleMatrix(parser.Distances); 288 307 Weights = new DoubleMatrix(parser.Weights); 289 UseDistanceMatrix.Value = true;290 308 Name = "Quadratic Assignment Problem (loaded instance " + instance + ")"; 291 309 Description = "Loaded embedded problem data of instance " + instance + "."; 292 310 OnReset(); 293 311 } 312 bool solutionExists = Assembly.GetExecutingAssembly() 313 .GetManifestResourceNames() 314 .Where(x => x.EndsWith(instance + ".sln")) 315 .Any(); 316 if (solutionExists) { 317 using (Stream solStream = Assembly.GetExecutingAssembly() 318 .GetManifestResourceStream(InstancePrefix + instance + ".sln")) { 319 QAPLIBSolutionParser solParser = new QAPLIBSolutionParser(); 320 solParser.Parse(solStream); 321 BestKnownQuality = new DoubleValue(solParser.Qualiy); 322 BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment); 323 } 324 } else { 325 BestKnownQuality = null; 326 BestKnownSolution = null; 327 } 294 328 } 295 329 }
Note: See TracChangeset
for help on using the changeset viewer.