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

Last change on this file since 7319 was 7319, checked in by abeham, 10 years ago

#1614: worked on GQAP (added operators, assignment view)

File size: 12.9 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, 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      SolutionCreator.IntegerVectorParameter.ActualName = "Assignment";
149      Parameterize();
150
151      InitializeOperators();
152      AttachEventHandlers();
153    }
154
155    public override IDeepCloneable Clone(Cloner cloner) {
156      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
157    }
158
159    #region Events
160    protected override void OnOperatorsChanged() {
161      base.OnOperatorsChanged();
162      Parameterize();
163    }
164    protected override void OnEvaluatorChanged() {
165      base.OnEvaluatorChanged();
166      Parameterize();
167      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
168    }
169    protected override void OnSolutionCreatorChanged() {
170      base.OnSolutionCreatorChanged();
171      Parameterize();
172      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
173    }
174
175    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
176      Parameterize();
177    }
178    private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
179      Parameterize();
180    }
181    #endregion
182
183    #region Helpers
184    [StorableHook(HookType.AfterDeserialization)]
185    private void AfterDeserializationHook() {
186      // BackwardsCompatibility3.3
187      #region Backwards compatible code, remove with 3.4
188      if (!Parameters.ContainsKey("EquipmentNames"))
189        Parameters.Add(new OptionalValueParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments.", null, false));
190      if (!Parameters.ContainsKey("LocationNames"))
191        Parameters.Add(new OptionalValueParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations.", null, false));
192      #endregion
193      AttachEventHandlers();
194    }
195
196    private void AttachEventHandlers() {
197      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
198      SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
199    }
200
201    private void InitializeOperators() {
202      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
203      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
204      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
205      Operators.Add(new BestGQAPSolutionAnalyzer());
206      Parameterize();
207    }
208
209    private void Parameterize() {
210      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
211      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
212      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
213      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
214      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
215      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
216      Evaluator.AssignmentParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
217
218      SolutionCreator.LengthParameter.Value = new IntValue(Demands.Length);
219      SolutionCreator.MinimumParameter.Value = new IntValue(0);
220      SolutionCreator.MaximumParameter.Value = new IntValue(Capacities.Length);
221
222      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
223        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
224        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
225      }
226      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
227        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
228      }
229      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
230        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
231        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
232      }
233      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
234        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
235      }
236      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
237        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
238      }
239      foreach (var op in Operators.OfType<IEquipmentAwareGQAPManipulator>()) {
240        op.DemandsParameter.ActualName = DemandsParameter.Name;
241      }
242
243      if (BestSolutionAnalyzer != null) {
244        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
245        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
246        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
247        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
248        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
249        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
250        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
251      }
252    }
253    #endregion
254  }
255}
Note: See TracBrowser for help on using the repository browser.