%%%% ATENÇÃO: parte dos programas neste ficheiro estão como %%%%%% "comentário" %--------------------------------------------------- % Corte de Andares -- modelo 1 (SICStus) %------------------------------------------------ :- use_module(library(clpfd)). solve(Exemp) :- see(Exemp), read(A), read(B), read(C), read(Andares), seen, length(C,NVars), length(Xs,NVars), restrs(A,Xs,B), scalar_product(C,Xs,#=,Z), dominio(Xs,Andares), sum(Xs,#=,Andares), %clpfd labeling([minimize(Z)],Xs), nl, write('optimal solution' ), nl, write_sol(Z,Xs). % restricoes do problema restrs([],_,[]). restrs([Ai|A],X,[AiX|AX]) :- scalar_product(Ai,X,#=,AiX), restrs(A,X,AX). dominio([],_). dominio([Xj|X],N) :- Xj in 0..N, dominio(X,N). % escrever solucao write_sol(Z,X) :- write('Custo: '), write(Z), nl, write_sol_(X,1), nl. write_sol_([],_). write_sol_([0|Xs],N) :- !, Nn is N+1, write_sol_(Xs,Nn). write_sol_([X|Xs],N) :- write(' tipo '), write(N), write(': '), write(X), nl, Nn is N+1, write_sol_(Xs,Nn). %------------------------------------------------ % Conteudo do FICHEIRO DE DADOS %------------------------------------------------ /* [[1,0,1,1,0,0,0,0,0,1], [2,2,1,0,3,2,0,4,0,0], [0,0,1,0,0,1,2,0,0,2], [0,1,0,1,0,0,0,0,2,0], [1,0,0,1,1,1,1,0,1,0], [0,1,1,1,1,0,1,0,0,0]]. [1,6,2,2,2,3]. [10, 10, 11, 12, 12, 12, 14, 15, 15, 20]. 4. */ %------------------------------------------------------------------------- % Corte de Andares -- modelo 2 (SICStus) %------------------------------------------------------------------------ /* solve(Ex) :- see(Ex), read(A), read(B), read(C), read(Andares), seen, length(C,NCortes), length(B,NSalas), length(CYks,Andares), sum(CYks,#=,Z), length(Yks,Andares), domain(Yks,1,NCortes), restrs_simetria(Yks), liga_custo(Yks,CYks,C), matrix(NSalas,Andares,AYks), restr_aij(A,B,AYks,Yks), labeling([minimize(Z)],Yks), write_sol(Z,Yks). matrix(0,_,[]) :- !. matrix(M,N,[Vs|LVs]) :- length(Vs,N), Mm is M-1, matrix(Mm,N,LVs). % Y_k in {1,2,...,N} dominio([],_). dominio([Yk|Yks],N) :- Yk in 1..N, dominio(Yks,N). % Y_k <= Y_(k+1) restrs_simetria([_]). restrs_simetria([Yk,Yk1|Ys]) :- Yk #=< Yk1, restrs_simetria([Yk1|Ys]). % ligar Cyk ao valor do custo de Yk liga_custo([],_,_). liga_custo([Yk|Ys],[Ck|CYks],Cs) :- element(Yk,Cs,Ck), liga_custo(Ys,CYks,Cs). % restricoes do problema restr_aij([],[],_,_). restr_aij([Ai|A],[Bi|B],[AYki|AYks],Yks) :- sum(AYki,#=,Bi), % definido em clpfd liga_aij_(Yks,AYki,Ai), restr_aij(A,B,AYks,Yks). % ligar Aiyk a Yk liga_aij_([],[],_). liga_aij_([Yk|Yks],[AiYk|AYki],Ai) :- element(Yk,Ai,AiYk), % definido em clpfd liga_aij_(Yks,AYki,Ai). % escrever solucao write_sol(Z,X) :- write('Custo: '), write(Z), nl, write_list(X), nl. write_list([]). write_list([X|Xs]) :- write(X), put(32), write_list(Xs). */