Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: Worked on GQAP

File size: 12.2 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 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 RandomSolutionCreator()) {
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.AssignmentParameter.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      AttachEventHandlers();
187    }
188
189    private void AttachEventHandlers() {
190      Evaluator.QualityParameter.ActualNameChanged += new System.EventHandler(Evaluator_QualityParameter_ActualNameChanged);
191      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
192    }
193
194    private void InitializeOperators() {
195      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
196      Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());
197      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
198      Operators.Add(new BestGQAPSolutionAnalyzer());
199      Parameterize();
200    }
201
202    private void Parameterize() {
203      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
204      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
205      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
206      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
207      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
208      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
209      Evaluator.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
210
211      SolutionCreator.DemandsParameter.ActualName = DemandsParameter.Name;
212      SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
213
214      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
215        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
216        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
217      }
218      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
219        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
220      }
221      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
222        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
223        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
224      }
225      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
226        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
227      }
228      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
229        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
230      }
231      foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
232        op.DemandsParameter.ActualName = DemandsParameter.Name;
233      }
234
235      if (BestSolutionAnalyzer != null) {
236        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
237        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
238        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
239        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
240        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
241        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
242        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
243      }
244    }
245    #endregion
246  }
247}
Note: See TracBrowser for help on using the repository browser.