gpt4 book ai didi

prolog - `is/2` 中的参数没有充分实例化

转载 作者:行者123 更新时间:2023-12-02 05:11:16 24 4
gpt4 key购买 nike

对于我的“声明性语言”类(class),我们必须编写一个解决七巧板谜题的序言程序。谜题由谜题点的坐标列表来标识。例如,puzzle(7,[(0,0),(8,0),(4,4)]) 是一个标识符为 7 的谜题,代表一个三角形。

这是我解决这个问题的(天真的)方法。执行从调用 tangram(Puzzle, Puts) 开始。该程序从拼图的所有可能部分开始。然后我挑选一 block ,尝试一个位置和旋转,如果这为拼图提供了有效的位置,我就放置拼图。 (= 将 block 放入 Puts 列表中,该列表将在程序结束时返回。)我回溯所有这些可能性。代码如下:

    %Harm De Weirdt
%3e Bachelor Informatica
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% MAIN PROGRAM %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%All possible rotations of a piece.
angle(0).
angle(90).
angle(180).
angle(270).

%Puzzle is a list of the coordinates of the corners of the puzzle to be solved.
%Puts is a list of 7 elements indicating how each piece should be placed in order to solve the puzzle.
tangram(Puzzle, Puts):-
findall(block(BlockId, PointList), block(BlockId, PointList), PossiblePieces),
placePieces(PossiblePieces, Puts, Puzzle).

%placePieces(Pieces, Puts)
%Place all the puzzle pieces from Pieces on the puzzle.
%Puts is a list containing the position of all the pieces.
placePieces([], _,_).
placePieces([block(BlockId, PointList)|OtherPieces], Puts, Puzzle):-
between(0,8,X),
between(0,6,Y),
angle(Angle),
allowedPosition(PointList, (X,Y), Angle, Puzzle, Puts),
append(Puts, [put(BlockId, ((X,Y), Angle))], NewPuts),
placePieces(OtherPieces, NewPuts, Puzzle),
write(Puts).

allowedPosition(Block, (X,Y), Angle, Puzzle, Puts):-
rotatePolygon(Block, Angle, RotatedPolygon),
translatePolygon(RotatedPolygon, (X,Y), TranslatedPolygon),
insideFigure(TranslatedPolygon, Puzzle),
noOverlap(TranslatedPolygon, Puts).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXTRA PREDICATES %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%translate(Point, TranslationVector, TranslatedPoint)
%TranslatedPoint is the result of Translating Point with TranslationVector
translate((X, Y), (TX, TY), (RX, RY)):-
RX is X + TX,
RY is Y + TY.

%translatePolygon(Polygon, TranslationVector, TranslatedPolygon)
%Translates a Polygon, defined by a list of its Points, by a given TranslationVector,
%resulting in the TranslatedPolygon
translatePolygon([], _Vector, []).
translatePolygon([(X,Y)|Rest], (TX, TY), TranslatedPolygon):-
translatePolygon(Rest, (TX, TY), PartiallyTranslatedPolygon),
translate((X, Y), (TX, TY), (NewX, NewY)),
TranslatedPolygon = [(NewX, NewY)| PartiallyTranslatedPolygon].

一些可能的谜题:

[(0,0),(4,0),(4,4),(0,4)]
[(3,0),(5,2),(5,4),(4,5),(2,5),(0,3)]
[(0,0),(6,0),(7,1),(7,3),(3,3)]

运行此命令时出现的问题是出现以下错误:

 ERROR: is/2: Arguments are not sufficiently instantiated

跟踪时,Translate 中的 TX 和 TY 值似乎未实例化。回溯起来,我认为 X 和 Y 没有在 placePieces 谓词中实例化。如果没有剩余值,谓词就会失败,对吗?

我已经查看了我的代码 5 个多小时,但似乎找不到我的错误。希望你们中有人有时间仔细研究一下这个问题,让我回到正确的方向。

提前致谢!

最佳答案

如果您仅使用 CLP(FD) 约束进行算术,此错误就会消失。只需将 (is)/2 替换为约束 (#=)/2:

:- use_module(library(clpfd)).

translate((X, Y), (TX, TY), (RX, RY)):-
RX #= X + TX,
RY #= Y + TY.

重要的是,(#=)/2 可以在所有方向使用,即使变量仍然出现在其参数中。

其他评论:

  1. 考虑使用(-)/2来表示,即X-Y等。
  2. maplist/3将帮助您缩短代码。

关于prolog - `is/2` 中的参数没有充分实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106948/

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