distributed config && float library

This commit is contained in:
Vítor Santos Costa
2013-11-03 14:12:38 +00:00
parent e84540b4f0
commit e423fc28e5
85 changed files with 68816 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
% Example with matrices,based on:
%
% Three jugs problem in Minzinc modelled as a shortest path problem.
%
% Problem from Taha "Introduction to Operations Research", page 245
%
% Model created by Hakan Kjellerstrand, hakank@bonetmail.com
% See also my MiniZinc page: http://www.hakank.org/minizinc
%
% VSC: had to transpose the matrix, and change the constraints....
%
:- style_check( all ).
:- use_module(library(gecode/clpfd)).
:- use_module(library(maplist)).
:- use_module(library(lists)).
main :-
problem(Z, X, InFlow, OutFlow, N),
out(Z, X, InFlow, OutFlow, N),
fail.
main.
problem(Z, X, InFlow, OutFlow, N) :-
N = 15,
Start = 1,
End = 15,
M = 999,
d( M, DD ),
D = array[1..N,1..N] of DD, % distance
RHS = array[1..N] of _, % requirements (right hand statement)
X = array[1..N, 1..N] of 0..1, % the resulting matrix, 1 if connected, 0 else
OutFlow = array[1..N] of 0..1,
InFlow = array[1..N] of 0..1,
% objective to minimize
Z in 0..M,
Z #= sum( [I in 1..N, J in 1..N] where D[I,J]<M,
D[I,J]*X[I,J]),
% solve minimize z;
% alternative solve statements which may give faster solution
%solve :: int_search([ x[i,j] | i,j in 1..n], first_fail, indomain_min, complete) minimize z;
% solve minimize z;
minimize(Z),
% constraint
foreach(I in 1..N,
( I == Start ->
RHS[I] <== 1 ;
I == End ->
RHS[I] <== -1 ;
RHS[I] <== 0 )
),
% must be larger than 0??
foreach( [I in 1..N, J in 1..N],
( D[J,I] = M ->
X[J,I] #= 0 ;
true )
),
% outflow constraint
foreach(I in 1..N,
OutFlow[I] #= sum(J in 1..N where D[J,I]<M, X[J,I])
),
% inflow constraint
foreach(J in 1..N,
InFlow[J] #= sum(I in 1..N where D[J,I]<M, X[J,I])
),
% inflow = outflow
foreach(I in 1..N, OutFlow[I]-InFlow[I]#=RHS[I]),
% labeling
labeling( [], X).
% data
d(M, [
M, 1, M, M, M, M, M, M, 1, M, M, M, M, M, M,
M, M, 1, M, M, M, M, M, M, M, M, M, M, M, M,
M, M, M, 1, M, M, M, M, 1, M, M, M, M, M, M,
M, M, M, M, 1, M, M, M, M, M, M, M, M, M, M,
M, M, M, M, M, 1, M, M, 1, M, M, M, M, M, M,
M, M, M, M, M, M, 1, M, M, M, M, M, M, M, M,
M, M, M, M, M, M, M, 1, 1, M, M, M, M, M, M,
M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1,
M, M, M, M, M, M, M, M, M, 1, M, M, M, M, M,
M, 1, M, M, M, M, M, M, M, M, 1, M, M, M, M,
M, M, M, M, M, M, M, M, M, M, M, 1, M, M, M,
M, 1, M, M, M, M, M, M, M, M, M, M, 1, M, M,
M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M,
M, 1, M, M, M, M, M, M, M, M, M, M, M, M, 1,
M, M, M, M, M, M, M, M, M, M, M, M, M, M, M
]).
/*
% shows the result matrix
output [
if i = 1 /\ j = 1 then
"z: " ++ show(z) ++ "\n" ++
"inFlow: " ++ show(inFlow) ++ "\n" ++ "outFlow: " ++ show(outFlow) ++ "\n" ++
" 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n"
else "" endif ++
if j = 1 then show(i) ++ " : " else "" endif ++
show(x[i,j]) ++ if j = n then "\n" else " " endif
| i in 1..n, j in 1..n
];
*/
out(Cost, Ts, Ins, Out, N) :-
format('cost = ~d~n', [Cost]),
InsL <== list(Ins),
OutL <== list(Out),
format('Inputs =', []), maplist(out, InsL), nl,
format('Outputs =', []), maplist(out, OutL), nl,
format('transitions =~n', []),
foreach(I in 1..N, outl(Ts[_,I]) ).
outl( X ) :-
L <== X, % evaluate matrix notation to Prolog lists.
format(' ', []),
maplist(out, L), nl.
out(0) :- format(' .', []).
out(1) :- format(' 1', []).

View File

@@ -0,0 +1,68 @@
%% -*- prolog -*-
%%=============================================================================
%% Copyright (C) 2011 by Denys Duchier
%%
%% This program is free software: you can redistribute it and/or modify it
%% under the terms of the GNU Lesser General Public License as published by the
%% Free Software Foundation, either version 3 of the License, or (at your
%% option) any later version.
%%
%% This program is distributed in the hope that it will be useful, but WITHOUT
%% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
%% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
%% more details.
%%
%% You should have received a copy of the GNU Lesser General Public License
%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%=============================================================================
:- use_module(library(gecode/clpfd)).
:- use_module(library(maplist)).
% 5 people want to have a photograph together, but they have preferences.
photo(Ex, People, Amount) :-
ex(Ex, People, Preferences),
length(People, Len),
Len0 is Len-1,
People ins 0..Len0,
all_distinct(People),
% Bools are the satisfied constraints
maplist(preference_satisfied, Preferences, Bools),
length(Preferences, PLen),
Amount in 0..PLen,
sum( Bools ) #= Amount,
% add all satisfied constraints
maximize(Amount),
labeling([], People).
%reification, use with care
preference_satisfied(X-Y, B) :-
abs(X - Y) #= 1 #<==> B.
ex(s,[Alice,Bob,Carl,Deb,Evan], [Alice-Carl,
Carl-Deb,
Deb-Alice,
Evan-Alice,
Bob-Evan,
Carl-Evan,
Deb-Evan,
Evan-Bob]).
ex(l,[Betty,Chris,Donald,Fred,Gary,Mary,Paul,Peter,Susan],
[Betty-Donald,
Betty-Gary,
Betty-Peter,
Chris-Gary,
Chris-Susan,
Donald-Fred,
Donald-Gary,
Fred-Betty,
Fred-Gary,
Gary-Mary,
Gary-Betty,
Mary-Betty,
Mary-Susan,
Paul-Donald,
Paul-Peter,
Peter-Susan,
Peter-Paul]).

View File

@@ -0,0 +1,38 @@
:- use_module(library(gecode/clpfd)).
:- use_module(library(maplist)).
queens(N, Queens) :-
length(Queens, N),
Queens ins 1..N,
all_distinct(Queens),
foldl(inc, Queens, Inc, 0, _), % [0, 1, 2, .... ]
foldl(dec, Queens, Dec, 0, _), % [0, -1, -2, ... ]
all_distinct(Inc,Queens),
all_distinct(Dec,Queens),
labeling([], Queens).
inc(_, I0, I0, I) :-
I is I0+1.
dec(_, I0, I0, I) :-
I is I0-1.
lqueens(N, Queens) :-
length(Queens, N),
Queens ins 1..N,
all_distinct(Queens),
lconstrain( Queens, 0 ),
labeling([], Queens).
lconstrain([], _).
lconstrain( [Q|Queens], I0) :-
I is I0+1,
foldl(constrain(Q, I0), Queens, I, _),
lconstrain( Queens, I).
constrain(Q, I, R, J, J1) :-
J1 is J+1,
Q + I #\= R + J,
Q - I #\= R - J.

View File

@@ -0,0 +1,34 @@
%% -*- prolog -*-
%%=============================================================================
%% Copyright (C) 2011, 2013 by Denys Duchier, Vitor Santos Costa
%%
%% This program is free software: you can redistribute it and/or modify it
%% under the terms of the GNU Lesser General Public License as published by the
%% Free Software Foundation, either version 3 of the License, or (at your
%% option) any later version.
%%
%% This program is distributed in the hope that it will be useful, but WITHOUT
%% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
%% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
%% more details.
%%
%% You should have received a copy of the GNU Lesser General Public License
%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%=============================================================================
:- use_module(library(gecode/clpfd)).
% S E N D
% + M O R E
% ---------
% M O N E Y
send_more_money(Letters) :-
[S,E,N,D,M,O,R,Y] = Letters,
Letters ins 0..9,
M #\= 0,
S #\= 0,
all_distinct(Letters),
1000*S + 100*E + 10*N + D +
1000*M + 100*O + 10*R + E #=
10000*M + 1000*O + 100*N + 10*E + Y,
labeling([], Letters).

View File

@@ -0,0 +1,36 @@
%% -*- prolog -*-
%%=============================================================================
%% Copyright (C) 2011, 2013 by Denys Duchier, Vitor Santos Costa
%%
%% This program is free software: you can redistribute it and/or modify it
%% under the terms of the GNU Lesser General Public License as published by the
%% Free Software Foundation, either version 3 of the License, or (at your
%% option) any later version.
%%
%% This program is distributed in the hope that it will be useful, but WITHOUT
%% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
%% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
%% more details.
%%
%% You should have received a copy of the GNU Lesser General Public License
%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%=============================================================================
:- use_module(library(gecode/clpfd)).
% S E N D
% + M O S T
% ---------
% M O N E Y
send_most_money(Letters, Money) :-
[S,E,N,D,M,O,T,Y] = Letters,
Letters ins 0..9,
Money in 0..99999,
M #\= 0,
S #\= 0,
all_distinct(Letters),
1000*S + 100*E + 10*N + D +
1000*M + 100*O + 10*S + T #= Money,
10000*M + 1000*O + 100*N + 10*E + Y #= Money,
maximize(Money),
labeling([], Letters).

View File

@@ -0,0 +1,79 @@
:- style_check(all).
:- use_module(library(gecode/clpfd)).
:- use_module(library(maplist)).
sudoku( Ex ) :-
problem(Ex, Els),
output(Els).
%
% gecode constraints
%
problem(Ex, Els) :-
length(Els, 81),
Els ins 1..9,
M <== matrix( Els, [dim=[9,9]] ),
% select rows
foreach( I in 0..8 , all_different(M[I,_]) ),
% select cols
foreach( J in 0..8, all_different(M[_,J]) ),
% select squares
foreach( [I,J] ins 0..2 ,
all_different(M[I*3+(0..2),J*3+(0..2)]) ),
ex(Ex, Els),
% maplist( bound, Els, Exs),
labeling( [], Els ).
% The gecode interface doesn't support wake-ups on binding constained variables, this is the closest.
%
bound(El, X) :-
( nonvar(X) -> El #= X ; true ).
%
% output using matrix library
%
output(Els) :-
M <== matrix( Els, [dim=[9,9]] ),
foreach( I in 0..2 , output(M, I) ),
output_line.
output(M, I) :-
output_line,
foreach( J in 0..2 , output_row(M, J+I*3) ).
output_row( M, Row ) :-
L <== M[Row,_],
format('| ~d ~d ~d | ~d ~d ~d | ~d ~d ~d |~n', L).
output_line :-
format(' ~|~`-t~24+~n', []).
ex( 1, [
_,6,_,1,_,4,_,5,_,
_,_,8,3,_,5,6,_,_,
2,_,_,_,_,_,_,_,1,
8,_,_,4,_,7,_,_,6,
_,_,6,_,_,_,3,_,_,
7,_,_,9,_,1,_,_,4,
5,_,_,_,_,_,_,_,2,
_,_,7,2,_,6,9,_,_,
_,4,_,5,_,8,_,7,_
] ).
ex(2, [
_,_,1,_,8,_,6,_,4,
_,3,7,6,_,_,_,_,_,
5,_,_,_,_,_,_,_,_,
_,_,_,_,_,5,_,_,_,
_,_,6,_,1,_,8,_,_,
_,_,_,4,_,_,_,_,_,
_,_,_,_,_,_,_,_,3,
_,_,_,_,_,7,5,2,_,
8,_,2,_,9,_,7,_,_
] ).

Binary file not shown.