Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • reworked parameterization (one interface for every parameter resp. parameter group)
  • unified parameter descriptions
File size: 17.4 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
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
42    }
43
44    public string Filename { get; set; }
45
46    #region Parameter Descriptions
47    public static readonly string MaximizationDescription = "False if the fitness function should be minimized (default) or otherwise True if it should be maximized.";
48    public static readonly string WeightsDescription = "The weights matrix describes the flows between the equipments.";
49    public static readonly string DistancesDescription = "The distances matrix describes the distances between the locations at which the equipment can be installed.";
50    public static readonly string InstallationCostsDescription = "The installation costs matrix describes the installation costs of installing equipment i at location j";
51    public static readonly string DemandsDescription = "The demands vector describes the space requirements of the equipments.";
52    public static readonly string CapacitiesDescription = "The capacities vector describes the available space at the locations.";
53    public static readonly string TransportationCostsDescription = "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.";
54    public static readonly string OverbookedCapacityPenaltyDescription = "The multiplier for the constraint violation when added to the quality.";
55    public static readonly string BestKnownSolutionDescription = "The best known solution (if available)";
56    public static readonly string EquipmentNamesDescription = "Optional: A list of names that describes the equipments.";
57    public static readonly string LocationNamesDescription = "Optional: A list of names that describes the locations.";
58    #endregion
59
60    #region Parameter Properties
61    public ValueParameter<DoubleMatrix> WeightsParameter {
62      get { return (ValueParameter<DoubleMatrix>)Parameters["Weights"]; }
63    }
64    public ValueParameter<DoubleMatrix> DistancesParameter {
65      get { return (ValueParameter<DoubleMatrix>)Parameters["Distances"]; }
66    }
67    public ValueParameter<DoubleMatrix> InstallationCostsParameter {
68      get { return (ValueParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
69    }
70    public ValueParameter<DoubleArray> DemandsParameter {
71      get { return (ValueParameter<DoubleArray>)Parameters["Demands"]; }
72    }
73    public ValueParameter<DoubleArray> CapacitiesParameter {
74      get { return (ValueParameter<DoubleArray>)Parameters["Capacities"]; }
75    }
76    public FixedValueParameter<DoubleValue> TransportationCostsParameter {
77      get { return (FixedValueParameter<DoubleValue>)Parameters["TransportationCosts"]; }
78    }
79    public FixedValueParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
80      get { return (FixedValueParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
81    }
82    public OptionalValueParameter<IItem> BestKnownSolutionParameter {
83      get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; }
84    }
85    public OptionalValueParameter<StringArray> EquipmentNamesParameter {
86      get { return (OptionalValueParameter<StringArray>)Parameters["EquipmentNames"]; }
87    }
88    public OptionalValueParameter<StringArray> LocationNamesParameter {
89      get { return (OptionalValueParameter<StringArray>)Parameters["LocationNames"]; }
90    }
91    #endregion
92
93    #region Properties
94    public DoubleMatrix Weights {
95      get { return WeightsParameter.Value; }
96      set { WeightsParameter.Value = value; }
97    }
98    public DoubleMatrix Distances {
99      get { return DistancesParameter.Value; }
100      set { DistancesParameter.Value = value; }
101    }
102    public DoubleMatrix InstallationCosts {
103      get { return InstallationCostsParameter.Value; }
104      set { InstallationCostsParameter.Value = value; }
105    }
106    public DoubleArray Demands {
107      get { return DemandsParameter.Value; }
108      set { DemandsParameter.Value = value; }
109    }
110    public DoubleArray Capacities {
111      get { return CapacitiesParameter.Value; }
112      set { CapacitiesParameter.Value = value; }
113    }
114    public double TransportationCosts {
115      get { return TransportationCostsParameter.Value.Value; }
116      set { TransportationCostsParameter.Value.Value = value; }
117    }
118    public double OverbookedCapacityPenalty {
119      get { return TransportationCostsParameter.Value.Value; }
120      set { TransportationCostsParameter.Value.Value = value; }
121    }
122    public StringArray EquipmentNames {
123      get { return EquipmentNamesParameter.Value; }
124      set { EquipmentNamesParameter.Value = value; }
125    }
126    public StringArray LocationNames {
127      get { return LocationNamesParameter.Value; }
128      set { LocationNamesParameter.Value = value; }
129    }
130    #endregion
131
132    public BestGQAPSolutionAnalyzer BestSolutionAnalyzer {
133      get { return Operators.OfType<BestGQAPSolutionAnalyzer>().FirstOrDefault(); }
134    }
135    public GQAPSolutionArchiveAnalyzer SolutionArchiveAnalyzer {
136      get { return Operators.OfType<GQAPSolutionArchiveAnalyzer>().FirstOrDefault(); }
137    }
138
139    [StorableConstructor]
140    private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
141    private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
142      : base(original, cloner) {
143      AttachEventHandlers();
144    }
145    public GeneralizedQuadraticAssignmentProblem()
146      : base(new GQAPEvaluator(), new RandomSolutionCreator()) {
147      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", WeightsDescription, new DoubleMatrix()));
148      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", DistancesDescription, new DoubleMatrix()));
149      Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", InstallationCostsDescription, new DoubleMatrix()));
150      Parameters.Add(new FixedValueParameter<DoubleValue>("TransportationCosts", TransportationCostsDescription, new DoubleValue(1)));
151      Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", OverbookedCapacityPenaltyDescription, new DoubleValue(1000)));
152      Parameters.Add(new ValueParameter<DoubleArray>("Demands", DemandsDescription, new DoubleArray()));
153      Parameters.Add(new ValueParameter<DoubleArray>("Capacities", CapacitiesDescription, new DoubleArray()));
154      Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", BestKnownSolutionDescription, null));
155      Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", EquipmentNamesDescription, null, false));
156      Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", LocationNamesDescription, null, false));
157
158      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
159      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
160      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
161
162      Weights = new DoubleMatrix(5, 5);
163      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
164      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;
165      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
166      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
167      Weights[3, 4] = Weights[4, 3] = 2.5;
168
169      Distances = new DoubleMatrix(3, 3);
170      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
171      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
172      Distances[0, 2] = Distances[2, 0] = 2;
173
174      InstallationCosts = new DoubleMatrix(5, 3);
175
176      TransportationCosts = 1;
177
178      Demands = new DoubleArray(5);
179      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
180
181      Capacities = new DoubleArray(3);
182      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
183
184      SolutionCreator.AssignmentParameter.ActualName = "Assignment";
185
186      InitializeOperators();
187      AttachEventHandlers();
188    }
189
190    public override IDeepCloneable Clone(Cloner cloner) {
191      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
192    }
193
194    #region Events
195    protected override void OnOperatorsChanged() {
196      base.OnOperatorsChanged();
197      Parameterize();
198    }
199    protected override void OnEvaluatorChanged() {
200      base.OnEvaluatorChanged();
201      Parameterize();
202      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
203    }
204    protected override void OnSolutionCreatorChanged() {
205      base.OnSolutionCreatorChanged();
206      Parameterize();
207      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
208    }
209
210    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
211      Parameterize();
212    }
213    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
214      Parameterize();
215    }
216    #endregion
217
218    #region Helpers
219    [StorableHook(HookType.AfterDeserialization)]
220    private void AfterDeserialization() {
221      AttachEventHandlers();
222    }
223
224    private void AttachEventHandlers() {
225      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
226      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
227    }
228
229    private void InitializeOperators() {
230      Operators.Add(new BestGQAPSolutionAnalyzer());
231      Operators.Add(new GQAPSolutionArchiveAnalyzer());
232      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
233      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
234      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
235      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>());
236      Parameterize();
237    }
238
239    private void Parameterize() {
240
241      var operators = Operators.Union(new IOperator[] { SolutionCreator, Evaluator }).ToArray();
242
243      foreach (var op in operators.OfType<IAssignmentAwareGQAPOperator>()) {
244        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
245      }
246      foreach (var op in operators.OfType<IAssignmentsAwareGQAPOperator>()) {
247        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
248      }
249      foreach (var op in operators.OfType<IBestKnownQualityAwareGQAPOperator>()) {
250        op.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
251      }
252      foreach (var op in operators.OfType<IBestKnownSolutionAwareGQAPOperator>()) {
253        op.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
254      }
255      foreach (var op in operators.OfType<ICapacitiesAwareGQAPOperator>()) {
256        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
257      }
258      foreach (var op in operators.OfType<IDemandsAwareGQAPOperator>()) {
259        op.DemandsParameter.ActualName = DemandsParameter.Name;
260      }
261      foreach (var op in operators.OfType<IDistancesAwareGQAPOperator>()) {
262        op.DistancesParameter.ActualName = DistancesParameter.Name;
263      }
264      foreach (var op in operators.OfType<IEquipmentNamesAwareGQAPOperator>()) {
265        op.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name;
266      }
267      foreach (var op in operators.OfType<IGQAPCrossover>()) {
268        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
269        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
270      }
271      foreach (var op in operators.OfType<IInstallationCostsAwareGQAPOperator>()) {
272        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
273      }
274      foreach (var op in operators.OfType<ILocationNamesAwareGQAPOperator>()) {
275        op.LocationNamesParameter.ActualName = LocationNamesParameter.Name;
276      }
277      var moveEvaluator = operators.OfType<IGQAPMoveEvaluator>().FirstOrDefault();
278      foreach (var op in operators.OfType<IGQAPMoveEvaluator>()) { // synchronize all move evaluators
279        if (moveEvaluator != null) {
280          op.MoveQualityParameter.ActualName = moveEvaluator.MoveQualityParameter.ActualName;
281          op.MoveFlowDistanceQualityParameter.ActualName = moveEvaluator.MoveFlowDistanceQualityParameter.ActualName;
282          op.MoveInstallationQualityParameter.ActualName = moveEvaluator.MoveInstallationQualityParameter.ActualName;
283          op.MoveOverbookedCapacityParameter.ActualName = moveEvaluator.MoveOverbookedCapacityParameter.ActualName;
284        }
285      }
286      foreach (var op in operators.OfType<IMoveQualityAwareGQAPOperator>()) {
287        if (moveEvaluator != null) {
288          op.MoveQualityParameter.ActualName = moveEvaluator.MoveQualityParameter.ActualName;
289          op.MoveFlowDistanceQualityParameter.ActualName = moveEvaluator.MoveFlowDistanceQualityParameter.ActualName;
290          op.MoveInstallationQualityParameter.ActualName = moveEvaluator.MoveInstallationQualityParameter.ActualName;
291          op.MoveOverbookedCapacityParameter.ActualName = moveEvaluator.MoveOverbookedCapacityParameter.ActualName;
292        }
293        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
294      }
295      foreach (var op in operators.OfType<IOverbookedCapacityPenaltyAwareGQAPOperator>()) {
296        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
297      }
298      foreach (var op in operators.OfType<IQualitiesAwareGQAPOperator>()) {
299        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
300        op.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
301        op.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
302        op.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
303        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
304      }
305      foreach (var op in operators.OfType<IQualityAwareGQAPOperator>()) {
306        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
307        op.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
308        op.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
309        op.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
310        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
311      }
312      foreach (var op in operators.OfType<ITransportationCostsAwareGQAPOperator>()) {
313        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
314      }
315      foreach (var op in operators.OfType<IWeightsAwareGQAPOperator>()) {
316        op.WeightsParameter.ActualName = WeightsParameter.Name;
317      }
318
319      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
320        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
321        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
322      }
323      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
324        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
325      }
326    }
327    #endregion
328  }
329}
Note: See TracBrowser for help on using the repository browser.