You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Clingo Python API实现命令行-c参数替换常量项功能

实现Clingo Python API等效命令行-c参数的方法

命令行的-c常量定义参数,可以直接在实例化Control对象时通过参数列表传入,和命令行传参格式完全对应。

完整可运行示例代码

from clingo.control import Control

# 实例化时传入-c参数,等效命令行的 -c x=1
ctl = Control(["-c", "x=1"])
# 加载lp文件
ctl.load("foo.lp")
# 接地base程序块,对应默认无参数的程序部分
ctl.ground([("base", [])])

# 1. 输出接地后的基架程序,等效gringo -t的输出效果
print("接地后基架程序:")
print(ctl)

# 2. 求解并遍历输出模型
print("\n求解模型:")
with ctl.solve(yield_=True) as handle:
    for model in handle:
        print(model)

运行效果说明

  • 打印ctl的输出和gringo -t -c x=1 foo.lp的输出完全一致,会得到foo(1).
  • 遍历模型可以拿到所有满足条件的答案集,和clingo命令行求解结果一致

内容的提问来源于stack exchange,提问作者user1747134

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 11:03:03