gpt4 book ai didi

c# - 当尝试从 swi-prolog 获取列表时,SwiPlCs 抛出 'Precondition Failed'

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:44 25 4
gpt4 key购买 nike

我在尝试从 C# 调用 SWI-Prolog 时遇到问题,它在我的 PlTerm 变量中给我一条消息“Precondition failed”。该程序是针对学校项目的,我没有时间和想法来解决这个问题。它应该向我显示从一个机场到另一个机场的所有可用航类。

让我向您展示我的代码,以便您更好地理解我想说的内容:

aeropuerto(a).
aeropuerto(b).
aeropuerto(c).
aeropuerto(d).
aeropuerto(m).

vuelo(1,b,a,1000).
vuelo(2,a,b,900).
vuelo(3,c,a,1200).
vuelo(4,a,c,1400).
vuelo(5,d,c,500).
vuelo(6,b,d,800).
vuelo(7,d,m,600).
vuelo(8,m,d,700).

vDirecto(A,X,Y,B):-vuelo(A,X,Y,B).

vEscalaSimple([T,M],X,Y,Z):-vDirecto(T,X,C,B),vDirecto(M,C,Y,P),suma(B,P,Z),X\=Y.

suma(B,P,Z):-Z is B + P.

reversa(X,A,B):-vuelo(X,B,A,C).

vuelos([A|_],X,Y,B):-vDirecto(A,X,Y,B).
vuelos([A|T],X,Y,B):-vEscalaSimple([A|T],X,Y,B).
vuelos([A|T],X,Y,B):-vDirecto(A,X,Z,D),vEscalaSimple(T,Z,Y,C),suma(D,C,B),reversa(J,X,Z),X\=Y,not(member(A,T)),not(member(J,T)).

misVuelos([A|T],X,Y,B):-vuelos([A|T],X,Y,B).
misVuelos([A|T],X,Y,B):-vDirecto(A,X,Z,D),vuelos(T,Z,Y,C),suma(D,C,B),reversa(J,X,Z),X\=Y,not(member(A,T)),not(member(J,T)),not(length([A|T],3)).

所以这是我在 SWI-Prolog 中的代码。当我调用“misVuelos(A,B,C,D)”时,Prolog 将所有可用航类显示为列表。一切都很完美,但在尝试用 C# 检索答案时出现了问题。

Environment.SetEnvironmentVariable("SWI_HOME_DIR", @"C:\Program Files\swipl");
Environment.SetEnvironmentVariable("Path", @"C:\Program Files\swipl\bin");
List<string[]> temp = new List<string[]>();
string[] _params = { "-q", "-f", _path };
if (!PlEngine.IsInitialized)
{
try
{
PlEngine.Initialize(_params);

PlTerm t1 = new PlTerm("A"), t2 = new PlTerm("B"), t3 = new PlTerm("C"), t4 = new PlTerm("D");
PlTermV termV = new PlTermV(new PlTerm[] { t1, t2, t3, t4 });

using (PlQuery q = new PlQuery("misVuelos", termV))
{
foreach (PlQueryVariables v in q.SolutionVariables)
{
Console.WriteLine(t1.ToString());
/*temp.Add(new string[] {
v["A"].ToString(),
v["B"].ToString(),
v["C"].ToString(),
v["D"].ToString()
});*/
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
PlEngine.PlCleanup();
}
}
return temp;

当尝试获取变量 t1 的值并将其打印到控制台时,它向我抛出一个错误消息(再次)“前提条件失败”。其他变量运行良好(t2、t3 和 t4),只是 t1 给我带来了问题。我已阅读文档,但没有找到解决方案。

有人关心解释为什么不起作用以及如何解决这个问题?我错过了什么?

最佳答案

4 个月前找到解决方案

NVM,我找到了解决方案。我还是不明白,我稍后会仔细阅读,但现在,它有效! ^^

来源:

http://www-users.york.ac.uk/~sjh1/courses/L334css/complete/complete2su6.html

序言代码更改:

vuelos([A|[]],X,Y,B):-vDirecto(A,X,Y,B).
vuelos([A|[]],X,Y,B):-vEscalaSimple([A|[]],X,Y,B).
vuelos([A|T],X,Y,B):-vDirecto(A,X,Z,D),vEscalaSimple(T,Z,Y,C),suma(D,C,B),reversa(J,X,Z),X\=Y,not(member(A,T)),not(member(J,T)).

现在我可以毫无问题地将解决方案打印到 C# 控制台。

关于c# - 当尝试从 swi-prolog 获取列表时,SwiPlCs 抛出 'Precondition Failed',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com