Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs @ 7418

Last change on this file since 7418 was 7418, checked in by abeham, 13 years ago

#1614

  • Added shaking operator based on n-moves
  • Added pareto analyzer regarding flowdistance and installation qualities
File size: 18.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
35  [Item("Generalized Quadratic Assignment Problem", "The Generalized Quadratic Assignment Problem (GQAP) is a generalization of the QAP in that it allows to assign multiple facilities (here called equipment) to a single location. The problem is described in Lee, C.G., and Ma, Z. 2003. The Generalized Quadratic Assignment Problem. Technical Report M5S 3G8, University of Toronto, Canada.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public sealed class GeneralizedQuadraticAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<IGQAPEvaluator, IGQAPSolutionCreator>, IStorableContent {
39    public override Image ItemImage {
40      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
41    }
42
43    public string Filename { get; set; }
44
45    #region Parameter Properties
46    public ValueParameter<DoubleMatrix> WeightsParameter {
47      get { return (ValueParameter<DoubleMatrix>)Parameters["Weights"]; }
48    }
49    public ValueParameter<DoubleMatrix> DistancesParameter {
50      get { return (ValueParameter<DoubleMatrix>)Parameters["Distances"]; }
51    }
52    public ValueParameter<DoubleMatrix> InstallationCostsParameter {
53      get { return (ValueParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
54    }
55    public ValueParameter<DoubleArray> DemandsParameter {
56      get { return (ValueParameter<DoubleArray>)Parameters["Demands"]; }
57    }
58    public ValueParameter<DoubleArray> CapacitiesParameter {
59      get { return (ValueParameter<DoubleArray>)Parameters["Capacities"]; }
60    }
61    public FixedValueParameter<DoubleValue> TransportationCostsParameter {
62      get { return (FixedValueParameter<DoubleValue>)Parameters["TransportationCosts"]; }
63    }
64    public FixedValueParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
65      get { return (FixedValueParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
66    }
67    public OptionalValueParameter<IItem> BestKnownSolutionParameter {
68      get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; }
69    }
70    public OptionalValueParameter<StringArray> EquipmentNamesParameter {
71      get { return (OptionalValueParameter<StringArray>)Parameters["EquipmentNames"]; }
72    }
73    public OptionalValueParameter<StringArray> LocationNamesParameter {
74      get { return (OptionalValueParameter<StringArray>)Parameters["LocationNames"]; }
75    }
76    #endregion
77
78    #region Properties
79    public DoubleMatrix Weights {
80      get { return WeightsParameter.Value; }
81      set { WeightsParameter.Value = value; }
82    }
83    public DoubleMatrix Distances {
84      get { return DistancesParameter.Value; }
85      set { DistancesParameter.Value = value; }
86    }
87    public DoubleMatrix InstallationCosts {
88      get { return InstallationCostsParameter.Value; }
89      set { InstallationCostsParameter.Value = value; }
90    }
91    public DoubleArray Demands {
92      get { return DemandsParameter.Value; }
93      set { DemandsParameter.Value = value; }
94    }
95    public DoubleArray Capacities {
96      get { return CapacitiesParameter.Value; }
97      set { CapacitiesParameter.Value = value; }
98    }
99    public double TransportationCosts {
100      get { return TransportationCostsParameter.Value.Value; }
101      set { TransportationCostsParameter.Value.Value = value; }
102    }
103    public double OverbookedCapacityPenalty {
104      get { return TransportationCostsParameter.Value.Value; }
105      set { TransportationCostsParameter.Value.Value = value; }
106    }
107    public StringArray EquipmentNames {
108      get { return EquipmentNamesParameter.Value; }
109      set { EquipmentNamesParameter.Value = value; }
110    }
111    public StringArray LocationNames {
112      get { return LocationNamesParameter.Value; }
113      set { LocationNamesParameter.Value = value; }
114    }
115    #endregion
116
117    public BestGQAPSolutionAnalyzer BestSolutionAnalyzer {
118      get { return Operators.OfType<BestGQAPSolutionAnalyzer>().FirstOrDefault(); }
119    }
120    public GQAPSolutionArchiveAnalyzer SolutionArchiveAnalyzer {
121      get { return Operators.OfType<GQAPSolutionArchiveAnalyzer>().FirstOrDefault(); }
122    }
123
124    [StorableConstructor]
125    private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
126    private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
127      : base(original, cloner) {
128      AttachEventHandlers();
129    }
130    public GeneralizedQuadraticAssignmentProblem()
131      : base(new GQAPEvaluator(), new RandomSolutionCreator()) {
132      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments.", new DoubleMatrix()));
133      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed.", new DoubleMatrix()));
134      Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j", new DoubleMatrix()));
135      Parameters.Add(new FixedValueParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already.", new DoubleValue(1)));
136      Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality.", new DoubleValue(1000)));
137      Parameters.Add(new ValueParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments.", new DoubleArray()));
138      Parameters.Add(new ValueParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations.", new DoubleArray()));
139      Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", "The best known solution (if available)", null));
140      Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
141      Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
142
143      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
144      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
145      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
146
147      Weights = new DoubleMatrix(5, 5);
148      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
149      Weights[0, 1] = Weights[1, 0] = Weights[0, 2] = Weights[2, 0] = Weights[0, 3] = Weights[3, 0] = Weights[0, 4] = Weights[4, 0] = 10;
150      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
151      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
152      Weights[3, 4] = Weights[4, 3] = 2.5;
153
154      Distances = new DoubleMatrix(3, 3);
155      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
156      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
157      Distances[0, 2] = Distances[2, 0] = 2;
158
159      InstallationCosts = new DoubleMatrix(5, 3);
160
161      TransportationCosts = 1;
162
163      Demands = new DoubleArray(5);
164      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
165
166      Capacities = new DoubleArray(3);
167      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
168
169      SolutionCreator.AssignmentParameter.ActualName = "Assignment";
170
171      InitializeOperators();
172      AttachEventHandlers();
173    }
174
175    public override IDeepCloneable Clone(Cloner cloner) {
176      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
177    }
178
179    #region Events
180    protected override void OnOperatorsChanged() {
181      base.OnOperatorsChanged();
182      Parameterize();
183    }
184    protected override void OnEvaluatorChanged() {
185      base.OnEvaluatorChanged();
186      Parameterize();
187      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
188    }
189    protected override void OnSolutionCreatorChanged() {
190      base.OnSolutionCreatorChanged();
191      Parameterize();
192      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
193    }
194
195    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
196      Parameterize();
197    }
198    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
199      Parameterize();
200    }
201    #endregion
202
203    #region Helpers
204    [StorableHook(HookType.AfterDeserialization)]
205    private void AfterDeserialization() {
206      AttachEventHandlers();
207    }
208
209    private void AttachEventHandlers() {
210      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
211      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
212    }
213
214    private void InitializeOperators() {
215      Operators.Add(new BestGQAPSolutionAnalyzer());
216      Operators.Add(new GQAPSolutionArchiveAnalyzer());
217      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
218      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
219      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
220      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>());
221      Parameterize();
222    }
223
224    private void Parameterize() {
225      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
226      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
227      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
228      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
229      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
230      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
231      Evaluator.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
232
233      SolutionCreator.DemandsParameter.ActualName = DemandsParameter.Name;
234      SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
235
236      foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
237        op.DemandsParameter.ActualName = DemandsParameter.Name;
238      }
239      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
240        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
241        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
242      }
243      foreach (var op in Operators.OfType<IGQAPEvaluationOperator>()) {
244        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
245        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
246        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
247        op.WeightsParameter.ActualName = WeightsParameter.Name;
248        op.DistancesParameter.ActualName = DistancesParameter.Name;
249        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
250        op.DemandsParameter.ActualName = DemandsParameter.Name;
251        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
252      }
253      foreach (var op in Operators.OfType<IGQAPLocalImprovementOperator>()) {
254        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
255        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
256        op.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
257        op.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
258        op.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
259        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
260        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
261        op.WeightsParameter.ActualName = WeightsParameter.Name;
262        op.DistancesParameter.ActualName = DistancesParameter.Name;
263        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
264        op.DemandsParameter.ActualName = DemandsParameter.Name;
265        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
266      }
267      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
268        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
269      }
270      foreach (var op in Operators.OfType<IGQAPMerger>()) {
271        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
272        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
273        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
274      }
275      foreach (var op in Operators.OfType<IGQAPMoveOperator>()) {
276        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
277      }
278      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
279        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
280      }
281
282      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
283        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
284        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
285      }
286      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
287        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
288      }
289
290      if (BestSolutionAnalyzer != null) {
291        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
292        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
293        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
294        BestSolutionAnalyzer.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
295        BestSolutionAnalyzer.DemandsParameter.ActualName = DemandsParameter.Name;
296        BestSolutionAnalyzer.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
297        BestSolutionAnalyzer.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
298        BestSolutionAnalyzer.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
299        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
300        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
301        BestSolutionAnalyzer.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
302        BestSolutionAnalyzer.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
303        BestSolutionAnalyzer.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
304        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
305        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
306        BestSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
307        BestSolutionAnalyzer.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name;
308        BestSolutionAnalyzer.LocationNamesParameter.ActualName = LocationNamesParameter.Name;
309      }
310      if (SolutionArchiveAnalyzer != null) {
311        SolutionArchiveAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
312        SolutionArchiveAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
313        SolutionArchiveAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
314        SolutionArchiveAnalyzer.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
315        SolutionArchiveAnalyzer.DemandsParameter.ActualName = DemandsParameter.Name;
316        SolutionArchiveAnalyzer.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
317        SolutionArchiveAnalyzer.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
318        SolutionArchiveAnalyzer.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
319        SolutionArchiveAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
320        SolutionArchiveAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
321        SolutionArchiveAnalyzer.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
322        SolutionArchiveAnalyzer.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
323        SolutionArchiveAnalyzer.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
324        SolutionArchiveAnalyzer.ResultsParameter.ActualName = "Results";
325        SolutionArchiveAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
326        SolutionArchiveAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
327        SolutionArchiveAnalyzer.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name;
328        SolutionArchiveAnalyzer.LocationNamesParameter.ActualName = LocationNamesParameter.Name;
329      }
330    }
331    #endregion
332  }
333}
Note: See TracBrowser for help on using the repository browser.