Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • updated GQAP
File size: 12.1 KB
Line 
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
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, IIntegerVectorCreator>, 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 FixedValueParameter<DoubleValue> TransportationCostsParameter {
56      get { return (FixedValueParameter<DoubleValue>)Parameters["TransportationCosts"]; }
57    }
58    public ValueParameter<DoubleArray> DemandsParameter {
59      get { return (ValueParameter<DoubleArray>)Parameters["Demands"]; }
60    }
61    public ValueParameter<DoubleArray> CapacitiesParameter {
62      get { return (ValueParameter<DoubleArray>)Parameters["Capacities"]; }
63    }
64    public OptionalValueParameter<IItem> BestKnownSolutionParameter {
65      get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; }
66    }
67    #endregion
68
69    #region Properties
70    public DoubleMatrix Weights {
71      get { return WeightsParameter.Value; }
72      set { WeightsParameter.Value = value; }
73    }
74    public DoubleMatrix Distances {
75      get { return DistancesParameter.Value; }
76      set { DistancesParameter.Value = value; }
77    }
78    public DoubleMatrix InstallationCosts {
79      get { return InstallationCostsParameter.Value; }
80      set { InstallationCostsParameter.Value = value; }
81    }
82    public double TransportationCosts {
83      get { return TransportationCostsParameter.Value.Value; }
84      set { TransportationCostsParameter.Value.Value = value; }
85    }
86    public DoubleArray Demands {
87      get { return DemandsParameter.Value; }
88      set { DemandsParameter.Value = value; }
89    }
90    public DoubleArray Capacities {
91      get { return CapacitiesParameter.Value; }
92      set { CapacitiesParameter.Value = value; }
93    }
94    #endregion
95
96    [Storable]
97    private BestGQAPSolutionAnalyzer bestSolutionAnalyzer;
98    public BestGQAPSolutionAnalyzer BestSolutionAnalyzer {
99      get { return bestSolutionAnalyzer; }
100      set { bestSolutionAnalyzer = value; }
101    }
102
103    [StorableConstructor]
104    private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
105    private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
106      : base(original, cloner) {
107      bestSolutionAnalyzer = cloner.Clone(original.bestSolutionAnalyzer);
108      AttachEventHandlers();
109    }
110    public GeneralizedQuadraticAssignmentProblem()
111      : base(new GQAPEvaluator(), new UniformRandomIntegerVectorCreator()) {
112      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments.", new DoubleMatrix()));
113      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed.", new DoubleMatrix()));
114      Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j", new DoubleMatrix()));
115      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)));
116      Parameters.Add(new ValueParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments.", new DoubleArray()));
117      Parameters.Add(new ValueParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations.", new DoubleArray()));
118      Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", "The best known solution (if available)", null));
119      Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
120      Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
121
122      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
123      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
124      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
125
126      Weights = new DoubleMatrix(5, 5);
127      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
128      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;
129      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
130      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
131      Weights[3, 4] = Weights[4, 3] = 2.5;
132
133      Distances = new DoubleMatrix(3, 3);
134      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
135      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
136      Distances[0, 2] = Distances[2, 0] = 2;
137
138      InstallationCosts = new DoubleMatrix(5, 3);
139
140      TransportationCosts = 1;
141
142      Demands = new DoubleArray(5);
143      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
144
145      Capacities = new DoubleArray(3);
146      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
147
148      Parameterize();
149
150      InitializeOperators();
151      AttachEventHandlers();
152    }
153
154    public override IDeepCloneable Clone(Cloner cloner) {
155      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
156    }
157
158    #region Events
159    protected override void OnOperatorsChanged() {
160      base.OnOperatorsChanged();
161      Parameterize();
162    }
163    protected override void OnEvaluatorChanged() {
164      base.OnEvaluatorChanged();
165      Parameterize();
166      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
167    }
168    protected override void OnSolutionCreatorChanged() {
169      base.OnSolutionCreatorChanged();
170      Parameterize();
171      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
172    }
173
174    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
175      Parameterize();
176    }
177    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
178      Parameterize();
179    }
180    #endregion
181
182    #region Helpers
183    [StorableHook(HookType.AfterDeserialization)]
184    private void AfterDeserializationHook() {
185      // BackwardsCompatibility3.3
186      #region Backwards compatible code, remove with 3.4
187      if (!Parameters.ContainsKey("EquipmentNames"))
188        Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
189      if (!Parameters.ContainsKey("LocationNames"))
190        Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
191      #endregion
192      AttachEventHandlers();
193    }
194
195    private void AttachEventHandlers() {
196      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
197      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
198    }
199
200    private void InitializeOperators() {
201      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
202      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
203      Operators.Add(new BestGQAPSolutionAnalyzer());
204      Parameterize();
205    }
206
207    private void Parameterize() {
208      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
209      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
210      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
211      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
212      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
213      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
214      Evaluator.AssignmentParameter.ActualName = "Assignment";
215
216      SolutionCreator.LengthParameter.Value = new IntValue(Demands.Length);
217      SolutionCreator.MinimumParameter.Value = new IntValue(0);
218      SolutionCreator.MaximumParameter.Value = new IntValue(Capacities.Length);
219      SolutionCreator.IntegerVectorParameter.ActualName = "Assignment";
220
221      foreach (IIntegerVectorCrossover op in Operators.OfType<IIntegerVectorCrossover>()) {
222        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
223        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
224      }
225      foreach (IIntegerVectorManipulator op in Operators.OfType<IIntegerVectorManipulator>()) {
226        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
227      }
228
229      if (BestSolutionAnalyzer != null) {
230        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
231        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
232        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
233        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
234        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
235        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
236        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
237      }
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.