Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPEvaluator.cs @ 13398

Last change on this file since 13398 was 7970, checked in by abeham, 13 years ago

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 9.6 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  [Item("GQAPEvaluator", "Base class for solution evaluators for the generalized quadratic assignment problem.")]
34  [StorableClass]
35  public abstract class GQAPEvaluator : SingleSuccessorOperator, IGQAPEvaluator,
36  IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
37  IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, IAssignmentAwareGQAPOperator,
38  IExpectedRandomQualityAwareGQAPOperator {
39
40    #region Parameter Descriptions
41    public static readonly string QualityDescription = "The quality of the solution.";
42    public static readonly string FlowDistanceQualityDescription = "The quality regarding the flow-distance criteria.";
43    public static readonly string InstallationQualityDescription = "The quality regarding the installation costs.";
44    public static readonly string OverbookedCapacityDescription = "The sum of the overbooked capacities relative to the capacity of each location.";
45    #endregion
46
47    public ILookupParameter<BoolValue> MaximizationParameter {
48      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
49    }
50    public ILookupParameter<DoubleValue> QualityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
55    }
56    public ILookupParameter<DoubleValue> InstallationQualityParameter {
57      get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
58    }
59    public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
60      get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
61    }
62    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
63      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
64    }
65    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
66      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
67    }
68    public ILookupParameter<DoubleMatrix> WeightsParameter {
69      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
70    }
71    public ILookupParameter<DoubleMatrix> DistancesParameter {
72      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
73    }
74    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
75      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
76    }
77    public ILookupParameter<DoubleArray> DemandsParameter {
78      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
79    }
80    public ILookupParameter<DoubleArray> CapacitiesParameter {
81      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
82    }
83    public ILookupParameter<IntegerVector> AssignmentParameter {
84      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
85    }
86
87    [StorableConstructor]
88    protected GQAPEvaluator(bool deserializing) : base(deserializing) { }
89    protected GQAPEvaluator(GQAPEvaluator original, Cloner cloner) : base(original, cloner) { }
90    public GQAPEvaluator() : base() {
91      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
92      Parameters.Add(new LookupParameter<DoubleValue>("Quality", QualityDescription));
93      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", FlowDistanceQualityDescription));
94      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", InstallationQualityDescription));
95      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", OverbookedCapacityDescription));
96      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
97      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
98      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
99      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
100      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
101      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
102      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
103      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
104    }
105
106    public virtual double Evaluate(IntegerVector assignment, DoubleMatrix weights,
107      DoubleMatrix distances, DoubleMatrix installCosts, DoubleArray demands,
108      DoubleArray capacities, double transportationCosts, double expectedRandomQuality) {
109      double flowDistanceQuality, installationQuality, overbookedCapacity;
110      Evaluate(assignment, weights, distances, installCosts, demands, capacities,
111        out flowDistanceQuality, out installationQuality, out overbookedCapacity);
112      return GetFitness(flowDistanceQuality, installationQuality, overbookedCapacity,
113        transportationCosts, expectedRandomQuality);
114    }
115
116    public virtual void Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
117      DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
118      out double flowDistanceQuality, out double installationQuality,
119      out double overbookedCapacity) {
120      flowDistanceQuality = 0;
121      installationQuality = 0;
122      int len = assignment.Length;
123      var slack = (DoubleArray)capacities.Clone();
124      for (int i = 0; i < len; i++) {
125        installationQuality += installCosts[i, assignment[i]];
126        for (int j = 0; j < len; j++) {
127          flowDistanceQuality += weights[i, j] * distances[assignment[i], assignment[j]];
128        }
129        slack[assignment[i]] -= demands[i];
130      }
131      overbookedCapacity = EvaluateOverbooking(slack, capacities);
132    }
133
134    public virtual double EvaluateOverbooking(DoubleArray slack, DoubleArray capacities) {
135      return slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -100.0 * x.V / capacities[x.Index]).Sum();
136    }
137
138    public abstract double GetFitness(double flowDistanceQuality,
139      double installationQuality, double overbookedCapacity,
140      double transportationCosts, double expectedRandomQuality);
141
142    public override IOperation Apply() {
143      var assignment = AssignmentParameter.ActualValue;
144      var weights = WeightsParameter.ActualValue;
145      var distances = DistancesParameter.ActualValue;
146      var installCosts = InstallationCostsParameter.ActualValue;
147      var demands = DemandsParameter.ActualValue;
148      var capacities = CapacitiesParameter.ActualValue;
149      var transportCosts = TransportationCostsParameter.ActualValue.Value;
150      var expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value;
151
152      if (weights.Rows != weights.Columns || distances.Rows != distances.Columns
153        || weights.Rows != installCosts.Rows || distances.Rows != installCosts.Columns
154        || demands.Length != weights.Rows || capacities.Length != distances.Rows)
155        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.");
156
157      double flowDistanceQuality, installationQuality, overbookedCapacity;
158      Evaluate(assignment, weights, distances, installCosts, demands, capacities,
159        out flowDistanceQuality, out installationQuality, out overbookedCapacity);
160
161      FlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality);
162      InstallationQualityParameter.ActualValue = new DoubleValue(installationQuality);
163      OverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity);
164
165      QualityParameter.ActualValue = new DoubleValue(GetFitness(flowDistanceQuality, installationQuality, overbookedCapacity, transportCosts, expectedRandomQuality));
166      return base.Apply();
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.