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( public record AStarNodeState(
String nodeId, // 当前节点ID String nodeId, // 当前节点ID
int direction, // 当前方向 (0,90,180,270) int direction, // 当前方向 (0,90,180,270)
double gCost, // 实际代价 float gCost, // 实际代价
double hCost, // 启发式代价 float hCost, // 启发式代价
AStarNodeState parent // 父节点 AStarNodeState parent // 父节点
) implements Comparable<AStarNodeState> { ) implements Comparable<AStarNodeState> {
@ -17,13 +17,13 @@ public record AStarNodeState(
} }
// 总代价 // 总代价
public double fCost() { public float fCost() {
return gCost + hCost; return gCost + hCost;
} }
@Override @Override
public int compareTo(AStarNodeState other) { 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<>(); PriorityQueue<AStarNodeState> openSet = new PriorityQueue<>();
// 状态管理 // 状态管理
Map<String, Double> gScoreMap = new HashMap<>(); Map<String, Float> gScoreMap = new HashMap<>();
Map<String, AStarNodeState> cameFrom = new HashMap<>(); Map<String, AStarNodeState> cameFrom = new HashMap<>();
// 初始化起点 // 初始化起点
@ -39,7 +39,7 @@ public class AStarPathPlanner {
); );
openSet.add(start); openSet.add(start);
gScoreMap.put(start.stateKey(), 0.0); gScoreMap.put(start.stateKey(), 0.0f);
while (!openSet.isEmpty()) { while (!openSet.isEmpty()) {
AStarNodeState current = openSet.poll(); AStarNodeState current = openSet.poll();
@ -55,12 +55,12 @@ public class AStarPathPlanner {
PathSegment segment = graph.getPathSegment(current.nodeId(), neighbor.id()); PathSegment segment = graph.getPathSegment(current.nodeId(), neighbor.id());
if (segment == null) continue; if (segment == null) continue;
double moveCost = segment.distance(); float moveCost = segment.distance();
double tentativeGCost = current.gCost() + moveCost; float tentativeGCost = current.gCost() + moveCost;
String neighborKey = neighbor.id() + ":" + current.direction(); 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( AStarNodeState neighborState = new AStarNodeState(
neighbor.id(), neighbor.id(),
current.direction(), current.direction(),
@ -80,12 +80,12 @@ public class AStarPathPlanner {
if (currentNode != null && currentNode.rotatable()) { if (currentNode != null && currentNode.rotatable()) {
for (int rotation : new int[]{90, -90}) { for (int rotation : new int[]{90, -90}) {
int newDirection = (current.direction() + rotation + 360) % 360; int newDirection = (current.direction() + rotation + 360) % 360;
double rotateCost = 1.0; // 旋转代价 float rotateCost = 1.0f; // 旋转代价
double tentativeGCost = current.gCost() + rotateCost; float tentativeGCost = current.gCost() + rotateCost;
String rotatedKey = current.nodeId() + ":" + newDirection; 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( AStarNodeState rotatedState = new AStarNodeState(
current.nodeId(), current.nodeId(),
newDirection, newDirection,

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

@ -25,9 +25,9 @@ public class GraphInitializer {
continue; continue;
} }
List<List<Double>> tf = (List<List<Double>>) nodeData.get("tf"); List<List<Float>> tf = (List<List<Float>>) nodeData.get("tf");
double x = tf.get(0).get(0); float x = tf.get(0).get(0);
double z = tf.get(0).get(2); float z = tf.get(0).get(2);
// 检查是否为可旋转点 // 检查是否为可旋转点
Map<String, Object> dt = (Map<String, Object>) nodeData.get("dt"); 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) { 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(a.id() + "->" + b.id(), new PathSegment(a, b, distance));
pathSegments.put(b.id() + "->" + a.id(), new PathSegment(b, a, distance)); pathSegments.put(b.id() + "->" + a.id(), new PathSegment(b, a, distance));
@ -55,15 +55,15 @@ public class NavigationGraph {
return nodes.values().parallelStream() return nodes.values().parallelStream()
.filter(NavigationNode::rotatable) .filter(NavigationNode::rotatable)
.min(Comparator.comparingDouble(start::distanceTo)) .min(Comparator.comparing(start::distanceTo))
.orElse(null); .orElse(null);
} }
// 计算启发式代价 (使用欧几里得距离) // 计算启发式代价 (使用欧几里得距离)
public double heuristicCost(String fromId, String toId) { public float heuristicCost(String fromId, String toId) {
NavigationNode from = nodes.get(fromId); NavigationNode from = nodes.get(fromId);
NavigationNode to = nodes.get(toId); 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); 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; package com.galaxis.rcs.plan.path;
import org.clever.core.Conv;
/** /**
* 路标节点地图中的顶点 * 路标节点地图中的顶点
*/ */
public record NavigationNode( public record NavigationNode(
String id, // 节点ID (如 "20", "charger1") String id, // 节点ID (如 "20", "charger1")
double x, // 向右增长 float x, // 向右增长
double z, // 向下增长 (y无效) float z, // 向下增长 (y无效)
boolean rotatable // 是否为可旋转位 boolean rotatable // 是否为可旋转位
) implements Comparable<NavigationNode> { ) implements Comparable<NavigationNode> {
// 计算到另一个节点的欧几里得距离 // 计算到另一个节点的欧几里得距离
public double distanceTo(NavigationNode other) { public float distanceTo(NavigationNode other) {
double dx = this.x - other.x; float dx = this.x - other.x;
double dz = this.z - other.z; float dz = this.z - other.z;
return Math.sqrt(dx * dx + dz * dz); return Conv.asFloat(Math.sqrt(dx * dx + dz * dz));
} }
@Override @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( public record PathSegment(
NavigationNode start, // 起点节点 NavigationNode start, // 起点节点
NavigationNode end, // 终点节点 NavigationNode end, // 终点节点
double distance // 路径距离 float distance // 路径距离
) { ) {
// 路径唯一标识 // 路径唯一标识
public String key() { public String key() {

0
yvan-rcs-dev/.lck

Loading…
Cancel
Save