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 |
|
---|
22 | using System.Drawing;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
34 | [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.")]
|
---|
35 | [Creatable("Problems")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class GeneralizedQuadraticAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<IGQAPEvaluator, IIntegerVectorCreator> {
|
---|
38 | public override Image ItemImage {
|
---|
39 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | #region Parameter Properties
|
---|
43 | public ValueParameter<DoubleMatrix> WeightsParameter {
|
---|
44 | get { return (ValueParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
45 | }
|
---|
46 | public ValueParameter<DoubleMatrix> DistancesParameter {
|
---|
47 | get { return (ValueParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
48 | }
|
---|
49 | public ValueParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
50 | get { return (ValueParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
51 | }
|
---|
52 | public FixedValueParameter<DoubleValue> TransportationCostsParameter {
|
---|
53 | get { return (FixedValueParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
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 OptionalValueParameter<IItem> BestKnownSolutionParameter {
|
---|
62 | get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region Properties
|
---|
68 | public DoubleMatrix Weights {
|
---|
69 | get { return WeightsParameter.Value; }
|
---|
70 | set { WeightsParameter.Value = value; }
|
---|
71 | }
|
---|
72 | public DoubleMatrix Distances {
|
---|
73 | get { return DistancesParameter.Value; }
|
---|
74 | set { DistancesParameter.Value = value; }
|
---|
75 | }
|
---|
76 | public DoubleMatrix InstallationCosts {
|
---|
77 | get { return InstallationCostsParameter.Value; }
|
---|
78 | set { InstallationCostsParameter.Value = value; }
|
---|
79 | }
|
---|
80 | public DoubleValue TransportationCosts {
|
---|
81 | get { return TransportationCostsParameter.Value; }
|
---|
82 | set { TransportationCostsParameter.Value = value; }
|
---|
83 | }
|
---|
84 | public DoubleArray Demands {
|
---|
85 | get { return DemandsParameter.Value; }
|
---|
86 | set { DemandsParameter.Value = value; }
|
---|
87 | }
|
---|
88 | public DoubleArray Capacities {
|
---|
89 | get { return CapacitiesParameter.Value; }
|
---|
90 | set { CapacitiesParameter.Value = value; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | [Storable]
|
---|
96 | private BestGQAPSolutionAnalyzer bestSolutionAnalyzer;
|
---|
97 | public BestGQAPSolutionAnalyzer BestSolutionAnalyzer {
|
---|
98 | get { return bestSolutionAnalyzer; }
|
---|
99 | set { bestSolutionAnalyzer = value; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [StorableConstructor]
|
---|
103 | private GeneralizedQuadraticAssignmentProblem(bool deserializing) : base(deserializing) { }
|
---|
104 | private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner)
|
---|
105 | : base(original, cloner) {
|
---|
106 | bestSolutionAnalyzer = cloner.Clone(original.bestSolutionAnalyzer);
|
---|
107 | AttachEventHandlers();
|
---|
108 | }
|
---|
109 | public GeneralizedQuadraticAssignmentProblem()
|
---|
110 | : base(new GQAPEvaluator(), new UniformRandomIntegerVectorCreator()) {
|
---|
111 | Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments.", new DoubleMatrix()));
|
---|
112 | Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed.", new DoubleMatrix()));
|
---|
113 | Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j", new DoubleMatrix()));
|
---|
114 | 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)));
|
---|
115 | Parameters.Add(new ValueParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments.", new DoubleArray()));
|
---|
116 | Parameters.Add(new ValueParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations.", new DoubleArray()));
|
---|
117 | Parameters.Add(new OptionalValueParameter<IItem>("BestKnownSolution", "The best known solution (if available)", null));
|
---|
118 |
|
---|
119 | Weights = new DoubleMatrix(5, 5);
|
---|
120 | Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
|
---|
121 | 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;
|
---|
122 | Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
|
---|
123 | Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
|
---|
124 | Weights[3, 4] = Weights[4, 3] = 2.5;
|
---|
125 |
|
---|
126 | Distances = new DoubleMatrix(3, 3);
|
---|
127 | Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
|
---|
128 | Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
|
---|
129 | Distances[0, 2] = Distances[2, 0] = 2;
|
---|
130 |
|
---|
131 | InstallationCosts = new DoubleMatrix(3, 3);
|
---|
132 |
|
---|
133 | TransportationCosts = new DoubleValue(1);
|
---|
134 |
|
---|
135 | Demands = new DoubleArray(5);
|
---|
136 | Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
|
---|
137 |
|
---|
138 | Capacities = new DoubleArray(3);
|
---|
139 | Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
|
---|
140 |
|
---|
141 | ParameterizeSolutionCreator();
|
---|
142 | ParameterizeEvaluator();
|
---|
143 |
|
---|
144 | InitializeOperators();
|
---|
145 | AttachEventHandlers();
|
---|
146 | }
|
---|
147 |
|
---|
148 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
149 | return new GeneralizedQuadraticAssignmentProblem(this, cloner);
|
---|
150 | }
|
---|
151 |
|
---|
152 | #region Events
|
---|
153 | // TODO: Add your event handlers here
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | #region Helpers
|
---|
157 | [StorableHook(HookType.AfterDeserialization)]
|
---|
158 | private void AfterDeserializationHook() {
|
---|
159 | AttachEventHandlers();
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void AttachEventHandlers() {
|
---|
163 | // TODO: Add event handlers to the parameters here
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void InitializeOperators() {
|
---|
167 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
|
---|
168 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
169 | Operators.Add(new BestGQAPSolutionAnalyzer());
|
---|
170 | ParameterizeAnalyzers();
|
---|
171 | ParameterizeOperators();
|
---|
172 | }
|
---|
173 | private void ParameterizeAnalyzers() {
|
---|
174 | if (BestSolutionAnalyzer != null) {
|
---|
175 | BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
176 | BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
177 | BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
178 | BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
179 | BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
180 | BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
181 | BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | private void ParameterizeOperators() {
|
---|
185 | foreach (IIntegerVectorCrossover op in Operators.OfType<IIntegerVectorCrossover>()) {
|
---|
186 | op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
187 | op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
188 | }
|
---|
189 | foreach (IIntegerVectorManipulator op in Operators.OfType<IIntegerVectorManipulator>()) {
|
---|
190 | op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | private void ParameterizeSolutionCreator() {
|
---|
194 | SolutionCreator.LengthParameter.Value = new IntValue(Demands.Length);
|
---|
195 | SolutionCreator.MinimumParameter.Value = new IntValue(0);
|
---|
196 | SolutionCreator.MaximumParameter.Value = new IntValue(Capacities.Length);
|
---|
197 | SolutionCreator.IntegerVectorParameter.ActualName = "Assignment";
|
---|
198 | }
|
---|
199 | private void ParameterizeEvaluator() {
|
---|
200 | Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
201 | Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
202 | Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
|
---|
203 | Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
|
---|
204 | Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
|
---|
205 | Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
|
---|
206 | Evaluator.AssignmentParameter.ActualName = "Assignment";
|
---|
207 | }
|
---|
208 | #endregion
|
---|
209 | }
|
---|
210 | }
|
---|