如何向量转换阵列在Eclipse(CLP)? (或序言) [英] How to convert vectors to arrays in ECLiPSe (CLP)? (or Prolog)

查看:152
本文介绍了如何向量转换阵列在Eclipse(CLP)? (或序言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决数独谜题含9向量(每个长度为9)矢量格式。眼见为载体的Prolog的链表,我想如果我在一个二维数组格式转换难题首先搜索将走得更快。

I have to solve Sudoku puzzles in the format of a vector containing 9 vectors (of length 9 each). Seeing as vectors are linked lists in Prolog, I figured the search would go faster if I transformed the puzzles in a 2D array format first.

例解谜:

puzzle(P) :- P = 
[[_,_,8,7,_,_,_,_,6],
[4,_,_,_,_,9,_,_,_],
[_,_,_,5,4,6,9,_,_],

[_,_,_,_,_,3,_,5,_],
[_,_,3,_,_,7,6,_,_],
[_,_,_,_,_,_,_,8,9],

[_,7,_,4,_,2,_,_,5],
[8,_,_,9,_,5,_,2,3],
[2,_,9,3,_,8,7,6,_]].

我使用Eclipse CLP实现求解。我想出了迄今为止最好的是写一个这样的域名:

I'm using ECLiPSe CLP to implement a solver. The best I've come up with so far is to write a domain like this:

domain(P):-
  dim(P,[9,9]),
  P[1..9,1..9] :: 1..9.

和拼图一个转换器(参数P是给定的困惑与数独是新定义网格与二维数组)。但我在链接从给定的初始拼图的值,以我的二维数组的麻烦。

and a converter for the puzzle (parameter P is the given puzzle and Sudoku is the new defined grid with the 2D array). But I'm having trouble linking the values from the given initial puzzle to my 2D array.

convertVectorsToArray(Sudoku,P):-
  ( for(I,1,9),
      param(Sudoku,P)
    do
      ( for(J,1,9),
          param(Sudoku,P,I)
        do
          Sudoku[I,J] is P[I,J]
      )
  ).

在这之前,我尝试使用array_list(的http:// eclipseclp。组织/ DOC / BIPS /内核/ termmanip / array_list-2.HTML ),但我一直得到类型错误。我怎么没收到:

Before this, I tried using array_list (http://eclipseclp.org/doc/bips/kernel/termmanip/array_list-2.html), but I kept getting type errors. How I did it before:

convertVectorsToArray(Sudoku,P):-
  ( for(I,1,9),
      param(Sudoku,P)
    do
      ( for(J,1,9),
          param(Sudoku,P,I)
        do
          A is Sudoku[I],
          array_list(A,P[I])
      )
  ).

当我终于独输出的例子拼图p在格式如下:

When my Sudoku finally outputs the example puzzle P in the following format:

Sudoku = []([](_Var1, _Var2, 8, 7, ..., 6), [](4, ...), ...)

然后我会很高兴。

then I'll be happy.

更新

我的array_list再次试图;它几乎与下面code工作:

I tried again with the array_list; it almost works with the following code:

convertVectorsToArray(Sudoku,P):-
  ( for(I,1,9),
      param(Sudoku,P)
    do
      X is Sudoku[I],
      Y is P[I],
      write(I),nl,
      write(X),nl,
      write(Y),nl,
      array_list(X, Y)
  ).

该写在那里看到的向量/阵列的样子。由于某些原因,它停止在第二次迭代(而不是9次)和示例难题的其余部分输出作为矢量的矢量。只有第一个向量被正确分配。

The writes are there to see how the vectors/arrays look like. For some reason, it stops at the second iteration (instead of 9 times) and outputs the rest of the example puzzle as a vector of vectors. Only the first vector gets assigned correctly.

UPDATE2

虽然我相信在jschimpf给出的答案是正确的,我也想通了,我自己的实现:

While I'm sure the answer given by jschimpf is correct, I also figured out my own implementation:

convertVectorsToArray(Sudoku,[],_).
convertVectorsToArray(Sudoku,[Y|Rest],Count):-
  X is Sudoku[Count],
  array_list(X, Y),
  NewCount is Count + 1,
  convertVectorsToArray(Sudoku,Rest,NewCount).

谢谢为什么它没有之前的工作,虽然增加的解释!

Thanks for the added explanation on why it didn't work before though!

推荐答案

最简单的解决方案是直接写您的难题规格为2-D阵列完全避免的转换。月食数组就是简单地用仿函数的结构[]/ N ,所以你可以写:

The easiest solution is to avoid the conversion altogether by writing your puzzle specification directly as a 2-D array. An ECLiPSe "array" is simply a structure with the functor '[]'/N, so you can write:

puzzle(P) :- P = [](
    [](_,_,8,7,_,_,_,_,6),
    [](4,_,_,_,_,9,_,_,_),
    [](_,_,_,5,4,6,9,_,_),

    [](_,_,_,_,_,3,_,5,_),
    [](_,_,3,_,_,7,6,_,_),
    [](_,_,_,_,_,_,_,8,9),

    [](_,7,_,4,_,2,_,_,5),
    [](8,_,_,9,_,5,_,2,3),
    [](2,_,9,3,_,8,7,6,_)).

您就可以直接使用此二维数组作为容器域变量:

You can then use this 2-D array directly as the container for your domain variables:

sudoku(P) :-
    puzzle(P),
    P[1..9,1..9] :: 1..9,
    ...

不过,如果你想保持你的列表-OF-名单谜规范,将其转换为阵列的阵列的格式,你可以使用 array_list / 2 。但由于只适用于一维数组,你必须嵌套层次分别转换:

However, if you want to keep your list-of-lists puzzle specification, and convert that to an array-of-arrays format, you can use array_list/2. But since that only works for 1-D arrays, you have to convert the nesting levels individually:

listoflists_to_matrix(Xss, Xzz) :-
    % list of lists to list of arrays
    ( foreach(Xs,Xss), foreach(Xz,Xzs) do
        array_list(Xz, Xs)
    ),
    % list of arrays to array of arrays
    array_list(Xzz, Xzs).

对于自己的code没有工作的原因:这是由于标符号 P [I] 。这


  • 要求 P 是一个数组(你使用它的列表)

  • 仅在上下文中,其中一个算术前pression的预期,例如的右手边是/ 2 ,算术约束等。

  • requires P to be an array (you were using it on lists)
  • works only in contexts where an arithmetic expression is expected, e.g. the right hand side of is/2, in arithmetic constraints, etc.

这篇关于如何向量转换阵列在Eclipse(CLP)? (或序言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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