Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • readded new simpler branch
File size: 6.0 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
32  [Item("GQAPEvaluator", "Evaluates solutions to the generalized quadratic assignment problem.")]
33  [StorableClass]
34  public class GQAPEvaluator : SingleSuccessorOperator, IGQAPEvaluator {
35
36    public ILookupParameter<DoubleValue> QualityParameter {
37      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
38    }
39    public ILookupParameter<DoubleValue> InfeasibilityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Infeasibility"]; }
41    }
42    public IValueLookupParameter<DoubleValue> PenaltyParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["Penalty"]; }
44    }
45    public ILookupParameter<DoubleMatrix> WeightsParameter {
46      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
47    }
48    public ILookupParameter<DoubleMatrix> DistancesParameter {
49      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
50    }
51    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
52      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
53    }
54    public ILookupParameter<DoubleValue> TransportationCostsParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
56    }
57    public ILookupParameter<DoubleArray> DemandsParameter {
58      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
59    }
60    public ILookupParameter<DoubleArray> CapacitiesParameter {
61      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
62    }
63    public ILookupParameter<IntegerVector> AssignmentParameter {
64      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
65    }
66
67    [StorableConstructor]
68    protected GQAPEvaluator(bool deserializing) : base(deserializing) { }
69    protected GQAPEvaluator(GQAPEvaluator original, Cloner cloner) : base(original, cloner) { }
70    public GQAPEvaluator()
71      : base() {
72      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality a.k.a. fitness of the solution."));
73      Parameters.Add(new LookupParameter<DoubleValue>("Infeasibility", "The infeasibility describes the sum of the overbooked capacities."));
74      Parameters.Add(new ValueLookupParameter<DoubleValue>("Penalty", "The multiplier for the constraint violation when added to the quality.", new DoubleValue(1000)));
75      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
76      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
77      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j"));
78      Parameters.Add(new LookupParameter<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."));
79      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
80      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
81      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The vector that encodes the assignment."));
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new GQAPEvaluator(this, cloner);
86    }
87
88    public override IOperation Apply() {
89      IntegerVector assignment = AssignmentParameter.ActualValue;
90      DoubleMatrix weights = WeightsParameter.ActualValue;
91      DoubleMatrix distances = DistancesParameter.ActualValue;
92      DoubleMatrix installCosts = InstallationCostsParameter.ActualValue;
93      double transportCosts = TransportationCostsParameter.ActualValue.Value;
94      DoubleArray demands = DemandsParameter.ActualValue;
95      DoubleArray capacities = (DoubleArray)CapacitiesParameter.ActualValue.Clone();
96      double penalty = PenaltyParameter.ActualValue.Value;
97      double quality = 0;
98      double infeasibility = 0;
99
100      int len = assignment.Length;
101      for (int i = 0; i < len; i++) {
102        quality += installCosts[i, assignment[i]];
103        for (int j = 0; j < len; j++) {
104          quality += transportCosts * weights[i, j] * distances[assignment[i], assignment[j]];
105        }
106        capacities[assignment[i]] -= demands[i];
107      }
108
109      infeasibility = -capacities.Where(x => x < 0).Sum();
110
111      InfeasibilityParameter.ActualValue = new DoubleValue(infeasibility);
112      QualityParameter.ActualValue = new DoubleValue(quality + penalty * infeasibility);
113      return base.Apply();
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.