0

我正在尝试用 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)or entity(4, 3)

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

4

1 回答 1

0

您似乎认为 ASP 语句是按从头到尾的顺序发生的或类似的。但实际上它们只是关于原子集合的规则。规则永远成立。特别是,规则:

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

没有提到重复。它只是说对于每个具有 ID 的实体IDID都是一个entity_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
于 2018-09-20T16:35:34.830 回答