在序言中表示线性函数 [英] Representing linear functions in prolog

查看:35
本文介绍了在序言中表示线性函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写复合术语,表示 Y = a + b*X 形式的总体中不同变量之间的线性关系(例如,对于由汽车组成的总体而言,fuelConsumption = 2 + 3 * 距离).我在说明关系是关于人口(组)的同时说明每个变量的值都在对象内相关联(即汽车 A 的油耗是 2 + 3 * 汽车 A 的距离,而不是汽车 B 的距离)时遇到问题).

I want to write compound terms that represent linear relations between different variables in a population of the form Y = a + b*X (e.g. fuelConsumption = 2 + 3 * distance, for a population consisting of cars). I have problems with stating that the relation is about a population (group) while at the same stating that values for each variable are linked within objects (i.e. that car A’s fuel consumption is 2 + 3 * car A’s distance, and not car B’s distance).

这可能表示该关系是针对总体的,但没有明确说明每个变量的值都在对象内链接:

This could represent that the relation is for a population but misses to explicitly state that values of each variable are linked within objects:

causes(
    cause(distance),
    effect(fuelConsumption),
    a(2),
    b(3)
).

<小时>

相反,这捕获了每个变量的值在对象内链接,但忽略了复合是一个关系(一条线).这个化合物的每个实例代表两个点,但我想要的是每个实例都是一条线.


Conversely, this captures that values of each variable are linked within objects but misses that the compound is a relation (a line). Each instantiation of this compound represents two points, but what i wanted was each instantiation to be a line.

car(aCar).
car(anotherCar).
causes(
        cause(Car, distance, D),
        effect(Car, fuelConsumption, F)
):- car(Car), F #= 2 + 3 * D.

<小时>

这似乎更接近解决方案,但我仍然不满意,原因有两个: 1. 关于线性关系的陈述应该适用于任何总体,而不仅仅是我在总体项中碰巧指定的对象;2. 距离与油耗相关的函数没有明确(例如,如果存在指数关系等呢?).


This seems closer to a solution but i am still not really satisfied for two reasons: 1. The statement about the linear relation should hold for any population and not just the objects that i happen to specify in the population term; 2. The function that relates distance to fuelConsumption is not made explicit (e.g. what about if there is an exponential relation, etc?).

population([car1, car2, car3, car4]).
causes(
    cause(P, distance),
    effect(P, fuelConsumption),
    a(2),
    b(3)
):-population(P).

<小时>

任何帮助将不胜感激.我的目标是尽可能准确和透明地声明关系(即应该是人类可读的).


Any help would be greatly appreciated. My goal is to declare relations are precisely and transparently as possible (i.e. should be human-readable).

谢谢!

/JC

一些相关问题的链接:
representing-a-system-of-equations-about-classes-对象,
how-to-encode-causal-relations-in-prolog-as-a-linear-function

Some links to related questions:
representing-a-system-of-equations-about-classes-of-objects,
how-to-encode-causal-relations-in-prolog-as-a-linear-function

推荐答案

这只是针对您的问题的又一次盲目尝试.这是一个有趣的问题.但我必须承认,我总是觉得这有点像 X-Y 情况,如果我对你的背景了解得更多,我就能更好地理解你为什么要寻找在我看来如此复杂的东西.

This is just another blind stab at your problem. It's an interesting problem. But I must confess I always feel like it's a bit of an X-Y situation, where if I knew more about your context I would be better able to understand why you're seeking something that looks, to me, so complex.

/*

For all cars in a set, distance causes fuelConsumption; and for any
car in this set, that car’s fuel consumption can be obtained as
2 + 3 * distance for that car.

*/

% prop(Set, PropertyName)
prop(car, distance).
prop(car, fuelConsumption).

% entity(Set, Individual, Properties)
entity(car, car1, [distance=23]).
entity(car, car2, [distance=0]).

%% There are two ways to find a property value:
%%
%%   1. Look for the property in the individual's properties
propvalue(Set, Individual, Property, Value) :-
    entity(Set, Individual, Properties),
    memberchk(Property=Value, Properties).

%%   2. Compute the property using other aspects of the individual
propvalue(Set, Individual, Property, Value) :-
    causes(Set, Individual, Property, Value, Query),
    call(Query).

