Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/DistanceMeasures/DistanceMatrixMeasure.cs @ 8670

Last change on this file since 8670 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 4.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using HeuristicLab.Common;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9using HeuristicLab.PDPSimulation.DomainModel;
10
11namespace HeuristicLab.PDPSimulation.DistanceMeasures {
12  [StorableClass]
13  public class DistanceMatrixMeasure: DistanceMeasure {
14    private ValueParameter<DoubleMatrix> DistanceMatrixParameter {
15      get { return (ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
16    }
17
18    private ValueParameter<IntMatrix> PointMappingParameter {
19      get { return (ValueParameter<IntMatrix>)Parameters["PointMapping"]; }
20    }
21
22    private OptionalValueParameter<StringArray> PointNamesParameter {
23      get { return (OptionalValueParameter<StringArray>)Parameters["PointNames"]; }
24    }
25
26    public DoubleMatrix DistanceMatrix {
27      get {
28        return DistanceMatrixParameter.Value;
29      }
30      set {
31        DistanceMatrixParameter.Value = value;
32      }
33    }
34
35    public IntMatrix PointMapping {
36      get {
37        return PointMappingParameter.Value;
38      }
39      set {
40        PointMappingParameter.Value = value;
41      }
42    }
43
44    public StringArray PointNames {
45      get {
46        return PointNamesParameter.Value;
47      }
48      set {
49        PointNamesParameter.Value = value;
50      }
51    }
52
53    public DistanceMatrixMeasure()
54      : base() {
55        Parameters.Add(new ValueParameter<DoubleMatrix>("DistanceMatrix", new DoubleMatrix()));
56        Parameters.Add(new ValueParameter<IntMatrix>("PointMapping", new IntMatrix()));
57        Parameters.Add(new OptionalValueParameter<StringArray>("PointNames"));
58
59        DistanceMatrixParameter.GetsCollected = false;
60        PointMappingParameter.GetsCollected = false;
61        PointNamesParameter.GetsCollected = false;
62    }
63    [StorableConstructor]
64    protected DistanceMatrixMeasure(bool deserializing) : base(deserializing) {
65    }
66    protected DistanceMatrixMeasure(DistanceMatrixMeasure original, Cloner cloner)
67      : base(original, cloner) {
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new DistanceMatrixMeasure(this, cloner);
72    }
73
74    public override bool DiversionSupported {
75      get { return false; }
76    }
77
78    public int GetMapping(double x, double y) {
79      IntMatrix pointMapping = PointMappingParameter.Value;
80      int index = -1;
81
82      int ix = (int)Math.Round(x);
83      int iy = (int)Math.Round(y);
84
85      int i = 0;
86      while (index < 0 && i < pointMapping.Rows) {
87        if (pointMapping[i, 0] == ix && pointMapping[i, 1] == iy) {
88          index = i;
89        }         
90        i++;
91      }
92
93      return index;
94    }
95
96    protected double GetEuclideanDistance(double sourceX, double sourceY, double destX, double destY) {
97      double vx = destX - sourceX;
98      double vy = destY - sourceY;
99      double length = Math.Sqrt(vx * vx + vy * vy);
100
101      return length;
102    }
103
104    public override double GetDistance(double sourceX, double sourceY,
105      double currentX, double currentY,
106      double destX, double destY) {
107      double total = GetEuclideanDistance(sourceX, sourceY, destX, destY);
108      double driven = GetEuclideanDistance(sourceX, sourceY, currentX, currentY);
109
110      double completed = 1.0;
111      if(total != 0)
112        completed = driven / total;
113
114      return GetDistance(sourceX, sourceY, destX, destY) * (1.0 - completed);
115    }
116
117    public override double GetDistance(double sourceX, double sourceY,
118     double destX, double destY) {
119      DoubleMatrix distanceMatrix = DistanceMatrixParameter.Value;
120
121      int i = GetMapping(sourceX, sourceY);
122      int j = GetMapping(destX, destY);
123
124      return distanceMatrix[i, j];
125    }
126
127    public override void Move(double sourceX, double sourceY,
128      double currentX, double currentY,
129      double destX, double destY, double time, Vehicle.MoveInformation moveInfo,
130      out double newPosX, out double newPosY, out double length) {
131     
132      length = GetDistance(sourceX, sourceY, currentX, currentY, destX, destY);
133      if (length < time) {
134        newPosX = destX;
135        newPosY = destY;
136      } else {
137        double step = GetEuclideanDistance(sourceX, sourceY, destX, destY) /
138          GetDistance(sourceX, sourceY, destX, destY);
139        double vx = destX - currentX;
140        double vy = destY - currentY;
141        double vl = GetEuclideanDistance(currentX, currentY, destX, destY);
142        vx = (vx / vl) * time * step;
143        vy = (vy / vl) * time * step;
144
145        newPosX = currentX + vx;
146        newPosY = currentY + vy;
147        length = time;
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.