Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • renamed AssemblyInfo frame files to proper name
  • fixed discovery of move operators
  • fixed a bug in the move evaluator
  • fixed a bug in the DiscreteLocationCrossover
File size: 15.1 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    #endregion
71
72    #region Properties
73    public DoubleMatrix Weights {
74      get { return WeightsParameter.Value; }
75      set { WeightsParameter.Value = value; }
76    }
77    public DoubleMatrix Distances {
78      get { return DistancesParameter.Value; }
79      set { DistancesParameter.Value = value; }
80    }
81    public DoubleMatrix InstallationCosts {
82      get { return InstallationCostsParameter.Value; }
83      set { InstallationCostsParameter.Value = value; }
84    }
85    public DoubleArray Demands {
86      get { return DemandsParameter.Value; }
87      set { DemandsParameter.Value = value; }
88    }
89    public DoubleArray Capacities {
90      get { return CapacitiesParameter.Value; }
91      set { CapacitiesParameter.Value = value; }
92    }
93    public double TransportationCosts {
94      get { return TransportationCostsParameter.Value.Value; }
95      set { TransportationCostsParameter.Value.Value = value; }
96    }
97    public double OverbookedCapacityPenalty {
98      get { return TransportationCostsParameter.Value.Value; }
99      set { TransportationCostsParameter.Value.Value = value; }
100    }
101    #endregion
102
103    [Storable]
104    private BestGQAPSolutionAnalyzer bestSolutionAnalyzer;
105    public BestGQAPSolutionAnalyzer BestSolutionAnalyzer {
106      get { return bestSolutionAnalyzer; }
107      set { bestSolutionAnalyzer = value; }
108    }
109
110    [StorableConstructor]
111    private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
112    private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
113      : base(original, cloner) {
114      bestSolutionAnalyzer = cloner.Clone(original.bestSolutionAnalyzer);
115      AttachEventHandlers();
116    }
117    public GeneralizedQuadraticAssignmentProblem()
118      : base(new GQAPEvaluator(), new RandomSolutionCreator()) {
119      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments.", new DoubleMatrix()));
120      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed.", new DoubleMatrix()));
121      Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j", new DoubleMatrix()));
122      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)));
123      Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality.", new DoubleValue(1000)));
124      Parameters.Add(new ValueParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments.", new DoubleArray()));
125      Parameters.Add(new ValueParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations.", new DoubleArray()));
126      Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", "The best known solution (if available)", null));
127      Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
128      Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
129
130      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
131      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
132      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
133
134      Weights = new DoubleMatrix(5, 5);
135      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
136      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;
137      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
138      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
139      Weights[3, 4] = Weights[4, 3] = 2.5;
140
141      Distances = new DoubleMatrix(3, 3);
142      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
143      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
144      Distances[0, 2] = Distances[2, 0] = 2;
145
146      InstallationCosts = new DoubleMatrix(5, 3);
147
148      TransportationCosts = 1;
149
150      Demands = new DoubleArray(5);
151      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
152
153      Capacities = new DoubleArray(3);
154      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
155
156      SolutionCreator.AssignmentParameter.ActualName = "Assignment";
157      Parameterize();
158
159      InitializeOperators();
160      AttachEventHandlers();
161    }
162
163    public override IDeepCloneable Clone(Cloner cloner) {
164      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
165    }
166
167    #region Events
168    protected override void OnOperatorsChanged() {
169      base.OnOperatorsChanged();
170      Parameterize();
171    }
172    protected override void OnEvaluatorChanged() {
173      base.OnEvaluatorChanged();
174      Parameterize();
175      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
176    }
177    protected override void OnSolutionCreatorChanged() {
178      base.OnSolutionCreatorChanged();
179      Parameterize();
180      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
181    }
182
183    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
184      Parameterize();
185    }
186    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
187      Parameterize();
188    }
189    #endregion
190
191    #region Helpers
192    [StorableHook(HookType.AfterDeserialization)]
193    private void AfterDeserialization() {
194      AttachEventHandlers();
195    }
196
197    private void AttachEventHandlers() {
198      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
199      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
200    }
201
202    private void InitializeOperators() {
203      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
204      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
205      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
206      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>());
207      Operators.Add(new BestGQAPSolutionAnalyzer());
208      Parameterize();
209    }
210
211    private void Parameterize() {
212      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
213      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
214      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
215      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
216      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
217      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
218      Evaluator.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
219
220      SolutionCreator.DemandsParameter.ActualName = DemandsParameter.Name;
221      SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
222
223      foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
224        op.DemandsParameter.ActualName = DemandsParameter.Name;
225      }
226      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
227        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
228        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
229      }
230      foreach (var op in Operators.OfType<IGQAPEvaluationOperator>()) {
231        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
232        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
233        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
234        op.WeightsParameter.ActualName = WeightsParameter.Name;
235        op.DistancesParameter.ActualName = DistancesParameter.Name;
236        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
237        op.DemandsParameter.ActualName = DemandsParameter.Name;
238        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
239      }
240      foreach (var op in Operators.OfType<IGQAPLocalImprovementOperator>()) {
241        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
242        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
243        op.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName;
244        op.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName;
245        op.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName;
246        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
247        op.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name;
248        op.WeightsParameter.ActualName = WeightsParameter.Name;
249        op.DistancesParameter.ActualName = DistancesParameter.Name;
250        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
251        op.DemandsParameter.ActualName = DemandsParameter.Name;
252        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
253      }
254      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
255        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
256      }
257      foreach (var op in Operators.OfType<IGQAPMerger>()) {
258        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
259        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
260        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
261      }
262      foreach (var op in Operators.OfType<IGQAPMoveOperator>()) {
263        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
264      }
265      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
266        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
267      }
268
269      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
270        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
271        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
272      }
273      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
274        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
275      }
276
277      if (BestSolutionAnalyzer != null) {
278        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
279        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
280        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
281        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
282        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
283        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
284        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
285      }
286    }
287    #endregion
288  }
289}
Note: See TracBrowser for help on using the repository browser.