%% causes(Set, Individual, Property, Value, Goal)
causes(car, C, fuelConsumption, V,
       (propvalue(car, C, distance, D), V is 2 + 3 * D)).

所以我的基本想法是具体化你的对象.prop/2 真的是为了反射,但我目前没有使用它.propvalue/4 抽象了属性是原因还是结果,让您可以统一访问两者.

So my basic idea here is to reify your objects. prop/2 is really here for reflection, but I'm not using it presently. propvalue/4 abstracts over whether the properties are causes or effects, giving you uniform access to either.

这里的关键思想是causes/5.这不是表示数据的最美观的方式,但我认为它汇集了您拥有的核心思想:在这个集合中,这个属性是通过这个计算得出的.这里没有代表的是原因的概念;我只是没有看到在这里具体化的价值.我希望,如果这是我的代码,一般来说我会将 Car 传递到这个计算中,并且你需要的汽车的任何属性,你都会得到.但我从来没有真正完全理解你的有趣问题.

The key idea here is causes/5. This is not the most aesthetically pleasing way to represent the data, but I think it brings together the core ideas you have: that, across this set, this property is derived by this calculation. What is not represented here, really, is the idea of the cause; I just didn't see the value in reifying that here. I would expect, if this were my code, that in general I'd be passing a Car into this calculation and whatever properties of the car you need, you just get. But I have never really fully understood your interesting problem.

如果这很接近,您可以采取一些措施使 UI 更美观.您可以将 causes/5 的前两个参数替换为 car(C) 之类的内容,并使用 =../2 组合该参数在 propvalue/4 中.您也可以使用 :- 将目标与头部分开,并使用 current_predicate/3 重新获得 propvalue/4 中的主体目标.结合这些想法,您将得到一个更简单的 causes/3,如下所示:

If this is close, there are things you can do to make the UI nicer. You could replace the first two arguments of causes/5 with something like car(C) and use =../2 to assemble that parameter in propvalue/4. You could also use :- to separate the goal from the head and use current_predicate/3 to re-obtain the body goal in propvalue/4. Combining these ideas, you would get a simpler causes/3 that would look like this:

propvalue(Set, Individual, Property, Value) :-
    SetIndividual =.. [Set, Individual],
    call(causes, SetIndividual, Property, Value).

causes(car(C), fuelConsumption, V) :-
    propvalue(car, C, distance, D),
    V is 2 + 3 * D.

这里的想法是 causes/3 规则实际上更像是一个由 propvalue/4 访问的数据结构,而不是你将直接调用的东西.propvalue/4 将枚举您的各种群体中所有实体的所有属性(已计算和未计算).您可以通过将 propvalue/4 更改为 propvalue/3 并在此处执行 =../2 解构来进一步提高可读性.但我认为要让它发挥作用,你需要一个类似于 entity/3 的表格表示你的人口,其中每个人的非计算属性.

The idea here is that causes/3 rules are really acting more like a data structure to be accessed by propvalue/4 than something you're going to call directly. propvalue/4 will enumerate all the properties (computed and not) of all the entities in your various populations. You could probably improve the readability more by changing propvalue/4 into propvalue/3 and doing the =../2 destructuring here instead. But I think for it to work, you're going to want a tabular representation of your populations akin to entity/3, with the non-computed properties for each one there.

我认为将这个建模问题转移到 Logtalk 可能是一个很好的主意,它有几个对象的先天概念可能会使您的设计更加简单明了.在我看来,您可能已经厌倦了必须如此冗长地编写您的属性,在这种情况下,您可能会从真实对象中受益匪浅,或者通过引入您自己的语法并将其解析为这个.

I think it would probably be a very good idea to take this modeling problem over to Logtalk, which has several innate notions of object that would probably make your design more straightforward. It seems likely to me that you'd tire of having to write your property-getting so verbosely, in which case you would probably benefit greatly from real objects, or else from introducing your own syntax and parsing it into this.

无论如何,这是我目前对您所追求的最好的猜测,希望对您有所帮助.

Anyway, this is currently my best guess as to what you're after, I hope it helps.

这篇关于在序言中表示线性函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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