不同的变量 - prolog难题解算器

时间:2015-12-15 01:46:27

标签: prolog clpfd

我正在开发基于prolog restrictions的解决方案。

基本上我需要在其中重新排列9个点,以制作骰子。我有9件,7件不同,但2件相同。我想制定一个说明它的限制,我得到了这个:

A #= 100000*V1 + 10000*V2+ 1000*V5, 
element(P1,Vars,A),

B #= 100000*V7 + 10000*V8 + 10*V11 + V12,
element(P2,Vars,B),

C #= 100000*V13 + 10*V17 + V18,
element(P3,Vars,C),

D #= 100000*V19,
element(P4,Vars,D),

E #= 100000*V25 + 10*V29,
element(P5,Vars,E),

F #= 100000*V31,
element(P6,Vars,F),

G #= 100000*V37,
element(P7,Vars,G),

H #= 100*V47,
element(P8,Vars,H),

I #= 0,
element(P9,Vars,I),

F#=P7,
F#=G,

PiecesIndex = [P1, P2, P3, P4, P5, P6, P8, P9],

all_different(PiecesIndex),

每个VXY是该片的一个面,它可以有0,1,2个点,如下面的代码所示。

我需要的帮助是: - 确保所有部件都不同 - 确保F和G相等 - 确保我不使用重复的碎片。

任何人都可以帮助我!我整晚都在这里试图解决这个问题,我疯了!

完整代码:

:-use_module(library(clpfd)).
:-use_module(library(lists)).

dice(Vars):-
Vars=[V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18,
  V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36,
  V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54],
% pecas dos topos
domain([V5, V6, V11, V12, V17, V18, V23, V24, V29, V30, V35, V36, V41, V42,  V47, V48, V53, V54], 0, 1),
% pecas laterais
domain([V1, V2, V3, V4, V7, V8, V9, V10, V13, V14, V15, V16, V19, V20, V21, V22, V25, V26, V27, V28,
    V31, V32, V33, V34, V37, V38, V39, V40, V43, V44, V45, V46, V49, V50, V51, V52], 0,2),
table([
[V1, V2, V3, V4, V5, V6], %%peca1
[V7, V8, V9, V10, V11, V12], %%peca2
[V13, V14, V15, V16, V17, V18], %%peca3
[V19, V20, V21, V22, V23, V24], %%peca4
[V25, V26, V27, V28, V29, V30], %%peca5
[V31, V32, V33, V34, V35, V36], %%peca6
[V37, V38, V39, V40, V41, V42], %%peca7
[V43, V44, V45, V46, V47, V48], %%peca8
[V49, V50, V51, V52, V53, V54]], %%peca9

  [[2,2,0,0,1,0], %%peca1
   [2,1,0,0,1,1], %%peca2
   [2,0,0,0,1,1], %%peca3
   [2,0,0,0,0,0], %%peca4
   [1,0,0,0,1,0], %%peca5
   [1,0,0,0,0,0], %%peca6
   [1,0,0,0,0,0], %%peca7
   [0,0,0,0,1,0], %%peca8
   [0,0,0,0,0,0]]), %%peca9

% restricoes para as faces que envolve os topos de cada peça
% Na figura de descriçºao das peças os topos de cima estão guardados 
% na posicao 5 de cada lista. Os topos de baixo estoa guardados na
% posicao 6 de cada lista


% Restrições quanto à soma de faces opostas(=7)

V2+V19+V7 #= TotalF1,

V8+V37+V25 #= TotalF2,

V5+V23+V11+V53+V47+V41+V29+V35+V17 #= TotalF3,

V18+V36+V30+V54+V48+V42+V6+V24+V12 #= TotalF4,

V1+V49+V13 #= TotalF5,

V16+V31+V26 #= TotalF6,

Totals = [TotalF1, TotalF2, TotalF3, TotalF4, TotalF5, TotalF6],

domain(Totals, 1,6),

all_different(Totals),

TotalF3 + TotalF4 #= 7,
TotalF1 + TotalF6 #= 7,
TotalF5 + TotalF2 #= 7,

A #= 100000*V1 + 10000*V2+ 1000*V5, 
element(P1,Vars,A),

B #= 100000*V7 + 10000*V8 + 10*V11 + V12,
element(P2,Vars,B),

C #= 100000*V13 + 10*V17 + V18,
element(P3,Vars,C),

D #= 100000*V19,
element(P4,Vars,D),

E #= 100000*V25 + 10*V29,
element(P5,Vars,E),

F #= 100000*V31,
element(P6,Vars,F),

G #= 100000*V37,
element(P7,Vars,G),

H #= 100*V47,
element(P8,Vars,H),

I #= 0,
element(P9,Vars,I),

F#=P7,
F#=G,

PiecesIndex = [P1, P2, P3, P4, P5, P6, P8, P9],

all_different(PiecesIndex),


%There are 21 spots on the pieces
%sum(Vars, #=, 21),




labeling([],Vars),
show(Vars).



show([V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18,
  V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36,
  V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54]) :-
write([V1, V2, V3, V4, V5, V6]), nl, 
write([V7, V8, V9, V10, V11, V12]), nl, 
write([V13, V14, V15, V16, V17, V18]), nl, 
write([V19, V20, V21, V22, V23, V24]), nl, 
write([V25, V26, V27, V28, V29, V30]), nl, 
write([V31, V32, V33, V34, V35, V36]), nl, 
write([V37, V38, V39, V40, V41, V42]), nl, 
write([V43, V44, V45, V46, V47, V48]), nl, 
write([V49, V50, V51, V52, V53, V54]), nl. 

祝你好运

0 个答案:

没有答案