简体   繁体   English

如何在表格中显示 SWI-Prolog 程序的结果? (中电问题)

[英]How can I display the results of an SWI-Prolog program in a table? (CLP problem)

I am trying to solve a CLP problem in SWI-Prolog.我正在尝试解决 SWI-Prolog 中的 CLP 问题。 The task is very similar to the zebra problem.该任务与斑马问题非常相似。 There are a total of 25 variables with the domains between 1 to 5. So the related variables will get the same tags.共有 25 个变量,其域在 1 到 5 之间。因此相关变量将获得相同的标签。 (The program is written in hungarian.) (程序是用匈牙利语编写的。)

I want the output to show not only the labels assigned to the variables, but the related variables in a table.我希望 output 不仅显示分配给变量的标签,还显示表中的相关变量。 Is there a way to do this?有没有办法做到这一点?

% Constraint Logic Programming
:- use_module(library(clpfd)).
:- use_module(library(lists)).
% Your program goes here

egyetemista(X, All):-
    All = [Egressy,Fenyvesi,Gallyas,Jeney,Vadkerti,
            Edina, Frida, Gabriella, Jozsef, Vince,
            Budapest,Debrecen,Miskolc,Pecs,Szeged,
            Biologia,Informatika,Jog,Kemia,Magyar],
    All ins 1..5,
    all_different([Egressy,Fenyvesi,Gallyas,Jeney,Vadkerti]),
    all_different([Edina, Frida, Gabriella, Jozsef, Vince]),
    all_different([Budapest,Debrecen,Miskolc,Pecs,Szeged]),
    all_different([Biologia,Informatika,Jog,Kemia,Magyar]),

    Fenyvesi #= Jog,
    Fenyvesi #\= Debrecen,
    Fenyvesi #\= Jozsef,
    Fenyvesi #\= Vince,

    Jozsef #\= Gallyas,
    Jozsef #\= Biologia,
    Jozsef #= Budapest,

    Vadkerti #= Gabriella,
    Vadkerti #\= Kemia,
    Vadkerti #\= Szeged,
    Gabriella #\= Kemia,
    Gabriella #\= Szeged,
    Kemia #= Szeged,

    Jeney #= Pecs,
    Jeney #\= Vince,

    Frida #= Magyar,

    Edina #= Egressy #\ Edina #= Miskolc,

    Informatika #\= Edina,
    Informatika #\= Frida,
    Informatika #\= Gabriella,

    labeling([], All),

    %Szak:
    nth0(N, All, Szeged),
    nth0(N, All, X).

    %egyetemista(X, All)

If you run the program like this, the output is: All = [1, 2, 3, 4, 5, 2, 4, 5, 1, 3, 1, 5, 2, 4, 3, 5, 1, 2, 3, 4], X = 3如果你这样运行程序,output 是:All = [1, 2, 3, 4, 5, 2, 4, 5, 1, 3, 1, 5, 2, 4, 3, 5, 1, 2 , 3, 4], X = 3

Which means that the label assigned to the variable 'Szeged' is 3, and the associated variables are obtained by substituting variables from the All list that also have a label of 3 on the output.这意味着分配给变量 'Szeged' 的 label 为 3,并且通过替换 All 列表中 Z78E6221F6393D1356ZD81DB36 上 label 为 3 的变量来获得关联变量So, for example, the first row of the table could be: 'gallyas vince szeged kemia'因此,例如,表格的第一行可能是:'galyas vince szeged kemia'

Thank you so much in advance.非常感谢你。

Prolog cannot map from a variable back to its name in the source code, so you need to keep a mapping yourself. Prolog 不能从一个变量 map 回到源代码中的名字,所以需要自己保留一个映射。 A simple way to do this is like this:一个简单的方法是这样的:

egyetemista(X, AllPairs):-
    AllPairs = [
        egressy-Egressy, fenyvesi-Fenyvesi, gallyas-Gallyas, jeney-Jeney, vadkerti-Vadkerti,
        edina-Edina, frida-Frida, gabriella-Gabriella, jozsef-Jozsef, vince-Vince,
        budapest-Budapest, debrecen-Debrecen, miskolc-Miskolc, pecs-Pecs, szeged-Szeged,
        biologia-Biologia, informatika-Informatika, jog-Jog, kemia-Kemia, magyar-Magyar
    ],
    pairs_values(AllPairs, All),
    All ins 1..5,
    % ... the rest of your definition unchanged

The result visible in a call of this is a list of Name-Value pairs:调用 this 时可见的结果是Name-Value对列表:

?- egyetemista(X, All).
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] ;
X = 3,
All = [egressy-1, fenyvesi-2, gallyas-3, jeney-4, vadkerti-5, edina-2, frida-4, gabriella-5, ... - ...|...] .  % etc.

You can then filter out only those entries that have their value equal to X , and print those entries in whatever format you like.然后,您可以只过滤掉那些其值等于X的条目,并以您喜欢的任何格式打印这些条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM