[5558] | 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;
|
---|
[5563] | 23 | using System.Collections.Generic;
|
---|
[5558] | 24 | using System.Drawing;
|
---|
[5562] | 25 | using System.IO;
|
---|
[5558] | 26 | using System.Linq;
|
---|
[5562] | 27 | using System.Reflection;
|
---|
[5558] | 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;
|
---|
[5562] | 35 | using HeuristicLab.PluginInfrastructure;
|
---|
[5558] | 36 |
|
---|
| 37 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
[5838] | 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.")]
|
---|
[5558] | 39 | [Creatable("Problems")]
|
---|
| 40 | [StorableClass]
|
---|
[5953] | 41 | public sealed class QuadraticAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<IQAPEvaluator, IPermutationCreator>, IStorableContent {
|
---|
[5563] | 42 | private static string InstancePrefix = "HeuristicLab.Problems.QuadraticAssignment.Data.";
|
---|
| 43 |
|
---|
[5953] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[5558] | 46 | public override Image ItemImage {
|
---|
| 47 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | #region Parameter Properties
|
---|
[6525] | 51 | public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
|
---|
| 52 | get { return (IValueParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
|
---|
[6342] | 53 | }
|
---|
[5641] | 54 | public IValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 55 | get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
[5558] | 56 | }
|
---|
[5641] | 57 | public IValueParameter<DoubleMatrix> WeightsParameter {
|
---|
| 58 | get { return (IValueParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
[5558] | 59 | }
|
---|
[5838] | 60 | public IValueParameter<DoubleMatrix> DistancesParameter {
|
---|
| 61 | get { return (IValueParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
[5558] | 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | #region Properties
|
---|
[6525] | 66 | public ItemSet<Permutation> BestKnownSolutions {
|
---|
[6342] | 67 | get { return BestKnownSolutionsParameter.Value; }
|
---|
| 68 | set { BestKnownSolutionsParameter.Value = value; }
|
---|
| 69 | }
|
---|
[5558] | 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 | }
|
---|
[5838] | 78 | public DoubleMatrix Distances {
|
---|
| 79 | get { return DistancesParameter.Value; }
|
---|
| 80 | set { DistancesParameter.Value = value; }
|
---|
[5558] | 81 | }
|
---|
[5563] | 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 | }
|
---|
[5583] | 93 |
|
---|
| 94 | private BestQAPSolutionAnalyzer BestQAPSolutionAnalyzer {
|
---|
| 95 | get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
| 96 | }
|
---|
[6342] | 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 | }
|
---|
[5558] | 105 | #endregion
|
---|
| 106 |
|
---|
| 107 | [StorableConstructor]
|
---|
| 108 | private QuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
|
---|
[5562] | 109 | private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
|
---|
[5558] | 110 | : base(original, cloner) {
|
---|
| 111 | AttachEventHandlers();
|
---|
| 112 | }
|
---|
| 113 | public QuadraticAssignmentProblem()
|
---|
[5931] | 114 | : base(new QAPEvaluator(), new RandomPermutationCreator()) {
|
---|
[6525] | 115 | Parameters.Add(new OptionalValueParameter<ItemSet<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));
|
---|
[5648] | 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));
|
---|
[5558] | 117 | Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
|
---|
[5838] | 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)));
|
---|
[5558] | 119 |
|
---|
[5563] | 120 | Maximization = new BoolValue(false);
|
---|
| 121 |
|
---|
[5558] | 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 |
|
---|
[5838] | 130 | Distances = new DoubleMatrix(new double[,] {
|
---|
[5558] | 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 |
|
---|
[5931] | 138 | SolutionCreator.PermutationParameter.ActualName = "Assignment";
|
---|
| 139 | ParameterizeSolutionCreator();
|
---|
| 140 | ParameterizeEvaluator();
|
---|
[5558] | 141 |
|
---|
| 142 | InitializeOperators();
|
---|
| 143 | AttachEventHandlers();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 147 | return new QuadraticAssignmentProblem(this, cloner);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[6342] | 150 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 151 | private void AfterDeserialization() {
|
---|
| 152 | // BackwardsCompatibility3.3
|
---|
| 153 | #region Backwards compatible code, remove with 3.4
|
---|
| 154 | if (!Parameters.ContainsKey("BestKnownSolutions")) {
|
---|
[6525] | 155 | Parameters.Add(new OptionalValueParameter<ItemSet<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));
|
---|
| 156 | } else if (Parameters["BestKnownSolutions"].GetType().Equals(typeof(OptionalValueParameter<ItemList<Permutation>>))) {
|
---|
| 157 | ItemList<Permutation> list = ((OptionalValueParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]).Value;
|
---|
[6540] | 158 | Parameters.Remove("BestKnownSolutions");
|
---|
[6571] | 159 | Parameters.Add(new OptionalValueParameter<ItemSet<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.", (list != null ? new ItemSet<Permutation>(list) : null)));
|
---|
[6342] | 160 | }
|
---|
| 161 | if (Parameters.ContainsKey("DistanceMatrix")) {
|
---|
[6525] | 162 | DoubleMatrix d = ((ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]).Value;
|
---|
[6342] | 163 | Parameters.Remove("DistanceMatrix");
|
---|
[6525] | 164 | 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.", d));
|
---|
[6342] | 165 | }
|
---|
| 166 | AttachEventHandlers();
|
---|
| 167 | #endregion
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[5558] | 170 | #region Events
|
---|
[5562] | 171 | protected override void OnSolutionCreatorChanged() {
|
---|
| 172 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 173 | ParameterizeSolutionCreator();
|
---|
| 174 | ParameterizeEvaluator();
|
---|
[5583] | 175 | ParameterizeAnalyzers();
|
---|
[5562] | 176 | ParameterizeOperators();
|
---|
| 177 | base.OnSolutionCreatorChanged();
|
---|
[5558] | 178 | }
|
---|
[5562] | 179 | protected override void OnEvaluatorChanged() {
|
---|
[5583] | 180 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5562] | 181 | ParameterizeEvaluator();
|
---|
[5583] | 182 | ParameterizeAnalyzers();
|
---|
[5562] | 183 | ParameterizeOperators();
|
---|
| 184 | base.OnEvaluatorChanged();
|
---|
[5558] | 185 | }
|
---|
[5562] | 186 |
|
---|
| 187 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 188 | ParameterizeEvaluator();
|
---|
[5583] | 189 | ParameterizeAnalyzers();
|
---|
[5562] | 190 | ParameterizeOperators();
|
---|
[5558] | 191 | }
|
---|
[5583] | 192 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 193 | ParameterizeAnalyzers();
|
---|
| 194 | ParameterizeOperators();
|
---|
| 195 | }
|
---|
[5562] | 196 | private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 197 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
[5855] | 198 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
[5562] | 199 | ParameterizeSolutionCreator();
|
---|
| 200 | ParameterizeEvaluator();
|
---|
| 201 | ParameterizeOperators();
|
---|
[5855] | 202 | AdjustDistanceMatrix();
|
---|
[5558] | 203 | }
|
---|
[5562] | 204 | private void Weights_RowsChanged(object sender, EventArgs e) {
|
---|
[5855] | 205 | if (Weights.Rows != Weights.Columns)
|
---|
| 206 | ((IStringConvertibleMatrix)Weights).Columns = Weights.Rows;
|
---|
| 207 | else {
|
---|
| 208 | ParameterizeSolutionCreator();
|
---|
| 209 | ParameterizeEvaluator();
|
---|
| 210 | ParameterizeOperators();
|
---|
| 211 | AdjustDistanceMatrix();
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | private void Weights_ColumnsChanged(object sender, EventArgs e) {
|
---|
| 215 | if (Weights.Rows != Weights.Columns)
|
---|
| 216 | ((IStringConvertibleMatrix)Weights).Rows = Weights.Columns;
|
---|
| 217 | else {
|
---|
| 218 | ParameterizeSolutionCreator();
|
---|
| 219 | ParameterizeEvaluator();
|
---|
| 220 | ParameterizeOperators();
|
---|
| 221 | AdjustDistanceMatrix();
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | private void DistancesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 225 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
| 226 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
[5562] | 227 | ParameterizeSolutionCreator();
|
---|
| 228 | ParameterizeEvaluator();
|
---|
| 229 | ParameterizeOperators();
|
---|
[5855] | 230 | AdjustWeightsMatrix();
|
---|
[5562] | 231 | }
|
---|
[5855] | 232 | private void Distances_RowsChanged(object sender, EventArgs e) {
|
---|
| 233 | if (Distances.Rows != Distances.Columns)
|
---|
| 234 | ((IStringConvertibleMatrix)Distances).Columns = Distances.Rows;
|
---|
| 235 | else {
|
---|
| 236 | ParameterizeSolutionCreator();
|
---|
| 237 | ParameterizeEvaluator();
|
---|
| 238 | ParameterizeOperators();
|
---|
| 239 | AdjustWeightsMatrix();
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | private void Distances_ColumnsChanged(object sender, EventArgs e) {
|
---|
| 243 | if (Distances.Rows != Distances.Columns)
|
---|
| 244 | ((IStringConvertibleMatrix)Distances).Rows = Distances.Columns;
|
---|
| 245 | else {
|
---|
| 246 | ParameterizeSolutionCreator();
|
---|
| 247 | ParameterizeEvaluator();
|
---|
| 248 | ParameterizeOperators();
|
---|
| 249 | AdjustWeightsMatrix();
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[5558] | 252 | #endregion
|
---|
| 253 |
|
---|
| 254 | #region Helpers
|
---|
| 255 | private void AttachEventHandlers() {
|
---|
[5598] | 256 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 257 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5562] | 258 | WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
| 259 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
[5855] | 260 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
| 261 | DistancesParameter.ValueChanged += new EventHandler(DistancesParameter_ValueChanged);
|
---|
| 262 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
| 263 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
[5558] | 264 | }
|
---|
| 265 |
|
---|
| 266 | private void InitializeOperators() {
|
---|
[5562] | 267 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
|
---|
[6088] | 268 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
| 269 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>());
|
---|
[5583] | 270 | Operators.Add(new BestQAPSolutionAnalyzer());
|
---|
[6342] | 271 | Operators.Add(new QAPAlleleFrequencyAnalyzer());
|
---|
| 272 | Operators.Add(new QAPPopulationDiversityAnalyzer());
|
---|
| 273 | Operators.Add(new QAPExhaustiveSwap2LocalImprovement());
|
---|
[5583] | 274 | ParameterizeAnalyzers();
|
---|
[5563] | 275 | ParameterizeOperators();
|
---|
[5558] | 276 | }
|
---|
| 277 | private void ParameterizeSolutionCreator() {
|
---|
[5563] | 278 | if (SolutionCreator != null) {
|
---|
| 279 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
|
---|
| 280 | SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
|
---|
| 281 | }
|
---|
[5558] | 282 | }
|
---|
| 283 | private void ParameterizeEvaluator() {
|
---|
[5563] | 284 | if (Evaluator != null) {
|
---|
| 285 | Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[5838] | 286 | Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
[5563] | 287 | Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 288 | }
|
---|
[5558] | 289 | }
|
---|
[5583] | 290 | private void ParameterizeAnalyzers() {
|
---|
| 291 | if (BestQAPSolutionAnalyzer != null) {
|
---|
| 292 | BestQAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[5838] | 293 | BestQAPSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
[5583] | 294 | BestQAPSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 295 | BestQAPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 296 | BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 297 | BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
[6342] | 298 | BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
|
---|
[5583] | 299 | BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 300 | }
|
---|
[6342] | 301 | if (QAPAlleleFrequencyAnalyzer != null) {
|
---|
| 302 | QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 303 | QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 304 | QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 305 | QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 306 | QAPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 307 | QAPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 308 | QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 309 | }
|
---|
| 310 | if (QAPPopulationDiversityAnalyzer != null) {
|
---|
| 311 | QAPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 312 | QAPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 313 | QAPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 314 | QAPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 315 | }
|
---|
[5583] | 316 | }
|
---|
[5562] | 317 | private void ParameterizeOperators() {
|
---|
| 318 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 319 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 320 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 321 | }
|
---|
| 322 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 323 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 324 | }
|
---|
| 325 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 326 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 327 | }
|
---|
[5563] | 328 | if (Operators.OfType<IMoveGenerator>().Any()) {
|
---|
| 329 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
| 330 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
|
---|
| 331 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
| 332 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
| 333 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
|
---|
| 334 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
[5838] | 335 | string swapMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationSwap2MoveOperator>().First().Swap2MoveParameter.ActualName;
|
---|
| 336 | foreach (IPermutationSwap2MoveOperator op in Operators.OfType<IPermutationSwap2MoveOperator>()) {
|
---|
| 337 | op.Swap2MoveParameter.ActualName = swapMove;
|
---|
[5785] | 338 | }
|
---|
[5563] | 339 | }
|
---|
[6042] | 340 | foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())
|
---|
| 341 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6342] | 342 |
|
---|
| 343 | QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault();
|
---|
| 344 | if (localOpt != null) {
|
---|
| 345 | localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 346 | localOpt.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 347 | localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 348 | localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 349 | localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 350 | }
|
---|
[5562] | 351 | }
|
---|
[5598] | 352 |
|
---|
[5855] | 353 | private void AdjustDistanceMatrix() {
|
---|
| 354 | if (Distances.Rows != Weights.Rows || Distances.Columns != Weights.Columns) {
|
---|
| 355 | ((IStringConvertibleMatrix)Distances).Rows = Weights.Rows;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | private void AdjustWeightsMatrix() {
|
---|
| 360 | if (Weights.Rows != Distances.Rows || Weights.Columns != Distances.Columns) {
|
---|
| 361 | ((IStringConvertibleMatrix)Weights).Rows = Distances.Rows;
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
[5558] | 364 | #endregion
|
---|
[5562] | 365 |
|
---|
[5563] | 366 | public void ImportFileInstance(string filename) {
|
---|
[5562] | 367 | QAPLIBParser parser = new QAPLIBParser();
|
---|
| 368 | parser.Parse(filename);
|
---|
[5641] | 369 | if (parser.Error != null) throw parser.Error;
|
---|
[5838] | 370 | Distances = new DoubleMatrix(parser.Distances);
|
---|
[5562] | 371 | Weights = new DoubleMatrix(parser.Weights);
|
---|
| 372 | Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")";
|
---|
| 373 | Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
|
---|
[5598] | 374 | BestKnownQuality = null;
|
---|
[6342] | 375 | BestKnownSolutions = null;
|
---|
[5562] | 376 | OnReset();
|
---|
| 377 | }
|
---|
[5563] | 378 |
|
---|
| 379 | public void LoadEmbeddedInstance(string instance) {
|
---|
| 380 | using (Stream stream = Assembly.GetExecutingAssembly()
|
---|
| 381 | .GetManifestResourceStream(InstancePrefix + instance + ".dat")) {
|
---|
| 382 | QAPLIBParser parser = new QAPLIBParser();
|
---|
| 383 | parser.Parse(stream);
|
---|
[5641] | 384 | if (parser.Error != null) throw parser.Error;
|
---|
[5838] | 385 | Distances = new DoubleMatrix(parser.Distances);
|
---|
[5563] | 386 | Weights = new DoubleMatrix(parser.Weights);
|
---|
[6628] | 387 | Name = instance;
|
---|
| 388 | Description = "Loaded embedded QAPLIB problem data of instance " + instance + ".";
|
---|
[5563] | 389 | OnReset();
|
---|
| 390 | }
|
---|
[5598] | 391 | bool solutionExists = Assembly.GetExecutingAssembly()
|
---|
| 392 | .GetManifestResourceNames()
|
---|
| 393 | .Where(x => x.EndsWith(instance + ".sln"))
|
---|
| 394 | .Any();
|
---|
| 395 | if (solutionExists) {
|
---|
| 396 | using (Stream solStream = Assembly.GetExecutingAssembly()
|
---|
| 397 | .GetManifestResourceStream(InstancePrefix + instance + ".sln")) {
|
---|
| 398 | QAPLIBSolutionParser solParser = new QAPLIBSolutionParser();
|
---|
[5948] | 399 | solParser.Parse(solStream, true); // most sln's seem to be of the type index = "facility" => value = "location"
|
---|
[5641] | 400 | if (solParser.Error != null) throw solParser.Error;
|
---|
[5838] | 401 | if (!solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) {
|
---|
[5814] | 402 | solStream.Seek(0, SeekOrigin.Begin);
|
---|
[5949] | 403 | solParser.Reset();
|
---|
[5948] | 404 | solParser.Parse(solStream, false); // some sln's seem to be of the type index = "location" => value = "facility"
|
---|
[5814] | 405 | if (solParser.Error != null) throw solParser.Error;
|
---|
[5838] | 406 | if (solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) {
|
---|
[5814] | 407 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
[6525] | 408 | BestKnownSolutions = new ItemSet<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }, new PermutationEqualityComparer());
|
---|
[5814] | 409 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
|
---|
| 410 | } else {
|
---|
[5949] | 411 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
[6342] | 412 | BestKnownSolutions = null;
|
---|
[5814] | 413 | BestKnownSolution = null;
|
---|
| 414 | }
|
---|
| 415 | } else {
|
---|
| 416 | BestKnownQuality = new DoubleValue(solParser.Quality);
|
---|
[6525] | 417 | BestKnownSolutions = new ItemSet<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }, new PermutationEqualityComparer());
|
---|
[5814] | 418 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
|
---|
| 419 | }
|
---|
[5598] | 420 | }
|
---|
| 421 | } else {
|
---|
| 422 | BestKnownQuality = null;
|
---|
[6525] | 423 | BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
[5598] | 424 | BestKnownSolution = null;
|
---|
| 425 | }
|
---|
[5563] | 426 | }
|
---|
[5558] | 427 | }
|
---|
| 428 | }
|
---|