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;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
37 | [Item("Quadratic Assignment Problem", "The Quadratic Assignment Problem (QAP) is the problem of assigning N facilities to N fixed locations such that there is exactly one facility in each location and that the distances multiplied by the connection strength between the facilities becomes minimal.")]
|
---|
38 | [Creatable("Problems")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class QuadraticAssignmentProblem : SingleObjectiveProblem<IQAPEvaluator, IPermutationCreator> {
|
---|
41 | public override Image ItemImage {
|
---|
42 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | #region Parameter Properties
|
---|
46 | public ValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
47 | get { return (ValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
48 | }
|
---|
49 | public ValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
50 | get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
51 | }
|
---|
52 | public ValueParameter<DoubleMatrix> WeightsParameter {
|
---|
53 | get { return (ValueParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
54 | }
|
---|
55 | public ValueParameter<DoubleMatrix> DistanceMatrixParameter {
|
---|
56 | get { return (ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
|
---|
57 | }
|
---|
58 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
59 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Properties
|
---|
65 | public Permutation BestKnownSolution {
|
---|
66 | get { return BestKnownSolutionParameter.Value; }
|
---|
67 | set { BestKnownSolutionParameter.Value = value; }
|
---|
68 | }
|
---|
69 | public DoubleMatrix Coordinates {
|
---|
70 | get { return CoordinatesParameter.Value; }
|
---|
71 | set { CoordinatesParameter.Value = value; }
|
---|
72 | }
|
---|
73 | public DoubleMatrix Weights {
|
---|
74 | get { return WeightsParameter.Value; }
|
---|
75 | set { WeightsParameter.Value = value; }
|
---|
76 | }
|
---|
77 | public DoubleMatrix DistanceMatrix {
|
---|
78 | get { return DistanceMatrixParameter.Value; }
|
---|
79 | set { DistanceMatrixParameter.Value = value; }
|
---|
80 | }
|
---|
81 | public BoolValue UseDistanceMatrix {
|
---|
82 | get { return UseDistanceMatrixParameter.Value; }
|
---|
83 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | private QuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
|
---|
89 | private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | AttachEventHandlers();
|
---|
92 | }
|
---|
93 | public QuadraticAssignmentProblem()
|
---|
94 | : 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 | 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 | Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations."));
|
---|
104 | Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
|
---|
105 | 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 | 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)));
|
---|
107 |
|
---|
108 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
109 | { 294.000, 3.000 },
|
---|
110 | { 585.246, 214.603 },
|
---|
111 | { 474.000, 556.983 },
|
---|
112 | { 114.000, 556.983 },
|
---|
113 | { 2.754, 214.603 }
|
---|
114 | });
|
---|
115 |
|
---|
116 | Weights = new DoubleMatrix(new double[,] {
|
---|
117 | { 0, 1, 0, 0, 1 },
|
---|
118 | { 1, 0, 1, 0, 0 },
|
---|
119 | { 0, 1, 0, 1, 0 },
|
---|
120 | { 0, 0, 1, 0, 1 },
|
---|
121 | { 1, 0, 0, 1, 0 }
|
---|
122 | });
|
---|
123 |
|
---|
124 | DistanceMatrix = new DoubleMatrix(new double[,] {
|
---|
125 | { 0, 360, 582, 582, 360 },
|
---|
126 | { 360, 0, 360, 582, 582 },
|
---|
127 | { 582, 360, 0, 360, 582 },
|
---|
128 | { 582, 582, 360, 0, 360 },
|
---|
129 | { 360, 582, 582, 360, 0 }
|
---|
130 | });
|
---|
131 |
|
---|
132 | ParameterizeSolutionCreator();
|
---|
133 | ParameterizeEvaluator();
|
---|
134 |
|
---|
135 | InitializeOperators();
|
---|
136 | AttachEventHandlers();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
140 | return new QuadraticAssignmentProblem(this, cloner);
|
---|
141 | }
|
---|
142 |
|
---|
143 | #region Events
|
---|
144 | protected override void OnSolutionCreatorChanged() {
|
---|
145 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
146 | ParameterizeSolutionCreator();
|
---|
147 | ParameterizeEvaluator();
|
---|
148 | ParameterizeOperators();
|
---|
149 | base.OnSolutionCreatorChanged();
|
---|
150 | }
|
---|
151 | protected override void OnEvaluatorChanged() {
|
---|
152 | ParameterizeEvaluator();
|
---|
153 | ParameterizeOperators();
|
---|
154 | base.OnEvaluatorChanged();
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
158 | ParameterizeEvaluator();
|
---|
159 | ParameterizeOperators();
|
---|
160 | }
|
---|
161 | private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
162 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
163 | ParameterizeSolutionCreator();
|
---|
164 | ParameterizeEvaluator();
|
---|
165 | ParameterizeOperators();
|
---|
166 | }
|
---|
167 | private void Weights_RowsChanged(object sender, EventArgs e) {
|
---|
168 | ParameterizeSolutionCreator();
|
---|
169 | ParameterizeEvaluator();
|
---|
170 | ParameterizeOperators();
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 |
|
---|
174 | #region Helpers
|
---|
175 | [StorableHook(HookType.AfterDeserialization)]
|
---|
176 | private void AfterDeserializationHook() {
|
---|
177 | AttachEventHandlers();
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void AttachEventHandlers() {
|
---|
181 | WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
182 | Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
|
---|
183 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void InitializeOperators() {
|
---|
187 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
|
---|
188 | }
|
---|
189 | private void ParameterizeSolutionCreator() {
|
---|
190 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.Absolute);
|
---|
191 | SolutionCreator.LengthParameter.Value = new IntValue(Weights.Rows);
|
---|
192 | }
|
---|
193 | 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;
|
---|
199 | }
|
---|
200 | private void ParameterizeOperators() {
|
---|
201 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
202 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
203 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
204 | }
|
---|
205 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
206 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
207 | }
|
---|
208 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
209 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
210 | }
|
---|
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) {
|
---|
221 | QAPLIBParser parser = new QAPLIBParser();
|
---|
222 | parser.Parse(filename);
|
---|
223 | DistanceMatrix = new DoubleMatrix(parser.Distances);
|
---|
224 | Weights = new DoubleMatrix(parser.Weights);
|
---|
225 | UseDistanceMatrix.Value = true;
|
---|
226 | Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")";
|
---|
227 | Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
|
---|
228 | OnReset();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|