我尝试使用基于 .dot 文件的 RoadModel
计算到送货位置的距离。但是,使用 Point.distance(currPos, destPos) 计算的距离给出的结果似乎不合适。我将其与 MoveProgress
(RoadModel.moveTo(...)
) 的距离进行了比较,发现点距离大约增加了 100 倍。
例如,使用 rinsim-example-4.1.1.jar
中的 Taxi 示例以及以下 Taxi
类实现
class Taxi extends Vehicle implements SimulatorUser {
private RandomGenerator rng;
Taxi(Point startPosition, int capacity) {
super(VehicleDTO.builder().capacity(capacity).startPosition(startPosition)
.speed(1000).build());
}
@Override
public void setSimulator(SimulatorAPI api) {
rng = api.getRandomGenerator();
}
@Override
protected void tickImpl(TimeLapse time) {
final RoadModel rm = getRoadModel();
Point start = rm.getPosition(this);
MoveProgress mp;
if (rm.getDestination(this) == null) {
mp = rm.moveTo(this, rm.getRandomPosition(rng), time);
}
if (rm.getPosition(this).equals(rm.getDestination(this)))
mp = rm.moveTo(this, rm.getRandomPosition(rng), time);
else
mp = rm.moveTo(this, rm.getDestination(this), time);
Point after = rm.getPosition(this);
System.out.println("Travelled distance");
System.out.println(mp.distance());
System.out.println("Straight line distance");
System.out.println(Point.distance(start, after));
}
那么行进距离约为 0.277,而计算出的直线距离约为 27.7。是否存在 100 倍差异的原因,或者是否有其他方法可以以与 MoveProgress
相同的度量来获取两点之间的距离?
最佳答案
通过 Point.distance(..,..)
和 MoveProgress
获得的距离存在差异的原因是后者采用任何可用的 ConnectionData考虑到前者计算两点之间的鸟类飞行距离(忽略任何连接相关信息)。
事实证明,TaxiExample
中使用的图表有许多连接,其中连接长度被覆盖。正如您提到的,这个长度看起来好像相差了 100 倍,但必须更彻底地研究它才能确定正确的值。
目前,解决方法是忽略所有 ConnectionData
。使用以下代码来解析图表:
final Graph<LengthData> originalGraph =
DotGraphIO.getLengthGraphIO().read(Paths.get("path/to/graph.dot"));
final Graph<LengthData> newGraph = new TableGraph<>();
for (final Connection<LengthData> conn : originalGraph.getConnections()) {
newGraph.addConnection(conn.from(), conn.to());
}
这将构造一个新图表,没有与任何连接关联的任何连接数据。
关于java - RinSim 4.1.1 中的距离计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38964565/