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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
33 | [Item("GQAPEvaluator", "Evaluates solutions to the generalized quadratic assignment problem.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class GQAPEvaluator : SingleSuccessorOperator, IGQAPEvaluator {
|
---|
36 |
|
---|
37 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
38 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
39 | }
|
---|
40 | public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
41 | get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
44 | get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
45 | }
|
---|
46 | public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
47 | get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
48 | }
|
---|
49 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
50 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
51 | }
|
---|
52 | public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
|
---|
53 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
56 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
59 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
62 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
65 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
66 | }
|
---|
67 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
68 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
71 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | protected GQAPEvaluator(bool deserializing) : base(deserializing) { }
|
---|
76 | protected GQAPEvaluator(GQAPEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
77 | public GQAPEvaluator()
|
---|
78 | : base() {
|
---|
79 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the solution."));
|
---|
80 | Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", "The quality regarding the flow-distance criteria."));
|
---|
81 | Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", "The quality regarding the installation costs."));
|
---|
82 | Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", "The sum of the overbooked capacities relative to the capacity of each location."));
|
---|
83 | Parameters.Add(new ValueLookupParameter<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 or distance matrix already."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
|
---|
85 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
|
---|
86 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
|
---|
87 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j"));
|
---|
88 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
|
---|
89 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
|
---|
90 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The vector that encodes the assignment."));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
94 | return new GQAPEvaluator(this, cloner);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static double Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
|
---|
98 | DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
|
---|
99 | DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty) {
|
---|
100 | double flowDistanceQuality, installationQuality, overbookedCapacity;
|
---|
101 | Evaluate(assignment, weights, distances, installCosts, demands, capacities,
|
---|
102 | out flowDistanceQuality, out installationQuality, out overbookedCapacity);
|
---|
103 | return GetCombinedQuality(flowDistanceQuality, installationQuality, overbookedCapacity,
|
---|
104 | transportationCosts.Value, overbookedCapacityPenalty.Value);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public static void Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
|
---|
108 | DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
|
---|
109 | out double flowDistanceQuality, out double installationQuality, out double overbookedCapacity) {
|
---|
110 | flowDistanceQuality = 0;
|
---|
111 | installationQuality = 0;
|
---|
112 | int len = assignment.Length;
|
---|
113 | var slack = (DoubleArray)capacities.Clone();
|
---|
114 | for (int i = 0; i < len; i++) {
|
---|
115 | installationQuality += installCosts[i, assignment[i]];
|
---|
116 | for (int j = 0; j < len; j++) {
|
---|
117 | flowDistanceQuality += weights[i, j] * distances[assignment[i], assignment[j]];
|
---|
118 | }
|
---|
119 | slack[assignment[i]] -= demands[i];
|
---|
120 | }
|
---|
121 | overbookedCapacity = slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static double GetCombinedQuality(double flowDistanceQuality, double installationQuality, double overbookedCapacity,
|
---|
125 | double transportationCosts, double overbookedCapacityPenalty) {
|
---|
126 | return installationQuality
|
---|
127 | + transportationCosts * flowDistanceQuality
|
---|
128 | + overbookedCapacityPenalty * overbookedCapacity;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override IOperation Apply() {
|
---|
132 | var assignment = AssignmentParameter.ActualValue;
|
---|
133 | var weights = WeightsParameter.ActualValue;
|
---|
134 | var distances = DistancesParameter.ActualValue;
|
---|
135 | var installCosts = InstallationCostsParameter.ActualValue;
|
---|
136 | var demands = DemandsParameter.ActualValue;
|
---|
137 | var capacities = CapacitiesParameter.ActualValue;
|
---|
138 | var transportCosts = TransportationCostsParameter.ActualValue.Value;
|
---|
139 | double penalty = OverbookedCapacityPenaltyParameter.ActualValue.Value;
|
---|
140 |
|
---|
141 | if (weights.Rows != weights.Columns || distances.Rows != distances.Columns
|
---|
142 | || weights.Rows != installCosts.Rows || distances.Rows != installCosts.Columns
|
---|
143 | || demands.Length != weights.Rows || capacities.Length != distances.Rows)
|
---|
144 | throw new InvalidOperationException("ERROR: The problem configuration is not valid! Check the sizes of the weights (NxN), distances (MxM) and installation costs (NxM) matrices as well as the length of the demand (N) and capacities (M) vectors.");
|
---|
145 |
|
---|
146 | double flowDistanceQuality, installationQuality, overbookedCapacity;
|
---|
147 | Evaluate(assignment, weights, distances, installCosts, demands, capacities,
|
---|
148 | out flowDistanceQuality, out installationQuality, out overbookedCapacity);
|
---|
149 |
|
---|
150 | FlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality);
|
---|
151 | InstallationQualityParameter.ActualValue = new DoubleValue(installationQuality);
|
---|
152 | OverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity);
|
---|
153 |
|
---|
154 | QualityParameter.ActualValue = new DoubleValue(GetCombinedQuality(flowDistanceQuality, installationQuality, overbookedCapacity, transportCosts, penalty));
|
---|
155 | return base.Apply();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|