Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7063 was 6956, checked in by abeham, 13 years ago

#1614

  • readded new simpler branch
File size: 10.4 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.Drawing;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace 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 double TransportationCosts {
81      get { return TransportationCostsParameter.Value.Value; }
82      set { TransportationCostsParameter.Value.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      WeightsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
120      DistancesParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
121      InstallationCostsParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
122
123      Weights = new DoubleMatrix(5, 5);
124      Weights[0, 0] = Weights[1, 1] = Weights[2, 2] = Weights[3, 3] = Weights[4, 4] = 0;
125      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;
126      Weights[1, 2] = Weights[2, 1] = Weights[1, 3] = Weights[3, 1] = Weights[1, 4] = Weights[4, 1] = 5;
127      Weights[2, 3] = Weights[3, 2] = Weights[2, 4] = Weights[4, 2] = 7.5;
128      Weights[3, 4] = Weights[4, 3] = 2.5;
129
130      Distances = new DoubleMatrix(3, 3);
131      Distances[0, 0] = Distances[1, 1] = Distances[2, 2] = 0;
132      Distances[0, 1] = Distances[1, 0] = Distances[1, 2] = Distances[2, 1] = 1;
133      Distances[0, 2] = Distances[2, 0] = 2;
134
135      InstallationCosts = new DoubleMatrix(3, 3);
136
137      TransportationCosts = 1;
138
139      Demands = new DoubleArray(5);
140      Demands[0] = 2; Demands[1] = 1; Demands[2] = 3; Demands[3] = 1; Demands[4] = 1;
141
142      Capacities = new DoubleArray(3);
143      Capacities[0] = 4; Capacities[1] = 1; Capacities[2] = 4;
144
145      ParameterizeSolutionCreator();
146      ParameterizeEvaluator();
147
148      InitializeOperators();
149      AttachEventHandlers();
150    }
151
152    public override IDeepCloneable Clone(Cloner cloner) {
153      return new GeneralizedQuadraticAssignmentProblem(this, cloner);
154    }
155
156    #region Events
157    // TODO: Add your event handlers here
158    #endregion
159
160    #region Helpers
161    [StorableHook(HookType.AfterDeserialization)]
162    private void AfterDeserializationHook() {
163      AttachEventHandlers();
164    }
165
166    private void AttachEventHandlers() {
167      // TODO: Add event handlers to the parameters here
168    }
169
170    private void InitializeOperators() {
171      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
172      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
173      Operators.Add(new BestGQAPSolutionAnalyzer());
174      ParameterizeAnalyzers();
175      ParameterizeOperators();
176    }
177    private void ParameterizeAnalyzers() {
178      if (BestSolutionAnalyzer != null) {
179        BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
180        BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
181        BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
182        BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
183        BestSolutionAnalyzer.ResultsParameter.ActualName = "Results";
184        BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
185        BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
186      }
187    }
188    private void ParameterizeOperators() {
189      foreach (IIntegerVectorCrossover op in Operators.OfType<IIntegerVectorCrossover>()) {
190        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
191        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
192      }
193      foreach (IIntegerVectorManipulator op in Operators.OfType<IIntegerVectorManipulator>()) {
194        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
195      }
196    }
197    private void ParameterizeSolutionCreator() {
198      SolutionCreator.LengthParameter.Value = new IntValue(Demands.Length);
199      SolutionCreator.MinimumParameter.Value = new IntValue(0);
200      SolutionCreator.MaximumParameter.Value = new IntValue(Capacities.Length);
201      SolutionCreator.IntegerVectorParameter.ActualName = "Assignment";
202    }
203    private void ParameterizeEvaluator() {
204      Evaluator.WeightsParameter.ActualName = WeightsParameter.Name;
205      Evaluator.DistancesParameter.ActualName = DistancesParameter.Name;
206      Evaluator.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
207      Evaluator.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
208      Evaluator.DemandsParameter.ActualName = DemandsParameter.Name;
209      Evaluator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
210      Evaluator.AssignmentParameter.ActualName = "Assignment";
211    }
212    #endregion
213  }
214}
Note: See TracBrowser for help on using the repository browser.