创建随机的CNF公式

发布于 2025-02-03 16:27:05 字数 320 浏览 3 评论 0原文

对于我的论文,我正在研究知识库的特定特性,以及这如何影响论证树的大小。我需要一个我已经拥有的prolog中的SAT求解器,但是我还需要一种创建随机从句的算法。 k-sat表示精确长度k的子句。为简单起见,我将k设置为3,最大4。我想将k(每个条款的文字数量)作为程序的参数。因此,如果我想要一个3个序列,具有1000个条款和300个文字,那么我的程序应创建一个具有100个长度3的知识库,其中所有这些文字都必须随机分散,以一定的负面比例(50) /50为简单起见)。我希望输出看起来像这样,输出:一组随机创建的子句:

v1:-v2,v3。

v2:-v3,-v4。

有人可以帮我吗?

For my thesis, I am looking at specific properties of knowledge bases and how this affects argumentation tree sizes. I need a sat solver in prolog which I already have, but I also need an algorithm that creates random clauses.
k-SAT means clauses of precisely length k. For simplicity, I will set k to 3, max 4. I want to give K (amount of literals per clause) as a parameter to the program. So if I want a 3-sat, with 1000 clauses and 300 literals, then my program should create a knowledge base with 100 clauses of length 3 where all of those literals randomly have to be spread out with a certain ratio of negative ones (50/50 for simplicity). I want the output to look like this, Output: a set of clauses randomly created:

V1 :- V2, V3.

V2 :- -V3, -V4.

could somebody help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花想c 2025-02-10 16:27:05

logTalk提供了用户 - 可扩展 nofollow noreferrer“> nutyary nutyary 图书馆和QuickCheck Support在其 lgtunit 开发器工具。您可以同时使用大多数Prolog Systems

Logtalk provides a user-extensible arbitrary library and QuickCheck support in its lgtunit developer tool. You can use both with most Prolog systems.

初吻给了烟 2025-02-10 16:27:05

尝试做类似的事情:

random_knowledge_base(NumberOfClauses,
                      LiteralsPerClause,
                      AmountOfLiterals,
                      Ratio,
                      KnowledgeBase) :-
    length(KnowledgeBase, NumberOfClauses),
    maplist(random_clause(LiteralsPerClause, AmountOfLiterals, Ratio), KnowledgeBase).

random_clause(LiteralsPerClause, AmountOfLiterals, Ratio, Head :- Body) :-
    randseq(LiteralsPerClause, AmountOfLiterals, [Number|Numbers]),
    atom_concat(p, Number, Head),
    maplist(literal(Ratio), Numbers, Literals),
    comma_list(Body, Literals).

literal(Ratio, Number, Literal) :-
    atom_concat(p, Number, Proposition),
    (   maybe(Ratio)
    ->  Literal = Proposition
    ;   Literal = -Proposition ).

示例:

?- random_knowledge_base(5, 3, 9, 0.7, B).
B = [(p2 :- p5, -p6),  (p8 :- p3, p6),  (p9 :- p7, p2),  (p4 :- -p1, p6),  (p8 :- p1, p9)].

代码的详细说明

?- atom_concat(p, 2, A).
A = p2. 
  • predicate 也许/1 概率 p 成功,概率为1- p
?- maybe(0.5).
true.

?- maybe(0.5).
false.
?- (maybe(0.5) -> writeln(positive) ; writeln(negative)).
negative
true.

?- (maybe(0.5) -> writeln(positive) ; writeln(negative)).
positive
true.
  • 例如,目标字面(0.5,1,l)随机生成字面p1- p1
?- literal(0.5, 1, L).
L = -p1.

?- literal(0.5, 1, L).
L = p1.
  • 要在范围1 .. m 中选择 k ,我们可以使用 randseq/3 。对于 k = 3和 m = 5,我们必须:
?- randseq(3, 5, Ns).
Ns = [4, 5, 2].

?- randseq(3, 5, Ns).
Ns = [1, 3, 2].
  • k 任意整数的列表转换为 k <> k <> k < /em>对应的文字,我们可以使用 mapList/3
?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls).
Ns = [1, 3, 5],
Ls = [p1, p3, p5].
  • 谓词 comma_list/2 可以使用将文字列表转换为一个连词:
?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls), comma_list(B, Ls).
Ns = [1, 5, 3],
Ls = [p1, p5, p3],
B =  (p1, p5, p3).

?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls), comma_list(B, Ls), C = (p3 :- B).
Ns = [1, 4, 2],
Ls = [-p1, p4, -p2],
B =  (-p1, p4, -p2),
C =  (p3 :- -p1, p4, -p2).
  • 将其整体放在一起,我们有peadicate rando_clause/4
