- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为一个简单的基于 2d 网格的游戏实现了 A* 算法。然而,该算法使游戏速度减慢很多,我最近了解了 HPA*,它旨在尝试在一定程度上解决这个问题。
我确实找不到任何对我有用的编码示例,因为我正在使用 Java 进行编码,而且我对从哪里开始调整我的初始代码有点困惑。
我的代码如下,如果有人想看的话。我真的只是想以某种方式调整代码,以便将路径分割成网格,这样我就可以计算出一条大约 5 个方格长度的路径,而不是一条 20 个方格长并减慢游戏速度的路径。
if (shortestPath == null) {
openLocations.add(playerLocation);
// While the goal has not been found yet
while (openLocations.size() != 0 || pathFound != true) {
// get the first node from the open list
Node current = openLocations.get(0);
shortestPath = reconstructPath(current);
// check if current node is the goal node
if (current.getX() == goalLocation.getX()
&& current.getY() == goalLocation.getY()
//|| shortestPath.getWayPointPath().size() > GameInfo.getPathLength() + GameInfo.getPathCounter()
//|| shortestPath.getWayPointPath().size() >= 5
) {
shortestPath = reconstructPath(current);
pathFound = true;
for(Node node: shortestPath.getWayPointPath())
totalClosedLocations.add(node);
// path has been found
break;
}
// move current node to the already searched (closed) list
openLocations.remove(current);
closedLocations.add(current);
// set the current nodes neighbours
current = setNeighbours(current);
// Now it's time to go through all of the current nodes
// neighbours and see if they should be the next step
for (Node neighbor : current.getNeighborList()) {
boolean neighborIsBetter;
// if we have already searched this Node, don't bother and
// continue to the next one
if (closedLocations.contains(neighbor)) {
continue;
}
boolean found = false;
for (Node neighbournode : closedLocations) {
if (neighbournode.getX() == neighbor.getX()
&& neighbournode.getY() == neighbor.getY()) {
found = true;
continue;
}
}
if (found)
continue;
Node movable = new Node(neighbor.getX(), neighbor.getY(),
neighbor.getCategory(), neighbor.getItype(), neighbor.getId());
if (grid[movable.getX()][movable.getY()].size() > 0) {
// check to make sure that the square is not of category
// 4(immovable object) or category 3(enemy)
if ((grid[movable.getX()][movable.getY()].get(0).category == 4 && grid[movable
.getX()][movable.getY()].get(0).itype == 0)
&& grid[movable.getX()][movable.getY()].get(0).obsID != goalLocation.getId()
) {
// You cannot move on this square
neighbor.setMoveable(false);
} else {
// You can move on this square. Set parent location
// as the players current position.
movable.setParent(playerLocation);
}
}
// also just continue if the neighbor is an obstacle
if (neighbor.getMoveable()) {
// calculate how long the path is if we choose this
// neighbor
// as the next step in the path
float neighborDistanceFromStart = (current
.getDistanceFromStart() + getDistanceBetween(
current, neighbor));
// add neighbor to the open list if it is not there
if (!openLocations.contains(neighbor)) {
openLocations.add(neighbor);
neighborIsBetter = true;
// if neighbor is closer to start it could also be
// better
} else if (neighborDistanceFromStart < current
.getDistanceFromStart()) {
neighborIsBetter = true;
} else {
neighborIsBetter = false;
}
// set neighbors parameters if it is better
if (neighborIsBetter) {
neighbor.setParent(current);
neighbor.setDistanceFromStart(neighborDistanceFromStart);
neighbor.setHeuristicDistanceFromGoal(heuristicStar
.getEstimatedDistanceToGoal(
neighbor.getX(), neighbor.getY(),
goalLocation.getX(),
goalLocation.getY()));
}
}
}
}
System.out.println("====================");
}
最佳答案
比方说:你实现了一个类似爬行的游戏,有一个迷宫、英雄和很多怪物......
英雄移动后,怪物会转动,它们可能会移动,朝英雄移动(a*星)。我猜你每次都会计算每个怪物的路径?!?正确的?好吧,那么有些事情就很不对劲了:
让每个怪物保持自己的路线。如果怪物距离很远,你不必搜索最短路径,因为即使你的英雄移动了,最短路径也基本上是相同的(只有最后几步不同)。然后,如果怪物移动了 10 圈,你可以重新计算路径。这样你就可以将计算时间加快 10 倍 - 考虑到较长的路径比较短的路径花费更多的时间,你可以做更多的优化
如果距离较远,可以每5步重新计算一次路径,也许如果它真的很接近,则需要每回合重新计算路径,但不用担心......如果它是一条短路径,则不需要太多时间来计算!
抱歉 - 未添加代码...
好吧,让我们继续:这是一个简单的迷宫/ map :
你从上/左开始,想要走到最后(不是 A,不是 B,你想要走到最后!)...
如果你不计算整个路径,你将永远找不到路,因为所有其他路径似乎都更短,尤其是当你在搜索中使用启发式方法时!!!
所以 - 没有办法不进行完整路径搜索!
优化:查找邻居通常是一个瓶颈:当您创建字段时,为每个字段设置邻居!
public class Test {
public Field[][] field;
public void doSomeTest(){
field = new Field[25][25]
MazeGenerator.createMaze(field); //this makes your map = this sets the fields passable or not
for (int dy == 0; dy < 25; dy ++){
for (int dx == 0; dx < 25; dx ++){
createNeighbours(field[dx][dy]);
}
}
}
public void createNeigbours(Field f){
//north
if (isPassable(f.xpos, f.ypos-1) f.neighbours.add(field[xpos][ypos-1]);
//east
if (isPassable(f.xpos+1, f.ypos) f.neighbours.add(field[xpos+1][ypos]);
//south
if (isPassable(f.xpos, f.ypos+1) f.neighbours.add(field[xpos][ypos+1]);
//west
if (isPassable(f.xpos-1, f.ypos) f.neighbours.add(field[xpos-1][ypos]);
}
public boolean isPassable(int xpos, int ypos){
if (xpos <= 0) return false; //outside [maybe you even have a border, then xpos <= 1
if (ypos <= 0) return false; //outside
if (xpos >= Map.width) return false; //outside
if (ypos >= Map.height) return false; //outside
if (field[xpos][ypos].isBlocked) return false; //blocked
return true;
}
}
public class Field{
public final int xpos;
public final int ypos;
public boolean isBlocked = false; // this makes your map
public ArrayList<Field> neigbours = new ArrayList<Field>();
public Field(int xpos, int ypos){
this.xpos = xpos;
this.ypos = ypos;
}
public List<Field> getNeighbours(){
return neigbours ;
}
}
上面的代码仅解释了如何在创建字段时创建邻居...但是如果扩展节点(a*),您可以非常快地获得邻居并且无需计算时间(想想您创建对象的频率在您的代码中)
关于java - 将 A* 路径查找更改为 HPA* - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136491/
前言 “大伙得眼里有活,看见同事忙的时候要互相帮助,这样我们团队才能快速成长,出成绩,多干点活没坏处的,领导都看在眼里记在心里,不会亏待大伙。
我目前正在尝试使用 prometheus 和 prometheus 适配器运行一个自动缩放演示,我想知道是否有一种方法可以根据 prometheus 从另一个部署中收集的指标自动缩放我的一个部署。 我
我们有一个Istio集群,我们正在尝试为Kubernetes配置水平容器自动缩放。我们希望将请求计数用作hpa的自定义指标。我们如何才能将Istio的Prometheus用于同一目的? 最佳答案 事实
我有两个彩色轨道,我在其中部署了两个不同版本的 web 应用程序 (nginx+php-fpm),这些轨道可通过称为 live 和 next 的服务使用。 经典的方式是部署Web应用程序的新版本接下来
副本和 HPA 有什么区别? 例如,以下部署配置了 3 个副本 apiVersion: apps/v1 kind: Deployment metadata: name: hello spec:
有没有办法在 Azure Kubernetes 服务上启用基于“每秒请求”的扩展? AKS 中的 HPA 确实允许基于 CPU 和内存限制进行水平 Pod 扩展,但没有直接的方法来实现这一点 - 每秒
HPA-如何避免因CPU使用率激增而扩大规模(不在启动时) 在为其他国家/地区加载业务配置时,CPU负载会增加1分钟,但我们要避免在1分钟内扩大规模。 在下面的图片中,CurrentMetricVal
我正在运行带有HPA的Kubernetes集群v1.16(当前是GKE的最新版本),该集群可基于自定义指标(具体来说是从Google云监控中获取的Rabbitmq消息计数)来扩展部署。 问题 当消息数
我在HPA上发现istio网关有问题。 为什么10m等于10%? 10m 1%不是吗? Kubernetes版本是1.18.5。 # kubectl get hpa --all-namespaces
我们的Web服务API上似乎遇到了一些内部服务器错误(500),似乎是在作为自动缩放器配置的一部分引入或删除了新容器时。 自然,这种事情很难解决,因为您并不总是能够控制 pods 的启动和停止的时间。
从 Kubernetes v1.18 开始,v2beta2 API 允许通过 Horizontal Pod Autoscalar (HPA) 行为字段配置缩放行为。 我计划将具有自定义指标的 HP
我想要实现的是创建一个能够扩展的 Horizontal Pod Autoscaler worker根据 controller 生成的自定义指标生成 Pod荚。 我已经让 Prometheus 抓取
问题设置。 假设我有 2 个 pod,A 和 B。我希望能够根据来自某个任意来源的某个任意数字动态缩放 pod A。假设 pod B 是这样一个源:例如,它可以有一个 HTTP 服务器,它的端点在请求
我正在尝试配置 kubernetes 水平 pod 自动缩放。我部署的定义是 here . 我开始 heapster 部署和 heapster 服务,如下所示: # kubectl run heaps
我终于部署了与其中一个部署相关的 hpa,但 hpa 没有按预期工作。我可以看到利用率远远超过实际情况,甚至不匹配所有 pod 的利用率总和。不确定这个平均利用率是如何计算的,当有 2 个 pod 的
是否有一个“主开关”来启用/禁用 Keda 和 HPA?我可以通过将副本计数编辑为 0 来启用/禁用扩展规则,但是是否有主要的启用/禁用字段? cooldownPeriod: 1800 max
我有一个在 Kubernetes 集群上运行的 openjdk:8 镜像。我添加了内存 HPA(Horizontal Pod Autoscaling),它可以很好地扩展,但由于 JVM 不会将内存
我正在尝试基于GPU利用率在GCP中实现水平荚自动缩放(HPA)。 我的hpa.yaml文件如下所示: kind: HorizontalPodAutoscaler metadata: name:
我已经为我在 Kubernetes 中的部署之一配置了 hpa。 有什么方法可以检查部署中是否发生 HPA 缩放以及何时发生? 我没有部署普罗米修斯或任何监控解决方案。 最佳答案 如果您创建了 HPA
所以这发生在 EKS K8s v1.15 上。您可以在 describe 输出中看到 api 版本。 millicpu 在 80 到 120 之间徘徊……这与 HPA 输出的副本数完全不匹配…… 这是
我是一名优秀的程序员,十分优秀!