Clyngor
什么是Clyngor?
进到它的项目主页,我就被这张图片深深吸引了。
在之前的文章中已经讲到了Answer Set Programming(回答集编程),以.lp做结尾的脚本,如果我们想将使用ASP做任务规划的话,如何将它使用到我们的项目工程当中至关重要。所以,Clyngor就可以来帮我们了。当然,也可以使用clingo 的API,但我们这次来简单讲讲Clyngor。
安装Clyngor
$ pip install clyngor
Python中嵌入ASP
from clyngor import ASP
answers=ASP("""
#你的ASP程序代码
""")
for answer in answers:
print (answer)
或者使用solve:
from clyngor import solve
answers = solve(inline="""
#你的ASP程序代码
""")
for answer in answers:
print (answer)
或者直接调用ASP源码脚本:
answers = solve('concepts.lp')
如果想要更加直接的得到ASP的结果,我们在ASP脚本中使用“#show”后,在Python中:
for answer in answers.sorted:
print (answer)
ASP中嵌入Python
Clyngor提供了converted_types功能,允许从ASP代码内部调用python时避免基于类型注释的样板代码。
#script(python)
from clyngor.upapi import converted_types
@converted_types #如果不使用converted_types,则用户必须确保该f函数返回列表,并且参数为预期类型。
def f():
............
#end. %结束
p(X):-(X)=@f(). %调用
一个简单地例子:输入城市一城市二,这两个城市之间有路。
#script(python)
from clyngor.upapi import converted_types
@converted_types
def road():
city=input('Please input the city:')
return city
#end.
city1(X):-(X)=@road().
city2(Y):-(Y)=@road().
road(X,Y):-city1(X),city2(Y).
% Displa
#show road/2.
结尾
Clyngor项目首页:https://github.com/aluriak/clyngor
Clyngor团队很活跃,他们的更新速度很快,大家可以时不时关注以下。战斗尚未结束,同志还需努力💪。