表示关于对象类别的方程组 [英] Representing a system of equations about classes of objects

查看:25
本文介绍了表示关于对象类别的方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关如何在 Prolog 中描述方程组的设计模式的建议,其中每个方程都是关于对象类别和时间点的一般陈述.

I am looking for suggestions about design patterns for how to describe systems of equations in Prolog, where each equation is a general statement about classes of objects and points in time.

说我有:A = 0.5 * B + C^2,B = 5 * D.给定,例如D = 1 且 C = 12,我希望 Prolog 计算 A 的值.但我想在 A、B、C 和 D 是对象和时间的一般类的情况下执行此操作.下面的代码正在运行,但我发现它很冗长,特别是原因谓词主体中的描述谓词(我的描述谓词比示例中的要长).我的目标也是拥有透明且易于他人阅读的代码.

Say that i have: A = 0.5 * B + C^2, and B = 5 * D. Given, e.g. D = 1 and C = 12, I want Prolog to compute the value of A. But I want to do this in a case where A, B, C, and D are general classes of objects and time. The code below is working but I find it to wordy, specially the description predicate in the body of the causes predicate (I have longer description predicates than in the example). My goal is also to have code which is transparent and easy to read for somebody else.

有人对如何表示诸如我的示例代码中的问题有任何建议吗?

Does anybody have suggestions for how problems such as the one in my example code can be represented?

谢谢!

predator(fox).
predator(lion).
prey(rabbit).
prey(deer).
time(1).
time(2).
time(3).

value(description(fox, at, 1), 100).

value(Description, Value):-
    causes(
        _,
        variable(name(_), value(Value), Description)
        ).

causes(
    variable(name(predators), value(X), description(Predator, at, Time1)),
    variable(name(prey), value(Y), description(Prey, at, Time2))
)
:-
predator(Predator), prey(Prey), time(Time1), time(Time2), Time2 > Time1,
value(description(Predator, at, Time1), X), Y is 10 / X.

causes(
    variable(name(prey), value(X), description(Prey, at, Time1)),
    variable(name(predators), value(Y), description(Predator, at, Time2))
)
:-
predator(Predator), prey(Prey), time(Time1), time(Time2), Time2 > Time1,
value(description(Prey, at, Time1), X), Y is 20 / X.

编辑 1:调用上述知识库的示例:

EDIT 1: An example call to the above knowledge base:

?-causes(A, B).

例如返回:

A = variable(name(predators), value(100), description(fox, at, 1)),
B = variable(name(prey), value(0.1), description(rabbit, at, 3))

编辑 2:包含三个变量的示例:

relation(
    effect(variable(name(prey), value(Y), description(Prey, at, Time2))),
    causes([
        variable(name(predators), value(X), description(Predator, at, Time1)),
        variable(name(amountOfFood), value(Z), description(Food, at, Time1))
    ])
)
:-
predator(Predator), prey(Prey), time(Time1), time(Time2), Time2 > Time1, food(Food),
value(description(Predator, at, Time1), X), value(description(Food, at, Time1), Z),
Y is 20 / X * 3 * Z.

推荐答案

一种方法似乎是使用约束并实例化一些变量,例如

A way to go seems to use constraints and to instantiate some variables, e.g.

:-use_module(library(clpr)).

predator(fox).
predator(lion).
prey(rabbit).
prey(deer).
time(1).
time(2).
time(3).

causes(
    variable(name(predators), value(X), description(Predator, at, Time1)),
    variable(name(prey), value(Y), description(Prey, at, Time2))
)
:-
predator(Predator), prey(Prey), time(Time1), time(Time2), Time2 > Time1,
{Y = 10 / X}.

causes(
    variable(name(prey), value(X), description(Prey, at, Time1)),
    variable(name(predators), value(Y), description(Predator, at, Time2))
)
:-
predator(Predator), prey(Prey), time(Time1), time(Time2), Time2 > Time1,
{Y = 20 / X}.

哪个,给定例如目标:

causes(X, variable(name(prey), value(15), description(Prey, at, Time2))).

产量:

Prey = rabbit,
Time2 = 2,
X = variable(name(predators), value(0.6666666666666666), description(fox, at, 1))

...等等.

这篇关于表示关于对象类别的方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