Java NP-hard算法

Java NP-hard算法,java,algorithm,np-hard,Java,Algorithm,Np Hard,我正在研究一个NP难问题的算法(比如卖手问题),但我找不到合适的算法。如果有人能帮我,我将不胜感激。我们有一个(x,y)矩阵,在(n,m)块中有一个机器人,矩阵块中有一些垃圾 我们希望机器人去每个有垃圾的街区,穿过所有街区后,它会回到它的第一个街区。 有关问题有两个条件: 机器人只能水平和垂直移动 输出是一个整数,即它所穿过的路径的长度。 此路径必须具有最小长度 例如,输入为: 10 10 ----> x,y 1 1 ----> n,m 4 ----> number

我正在研究一个NP难问题的算法(比如卖手问题),但我找不到合适的算法。如果有人能帮我,我将不胜感激。我们有一个
(x,y)
矩阵,在
(n,m)
块中有一个机器人,矩阵块中有一些垃圾

我们希望机器人去每个有垃圾的街区,穿过所有街区后,它会回到它的第一个街区。 有关问题有两个条件:

  • 机器人只能水平和垂直移动
  • 输出是一个整数,即它所穿过的路径的长度。 此路径必须具有最小长度
  • 例如,输入为:

    10 10 ----> x,y
    1 1   ----> n,m
    4     ----> number of rubbishes
    
    垃圾的位置:

    2 3
    5 5  
    9 4  
    6 5
    
    输出为:

    24
    
    像这样

    点定义:

    public class Point {
    
        int x;
        int y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    }
    
    节点:

    public class Node {
    
        private Point p;
        private int cost;
        private int estimatedCost;
        private Set<Point> points;
        private Point origin;
    
        public Node(Point p, int cost, Set<Point> points, Point origin) {
            this.p = p;
            this.points = points;
            this.origin = origin;
            this.cost = cost;
            // Heuristic estimate cost
            if (points.isEmpty()) {
                this.estimatedCost = cost + distance(p, origin);
                this.cost = estimatedCost;
            } else {
                this.estimatedCost = cost + nearest(p, points) + nearest(origin, points);
                for (Point point : points) {
                    estimatedCost += nearest(point, points);
                }
            }
        }
    
        private static int distance(Point p0, Point p1) {
            return Math.abs(p0.x - p1.x) + Math.abs(p0.y - p1.y);
        }
    
        private int nearest(Point p, Set<Point> points) {
            int result = Integer.MAX_VALUE;
            for (Point point : points) {
                int d = distance(point, p);
                if (d != 0 && d < result) {
                    result = d;
                }
            }
            return result;
        }
    
        public int getCost() {
            return cost;
        }
    
    
        public boolean isClosed(){
            return cost == estimatedCost;
        }
    
        public int getEstimatedCost() {
            return estimatedCost;
        }
    
        public Set<Point> getPoints() {
            return points;
        }
    
        public Node goTo(Point target) {
            int newCost = cost + distance(this.p, target);
            Set<Point> newPoints;
            if (points.isEmpty()) {
                newPoints = Collections.emptySet();
            } else {
                newPoints = new HashSet<>(points);
                newPoints.remove(target);
            }
            return new Node(target, newCost, newPoints, origin);
        }
    }
    
    公共类节点{
    私人点p;
    私人成本;
    私人内部估算成本;
    私人设定点;
    私人点源;
    公共节点(点p、整数成本、设定点、原点){
    这个,p=p;
    这个点=点;
    this.origin=origin;
    成本=成本;
    //启发式估计成本
    if(points.isEmpty()){
    this.estimatedCost=成本+距离(p,原点);
    该成本=估计成本;
    }否则{
    this.estimatedCost=成本+最近(p,点)+最近(原点,点);
    用于(点:点){
    估计成本+=最近的(点,点);
    }
    }
    }
    专用静态整数距离(点p0,点p1){
    返回Math.abs(p0.x-p1.x)+Math.abs(p0.y-p1.y);
    }
    私有整数最近(点p,设定点){
    int result=Integer.MAX_值;
    用于(点:点){
    int d=距离(点,p);
    如果(d!=0&&d<结果){
    结果=d;
    }
    }
    返回结果;
    }
    公共int getCost(){
    退货成本;
    }
    公共布尔值isClosed(){
    退货成本==估计成本;
    }
    public int getEstimatedCost(){
    返回估计成本;
    }
    公共设置getPoints(){
    返回点;
    }
    公共节点转到(点目标){
    int newCost=成本+距离(此.p,目标);
    设置新点;
    if(points.isEmpty()){
    newPoints=Collections.emptySet();
    }否则{
    newPoints=新哈希集(点);
    newPoints.remove(目标);
    }
    返回新节点(目标、新成本、新点数、原点);
    }
    }
    
    解算器算法:

    public static void main(String[] args) {
        Point origin = new Point(1, 1);
        Set<Point> points = new HashSet<>(Arrays.asList(new Point(2, 3), new Point(5, 5), new Point(6, 5), new Point(9, 4)));
        Node begin = new Node(origin, 0, points, origin);
        PriorityQueue<Node> candidates = new PriorityQueue<>((n0, n1) -> Integer.compare(n0.estimatedCost, n1.estimatedCost));
        candidates.add(begin);
        Node solution = null;
        while (!candidates.isEmpty()) {
            Node candidate = candidates.poll();
            if (candidate.isClosed()) {
                solution = candidate;
                candidates.clear();
            } else {
                for (Point p : candidate.getPoints()) {
                    candidates.add(candidate.goTo(p));
                }
            }
        }
        if (solution != null) {
            System.out.println(solution.getCost());
        }
    }
    
    publicstaticvoidmain(字符串[]args){
    点原点=新点(1,1);
    Set points=newhashset(Arrays.asList(新点(2,3)、新点(5,5)、新点(6,5)、新点(9,4));
    节点开始=新节点(原点,0,点,原点);
    PriorityQueue候选者=新的PriorityQueue((n0,n1)->Integer.compare(n0.estimatedCost,n1.estimatedCost));
    添加(开始);
    节点解决方案=null;
    而(!candidates.isEmpty()){
    节点候选者=候选者。poll();
    if(candidate.isClosed()){
    解决方案=候选人;
    候选人;
    }否则{
    对于(点p:candidate.getPoints()){
    添加(candidate.goTo(p));
    }
    }
    }
    如果(解决方案!=null){
    System.out.println(solution.getCost());
    }
    }
    
    这看起来很像TSP(旅行推销员问题)。基本上,你需要以最短路径的方式去参观,而不是去你有垃圾的城市。TSP有数百种算法,只需检查其中一种并将其应用到您的案例中即可。有几种优化算法可以很好地工作,但没有一种是完美的(P vs NP等等)。