1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Reflection;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 | using HeuristicLab.PluginInfrastructure;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
38 | [Item("Quadratic Assignment Problem", "The Quadratic Assignment Problem (QAP) can be described as the problem of assigning N facilities to N fixed locations such that there is exactly one facility in each location and that the sum of the distances multiplied by the connection strength between the facilities becomes minimal.")]
|
---|
39 | [Creatable("Problems")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class QuadraticAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<IQAPEvaluator, IPermutationCreator>, IStorableContent {
|
---|
42 | private static string InstancePrefix = "HeuristicLab.Problems.QuadraticAssignment.Data.";
|
---|
43 |
|
---|
44 | public string Filename { get; set; }
|
---|
45 |
|
---|
46 | public override Image ItemImage {
|
---|
47 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | #region Parameter Properties
|
---|
51 | public IValueParameter<ItemList<Permutation>> BestKnownSolutionsParameter {
|
---|
52 | get { return (IValueParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
55 | get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<DoubleMatrix> WeightsParameter {
|
---|
58 | get { return (IValueParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
59 | }
|
---|
60 | public IValueParameter<DoubleMatrix> DistancesParameter {
|
---|
61 | get { return (IValueParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region Properties
|
---|
66 | public ItemList<Permutation> BestKnownSolutions {
|
---|
67 | get { return BestKnownSolutionsParameter.Value; }
|
---|
68 | set { BestKnownSolutionsParameter.Value = value; }
|
---|
69 | }
|
---|
70 | public Permutation BestKnownSolution {
|
---|
71 | get { return BestKnownSolutionParameter.Value; }
|
---|
72 | set { BestKnownSolutionParameter.Value = value; }
|
---|
73 | }
|
---|
74 | public DoubleMatrix Weights {
|
---|
75 | get { return WeightsParameter.Value; }
|
---|
76 | set { WeightsParameter.Value = value; }
|
---|
77 | }
|
---|
78 | public DoubleMatrix Distances {
|
---|
79 | get { return DistancesParameter.Value; }
|
---|
80 | set { DistancesParameter.Value = value; }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IEnumerable<string> EmbeddedInstances {
|
---|
84 | get {
|
---|
85 | return Assembly.GetExecutingAssembly()
|
---|
86 | .GetManifestResourceNames()
|
---|
87 | .Where(x => x.EndsWith(".dat"))
|
---|
88 | .OrderBy(x => x)
|
---|
89 | .Select(x => x.Replace(".dat", String.Empty))
|
---|
90 | .Select(x => x.Replace(InstancePrefix, String.Empty));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private BestQAPSolutionAnalyzer BestQAPSolutionAnalyzer {
|
---|
95 | get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private QAPAlleleFrequencyAnalyzer QAPAlleleFrequencyAnalyzer {
|
---|
99 | get { return Operators.OfType<QAPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private QAPPopulationDiversityAnalyzer QAPPopulationDiversityAnalyzer {
|
---|
103 | get { return Operators.OfType<QAPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | [StorableConstructor]
|
---|
108 | private QuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
|
---|
109 | private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
|
---|
110 | : base(original, cloner) {
|
---|
111 | AttachEventHandlers();
|
---|
112 | }
|
---|
113 | public QuadraticAssignmentProblem()
|
---|
114 | : base(new QAPEvaluator(), new RandomPermutationCreator()) {
|
---|
115 | Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
|
---|
116 | Parameters.Add(new OptionalValueParameter<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));
|
---|
117 | Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
|
---|
118 | Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates.", new DoubleMatrix(5, 5)));
|
---|
119 |
|
---|
120 | Maximization = new BoolValue(false);
|
---|
121 |
|
---|
122 | Weights = new DoubleMatrix(new double[,] {
|
---|
123 | { 0, 1, 0, 0, 1 },
|
---|
124 | { 1, 0, 1, 0, 0 },
|
---|
125 | { 0, 1, 0, 1, 0 },
|
---|
126 | { 0, 0, 1, 0, 1 },
|
---|
127 | { 1, 0, 0, 1, 0 }
|
---|
128 | });
|
---|
129 |
|
---|
130 | Distances = new DoubleMatrix(new double[,] {
|
---|
131 | { 0, 360, 582, 582, 360 },
|
---|
132 | { 360, 0, 360, 582, 582 },
|
---|
133 | { 582, 360, 0, 360, 582 },
|
---|
134 | { 582, 582, 360, 0, 360 },
|
---|
135 | { 360, 582, 582, 360, 0 }
|
---|
136 | });
|
---|
137 |
|
---|
138 | SolutionCreator.PermutationParameter.ActualName = "Assignment";
|
---|
139 | ParameterizeSolutionCreator();
|
---|
140 | ParameterizeEvaluator();
|
---|
141 |
|
---|
142 | InitializeOperators();
|
---|
143 | AttachEventHandlers();
|
---|
144 | }
|
---|
145 |
|
---|
146 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
147 | return new QuadraticAssignmentProblem(this, cloner);
|
---|
148 | }
|
---|
149 |
|
---|
150 | [StorableHook(HookType.AfterDeserialization)]
|
---|
151 | private void AfterDeserialization() {
|
---|
152 | // BackwardsCompatibility3.3
|
---|
153 | #region Backwards compatible code, remove with 3.4
|
---|
154 | /*if (Parameters.ContainsKey("BestKnownSolution")) {
|
---|
155 | Permutation solution = ((IValueParameter<Permutation>)Parameters["BestKnownSolution"]).Value;
|
---|
156 | Parameters.Remove("BestKnownSolution");
|
---|
157 | Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
|
---|
158 | if (solution != null) {
|
---|
159 | BestKnownSolutions = new ItemList<Permutation>();
|
---|
160 | BestKnownSolutions.Add(solution);
|
---|
161 | }
|
---|
162 | }*/
|
---|
163 | if (!Parameters.ContainsKey("BestKnownSolutions")) {
|
---|
164 | Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
|
---|
165 | }
|
---|
166 | if (Parameters.ContainsKey("DistanceMatrix")) {
|
---|
167 | DoubleMatrix bla = ((ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]).Value;
|
---|
168 | Parameters.Remove("DistanceMatrix");
|
---|
169 | Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "bla", bla));
|
---|
170 | }
|
---|
171 | AttachEventHandlers();
|
---|
172 | #endregion
|
---|
173 | }
|
---|
174 |
|
---|
175 | #region Events
|
---|
176 | protected override void OnSolutionCreatorChanged() {
|
---|
177 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
178 | ParameterizeSolutionCreator();
|
---|
179 | ParameterizeEvaluator();
|
---|
180 | ParameterizeAnalyzers();
|
---|
181 | ParameterizeOperators();
|
---|
182 | base.OnSolutionCreatorChanged();
|
---|
183 | }
|
---|
184 | protected override void OnEvaluatorChanged() {
|
---|
185 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
186 | ParameterizeEvaluator();
|
---|
187 | ParameterizeAnalyzers();
|
---|
188 | ParameterizeOperators();
|
---|
189 | base.OnEvaluatorChanged();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
193 | ParameterizeEvaluator();
|
---|
194 | ParameterizeAnalyzers();
|
---|
195 | ParameterizeOperators();
|
---|
196 | }
|
---|
197 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
198 | ParameterizeAnalyzers();
|
---|
199 | ParameterizeOperators();
|
---|
200 | }
|
---|
201 | private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
202 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
203 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
204 | ParameterizeSolutionCreator();
|
---|
205 | ParameterizeEvaluator();
|
---|
206 | ParameterizeOperators();
|
---|
207 | AdjustDistanceMatrix();
|
---|
208 | }
|
---|
209 | private void Weights_RowsChanged(object sender, EventArgs e) {
|
---|
210 | if (Weights.Rows != Weights.Columns)
|
---|
211 | ((IStringConvertibleMatrix)Weights).Columns = Weights.Rows;
|
---|
212 | else {
|
---|
213 | ParameterizeSolutionCreator();
|
---|
214 | ParameterizeEvaluator();
|
---|
215 | ParameterizeOperators();
|
---|
216 | AdjustDistanceMatrix();
|
---|
217 | }
|
---|
218 | }
|
---|
219 | private void Weights_ColumnsChanged(object sender, EventArgs e) {
|
---|
220 | if (Weights.Rows != Weights.Columns)
|
---|
221 | ((IStringConvertibleMatrix)Weights).Rows = Weights.Columns;
|
---|
222 | else {
|
---|
223 | ParameterizeSolutionCreator();
|
---|
224 | ParameterizeEvaluator();
|
---|
225 | ParameterizeOperators();
|
---|
226 | AdjustDistanceMatrix();
|
---|
227 | }
|
---|
228 | }
|
---|
229 | private void DistancesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
230 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
231 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
232 | ParameterizeSolutionCreator();
|
---|
233 | ParameterizeEvaluator();
|
---|
234 | ParameterizeOperators();
|
---|
235 | AdjustWeightsMatrix();
|
---|
236 | }
|
---|
237 | private void Distances_RowsChanged(object sender, EventArgs e) {
|
---|
238 | if (Distances.Rows != Distances.Columns)
|
---|
239 | ((IStringConvertibleMatrix)Distances).Columns = Distances.Rows;
|
---|
240 | else {
|
---|
241 | ParameterizeSolutionCreator();
|
---|
242 | ParameterizeEvaluator();
|
---|
243 | ParameterizeOperators();
|
---|
244 | AdjustWeightsMatrix();
|
---|
245 | }
|
---|
246 | }
|
---|
247 | private void Distances_ColumnsChanged(object sender, EventArgs e) {
|
---|
248 | if (Distances.Rows != Distances.Columns)
|
---|
249 | ((IStringConvertibleMatrix)Distances).Rows = Distances.Columns;
|
---|
250 | else {
|
---|
251 | ParameterizeSolutionCreator();
|
---|
252 | ParameterizeEvaluator();
|
---|
253 | ParameterizeOperators();
|
---|
254 | AdjustWeightsMatrix();
|
---|
255 | }
|
---|
256 | }
|
---|
257 | #endregion
|
---|
258 |
|
---|
259 | #region Helpers
|
---|
260 | private void AttachEventHandlers() {
|
---|
261 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
262 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
263 | WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
264 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
265 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
266 | DistancesParameter.ValueChanged += new EventHandler(DistancesParameter_ValueChanged);
|
---|
267 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
268 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void InitializeOperators() {
|
---|
272 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
|
---|
273 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
274 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>());
|
---|
275 | Operators.Add(new BestQAPSolutionAnalyzer());
|
---|
276 | Operators.Add(new QAPAlleleFrequencyAnalyzer());
|
---|
277 | Operators.Add(new QAPPopulationDiversityAnalyzer());
|
---|
278 | Operators.Add(new QAPExhaustiveSwap2LocalImprovement());
|
---|
279 | ParameterizeAnalyzers();
|
---|
280 | ParameterizeOperators();
|
---|
281 | }
|
---|
282 | private void ParameterizeSolutionCreator() {
|
---|
283 | if (SolutionCreator != null) {
|
---|
284 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
|
---|
285 | SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | private void ParameterizeEvaluator() {
|
---|
289 | if (Evaluator != null) {
|
---|
290 | Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
291 | Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
292 | Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | private void ParameterizeAnalyzers() {
|
---|
296 | if (BestQAPSolutionAnalyzer != null) {
|
---|
297 | BestQAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
298 | BestQAPSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
299 | BestQAPSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
300 | BestQAPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
301 | BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
302 | BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
303 | BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
|
---|
304 | BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
305 | }
|
---|
306 | if (QAPAlleleFrequencyAnalyzer != null) {
|
---|
307 | QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
308 | QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
309 | QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
310 | QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
311 | QAPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
312 | QAPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
313 | QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
314 | }
|
---|
315 | if (QAPPopulationDiversityAnalyzer != null) {
|
---|
316 | QAPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
317 | QAPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
318 | QAPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
319 | QAPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | private void ParameterizeOperators() {
|
---|
323 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
324 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
325 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
326 | }
|
---|
327 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
328 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
329 | }
|
---|
330 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
331 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
332 | }
|
---|
333 | if (Operators.OfType<IMoveGenerator>().Any()) {
|
---|
334 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
335 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
|
---|
336 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
337 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
338 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
|
---|
339 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
340 | string swapMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationSwap2MoveOperator>().First().Swap2MoveParameter.ActualName;
|
---|
341 | foreach (IPermutationSwap2MoveOperator op in Operators.OfType<IPermutationSwap2MoveOperator>()) {
|
---|
342 | op.Swap2MoveParameter.ActualName = swapMove;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())
|
---|
346 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
347 |
|
---|
348 | QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault();
|
---|
349 | if (localOpt != null) {
|
---|
350 | localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
351 | localOpt.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
352 | localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
353 | localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
354 | localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | private void AdjustDistanceMatrix() {
|
---|
359 | if (Distances.Rows != Weights.Rows || Distances.Columns != Weights.Columns) {
|
---|
360 | ((IStringConvertibleMatrix)Distances).Rows = Weights.Rows;
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | private void AdjustWeightsMatrix() {
|
---|
365 | if (Weights.Rows != Distances.Rows || Weights.Columns != Distances.Columns) {
|
---|
366 | ((IStringConvertibleMatrix)Weights).Rows = Distances.Rows;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | #endregion
|
---|
370 |
|
---|
371 | public void ImportFileInstance(string filename) {
|
---|
372 | QAPLIBParser parser = new QAPLIBParser();
|
---|
373 | parser.Parse(filename);
|
---|
374 | if (parser.Error != null) throw parser.Error;
|
---|
375 | Distances = new DoubleMatrix(parser.Distances);
|
---|
376 | Weights = new DoubleMatrix(parser.Weights);
|
---|
377 | Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")";
|
---|
378 | Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
|
---|
379 | BestKnownQuality = null;
|
---|
380 | BestKnownSolutions = null;
|
---|
381 | OnReset();
|
---|
382 | }
|
---|
383 |
|
---|
384 | public void LoadEmbeddedInstance(string instance) {
|
---|
385 | using (Stream stream = Assembly.GetExecutingAssembly()
|
---|
386 | .GetManifestResourceStream(InstancePrefix + instance + ".dat")) {
|
---|
387 | QAPLIBParser parser = new QAPLIBParser();
|
---|
388 | parser.Parse(stream);
|
---|
389 | if (parser.Error != null) throw parser.Error;
|
---|
390 | Distances = new DoubleMatrix(parser.Distances);
|
---|
391 | Weights = new DoubleMatrix(parser.Weights);
|
---|
392 | Name = "Quadratic Assignment Problem (loaded instance " + instance + ")";
|
---|
393 | Description = "Loaded embedded problem data of instance " + instance + ".";
|
---|
394 | OnReset();
|
---|
395 | }
|
---|
396 | bool solutionExists = Assembly.GetExecutingAssembly()
|
---|
397 | .GetManifestResourceNames()
|
---|
398 | .Where(x => x.EndsWith(instance + ".sln"))
|
---|
399 | .Any();
|
---|
400 | if (solutionExists) {
|
---|
401 | using (Stream solStream = Assembly.GetExecutingAssembly()
|
---|
402 | .GetManifestResourceStream(InstancePrefix + instance + ".sln")) {
|
---|
403 | QAPLIBSolutionParser solParser = new QAPLIBSolutionParser();
|
---|
404 | solParser.Parse(solStream, true); // most sln's seem to be of the type index = "facility" => value = "location"
|
---|
405 | if (solParser.Error != null) throw solParser.Error;
|
---|
406 | if (!solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) {
|
---|
407 | solStream.Seek(0, SeekOrigin.Begin);
|
---|
408 | solParser.Reset();
|
---|
409 | solParser.Parse(solStream, false); // some sln's seem to be of the type index = "location" => value = "facility"
|
---|
410 | if (solParser.Error != null) throw solParser.Error;
|
---|
411 | if (solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) {
|
---|
412 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
413 | BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) });
|
---|
414 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
|
---|
415 | } else {
|
---|
416 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
417 | BestKnownSolutions = null;
|
---|
418 | BestKnownSolution = null;
|
---|
419 | }
|
---|
420 | } else {
|
---|
421 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
422 | BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) });
|
---|
423 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | } else {
|
---|
427 | BestKnownQuality = null;
|
---|
428 | BestKnownSolutions = null;
|
---|
429 | BestKnownSolution = null;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|