Clingo / ASP:生成一个故事发电机角色的最佳方法(Clingo/ASP: Best way

2019-10-31 06:52发布

我试图写在Clingo故事发电机。

我想说的“新角色可以,如果现有的字符生下他们生的希望。” 我定义新的字符作为entity(<int\>)这是最好的方法,我可以想到的代表实体。 我不能硬编码这个作为实体不同而不同的号码可以在一个故事中创建。

我的code是:

% Create instances of time, only 3 for testing
time(0..2).

% Arrow of time flows forward
next_t(T, T+1) :- time(T), time(T+1).

% Entity 1 exists at time 0.
entity(1, 0).

% If an entity ever existed, that ID is taken and cannot be assigned to
% other entities
entity_id(ID) :- entity(ID, _).

% If an entity exists, he can give birth to a new entity
% The ID of the new entity will be 1 more than ID of all current entities.
birth(X, Y, T) :- entity(Y, T), X = #max{X1+1:entity_id(X1)}, time(T).

% At each time instant, only 1 entity can be born, as only 1 event can happen per time instant.
% This also should prevent infinite entities to be created.
:- birth(X1, _, T), birth(Y1, _, T), X1!=Y1.

% In case of a birth, create a new entiti the next time instant.
entity(X, T1) :- birth(X, _, T), next(T, T1).

#show entity_id/1.
#show entity/2.
#show birth/3 .

然而,输出为:

entity_id(1) entity(1,0) birth(2,1,0)

entity(2, 1)被从未创建,也不是entity(3, 2)entity(4, 3)

我究竟做错了什么? 有一个更好的方法吗?

Answer 1:

你似乎认为ASP 语句从第一到最后的次序或类似的东西发生。 但事实上,他们只是规则有关原子的集合。 规则总是抱着。 特别是,该规则:

entity_id(ID) :- entity(ID, _).

一无所知重复说。 它只是说,对具有ID的每个实体IDIDentity_id 。 如果您想编码,每个规则的ID被使用一次,你应该把它写成:

:- {entity(ID,_)} > 1; entity_id(ID).

另外你试图建立“ID的”,这是“比目前所有实体多了一个”,但有没有这样的事情“当前”的实体。 所有我们要引导我们的时间步骤。

让我们来尝试,使我们的整个时间步明确的方式写的。


% Create instances of time, only 3 for testing
time(0..2).

% Entity eve exists at time 0.
entity(1, 0).

nextID(2, 0).

% If an entity existed at the previous time-step, they continue to exist
% at the next time-step (as I understand, no entity dies).
entity(ID, T) :- entity(ID, T-1); time(T).

% Any entity who was alive on the previous time-step can give birth to
% a child at time T. This child's ID is the current `nextID`
% Note this is a _choice_ rule. The entity _can_ give birth, they don't
% have to. Also we only allow at most one of these births to happen at
% each time-step.
{birth(ChildID, ParentID, T) : entity(ParentID,T-1)} <= 1 :- nextID(ChildID,T).

% Once born, an ID becomes an entity.
entity(ID,T) :- birth(ID,_,T).

% If an entity was born at the previous time-step, the nextID increases by one
% for this time-step.
nextID(ID+1,T) :- nextID(ID,T-1); time(T); entity(ID,T-1).
% Otherwise it stays the same.
nextID(ID,T) :- nextID(ID,T-1); time(T); not entity(ID,T-1).

#show birth/3.

运行此我发现有5种型号。

$ clingo entities.asp 0
clingo version 5.3.1
Reading from entities.asp
Solving...
Answer: 1

% ^ Nobody is ever born
Answer: 2
birth(2,1,2)
% ^ Nobody born at time 1. 1 births a child at time 2
Answer: 3
birth(2,1,1) birth(3,2,2)
% ^ 1 births a child at time 1 and that child gives birth at time 2.
Answer: 4
birth(2,1,1)
% ^ 1 births a child at time 1. Nobody is born at time 2.
Answer: 5
birth(2,1,1) birth(3,1,2)
% ^ 1 births two children; one at time 1 and another at time 2.
SATISFIABLE

Models       : 5
Calls        : 1
Time         : 0.011s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.004s


文章来源: Clingo/ASP: Best way to generate characters in a story generator