java - 转换分支定界循环以使用 Java Stream API

标签 java algorithm lambda java-8 java-stream

我有一个简单的分支限界算法,适用于旅行商问题的变体,我认为尝试将其转换为使用 Java 8 Stream API 会很有趣。但是,我很难弄清楚如何在不依赖副作用的情况下做到这一点。

初始代码

int bound = Integer.MAX_VALUE;
List<Location> bestPath = null;

while(!queue.isEmpty()) {
    Node curr = queue.poll();
    //bound exceeds best, bail
    if (curr.getBound() >= bound) { 
        return bestPath;
    }
    //have a complete path, save it
    if(curr.getPath().size() == locations.size()) {
        bestPath = curr.getPath();
        bound = curr.getBound();
        continue;
    }
    //incomplete path - add all possible next steps
    Set<Location> unvisited = new HashSet<>(locations);
    unvisited.removeAll(curr.getPath());
    for (Location l : unvisited) {
        List<Location> newPath = new ArrayList<>(curr.getPath());
        newPath.add(l);
        Node newNode = new Node(newPath, getBoundForPath(newPath));
        if (newNode.getBound() <= bound){
            queue.add(newNode);
        }
    }
}

我第一次尝试将其转换为 Stream API,并得出以下结论:

Java 8 版本

Consumer<Node> nodeConsumer = node -> {
    if(node.getPath().size() == locations.size() ) {
        bestPath = node.getPath();
        bound = node.getBound();
    } else {
        locations.stream()
            .filter(l -> !node.getPath().contains(l))
            .map(l -> {
                List<Location> newPath = new ArrayList<>(node.getPath());
                newPath.add(s);
                return new Node(newPath, getBoundForPath(newPath));
            })
            .filter(newNode -> newNode.getBound() <= bound)
            .forEach(queue::add);
    }
};

Stream.generate(() -> queue.poll())
    .peek(nodeConsumer)
    .filter(s -> s.getBound() > bound)
    .findFirst();

return bestPath;

主要问题是 nodeConsumer 必须引用 bestPath 和 bound,它们不是最终变量。我可以让它们成为最终的 AtomicReference 变量来解决这个问题,但我觉得这有点违反了流 API 的精神。任何人都可以帮助我将初始算法提炼成更惯用的实现吗?

最佳答案

我想知道使用 reduce 是否是解决此问题的方法,因为它允许您在不需要外部变量的情况下跟踪值。

类似于以下内容(我不得不猜测您上面代码的一些细节,但希望我走在正确的轨道上)。

    final BiFunction<Entry<Integer, List<Location>>, Node, Entry<Integer, List<Location>>> accumulator
        = (identity, node) -> {
            if (node.getPath().size() == locations.size() ) {
                return new SimpleEntry<>(node.getBound(), node.getPath());
            } else {
                locations.stream()
                    .filter(l -> !node.getPath().contains(l))
                    .map(l -> {
                        List<Location> newPath = new ArrayList<>(node.getPath());
                        newPath.add(l);
                        return new Node(newPath, getBoundForPath(newPath));
                    })
                    .filter(newNode -> newNode.getBound() <= identity.getKey())
                    .forEach(queue::add);
                return identity;
            }
        };

    final BinaryOperator<Entry<Integer, List<Location>>> combiner
        = (left, right) -> left.getKey() < right.getKey() ? left : right;

    final Entry<Integer, List<Location>> identity
        = new SimpleEntry<>(Integer.MAX_VALUE, null);

    final List<Location> bestValue = Stream.generate(queue::poll)
        .reduce(identity, accumulator, combiner)
        .getValue();

或者,您可以查看在 jOOλ 中使用 Seq (对 Streams 的顺序扩展),并改用 foldLeft

关于java - 转换分支定界循环以使用 Java Stream API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32855982/

相关文章:

c# - Evaluator.PartialEval 减少提供的表达式

c# - 嵌套泛型函数的类型推断

Java CSV Reader,读取剩余数据

java - 使用函数更改位图颜色

java - DS_Store 文件导致 Minecraft forge 崩溃

algorithm - 选择排序运行时困惑

algorithm - 如何从二进制堆中删除元素?

algorithm - 渐近分析

java - config.getInitParameter 始终返回 null

c++ - 将带有 lambda 的 std::shared_ptr 插入 vector 中