Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7415 was 7415, checked in by abeham, 12 years ago

#1614: improved results output of GQAP

File size: 16.7 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>().First(); }
119    }
120
121    [StorableConstructor]
122    private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
123    private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
124      : base(original, cloner) {
125      AttachEventHandlers();
126    }
127    public GeneralizedQuadraticAssignmentProblem()
128      : base(new GQAPEvaluator(), new RandomSolutionCreator()) {
129      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments.", new DoubleMatrix()));
130      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed.", new DoubleMatrix()));
131      Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j", new DoubleMatrix()));
132      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)));
133      Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality.", new DoubleValue(1000)));
134      Parameters.Add(new ValueParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments.", new DoubleArray()));
135      Parameters.Add(new ValueParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations.", new DoubleArray()));
136      Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", "The best known solution (if available)", null));
137      Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
138      Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
139
140      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
141      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
142      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
143
144      Weights = new DoubleMatrix(5, 5);
145      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
146      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;
147      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
148      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
149      Weights[3, 4] = Weights[4, 3] = 2.5;
150
151      Distances = new DoubleMatrix(3, 3);
152      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
153      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
154      Distances[0, 2] = Distances[2, 0] = 2;
155
156      InstallationCosts = new DoubleMatrix(5, 3);
157
158      TransportationCosts = 1;
159
160      Demands = new DoubleArray(5);
161      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
162
163      Capacities = new DoubleArray(3);
164      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
165
166      SolutionCreator.AssignmentParameter.ActualName = "Assignment";
167      Parameterize();
168
169      InitializeOperators();
170      AttachEventHandlers();
171    }
172
173    public override IDeepCloneable Clone(Cloner cloner) {
174      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
175    }
176
177    #region Events
178    protected override void OnOperatorsChanged() {
179      base.OnOperatorsChanged();
180      Parameterize();
181    }
182    protected override void OnEvaluatorChanged() {
183      base.OnEvaluatorChanged();
184      Parameterize();
185      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
186    }
187    protected override void OnSolutionCreatorChanged() {
188      base.OnSolutionCreatorChanged();
189      Parameterize();
190      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
191    }
192
193    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
194      Parameterize();
195    }
196    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
197      Parameterize();
198    }
199    #endregion
200
201    #region Helpers
202    [StorableHook(HookType.AfterDeserialization)]
203    private void AfterDeserialization() {
204      AttachEventHandlers();
205    }
206
207    private void AttachEventHandlers() {
208      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
209      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
210    }
211
212    private void InitializeOperators() {
213      Operators.Add(new BestGQAPSolutionAnalyzer());
214      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
215      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
216      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
217      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>());
218      Parameterize();
219    }
220
221    private void Parameterize() {
222      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
223      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
224      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
225      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
226      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
227      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
228      Evaluator.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
229
230      SolutionCreator.DemandsParameter.ActualName = DemandsParameter.Name;
231      SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
232
233      foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
234        op.DemandsParameter.ActualName = DemandsParameter.Name;
235      }
236      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
237        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
238        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
239      }
240      foreach (var op in Operators.OfType<IGQAPEvaluationOperator>()) {
241        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
242        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
243        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
244        op.WeightsParameter.ActualName = WeightsParameter.Name;
245        op.DistancesParameter.ActualName = DistancesParameter.Name;
246        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
247        op.DemandsParameter.ActualName = DemandsParameter.Name;
248        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
249      }
250      foreach (var op in Operators.OfType<IGQAPLocalImprovementOperator>()) {
251        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
252        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
253        op.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
254        op.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
255        op.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
256        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
257        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
258        op.WeightsParameter.ActualName = WeightsParameter.Name;
259        op.DistancesParameter.ActualName = DistancesParameter.Name;
260        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
261        op.DemandsParameter.ActualName = DemandsParameter.Name;
262        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
263      }
264      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
265        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
266      }
267      foreach (var op in Operators.OfType<IGQAPMerger>()) {
268        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
269        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
270        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
271      }
272      foreach (var op in Operators.OfType<IGQAPMoveOperator>()) {
273        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
274      }
275      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
276        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
277      }
278
279      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
280        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
281        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
282      }
283      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
284        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
285      }
286
287      if (BestSolutionAnalyzer != null) {
288        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
289        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
290        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
291        BestSolutionAnalyzer.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
292        BestSolutionAnalyzer.DemandsParameter.ActualName = DemandsParameter.Name;
293        BestSolutionAnalyzer.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
294        BestSolutionAnalyzer.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
295        BestSolutionAnalyzer.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
296        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
297        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
298        BestSolutionAnalyzer.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
299        BestSolutionAnalyzer.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
300        BestSolutionAnalyzer.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
301        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
302        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
303        BestSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
304        BestSolutionAnalyzer.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name;
305        BestSolutionAnalyzer.LocationNamesParameter.ActualName = LocationNamesParameter.Name;
306      }
307    }
308    #endregion
309  }
310}
Note: See TracBrowser for help on using the repository browser.