Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图形算法是纵向的,而不是水平的_Java_Graph_Artificial Intelligence_Iterative Deepening - Fatal编程技术网

Java 图形算法是纵向的,而不是水平的

Java 图形算法是纵向的,而不是水平的,java,graph,artificial-intelligence,iterative-deepening,Java,Graph,Artificial Intelligence,Iterative Deepening,我尝试实现一个*搜索的迭代深化。 问题是他走得很协调,不像我想的那样 当我声明一个节点时,我还写了他所在的级别(adancime) 所以我的逻辑是,如果(他得到的级别与作业中的级别(adancime)相同)是可以的,那么试试那里 我的图表如下: 来源:S目的地:G 他应该走的道路是:S B D H F H F。。。。(他应该找不到节点)因为他总是在某个点上得到最低值H或F 这是我的密码: package com.ida.algorithm; import java.util.Priority

我尝试实现一个*搜索的迭代深化。 问题是他走得很协调,不像我想的那样

当我声明一个节点时,我还写了他所在的级别(adancime)

所以我的逻辑是,如果(他得到的级别与作业中的级别(adancime)相同)是可以的,那么试试那里

我的图表如下:

来源:S目的地:G

他应该走的道路是:S B D H F H F。。。。(他应该找不到节点)因为他总是在某个点上得到最低值H或F

这是我的密码:

package com.ida.algorithm;

import java.util.PriorityQueue;
import java.util.HashSet;
import java.util.Set;

import java.util.List;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;

public class ItDeepAStar {

        public static void main(String[] args){

                Node s = new Node("S", 12, 0);
                Node a = new Node("A", 5, 1);
                Node b = new Node("B", 5, 1);
                Node c = new Node("C", 5, 2);
                Node d = new Node("D", 2, 2);
                Node e = new Node("E", 2, 3);
                Node f = new Node("F", 1, 4);
                Node h = new Node("H", 1, 3);
                Node g = new Node("G", 0, 2);

                s.adjacencies = new Edge[]{
                        new Edge(b, 8),
                        new Edge(a, 10)
                };

                b.adjacencies = new Edge[]{
                        new Edge(d, 8),
                        new Edge(g, 16)
                };

                d.adjacencies = new Edge[]{
                        new Edge(g, 3),
                        new Edge(h, 1)
                };

                h.adjacencies = new Edge[]{
                        new Edge(f, 1)
                };

                a.adjacencies = new Edge[]{
                        new Edge(g, 10),
                        new Edge(c, 2)
                };

                c.adjacencies = new Edge[]{
                        new Edge(e, 3)
                };

                e.adjacencies = new Edge[]{
                        new Edge(g, 2)
                };

                AstarSearch(s, g);

                List<Node> path = printPath(g);

                        System.out.println("Path: " + path);


        }

        public static List<Node> printPath(Node target){
                List<Node> path = new ArrayList<Node>();

        for(Node node = target; node != null; node = node.parent){
            path.add(node);
        }

        Collections.reverse(path);

        return path;
        }

        public static void AstarSearch(Node source, Node goal){

                Set<Node> explored = new HashSet<Node>();
                int level = 0;
                PriorityQueue<Node> queue = new PriorityQueue<Node>(8, new Comparator<Node>(){
                                 //override compare method
                 public int compare(Node i, Node j){
                    if(i.f_scores > j.f_scores){
                        return 1;
                    }

                    else if (i.f_scores < j.f_scores){
                        return -1;
                    }

                    else{
                        return 0;
                    }
                 }

                });

                //cost from start
                source.g_scores = 0;

                queue.add(source);

                boolean found = false;

                while((!queue.isEmpty()) && (!found)){

                        //the node in having the lowest f_score value
                        Node current = queue.poll();

                        explored.add(current);

                        //goal found
                        if(current.value.equals(goal.value)){
                                found = true;
                        }

                        //check every child of current node
                        for(Edge o : current.adjacencies){
                                Node child = o.target;
                                double cost = o.cost;
                                double temp_g_scores = current.g_scores + cost;
                                double temp_f_scores = temp_g_scores + child.h_scores;
                                level++;
                                /*if child node has been evaluated and 
                                the newer f_score is higher, skip*/

                                if((explored.contains(child)) && (temp_f_scores >= child.f_scores)  ) {
                                        level--;
                                        continue;
                                }

                                /*else if child node is not in queue or 
                                newer f_score is lower*/

                                else if((!queue.contains(child)) || (temp_f_scores < child.f_scores) && level == child.adancime){

                                        child.parent = current;
                                        child.g_scores = temp_g_scores;
                                        child.f_scores = temp_f_scores;

                                        if(queue.contains(child)){
                                                queue.remove(child);
                                                level--;
                                        }

                                        queue.add(child);
                                        level++;
                                }
                        }
                }
        }
}

class Node{
    public int adancime;
    public final String value;
    public double g_scores;
    public final double h_scores;
    public double f_scores = 0;
    public Edge[] adjacencies = new Edge[]{};
    public Node parent;

    public Node(String val, double hVal, int adaincime){
            value = val;
            h_scores = hVal;
            this.adancime = adaincime;
    }

    public String toString(){
            return value;
    }    
}

class Edge{
        public final double cost;
        public final Node target;

        public Edge[] adjacencies = new Edge[]{};

        public Edge(Node targetNode, double costVal){
                target = targetNode;
                cost = costVal;
        }
}
package com.ida.algorithm;
导入java.util.PriorityQueue;
导入java.util.HashSet;
导入java.util.Set;
导入java.util.List;
导入java.util.Comparator;
导入java.util.ArrayList;
导入java.util.Collections;
公共级ItDeepAStar{
公共静态void main(字符串[]args){
节点s=新节点(“s”,12,0);
节点a=新节点(“a”,5,1);
节点b=新节点(“b”,5,1);
节点c=新节点(“c”,5,2);
节点d=新节点(“d”,2,2);
节点e=新节点(“e”,2,3);
节点f=新节点(“f”,1,4);
节点h=新节点(“h”,1,3);
节点g=新节点(“g”,0,2);
s、 邻接=新边[]{
新边(b,8),
新边(a,10)
};
b、 邻接=新边[]{
新边(d,8),
新边(g,16)
};
d、 邻接=新边[]{
新边(g,3),
新边(h,1)
};
h、 邻接=新边[]{
新边(f,1)
};
a、 邻接=新边[]{
新边(g,10),
新边(c,2)
};
c、 邻接=新边[]{
新边(e,3)
};
e、 邻接=新边[]{
新边(g,2)
};
AstarSearch(s,g);
列表路径=打印路径(g);
System.out.println(“路径:+Path”);
}
公共静态列表打印路径(节点目标){
列表路径=新的ArrayList();
for(Node=target;Node!=null;Node=Node.parent){
添加(节点);
}
集合。反向(路径);
返回路径;
}
公共静态void AstarSearch(节点源、节点目标){
Set explored=新的HashSet();
智力水平=0;
PriorityQueue队列=新的PriorityQueue(8,新的比较器(){
//覆盖比较法
公共整数比较(节点i、节点j){
如果(i.f_分数>j.f_分数){
返回1;
}
否则如果(i.f_分数=子项f_分数)){
级别--;
继续;
}
/*如果子节点不在队列中或
较新的f_分数较低*/
如果(!queue.contains(child))| |(temp_f_分数