刚开始学习prolog语言,很有趣但是遇到了一个问题,想请教吧内各位大佬。
要完成一个给化学方程式配平的程序,例如:
balance([A-[al-1],B-[o-2]],[C-[al-2,o-3]]).
A = 4,
B = 3,
C = 2.
或者:
balance([A-[li-1,fe-1,o-2],B-[h-2,s-1,o-4]],
[C-[fe-1,s-1,o-4],D-[li-2,s-1,o-4],E-[h-2,o-1],F-[o-2]]).
A = 4, B= 6, C= 4, D = 2, E = 6, F = 1.
还有:
balance([A-[li-1,fe-1,o-2],B-[h-1,cl-1]],
[C-[fe-1,cl-2],D-[li-1,cl-1],E-[h-2,o-1],F-[o-2]]).
A = 4, B= 12, C= 4, D = 4, E = 6, F = 1.
这些通过我的代码都可以验证,但是当我想要验证这下面给两个的时候就毫无反应:
balance([A-[li-1,fe-1,o-2],B-[h-1,n-1,o-3]],
[C-[fe-1,[n-1,o-3]-2],D-[li-1,n-1,o-3],E-[h-2,o-1],F-[o-2]]).
A = 4, B = 12, C = 4, D = 4, E = 6, F = 1
balance([A-[li-1,fe-1,o-2],B-[h-1,c-1,o-1,o-1,h-1],C-[h-2,o-2]],
[D-[fe-1,[c-1,o-1,o-1,h-1]-2],E-[li-1,c-1,o-1,o-1,h-1],
F-[h-2,o-1],G-[o-2]]).
A = 2, B = 6, C = 1, D =2, E = 2, F =4, G = 1
我的代码是:
:- use_module(library(clpfd)).
:- use_module(library(lists)).
:- use_module(library(pairs)).
balance(L, R) :-
extract_keys(L,L_keys),
extract_keys(R,R_keys),
append(L_keys,R_keys,Keys_All),
extraire(L, [], Result_L),
sort(1,@>=,Result_L,Result_L_Sorted),
group_pairs_by_key(Result_L_Sorted, Expr_L),
%writeln(Expr_L),
extraire(R, [], Result_R),
sort(1,@>=,Result_R,Result_R_Sorted),
group_pairs_by_key(Result_R_Sorted, Expr_R),
extract_value_lists(Expr_L,Expr_L_Value),
%writeln(Expr_L_Value),
extract_value_lists(Expr_R,Expr_R_Value),
%writeln(Expr_R_Value).
extract_all_keys_values(Expr_L_Value,Atom_All_L,Coef_All_L),
writeln(Coef_All_L),
writeln(Atom_All_L),
extract_all_keys_values(Expr_R_Value,Atom_All_R,Coef_All_R),
writeln(Coef_All_R),
writeln(Atom_All_R),
solve(Coef_All_L,Atom_All_L,Coef_All_R,Atom_All_R,Keys_All).
extraire([], Result, Result).
extraire([L_coefficient-L_inner|LS], Expr_L, Result) :-
parcours_inner(L_coefficient, L_inner, Expr_L, Expr_L_New),
extraire(LS, Expr_L_New, Result).
parcours_inner(_, [], Expr_L, Expr_L).
parcours_inner(Coefficient, [Atom-Number|Rest], Expr, Expr_L) :-
Tmp1 = pair(Coefficient,Number),
parcours_inner(Coefficient, Rest, [Atom-Tmp1|Expr], Expr_L).
extract_value_lists(Structures, CombinedList) :-
extract_value_lists(Structures, [], CombinedList).
extract_value_lists([], CombinedList, CombinedList).
extract_value_lists([_-Values | RestStructures], Acc, CombinedList) :-
append(Acc,[Values], NewAcc),
extract_value_lists(RestStructures, NewAcc, CombinedList).
extract_all_keys_values(NestedPairsList, CombinedKeys, CombinedValues) :-
extract_all_keys_values(NestedPairsList, [], [], CombinedKeys, CombinedValues).
extract_all_keys_values([], AccKeys, AccValues, AccKeys, AccValues).
extract_all_keys_values([Pairs|RestPairsList], AccKeys, AccValues, CombinedKeys, CombinedValues) :-
extract_keys_values(Pairs, Keys, Values),
append(AccKeys, [Keys], NewAccKeys),
append(AccValues, [Values], NewAccValues),
extract_all_keys_values(RestPairsList, NewAccKeys, NewAccValues, CombinedKeys, CombinedValues).
extract_keys_values(Pairs, Keys, Values) :-
extract_keys_values(Pairs, [], [], Keys, Values).
extract_keys_values([], AccKeys, AccValues, AccKeys, AccValues).
extract_keys_values([pair(Key,Value) | RestPairs], AccKeys, AccValues, Keys, Values) :-
append(AccKeys, [Key], NewAccKeys),
append(AccValues, [Value], NewAccValues),
extract_keys_values(RestPairs, NewAccKeys, NewAccValues, Keys, Values).
extract_keys([], []).
extract_keys([Key-_|T], [Key|TKeys]) :-
extract_keys(T, TKeys).
solve(CoefL, VarL,CoefR,VarR,Vars) :-
Vars ins 1..50,
construct_constraints(CoefL, VarL,CoefR,VarR),
labeling([min], Vars),
maplist(writeln, Vars).
% 构造等式约束
construct_constraints([], _, [], _).
construct_constraints([CoefL|CoefL_Rest], [VarL|VarL_Rest],[CoefR|CoefR_Rest],[VarR|VarR_Rest]) :-
sum_product(CoefL, VarL, Sum1),
sum_product(CoefR, VarR, Sum2),
Sum1 #= Sum2,
construct_constraints(CoefL_Rest, VarL_Rest,CoefR_Rest,VarR_Rest).
% 计算系数和变量的乘积之和
sum_product([], [], 0).
sum_product([Coeff | Coeffs], [Var | Vars], Sum) :-
sum_product(Coeffs, Vars, RestSum),
Sum #= Coeff * Var + RestSum.
请求各位大佬帮忙看看问题所在,十分感谢
要完成一个给化学方程式配平的程序,例如:
balance([A-[al-1],B-[o-2]],[C-[al-2,o-3]]).
A = 4,
B = 3,
C = 2.
或者:
balance([A-[li-1,fe-1,o-2],B-[h-2,s-1,o-4]],
[C-[fe-1,s-1,o-4],D-[li-2,s-1,o-4],E-[h-2,o-1],F-[o-2]]).
A = 4, B= 6, C= 4, D = 2, E = 6, F = 1.
还有:
balance([A-[li-1,fe-1,o-2],B-[h-1,cl-1]],
[C-[fe-1,cl-2],D-[li-1,cl-1],E-[h-2,o-1],F-[o-2]]).
A = 4, B= 12, C= 4, D = 4, E = 6, F = 1.
这些通过我的代码都可以验证,但是当我想要验证这下面给两个的时候就毫无反应:
balance([A-[li-1,fe-1,o-2],B-[h-1,n-1,o-3]],
[C-[fe-1,[n-1,o-3]-2],D-[li-1,n-1,o-3],E-[h-2,o-1],F-[o-2]]).
A = 4, B = 12, C = 4, D = 4, E = 6, F = 1
balance([A-[li-1,fe-1,o-2],B-[h-1,c-1,o-1,o-1,h-1],C-[h-2,o-2]],
[D-[fe-1,[c-1,o-1,o-1,h-1]-2],E-[li-1,c-1,o-1,o-1,h-1],
F-[h-2,o-1],G-[o-2]]).
A = 2, B = 6, C = 1, D =2, E = 2, F =4, G = 1
我的代码是:
:- use_module(library(clpfd)).
:- use_module(library(lists)).
:- use_module(library(pairs)).
balance(L, R) :-
extract_keys(L,L_keys),
extract_keys(R,R_keys),
append(L_keys,R_keys,Keys_All),
extraire(L, [], Result_L),
sort(1,@>=,Result_L,Result_L_Sorted),
group_pairs_by_key(Result_L_Sorted, Expr_L),
%writeln(Expr_L),
extraire(R, [], Result_R),
sort(1,@>=,Result_R,Result_R_Sorted),
group_pairs_by_key(Result_R_Sorted, Expr_R),
extract_value_lists(Expr_L,Expr_L_Value),
%writeln(Expr_L_Value),
extract_value_lists(Expr_R,Expr_R_Value),
%writeln(Expr_R_Value).
extract_all_keys_values(Expr_L_Value,Atom_All_L,Coef_All_L),
writeln(Coef_All_L),
writeln(Atom_All_L),
extract_all_keys_values(Expr_R_Value,Atom_All_R,Coef_All_R),
writeln(Coef_All_R),
writeln(Atom_All_R),
solve(Coef_All_L,Atom_All_L,Coef_All_R,Atom_All_R,Keys_All).
extraire([], Result, Result).
extraire([L_coefficient-L_inner|LS], Expr_L, Result) :-
parcours_inner(L_coefficient, L_inner, Expr_L, Expr_L_New),
extraire(LS, Expr_L_New, Result).
parcours_inner(_, [], Expr_L, Expr_L).
parcours_inner(Coefficient, [Atom-Number|Rest], Expr, Expr_L) :-
Tmp1 = pair(Coefficient,Number),
parcours_inner(Coefficient, Rest, [Atom-Tmp1|Expr], Expr_L).
extract_value_lists(Structures, CombinedList) :-
extract_value_lists(Structures, [], CombinedList).
extract_value_lists([], CombinedList, CombinedList).
extract_value_lists([_-Values | RestStructures], Acc, CombinedList) :-
append(Acc,[Values], NewAcc),
extract_value_lists(RestStructures, NewAcc, CombinedList).
extract_all_keys_values(NestedPairsList, CombinedKeys, CombinedValues) :-
extract_all_keys_values(NestedPairsList, [], [], CombinedKeys, CombinedValues).
extract_all_keys_values([], AccKeys, AccValues, AccKeys, AccValues).
extract_all_keys_values([Pairs|RestPairsList], AccKeys, AccValues, CombinedKeys, CombinedValues) :-
extract_keys_values(Pairs, Keys, Values),
append(AccKeys, [Keys], NewAccKeys),
append(AccValues, [Values], NewAccValues),
extract_all_keys_values(RestPairsList, NewAccKeys, NewAccValues, CombinedKeys, CombinedValues).
extract_keys_values(Pairs, Keys, Values) :-
extract_keys_values(Pairs, [], [], Keys, Values).
extract_keys_values([], AccKeys, AccValues, AccKeys, AccValues).
extract_keys_values([pair(Key,Value) | RestPairs], AccKeys, AccValues, Keys, Values) :-
append(AccKeys, [Key], NewAccKeys),
append(AccValues, [Value], NewAccValues),
extract_keys_values(RestPairs, NewAccKeys, NewAccValues, Keys, Values).
extract_keys([], []).
extract_keys([Key-_|T], [Key|TKeys]) :-
extract_keys(T, TKeys).
solve(CoefL, VarL,CoefR,VarR,Vars) :-
Vars ins 1..50,
construct_constraints(CoefL, VarL,CoefR,VarR),
labeling([min], Vars),
maplist(writeln, Vars).
% 构造等式约束
construct_constraints([], _, [], _).
construct_constraints([CoefL|CoefL_Rest], [VarL|VarL_Rest],[CoefR|CoefR_Rest],[VarR|VarR_Rest]) :-
sum_product(CoefL, VarL, Sum1),
sum_product(CoefR, VarR, Sum2),
Sum1 #= Sum2,
construct_constraints(CoefL_Rest, VarL_Rest,CoefR_Rest,VarR_Rest).
% 计算系数和变量的乘积之和
sum_product([], [], 0).
sum_product([Coeff | Coeffs], [Var | Vars], Sum) :-
sum_product(Coeffs, Vars, RestSum),
Sum #= Coeff * Var + RestSum.
请求各位大佬帮忙看看问题所在,十分感谢