Browse Source

double -> float 节约空间

master
修宁 6 months ago
parent
commit
143de653d2
  1. 8
      servo/src/main/java/com/galaxis/rcs/plan/path/AStarNodeState.java
  2. 16
      servo/src/main/java/com/galaxis/rcs/plan/path/AStarPathPlanner.java
  3. 6
      servo/src/main/java/com/galaxis/rcs/plan/path/GraphInitializer.java
  4. 8
      servo/src/main/java/com/galaxis/rcs/plan/path/NavigationGraph.java
  5. 14
      servo/src/main/java/com/galaxis/rcs/plan/path/NavigationNode.java
  6. 2
      servo/src/main/java/com/galaxis/rcs/plan/path/PathSegment.java
  7. 0
      yvan-rcs-dev/.lck

8
servo/src/main/java/com/galaxis/rcs/plan/path/AStarNodeState.java

@ -6,8 +6,8 @@ package com.galaxis.rcs.plan.path;
public record AStarNodeState(
String nodeId, // 当前节点ID
int direction, // 当前方向 (0,90,180,270)
double gCost, // 实际代价
double hCost, // 启发式代价
float gCost, // 实际代价
float hCost, // 启发式代价
AStarNodeState parent // 父节点
) implements Comparable<AStarNodeState> {
@ -17,13 +17,13 @@ public record AStarNodeState(
}
// 总代价
public double fCost() {
public float fCost() {
return gCost + hCost;
}
@Override
public int compareTo(AStarNodeState other) {
return Double.compare(this.fCost(), other.fCost());
return Float.compare(this.fCost(), other.fCost());
}
}

16
servo/src/main/java/com/galaxis/rcs/plan/path/AStarPathPlanner.java

@ -26,7 +26,7 @@ public class AStarPathPlanner {
PriorityQueue<AStarNodeState> openSet = new PriorityQueue<>();
// 状态管理
Map<String, Double> gScoreMap = new HashMap<>();
Map<String, Float> gScoreMap = new HashMap<>();
Map<String, AStarNodeState> cameFrom = new HashMap<>();
// 初始化起点
@ -39,7 +39,7 @@ public class AStarPathPlanner {
);
openSet.add(start);
gScoreMap.put(start.stateKey(), 0.0);
gScoreMap.put(start.stateKey(), 0.0f);
while (!openSet.isEmpty()) {
AStarNodeState current = openSet.poll();
@ -55,12 +55,12 @@ public class AStarPathPlanner {
PathSegment segment = graph.getPathSegment(current.nodeId(), neighbor.id());
if (segment == null) continue;
double moveCost = segment.distance();
double tentativeGCost = current.gCost() + moveCost;
float moveCost = segment.distance();
float tentativeGCost = current.gCost() + moveCost;
String neighborKey = neighbor.id() + ":" + current.direction();
// 发现更好路径
if (tentativeGCost < gScoreMap.getOrDefault(neighborKey, Double.MAX_VALUE)) {
if (tentativeGCost < gScoreMap.getOrDefault(neighborKey, Float.MAX_VALUE)) {
AStarNodeState neighborState = new AStarNodeState(
neighbor.id(),
current.direction(),
@ -80,12 +80,12 @@ public class AStarPathPlanner {
if (currentNode != null && currentNode.rotatable()) {
for (int rotation : new int[]{90, -90}) {
int newDirection = (current.direction() + rotation + 360) % 360;
double rotateCost = 1.0; // 旋转代价
double tentativeGCost = current.gCost() + rotateCost;
float rotateCost = 1.0f; // 旋转代价
float tentativeGCost = current.gCost() + rotateCost;
String rotatedKey = current.nodeId() + ":" + newDirection;
// 发现更好路径
if (tentativeGCost < gScoreMap.getOrDefault(rotatedKey, Double.MAX_VALUE)) {
if (tentativeGCost < gScoreMap.getOrDefault(rotatedKey, Float.MAX_VALUE)) {
AStarNodeState rotatedState = new AStarNodeState(
current.nodeId(),
newDirection,

6
servo/src/main/java/com/galaxis/rcs/plan/path/GraphInitializer.java

@ -25,9 +25,9 @@ public class GraphInitializer {
continue;
}
List<List<Double>> tf = (List<List<Double>>) nodeData.get("tf");
double x = tf.get(0).get(0);
double z = tf.get(0).get(2);
List<List<Float>> tf = (List<List<Float>>) nodeData.get("tf");
float x = tf.get(0).get(0);
float z = tf.get(0).get(2);
// 检查是否为可旋转点
Map<String, Object> dt = (Map<String, Object>) nodeData.get("dt");

8
servo/src/main/java/com/galaxis/rcs/plan/path/NavigationGraph.java

@ -20,7 +20,7 @@ public class NavigationGraph {
// 添加双向路径
public void addBidirectionalPath(NavigationNode a, NavigationNode b) {
double distance = a.distanceTo(b);
float distance = a.distanceTo(b);
pathSegments.put(a.id() + "->" + b.id(), new PathSegment(a, b, distance));
pathSegments.put(b.id() + "->" + a.id(), new PathSegment(b, a, distance));
@ -55,15 +55,15 @@ public class NavigationGraph {
return nodes.values().parallelStream()
.filter(NavigationNode::rotatable)
.min(Comparator.comparingDouble(start::distanceTo))
.min(Comparator.comparing(start::distanceTo))
.orElse(null);
}
// 计算启发式代价 (使用欧几里得距离)
public double heuristicCost(String fromId, String toId) {
public float heuristicCost(String fromId, String toId) {
NavigationNode from = nodes.get(fromId);
NavigationNode to = nodes.get(toId);
if (from == null || to == null) return Double.MAX_VALUE;
if (from == null || to == null) return Float.MAX_VALUE;
return from.distanceTo(to);
}
}

14
servo/src/main/java/com/galaxis/rcs/plan/path/NavigationNode.java

@ -1,19 +1,21 @@
package com.galaxis.rcs.plan.path;
import org.clever.core.Conv;
/**
* 路标节点地图中的顶点
*/
public record NavigationNode(
String id, // 节点ID (如 "20", "charger1")
double x, // 向右增长
double z, // 向下增长 (y无效)
float x, // 向右增长
float z, // 向下增长 (y无效)
boolean rotatable // 是否为可旋转位
) implements Comparable<NavigationNode> {
// 计算到另一个节点的欧几里得距离
public double distanceTo(NavigationNode other) {
double dx = this.x - other.x;
double dz = this.z - other.z;
return Math.sqrt(dx * dx + dz * dz);
public float distanceTo(NavigationNode other) {
float dx = this.x - other.x;
float dz = this.z - other.z;
return Conv.asFloat(Math.sqrt(dx * dx + dz * dz));
}
@Override

2
servo/src/main/java/com/galaxis/rcs/plan/path/PathSegment.java

@ -6,7 +6,7 @@ package com.galaxis.rcs.plan.path;
public record PathSegment(
NavigationNode start, // 起点节点
NavigationNode end, // 终点节点
double distance // 路径距离
float distance // 路径距离
) {
// 路径唯一标识
public String key() {

0
yvan-rcs-dev/.lck

Loading…
Cancel
Save