gpt4 book ai didi

list - 使用 Prolog 读取记录列表并执行有关先前记录的持续计算

转载 作者:行者123 更新时间:2023-12-01 09:12:54 26 4
gpt4 key购买 nike

这是一个 playground 示例,灵感来 self 曾经做过的一项真实任务(复杂得多)。基本流程是从顺序文件中读取记录。一些记录包含需要检查以前的记录以计算值的命令。

这个解决方案的缺点是它需要一个额外的列表,因此需要额外的重复存储。该额外列表在以下代码中称为 REMEMBER。这个例子有一个简单的记录结构,只包含一个数据值,所以复制 REMEMBER 列表中的所有内容并不是真正的问题。但是请假设实际任务涉及更复杂的记录结构,以至于复制 REMEMBER 列表中的所有内容是非常不可取的。

我倾向于使用双向链表,但是根据此链接上的讨论 Doubly Linked List in Prolog似乎这不是 Prolog 做事的方式。因此,我很想知道 Prolog 的处理方式应该是什么。

/*
A file contains sequential records.
There are two types of record.
A data record provides a data value.
An average record provides a count and is a request for an average of the last count data values.
The parse dcg below parses a list from the data file.
The report dcg uses that list to generate a report.

After parse the list looks like this:

[value=5.9,value=4.7,value=7.5,average=3,value=9.0,value=1.1,value=8.3,average=5,value=7.1,value=1.3,value=6.7,value=9.9,value=0.5,value=0.3,value=1.5,value=0.2,average=7,value=2.2,value=7.8,value=2.5,value=4.5,value=2.4,value=9.7,average=4,value=5.2,value=8.5,value=2.2,value=8.0,value=0.7].

An example report looks like this:

[[count=3,total=18.1,average=6.033333333333333],[count=5,total=30.599999999999998,average=6.12],[count=7,total=20.400000000000002,average=2.9142857142857146],[count=4,total=19.1,average=4.775]].
*/

:- use_module(library(dcg/basics)).
:- use_module(library(readutil)).
:- use_module(library(clpfd)).
:- use_module(library(clpr)).

dospy
:-
spy(report),
spy(average),
leash(-all).

:- initialization main.

report(LIST)
-->
% the report starts with nothing to REMEMBER.
report(LIST,[]).

report([value=VALUE|LIST],REMEMBER)
-->
% value in the LIST goes into REMEMBER.
report(LIST,[value=VALUE|REMEMBER]).

report([average=COUNT|LIST],REMEMBER)
-->
% request for average in the LIST.
average(REMEMBER,COUNT),
report(LIST,REMEMBER).

report([],_REMEMBER)
% the LIST is empty so the report is done.
--> [].

average(REMEMBER,COUNT)
-->
% the average starts at 0 then accumulates for COUNT values from REMEMBER.
average(REMEMBER,COUNT,0,0.0).

average([value=VALUE|REMEMBER],COUNT,AT,TOTAL)
-->
% found needed value in the REMEMBER.
clpfd( AT #\= COUNT ),
clpfd( AT_NEXT #= AT + 1 ),
clpr( TOTAL_NEXT = TOTAL + VALUE ),
average(REMEMBER,COUNT,AT_NEXT,TOTAL_NEXT).

average(_REMEMBER,COUNT,COUNT,TOTAL)
-->
% all of the needed value have been seen so calculate and add to report.
clpr( AVERAGE = TOTAL / COUNT ),
[[count=COUNT,total=TOTAL,average=AVERAGE]].

% now the part that does the parse of the data file.

parse(LIST) --> parse(data,LIST).
parse(LIST) --> parse(average,LIST).
parse(LIST) --> parse(end,LIST).

parse(data,[value=FLOAT|LIST])
-->
"data", whites, float(FLOAT), blanks, !,
parse(LIST).

parse(average,[average=COUNT|LIST])
-->
"average", whites, integer(COUNT), blanks, !,
parse(LIST).

parse(end,[])
-->
[].

clpr( CLPR )
-->
{ clpr:{ CLPR } }.

clpfd( CLPFD )
-->
{ CLPFD }.

main :-
system:( read_file_to_codes("doubly_motivation_data.txt",CODES,[]) ),
prolog:( phrase(parse(LIST),CODES) ),
system:( writeq(LIST),writeln(.) ),
prolog:( phrase(report(LIST),REPORT) ),
system:( writeq(REPORT),writeln(.) ).

/* doubly_motivation_data.txt
data 5.9
data 4.7
data 7.5
average 3
data 9.0
data 1.1
data 8.3
average 5
data 7.1
data 1.3
data 6.7
data 9.9
data 0.5
data 0.3
data 1.5
data 0.2
average 7
data 2.2
data 7.8
data 2.5
data 4.5
data 2.4
data 9.7
average 4
data 5.2
data 8.5
data 2.2
data 8.0
data 0.7
*/

最佳答案

遵循 Logtalk + SWI-Prolog 解决方案,不需要双链表的任何具体化。只需要一个堆栈,使用列表简单地实现:

------------ reports.lgt ------------
% load the required modules
:- use_module(library(dcg/basics), []).

% ensure desired interpretation of double-quoted text
:- set_prolog_flag(double_quotes, codes).

% optimize the generated code
:- set_logtalk_flag(optimize, on).


:- object(reports).

:- public(process/2).

:- uses(list, [take/3]).
:- uses(numberlist, [sum/2]).
:- uses(reader, [file_to_codes/2]).

:- use_module(dcg_basics, [blanks//0, whites//0, integer//1, float//1]).

process(File, Report) :-
file_to_codes(File, Codes),
phrase(report(Report), Codes).

report(Report) -->
data(Value),
report(Report, [Value]).

report([], _) -->
eos.
report([Record| Records], Stack) -->
average(Count),
{compute_record(Count, Stack, Record)},
report(Records, Stack).
report(Records, Stack) -->
data(Value),
report(Records, [Value| Stack]).

average(Count) -->
"average", whites, integer(Count), blanks.

data(Value) -->
"data", whites, float(Value), blanks.

compute_record(Count, Stack, r(Count,Total,Average)) :-
take(Count, Stack, Values),
sum(Values, Total),
Average is Total / Count.

:- end_object.
-------------------------------------

使用问题中的数据文件调用示例:

?- {library(types_loader), library(reader_loader), reports}.
...

?- reports::process('doubly_motivation_data.txt', Report).
Report = [r(3, 18.1, 6.033333333333334), r(5, 30.599999999999998, 6.119999999999999), r(7, 20.400000000000002, 2.9142857142857146), r(4, 19.1, 4.775)] .

正如您所注意到的,我对报告使用了比列表列表更合理的表示形式。可以通过组合 take/3 来编写更有效的解决方案。和 sum/2通过避免使用长度为 Count 的堆栈前缀来调用自定义谓词被遍历两次并创建一个临时值列表。例如:

compute_record(Count, Stack, r(Count,Total,Average)) :-
compute_record(0, Count, Stack, 0, Total),
Average is Total / Count.

compute_record(Count, Count, _, Total, Total) :-
!.
compute_record(Count0, Count, [Value| Stack], Total0, Total) :-
Count1 is Count0 + 1,
Total1 is Total0 + Value,
compute_record(Count1, Count, Stack, Total1, Total).

从示例数据文件来看,文件似乎可以以计算文件中所有 值的平均值的请求结束。因此,report//2非终端必须保留整个堆栈,直到处理完所有数据文件。

关于list - 使用 Prolog 读取记录列表并执行有关先前记录的持续计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54407104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com