顺序程序(迷宫)的并行化

时间:2020-06-03 14:18:28

标签: java multithreading concurrency task java.util.concurrent

在大学里,我们有一个项目,我们必须在迷宫中从头到尾并行化顺序搜索。 我的方法基于ForkJoinTasks。 触发“开始”任务,并从起点到第一个分支点执行搜索算法。对于在分支点(2或3)处可以采用的每个其他潜在路径,将执行(递归)具有相同搜索算法的新SubTask,从而沿路径到达该分支点以及第一个点该分支点之后的潜在进一步路径的最大距离将作为起点。 这将一直运行到任务找到出口为止,然后返回从开始到出口的总路径。在某些时候没有进一步执行的其他任务将返回null。 父任务递归比较结果,最后返回路径。

此方法的最大尺寸为〜500X500,超出此尺寸无法使用或比顺序方法慢。我怀疑在如此大的迷宫中,对于分支分支处的每个其他路径而言,任务生成太大,并且系统超载。因此,似乎该程序无法以这种方式并行化,因为它至少必须对大小为1000X1000的迷宫起作用。 您有什么想法可以并行化,而不是进一步探索吗?我不想有一个解决方案,我只需要替代方法就可以代替。

提前谢谢!

那是我已经做过的,它适用于“小”迷宫:

    @Override
    public Point[] compute() {
        Point current = p;

        ArrayDeque<Point> resQ = null;

        Point[] finalPath = getTaskQ().clone();
        List<Point> l = Arrays.asList(finalPath);
        ArrayDeque<Point> pathSoFar = new ArrayDeque<Point>(l);

        while (!labyrinth.isDestination(current)) {

          visit(current); //turns into true in list with false boolean for every point


          Direction[] dirs = Direction.values();
          int dirsCount = 0;
          HashMap<Integer, Point> hmap = new HashMap<Integer, Point>();


          for (Direction directionToNeighbor : dirs) {
            Point neighbor = current.getNeighbor(directionToNeighbor);
            if (labyrinth.hasPassage(current, directionToNeighbor)
                    && !visitedBefore(neighbor)
                    && (!labyrinth.isBlindAlley(neighbor, 
          directionToNeighbor.opposite)
                    || labyrinth.isDestination(neighbor))) {
                ++dirsCount;
                hmap.put(dirsCount, neighbor);
            }
            }  if (dirsCount == 3) {

            pathSoFar.add(current);

            ParallelMazeSolver newDirectionTask1 = new 
            ParallelMazeSolver(hmap.get(1), i, pathSoFar.toArray(new 
            Point[0]));

            ParallelMazeSolver newDirectionTask2 = new 
            ParallelMazeSolver(hmap.get(2), i, pathSoFar.toArray(new 
            Point[0]));

            ParallelMazeSolver newDirectionTask3 = new 
            ParallelMazeSolver(hmap.get(3), i, pathSoFar.toArray(new 
            Point[0]));
            invokeAll(newDirectionTask1, newDirectionTask2, 
            newDirectionTask3);
            try {
                return 
                    (compare(newDirectionTask1.get(),newDirectionTask2.get(),
                     newDirectionTask3.get()));
            } catch (Exception e) {
                e.printStackTrace();
             }
            }
            if (dirsCount == 2) {

            pathSoFar.add(current);

            ParallelMazeSolver newDirectionTask1 = new 
            ParallelMazeSolver(hmap.get(1), i, pathSoFar.toArray(new 
            Point[0]));

            ParallelMazeSolver newDirectionTask2 = new 
            ParallelMazeSolver(hmap.get(2), i, pathSoFar.toArray(new 
            Point[0]));

            invokeAll(newDirectionTask1, newDirectionTask2);
            try {
                return 
            (compare(newDirectionTask1.get(),newDirectionTask2.get()));
            } catch (Exception e) {
                e.printStackTrace();
             }

            }
            else if (dirsCount == 1) {

            if(pathSoFar.peekLast() != current)
                pathSoFar.add(current);
            current = hmap.get(1);
            hmap.clear();

            } else if (dirsCount == 0) {
            return null;

            }

        }


        if(pathSoFar.peekLast() != current)
        pathSoFar.addLast(current);

        return pathSoFar.toArray(new Point[0]);
       }
      }

0 个答案:

没有答案
相关问题