我正在尝试创建一个 SAT 求解器,它通过 Boolean Grobner Bases 的实现从结合范式 (CNF) 转换:
a) 对特定变量的否定,例如-x
将转换为1+x
。
b)添加相同的变量将导致0。例如x + x = 0
。(将需要使用 XOR)。
c) 相同变量的乘法将产生相同的变量。例如x*x = x
。
目前,我仍在尝试弄清楚如何开始,因为输入必须是文本文件,就像他们在 SAT 比赛中一样,如下所示:
p cnf 3 4
1 3 -2 0
3 1 0
-1 2 0
2 3 1 0
谢谢。
编辑
parser :-
open(File, read, Stream),
read_literals(Stream,Literals),
close(Stream),
read_clauses(Literals,[],Clauses),
write(Clauses).
read_literals(Stream,Literals) :-
get_char(Stream,C),
read_literals(C,Stream,Literals).
read_literals(end_of_file,_Stream,Literals) :-
!,
Literals = [].
read_literals(' ',Stream,Literals) :-
!,
read_literals(Stream,Literals).
read_line_then_literals('\n',Stream,Literals) :-
!,
read_literals(Stream,Literals).
read_line_then_literals(_C,Stream,Literals) :-
read_line_then_literals(Stream,Literals).
read_literal_then_literals(Stream,As,Literals) :-
get_char(Stream,C),
read_literal_then_literals(C,Stream,As,Literals).
read_literal_then_literals(C,Stream,As,Literals) :-
digit(C),
!,
name(C,[A]),
read_literal_then_literals(Stream,[A|As],Literals).
read_literal_then_literals(C,Stream,As,Literals) :-
reverse(As,RevAs),
name(Literal,RevAs),
Literals = [Literal|Rest_Literals],
read_literals(C,Stream,Rest_Literals).
digit('0').
digit('1').
digit('2').
digit('3').
digit('4').
digit('5').
digit('6').
digit('7').
digit('8').
digit('9').
read_clauses([],[],[]).
read_clauses([0|Literals],Clause,Clauses) :-
!,
reverse(Clause,RevClause),
Clauses=[RevClause|RestClauses],
read_clauses(Literals,[],RestClauses).
read_clauses([Literal|Literals],Clause,Clauses) :-
read_clauses(Literals,[Literal|Clause],Clauses).