clingo编译器计算多个值

2024-05-16 22:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在ASP/`clingo(Version 4+)中使用python脚本时,我遇到了一个基本问题。为了说明这一点,我用一个最小的例子重新构造了这个问题。显然,在这个例子中,我不需要使用脚本。然而,在我更复杂的应用程序中,我确实这样做了,在那里我以更容易理解的方式人为地重新创建了问题。在

问题是,在调用聚合/优化时,编译器不知何故没有注册用于索引值的所有完整谓词。相反,它似乎在连续计算最小值,结果是一路输出所有值。(参见下面的输出:注意最小值从59变为19,然后没有变为29。这对代码的#script (python)部分中prg.ground调用的顺序高度敏感。)

这是非常不可取的,我想知道如何避免这个问题。一、 例如,如何修改下面的代码仍然使用python脚本(可能已修改),以便计算出正确的模型。(在这个例子中,显然,谓词min_sel_weight/1的解决方案是min_sel_weight(19),没有其他值。在

节目。

weight("ant",3). weight("bat",53). weight("cat",19). weight("dot",13). weight("eel",29).

#script (python)
import gringo;
def main(prg):
    prg.ground([('base', [])]);
    prg.ground([('sel', ['bat'])]);
    prg.ground([('sel', ['cat'])]);
    prg.ground([('sel', ['eel'])]);
    prg.solve();
#end.

%% call python-script, to select certain objects.
#program sel(t). sel(t).

%% compute minimum of weights of selected objects:
min_sel_weight(X) :- weight(_,X), #min {XX : weight(OBJ,XX),sel(OBJ)} = X.

#show sel/1. #show min_sel_weight/1.

调用clingo 0 myprogramme.lp我得到以下输出:

clingo version 4.5.4
Reading from myprogramme.lp
Solving...
Answer: 1
    sel("bat")
    min_sel_weight(53)
    sel("cat")
    min_sel_weight(19)
    sel("eel")
SATISFIABLE

Models       : 1    
Calls        : 1
Time         : 0.096s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.040s

Tags: 代码脚本objectsscriptmin例子cat谓词
1条回答
网友
1楼 · 发布于 2024-05-16 22:19:52

试试这个:

% instance
weight("ant",3). weight("bat",53). weight("cat",19). weight("dot",13). weight("eel",29).


% Assuming you will get certain selected objects like this:
selected("cat"). selected("bat"). selected("eel"). %this will be python generated

% encoding
selectedWeight(OBJ, XX):- weight(OBJ,XX), selected(OBJ).
1{min_sel_weight(X)}1 :- selectedWeight(_,X), #min {XX : selectedWeight(OBJ,XX),selected(OBJ)} = X.

#show min_sel_weight/1.

输出:

enter image description here

希望这有帮助!在

相关问题 更多 >