Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPNMoveEvaluator.cs @ 7407

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

#1614: worked on GQAP

File size: 7.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.Collections.Generic;
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("GQAPNMoveEvaluator", "Evaluates an n-move.")]
34  [StorableClass]
35  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator {
36
37    public ILookupParameter<IntegerVector> AssignmentParameter {
38      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
39    }
40    public ILookupParameter<NMove> MoveParameter {
41      get { return (ILookupParameter<NMove>)Parameters["Move"]; }
42    }
43    public ILookupParameter<DoubleValue> QualityParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46    public ILookupParameter<DoubleValue> MoveQualityParameter {
47      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
48    }
49    public ILookupParameter<DoubleValue> MoveInfeasibilityParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters["MoveInfeasibility"]; }
51    }
52    public ILookupParameter<DoubleMatrix> WeightsParameter {
53      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
54    }
55    public ILookupParameter<DoubleMatrix> DistancesParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
57    }
58    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
59      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
60    }
61    public ILookupParameter<DoubleValue> TransportationCostsParameter {
62      get { return (ILookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
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
71    [StorableConstructor]
72    protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { }
73    protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
74    public GQAPNMoveEvaluator()
75      : base() {
76      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The equipment-location assignment vector."));
77      Parameters.Add(new LookupParameter<NMove>("Move", "The move to perform."));
78      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
79      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move if it would be applied."));
80      Parameters.Add(new LookupParameter<DoubleValue>("MoveInfeasibility", "The infeasibility of the move if it would be applied."));
81      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
82      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
83      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j"));
84      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."));
85      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
86      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new GQAPNMoveEvaluator(this, cloner);
91    }
92
93    public static DoubleValue Evaluate(NMove move, IntegerVector assignment, DoubleValue quality,
94                                  DoubleArray demands, DoubleArray capacities,
95                                  DoubleMatrix weights, DoubleMatrix distances,
96                                  DoubleMatrix installationCosts,
97                                  DoubleValue transportationCosts,
98                                  out DoubleValue moveInfeasibility) {
99      double moveQuality = quality.Value;
100      int moves = move.N;
101      var slack = (DoubleArray)capacities.Clone();
102      Dictionary<int, int> moving = new Dictionary<int, int>();
103      for (int i = 0; i < moves; i++) moving.Add(move.Equipments[i], move.Locations[i]);
104
105      for (int i = 0; i < moves; i++) {
106        int equip = move.Equipments[i];
107        int newLoc = move.Locations[i];
108        moveQuality -= installationCosts[equip, assignment[equip]];
109        moveQuality += installationCosts[equip, newLoc];
110        for (int j = 0; j < assignment.Length; j++) {
111          if (!moving.ContainsKey(j)) {
112            moveQuality += transportationCosts.Value * weights[equip, j] * (distances[newLoc, assignment[j]] - distances[assignment[equip], assignment[j]]);
113            moveQuality += transportationCosts.Value * weights[j, equip] * (distances[assignment[j], newLoc] - distances[assignment[j], assignment[equip]]);
114            if (i == 0) { // only once for each untouched equipment deduct the demand from the capacity
115              slack[assignment[j]] -= demands[j];
116            }
117          } else {
118            moveQuality += transportationCosts.Value * weights[equip, j] * (distances[newLoc, moving[j]] - distances[assignment[equip], assignment[j]]);
119            moveQuality += transportationCosts.Value * weights[j, equip] * (distances[moving[j], newLoc] - distances[assignment[j], assignment[equip]]);
120          }
121        }
122        slack[newLoc] -= demands[equip];
123      }
124
125      moveInfeasibility = new DoubleValue(-slack.Where(x => x < 0).Sum());
126      return new DoubleValue(moveQuality);
127
128    }
129
130    public override IOperation Apply() {
131      DoubleValue moveInfeasibility;
132      MoveQualityParameter.ActualValue = Evaluate(MoveParameter.ActualValue,
133                 AssignmentParameter.ActualValue, QualityParameter.ActualValue,
134                 DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
135                 WeightsParameter.ActualValue, DistancesParameter.ActualValue,
136                 InstallationCostsParameter.ActualValue,
137                 TransportationCostsParameter.ActualValue, out moveInfeasibility);
138      MoveInfeasibilityParameter.ActualValue = moveInfeasibility;
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.