?- random_clause(3, 4, 0.5, C).
C =  (p3 :- p4, -p2).

?- random_clause(3, 4, 0.5, C).
C =  (p1 :- -p5, p4).

以例如4个条款来创建一个随机知识基础:

?- length(KB, 4).
KB = [_, _, _, _].

?- length(KB, 4), maplist(random_clause(3,5,0.5), KB).
KB = [(p2 :-p1, -p3),  (p5 :-p2, p3),  (p3 :- -p4, p5),  (p3 :- -p1, p4)].

或者,使用predenate randoge> randoge_knwoLEDGE_BASE /5

?- random_knowledge_base(4, 3, 5, 0.5, KB).
KB = [(p1 :- -p4, -p5),  (p2 :- p3, p4),  (p2 :-p3, -p5),  (p3 :- -p1, -p4)].

Try doing something like this:

random_knowledge_base(NumberOfClauses,
                      LiteralsPerClause,
                      AmountOfLiterals,
                      Ratio,
                      KnowledgeBase) :-
    length(KnowledgeBase, NumberOfClauses),
    maplist(random_clause(LiteralsPerClause, AmountOfLiterals, Ratio), KnowledgeBase).

random_clause(LiteralsPerClause, AmountOfLiterals, Ratio, Head :- Body) :-
    randseq(LiteralsPerClause, AmountOfLiterals, [Number|Numbers]),
    atom_concat(p, Number, Head),
    maplist(literal(Ratio), Numbers, Literals),
    comma_list(Body, Literals).

literal(Ratio, Number, Literal) :-
    atom_concat(p, Number, Proposition),
    (   maybe(Ratio)
    ->  Literal = Proposition
    ;   Literal = -Proposition ).

Example:

?- random_knowledge_base(5, 3, 9, 0.7, B).
B = [(p2 :- p5, -p6),  (p8 :- p3, p6),  (p9 :- p7, p2),  (p4 :- -p1, p6),  (p8 :- p1, p9)].

Detailed explanation of the code

  • The ISO predicate atom_concat/3 concatenates two atoms to form a third one:
?- atom_concat(p, 2, A).
A = p2. 
  • The predicate maybe/1 succeeds with probability P and fails with probability 1-P:
?- maybe(0.5).
true.

?- maybe(0.5).
false.
?- (maybe(0.5) -> writeln(positive) ; writeln(negative)).
negative
true.

?- (maybe(0.5) -> writeln(positive) ; writeln(negative)).
positive
true.
  • Thus, for example, the goal literal(0.5, 1, L) randomly generates a literal p1 or -p1:
?- literal(0.5, 1, L).
L = -p1.

?- literal(0.5, 1, L).
L = p1.
  • To choose K unique random integers in the range 1..M, we can use randseq/3. For K = 3 and M = 5, we have:
?- randseq(3, 5, Ns).
Ns = [4, 5, 2].

?- randseq(3, 5, Ns).
Ns = [1, 3, 2].
  • To transform a list of K arbitrary integers into a list of K corresponding literals, we can use maplist/3:
?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls).
Ns = [1, 3, 5],
Ls = [p1, p3, p5].
  • The predicate comma_list/2 can be used to transform a list of literals into a conjunction:
?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls), comma_list(B, Ls).
Ns = [1, 5, 3],
Ls = [p1, p5, p3],
B =  (p1, p5, p3).

?- randseq(3, 5, Ns), maplist(literal(0.5), Ns, Ls), comma_list(B, Ls), C = (p3 :- B).
Ns = [1, 4, 2],
Ls = [-p1, p4, -p2],
B =  (-p1, p4, -p2),
C =  (p3 :- -p1, p4, -p2).
  • Putting it all together, we have predicate random_clause/4:
?- random_clause(3, 4, 0.5, C).
C =  (p3 :- p4, -p2).

?- random_clause(3, 4, 0.5, C).
C =  (p1 :- -p5, p4).

To create a random knowledge base with, for example, 4 clauses:

?- length(KB, 4).
KB = [_, _, _, _].

?- length(KB, 4), maplist(random_clause(3,5,0.5), KB).
KB = [(p2 :-p1, -p3),  (p5 :-p2, p3),  (p3 :- -p4, p5),  (p3 :- -p1, p4)].

Or, using predicate random_knwoledge_base/5:

?- random_knowledge_base(4, 3, 5, 0.5, KB).
KB = [(p1 :- -p4, -p5),  (p2 :- p3, p4),  (p2 :-p3, -p5),  (p3 :- -p1, -p4)].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文