[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;
|
---|
| 23 | using System.Drawing;
|
---|
[5562] | 24 | using System.IO;
|
---|
[5558] | 25 | using System.Linq;
|
---|
[5562] | 26 | using System.Reflection;
|
---|
[6891] | 27 | using HeuristicLab.Collections;
|
---|
[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 |
|
---|
[5583] | 83 | private BestQAPSolutionAnalyzer BestQAPSolutionAnalyzer {
|
---|
| 84 | get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
| 85 | }
|
---|
[6342] | 86 |
|
---|
| 87 | private QAPAlleleFrequencyAnalyzer QAPAlleleFrequencyAnalyzer {
|
---|
| 88 | get { return Operators.OfType<QAPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private QAPPopulationDiversityAnalyzer QAPPopulationDiversityAnalyzer {
|
---|
| 92 | get { return Operators.OfType<QAPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
| 93 | }
|
---|
[6891] | 94 |
|
---|
| 95 | private ObservableList<string> instances = new ObservableList<string>();
|
---|
| 96 | public ObservableList<string> Instances {
|
---|
| 97 | get { return instances; }
|
---|
| 98 | }
|
---|
[5558] | 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | [StorableConstructor]
|
---|
| 102 | private QuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
|
---|
[5562] | 103 | private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
|
---|
[5558] | 104 | : base(original, cloner) {
|
---|
[6891] | 105 | instances = new ObservableList<string>(original.instances);
|
---|
[5558] | 106 | AttachEventHandlers();
|
---|
| 107 | }
|
---|
| 108 | public QuadraticAssignmentProblem()
|
---|
[5931] | 109 | : base(new QAPEvaluator(), new RandomPermutationCreator()) {
|
---|
[6525] | 110 | 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] | 111 | 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] | 112 | Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
|
---|
[5838] | 113 | 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] | 114 |
|
---|
[5563] | 115 | Maximization = new BoolValue(false);
|
---|
| 116 |
|
---|
[5558] | 117 | Weights = new DoubleMatrix(new double[,] {
|
---|
| 118 | { 0, 1, 0, 0, 1 },
|
---|
| 119 | { 1, 0, 1, 0, 0 },
|
---|
| 120 | { 0, 1, 0, 1, 0 },
|
---|
| 121 | { 0, 0, 1, 0, 1 },
|
---|
| 122 | { 1, 0, 0, 1, 0 }
|
---|
| 123 | });
|
---|
| 124 |
|
---|
[5838] | 125 | Distances = new DoubleMatrix(new double[,] {
|
---|
[5558] | 126 | { 0, 360, 582, 582, 360 },
|
---|
| 127 | { 360, 0, 360, 582, 582 },
|
---|
| 128 | { 582, 360, 0, 360, 582 },
|
---|
| 129 | { 582, 582, 360, 0, 360 },
|
---|
| 130 | { 360, 582, 582, 360, 0 }
|
---|
| 131 | });
|
---|
| 132 |
|
---|
[5931] | 133 | SolutionCreator.PermutationParameter.ActualName = "Assignment";
|
---|
| 134 | ParameterizeSolutionCreator();
|
---|
| 135 | ParameterizeEvaluator();
|
---|
[5558] | 136 |
|
---|
| 137 | InitializeOperators();
|
---|
| 138 | AttachEventHandlers();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 142 | return new QuadraticAssignmentProblem(this, cloner);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[6342] | 145 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 146 | private void AfterDeserialization() {
|
---|
| 147 | // BackwardsCompatibility3.3
|
---|
| 148 | #region Backwards compatible code, remove with 3.4
|
---|
| 149 | if (!Parameters.ContainsKey("BestKnownSolutions")) {
|
---|
[6525] | 150 | 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));
|
---|
| 151 | } else if (Parameters["BestKnownSolutions"].GetType().Equals(typeof(OptionalValueParameter<ItemList<Permutation>>))) {
|
---|
| 152 | ItemList<Permutation> list = ((OptionalValueParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]).Value;
|
---|
[6540] | 153 | Parameters.Remove("BestKnownSolutions");
|
---|
[6571] | 154 | 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] | 155 | }
|
---|
| 156 | if (Parameters.ContainsKey("DistanceMatrix")) {
|
---|
[6525] | 157 | DoubleMatrix d = ((ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]).Value;
|
---|
[6342] | 158 | Parameters.Remove("DistanceMatrix");
|
---|
[6525] | 159 | 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] | 160 | }
|
---|
| 161 | AttachEventHandlers();
|
---|
| 162 | #endregion
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[5558] | 165 | #region Events
|
---|
[5562] | 166 | protected override void OnSolutionCreatorChanged() {
|
---|
| 167 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 168 | ParameterizeSolutionCreator();
|
---|
| 169 | ParameterizeEvaluator();
|
---|
[5583] | 170 | ParameterizeAnalyzers();
|
---|
[5562] | 171 | ParameterizeOperators();
|
---|
| 172 | base.OnSolutionCreatorChanged();
|
---|
[5558] | 173 | }
|
---|
[5562] | 174 | protected override void OnEvaluatorChanged() {
|
---|
[5583] | 175 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5562] | 176 | ParameterizeEvaluator();
|
---|
[5583] | 177 | ParameterizeAnalyzers();
|
---|
[5562] | 178 | ParameterizeOperators();
|
---|
| 179 | base.OnEvaluatorChanged();
|
---|
[5558] | 180 | }
|
---|
[5562] | 181 |
|
---|
| 182 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 183 | ParameterizeEvaluator();
|
---|
[5583] | 184 | ParameterizeAnalyzers();
|
---|
[5562] | 185 | ParameterizeOperators();
|
---|
[5558] | 186 | }
|
---|
[5583] | 187 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 188 | ParameterizeAnalyzers();
|
---|
| 189 | ParameterizeOperators();
|
---|
| 190 | }
|
---|
[5562] | 191 | private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 192 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
[5855] | 193 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
[5562] | 194 | ParameterizeSolutionCreator();
|
---|
| 195 | ParameterizeEvaluator();
|
---|
| 196 | ParameterizeOperators();
|
---|
[5855] | 197 | AdjustDistanceMatrix();
|
---|
[5558] | 198 | }
|
---|
[5562] | 199 | private void Weights_RowsChanged(object sender, EventArgs e) {
|
---|
[5855] | 200 | if (Weights.Rows != Weights.Columns)
|
---|
| 201 | ((IStringConvertibleMatrix)Weights).Columns = Weights.Rows;
|
---|
| 202 | else {
|
---|
| 203 | ParameterizeSolutionCreator();
|
---|
| 204 | ParameterizeEvaluator();
|
---|
| 205 | ParameterizeOperators();
|
---|
| 206 | AdjustDistanceMatrix();
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | private void Weights_ColumnsChanged(object sender, EventArgs e) {
|
---|
| 210 | if (Weights.Rows != Weights.Columns)
|
---|
| 211 | ((IStringConvertibleMatrix)Weights).Rows = Weights.Columns;
|
---|
| 212 | else {
|
---|
| 213 | ParameterizeSolutionCreator();
|
---|
| 214 | ParameterizeEvaluator();
|
---|
| 215 | ParameterizeOperators();
|
---|
| 216 | AdjustDistanceMatrix();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | private void DistancesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 220 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
| 221 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
[5562] | 222 | ParameterizeSolutionCreator();
|
---|
| 223 | ParameterizeEvaluator();
|
---|
| 224 | ParameterizeOperators();
|
---|
[5855] | 225 | AdjustWeightsMatrix();
|
---|
[5562] | 226 | }
|
---|
[5855] | 227 | private void Distances_RowsChanged(object sender, EventArgs e) {
|
---|
| 228 | if (Distances.Rows != Distances.Columns)
|
---|
| 229 | ((IStringConvertibleMatrix)Distances).Columns = Distances.Rows;
|
---|
| 230 | else {
|
---|
| 231 | ParameterizeSolutionCreator();
|
---|
| 232 | ParameterizeEvaluator();
|
---|
| 233 | ParameterizeOperators();
|
---|
| 234 | AdjustWeightsMatrix();
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | private void Distances_ColumnsChanged(object sender, EventArgs e) {
|
---|
| 238 | if (Distances.Rows != Distances.Columns)
|
---|
| 239 | ((IStringConvertibleMatrix)Distances).Rows = Distances.Columns;
|
---|
| 240 | else {
|
---|
| 241 | ParameterizeSolutionCreator();
|
---|
| 242 | ParameterizeEvaluator();
|
---|
| 243 | ParameterizeOperators();
|
---|
| 244 | AdjustWeightsMatrix();
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
[5558] | 247 | #endregion
|
---|
| 248 |
|
---|
| 249 | #region Helpers
|
---|
| 250 | private void AttachEventHandlers() {
|
---|
[5598] | 251 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 252 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[5562] | 253 | WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
| 254 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
[5855] | 255 | Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
|
---|
| 256 | DistancesParameter.ValueChanged += new EventHandler(DistancesParameter_ValueChanged);
|
---|
| 257 | Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
|
---|
| 258 | Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
|
---|
[5558] | 259 | }
|
---|
| 260 |
|
---|
| 261 | private void InitializeOperators() {
|
---|
[5562] | 262 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
|
---|
[6088] | 263 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
| 264 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>());
|
---|
[5583] | 265 | Operators.Add(new BestQAPSolutionAnalyzer());
|
---|
[6342] | 266 | Operators.Add(new QAPAlleleFrequencyAnalyzer());
|
---|
| 267 | Operators.Add(new QAPPopulationDiversityAnalyzer());
|
---|
| 268 | Operators.Add(new QAPExhaustiveSwap2LocalImprovement());
|
---|
[5583] | 269 | ParameterizeAnalyzers();
|
---|
[5563] | 270 | ParameterizeOperators();
|
---|
[5558] | 271 | }
|
---|
| 272 | private void ParameterizeSolutionCreator() {
|
---|
[5563] | 273 | if (SolutionCreator != null) {
|
---|
| 274 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
|
---|
| 275 | SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
|
---|
| 276 | }
|
---|
[5558] | 277 | }
|
---|
| 278 | private void ParameterizeEvaluator() {
|
---|
[5563] | 279 | if (Evaluator != null) {
|
---|
| 280 | Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[5838] | 281 | Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
[5563] | 282 | Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 283 | }
|
---|
[5558] | 284 | }
|
---|
[5583] | 285 | private void ParameterizeAnalyzers() {
|
---|
| 286 | if (BestQAPSolutionAnalyzer != null) {
|
---|
| 287 | BestQAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[5838] | 288 | BestQAPSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
[5583] | 289 | BestQAPSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 290 | BestQAPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 291 | BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 292 | BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
[6342] | 293 | BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
|
---|
[5583] | 294 | BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 295 | }
|
---|
[6342] | 296 | if (QAPAlleleFrequencyAnalyzer != null) {
|
---|
| 297 | QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 298 | QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 299 | QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 300 | QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 301 | QAPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 302 | QAPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 303 | QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 304 | }
|
---|
| 305 | if (QAPPopulationDiversityAnalyzer != null) {
|
---|
| 306 | QAPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 307 | QAPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 308 | QAPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 309 | QAPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 310 | }
|
---|
[5583] | 311 | }
|
---|
[5562] | 312 | private void ParameterizeOperators() {
|
---|
| 313 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 314 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 315 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 316 | }
|
---|
| 317 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 318 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 319 | }
|
---|
| 320 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 321 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 322 | }
|
---|
[5563] | 323 | if (Operators.OfType<IMoveGenerator>().Any()) {
|
---|
| 324 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
| 325 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
|
---|
| 326 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
| 327 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
| 328 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
|
---|
| 329 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
[5838] | 330 | string swapMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationSwap2MoveOperator>().First().Swap2MoveParameter.ActualName;
|
---|
| 331 | foreach (IPermutationSwap2MoveOperator op in Operators.OfType<IPermutationSwap2MoveOperator>()) {
|
---|
| 332 | op.Swap2MoveParameter.ActualName = swapMove;
|
---|
[5785] | 333 | }
|
---|
[5563] | 334 | }
|
---|
[6042] | 335 | foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>())
|
---|
| 336 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6342] | 337 |
|
---|
| 338 | QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault();
|
---|
| 339 | if (localOpt != null) {
|
---|
| 340 | localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 341 | localOpt.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 342 | localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 343 | localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 344 | localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 345 | }
|
---|
[5562] | 346 | }
|
---|
[5598] | 347 |
|
---|
[5855] | 348 | private void AdjustDistanceMatrix() {
|
---|
| 349 | if (Distances.Rows != Weights.Rows || Distances.Columns != Weights.Columns) {
|
---|
| 350 | ((IStringConvertibleMatrix)Distances).Rows = Weights.Rows;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | private void AdjustWeightsMatrix() {
|
---|
| 355 | if (Weights.Rows != Distances.Rows || Weights.Columns != Distances.Columns) {
|
---|
| 356 | ((IStringConvertibleMatrix)Weights).Rows = Distances.Rows;
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
[5558] | 359 | #endregion
|
---|
[5562] | 360 |
|
---|
[6891] | 361 | public void LoadInstanceFromFile(string filename) {
|
---|
[5562] | 362 | QAPLIBParser parser = new QAPLIBParser();
|
---|
| 363 | parser.Parse(filename);
|
---|
[5641] | 364 | if (parser.Error != null) throw parser.Error;
|
---|
[5838] | 365 | Distances = new DoubleMatrix(parser.Distances);
|
---|
[5562] | 366 | Weights = new DoubleMatrix(parser.Weights);
|
---|
| 367 | Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")";
|
---|
| 368 | Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
|
---|
[5598] | 369 | BestKnownQuality = null;
|
---|
[6891] | 370 | BestKnownSolution = null;
|
---|
[6342] | 371 | BestKnownSolutions = null;
|
---|
[5562] | 372 | OnReset();
|
---|
| 373 | }
|
---|
[5563] | 374 |
|
---|
[6891] | 375 | public void LoadInstanceFromFile(string datFilename, string slnFilename) {
|
---|
| 376 | QAPLIBParser datParser = new QAPLIBParser();
|
---|
| 377 | datParser.Parse(datFilename);
|
---|
| 378 | if (datParser.Error != null) throw datParser.Error;
|
---|
| 379 | Distances = new DoubleMatrix(datParser.Distances);
|
---|
| 380 | Weights = new DoubleMatrix(datParser.Weights);
|
---|
| 381 | Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(datFilename) + ")";
|
---|
| 382 | Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
|
---|
| 383 |
|
---|
| 384 | QAPLIBSolutionParser slnParser = new QAPLIBSolutionParser();
|
---|
| 385 | slnParser.Parse(slnFilename, true);
|
---|
| 386 | if (slnParser.Error != null) throw slnParser.Error;
|
---|
| 387 |
|
---|
| 388 | BestKnownQuality = new DoubleValue(slnParser.Quality);
|
---|
| 389 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, slnParser.Assignment);
|
---|
| 390 | BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
| 391 | BestKnownSolutions.Add((Permutation)BestKnownSolution.Clone());
|
---|
| 392 |
|
---|
| 393 | if (!BestKnownQuality.Value.IsAlmost(QAPEvaluator.Apply(BestKnownSolution, Weights, Distances))) {
|
---|
| 394 | // the solution doesn't result in the given quality, maybe indices and values are inverted
|
---|
| 395 | // try parsing again, this time inverting them
|
---|
| 396 | slnParser.Reset();
|
---|
| 397 | slnParser.Parse(slnFilename, false);
|
---|
| 398 | if (slnParser.Error != null) throw slnParser.Error;
|
---|
| 399 |
|
---|
| 400 | BestKnownQuality = new DoubleValue(slnParser.Quality);
|
---|
| 401 | BestKnownSolution = new Permutation(PermutationTypes.Absolute, slnParser.Assignment);
|
---|
| 402 | BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
| 403 | BestKnownSolutions.Add((Permutation)BestKnownSolution.Clone());
|
---|
| 404 |
|
---|
| 405 | if (!BestKnownQuality.Value.IsAlmost(QAPEvaluator.Apply(BestKnownSolution, Weights, Distances))) {
|
---|
| 406 | // if the solution still doesn't result in the given quality, remove it and only take the quality
|
---|
| 407 | BestKnownSolution = null;
|
---|
| 408 | BestKnownSolutions = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
[5598] | 409 | }
|
---|
| 410 | }
|
---|
[6891] | 411 | OnReset();
|
---|
[5563] | 412 | }
|
---|
[5558] | 413 | }
|
---|
| 414 | }
|
---|