Changeset 5563 for branches/QAP/HeuristicLab.Problems.QuadraticAssignment
- Timestamp:
- 02/25/11 14:37:13 (14 years ago)
- Location:
- branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBParser.cs
r5562 r5563 21 21 22 22 public bool Parse(string file) { 23 using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { 24 return Parse(stream); 25 } 26 } 27 28 /// <summary> 29 /// Reads from the given stream data which is expected to be in the QAPLIB format. 30 /// </summary> 31 /// <remarks> 32 /// The stream is not closed or disposed. The caller has to take care of that. 33 /// </remarks> 34 /// <param name="stream">The stream to read data from.</param> 35 /// <returns>True if the file was successfully read or false otherwise.</returns> 36 public bool Parse(Stream stream) { 23 37 Error = null; 24 38 try { 25 StreamReader reader = new StreamReader( file);39 StreamReader reader = new StreamReader(stream); 26 40 Size = int.Parse(reader.ReadLine()); 27 41 Distances = new double[Size, Size]; … … 65 79 return false; 66 80 } 67 68 81 } 69 82 } -
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Plugin.cs.frame
r5562 r5563 23 23 24 24 namespace HeuristicLab.Problems.QuadraticAssignment { 25 [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3. 0.$WCREV$")]25 [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.3.$WCREV$")] 26 26 [PluginFile("HeuristicLab.Problems.QuadraticAssignment-3.3.dll", PluginFileType.Assembly)] 27 27 [PluginDependency("HeuristicLab.Collections", "3.3.3")] -
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
r5562 r5563 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Drawing; 24 25 using System.IO; … … 39 40 [StorableClass] 40 41 public sealed class QuadraticAssignmentProblem : SingleObjectiveProblem<IQAPEvaluator, IPermutationCreator> { 42 private static string InstancePrefix = "HeuristicLab.Problems.QuadraticAssignment.Data."; 43 41 44 public override Image ItemImage { 42 45 get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; } … … 82 85 get { return UseDistanceMatrixParameter.Value; } 83 86 set { UseDistanceMatrixParameter.Value = value; } 87 } 88 89 public IEnumerable<string> EmbeddedInstances { 90 get { 91 return Assembly.GetExecutingAssembly() 92 .GetManifestResourceNames() 93 .Where(x => x.EndsWith(".dat")) 94 .OrderBy(x => x) 95 .Select(x => x.Replace(".dat", String.Empty)) 96 .Select(x => x.Replace(InstancePrefix, String.Empty)); 97 } 84 98 } 85 99 #endregion … … 93 107 public QuadraticAssignmentProblem() 94 108 : base() { 95 RandomPermutationCreator solutionCreator = new RandomPermutationCreator();96 solutionCreator.LengthParameter.Value = new IntValue(5);97 solutionCreator.PermutationParameter.ActualName = "Assignment";98 QAPEvaluator evaluator = new QAPEvaluator();99 100 Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new solutions.", solutionCreator));101 Parameters.Add(new ValueParameter<IQAPEvaluator>("Evaluator", "The operator which should be used to evaluate solutions.", evaluator));102 109 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)); 103 110 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations.")); … … 105 112 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))); 106 113 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))); 114 115 Maximization = new BoolValue(false); 107 116 108 117 Coordinates = new DoubleMatrix(new double[,] { … … 130 139 }); 131 140 132 ParameterizeSolutionCreator(); 133 ParameterizeEvaluator(); 141 RandomPermutationCreator solutionCreator = new RandomPermutationCreator(); 142 solutionCreator.LengthParameter.Value = new IntValue(5); 143 solutionCreator.PermutationParameter.ActualName = "Assignment"; 144 QAPEvaluator evaluator = new QAPEvaluator(); 145 146 SolutionCreatorParameter.Value = solutionCreator; 147 EvaluatorParameter.Value = evaluator; 134 148 135 149 InitializeOperators(); … … 186 200 private void InitializeOperators() { 187 201 Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>()); 202 ParameterizeOperators(); 188 203 } 189 204 private void ParameterizeSolutionCreator() { 190 SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute); 191 SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows); 205 if (SolutionCreator != null) { 206 SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute); 207 SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows); 208 } 192 209 } 193 210 private void ParameterizeEvaluator() { 194 Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 195 Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; 196 Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 197 Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 198 Evaluator.WeightsParameter.ActualName = WeightsParameter.Name; 211 if (Evaluator != null) { 212 Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 213 Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; 214 Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 215 Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 216 Evaluator.WeightsParameter.ActualName = WeightsParameter.Name; 217 } 199 218 } 200 219 private void ParameterizeOperators() { … … 209 228 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 210 229 } 211 string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName; 212 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) 213 op.InversionMoveParameter.ActualName = inversionMove; 214 string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName; 215 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) 216 op.TranslocationMoveParameter.ActualName = translocationMove; 217 } 218 #endregion 219 220 public void ImportFromQAPLib(string filename) { 230 if (Operators.OfType<IMoveGenerator>().Any()) { 231 string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName; 232 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) 233 op.InversionMoveParameter.ActualName = inversionMove; 234 string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName; 235 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) 236 op.TranslocationMoveParameter.ActualName = translocationMove; 237 } 238 } 239 #endregion 240 241 public void ImportFileInstance(string filename) { 221 242 QAPLIBParser parser = new QAPLIBParser(); 222 243 parser.Parse(filename); 244 Coordinates = new DoubleMatrix(); 223 245 DistanceMatrix = new DoubleMatrix(parser.Distances); 224 246 Weights = new DoubleMatrix(parser.Weights); … … 228 250 OnReset(); 229 251 } 252 253 public void LoadEmbeddedInstance(string instance) { 254 using (Stream stream = Assembly.GetExecutingAssembly() 255 .GetManifestResourceStream(InstancePrefix + instance + ".dat")) { 256 QAPLIBParser parser = new QAPLIBParser(); 257 parser.Parse(stream); 258 Coordinates = new DoubleMatrix(); 259 DistanceMatrix = new DoubleMatrix(parser.Distances); 260 Weights = new DoubleMatrix(parser.Weights); 261 UseDistanceMatrix.Value = true; 262 Name = "Quadratic Assignment Problem (loaded instance " + instance + ")"; 263 Description = "Loaded embedded problem data of instance " + instance + "."; 264 OnReset(); 265 } 266 } 230 267 } 231 268 }
Note: See TracChangeset
for help on using the changeset viewer.