问题标签 [clpfd]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1671 浏览

list - prolog 列出 fd_all_different 和 is_set 什么更快?

我只是想知道你们中是否有人知道什么更快,

或者

有谁知道?我的数独求解器需要更快的解决方案。谢谢!

0 投票
2 回答
1327 浏览

prolog - NP完全背包

我看到了这个XKCD 漫画中提到的问题的 ECLiPSe 解决方案我试图将其转换为纯 Prolog。

我不认为这是人们能想到的最好/最具声明性的解决方案。有没有人有任何改进的建议?提前致谢!

0 投票
1 回答
227 浏览

recursion - Prolog递归溢出

它导致stackoverflow(不是站点)!这不应该是因为cut(!)。它在 SWI-Prolog 中有效吗?

0 投票
2 回答
501 浏览

lambda - 序言:迭代

晚上好,我有一个简单的问题,我警告你我对 prolog 很陌生。假设有三个相同大小的列表,每个列表只包含 1、0 或 -1。我想验证对于所有 i,在三个列表的第 i 个元素中,只有一个是非零的。

此代码为固定 i 执行此操作:

我怎么知道“对于所有我,compat1(V1,V2,V3,I)”?我试图定义

这样我就可以用我感兴趣的 K=maximum 值来调用它。但是 compat2 不起作用:在 ";" 之后给出 true,然后 无限期地运行。

谢谢!

0 投票
1 回答
903 浏览

prolog - 无法从 CLPFD 最小化工作

我和一个朋友正在编写一个应该解决 CLP 问题的程序。我们想使用最小化来优化解决方案,但它不起作用,因为它一直说我们从 sum(P,#=,S) 得到的数字介于两个数字之间(例如 5..7)。我们还没有找到一种很好的方法来从中提取任何数字或以任何方式对其进行操作,因此正在寻求您的帮助。

问题似乎来自我们的 gen_var 方法,该方法说列表的每个元素必须介于 0 和 1 之间,因此有些数字显示为“0..1”,而不是正确设置。

即使我们得到像“5..7”这样的数字,是否有任何方法可以使用最小化,或者有任何方法可以操纵该数字以便我们只得到 5?S(列表中元素的总和)是我们试图最小化的。

0 投票
3 回答
6824 浏览

prolog - Prolog 中的布尔赋值

全部。

我想为变量分配一个布尔值。

我试过类似的东西。

没有工作...

有什么解决办法吗?

0 投票
4 回答
4439 浏览

prolog - 从Prolog中的给定间隔检索所有数字

我是 Prolog 世界的新手,我想编写一个规则来返回特定范围内的所有元素。

我打算做类似的事情

前任:

当我输入 foo(X, 2, 5) 时,它应该返回 3,然后是 4。

看来我的方法是错误的,我想知道哪种方法是正确的。

0 投票
3 回答
158 浏览

prolog - PROLOG - 检查正确放置

我有一个拼图,它是一个 3*3 的网格,里面有数字 1-8,有一个可以移动的空白点 (0)。这是拼图的最终状态:

通过横向阅读,整个“状态”由 state(1,2,3,8,0,4,7,6,5) 表示。我需要一个函数来检查哪些部分在正确的位置。

我有:

现在 Z 将是正确位置的件数。

有什么简单的方法可以为 Z 提供输出吗?任何帮助,将不胜感激。谢谢。

0 投票
2 回答
1692 浏览

prolog - Not sufficiently instantiated arguments in `is/2`

For my "Declarative Languages" class we have to write a prolog program that solves Tangram puzzles. A puzzle is identified by a list of coordinates of the points of the puzzle. For example, puzzle(7,[(0,0),(8,0),(4,4)]) is a puzzle with identifier 7 and represents a triangle.

Here is my (naive) way of solving this. The execution starts with calling tangram(Puzzle, Puts). The program starts with all the possible pieces of the puzzle. I then pick a piece, try a position and a rotation and if this gives a valid position for the puzzle, I place the puzzle. (= place the block in the Puts list, which will be returned at the end of the program.) I backtrack over all these possibilities. Here's the code:

Some possible puzzles:

The problem when I run this is that I get the following error:

When tracing it seems that somehow the TX and TY values in Translate aren't instantiated. Tracing back I think that somehow X and Y don't get instantiated in the placePieces predicate. If there were no values left, the predicate would just fail, right?

I have been looking at my code for over 5 hours and can't seem to find my mistake. Hopefully one of you has the time to look this over and set me back in the right direction.

Thanks in advance!

0 投票
3 回答
1854 浏览

prolog - 使用 Prolog 优化约束逻辑编程中的寻路

我正在开发一个小型 prolog 应用程序来解决摩天大楼和栅栏难题。

一个未解之谜:

篱笆拼图中的摩天大楼(未解决)

已解决的谜题:

篱笆拼图中的摩天大楼(已解决)

当我通过程序已经解决的难题时,它会很快,几乎是瞬间的,为我验证它。当我通过程序非常小的谜题(例如 2x2,当然要修改规则)时,找到解决方案也很快。

问题在于计算“原生”大小为 6x6 的谜题。在中止它之前,我已经让它运行了 5 个小时左右。时间太多了。

我发现耗时最长的部分是“栅栏”,而不是“摩天大楼”。单独运行“摩天大楼”会产生一个快速的解决方案。

这是我的栅栏算法:

  • 顶点由数字表示,0 表示路径不经过该特定顶点,> 1 表示该顶点在路径中的顺序。
  • 约束每个单元格使其周围有适当数量的线条。
    • 这意味着如果两个顶点具有连续编号,则它们是连接的,例如 1 -> 2, 2 -> 1, 1 -> Max, Max-> 1 (Max是路径中最后一个顶点的编号。通过计算maximum/2
  • 确保每个非零顶点至少有两个具有序号的相邻顶点
  • 约束Max等于(BoardWidth + 1)^2 - NumberOfZerosBoardWidth+1是沿边的顶点数,NumberOfZeros通过 计算count/4)。
  • 用于nvalue(Vertices, Max + 1)确保不同值的数量VerticesMax(即路径中的顶点数)加上1(零值)
  • 找到第一个包含 a 的单元格3并强制路径从那里开始和结束,以提高效率

我可以做些什么来提高效率?代码包含在下面以供参考。

摩天大楼犯罪.pro

实用程序.pro

s1.pro