distributed config && float library
This commit is contained in:
80
packages/gecode/examples/photo.yap
Normal file
80
packages/gecode/examples/photo.yap
Normal file
@@ -0,0 +1,80 @@
|
||||
%% -*- 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)).
|
||||
:- use_module(library(maplist)).
|
||||
|
||||
% 5 people want to have a photograph together, but they have preferences.
|
||||
photo(Ex, Solution,Amount) :-
|
||||
db(Ex, People, Preferences),
|
||||
Space := space,
|
||||
length(People, Len),
|
||||
Len0 is Len-1,
|
||||
People := intvars(Space,Len,0,Len0),
|
||||
Space += distinct(People),
|
||||
% Bools are the satisfied constraints
|
||||
maplist(preferences(Space, Len), Preferences, Bools),
|
||||
length(Preferences, PLen),
|
||||
Sum := intvar(Space,0,PLen),
|
||||
Space += linear(Bools,'IRT_EQ',Sum),
|
||||
% add all satisfied constraints
|
||||
Space += maximize(Sum),
|
||||
Space += branch(People,'INT_VAR_SIZE_MIN','INT_VAL_MIN'),
|
||||
SolSpace := search(Space),
|
||||
Solution := val(SolSpace,People),
|
||||
Amount := val(SolSpace,Sum).
|
||||
|
||||
%reification, use with care
|
||||
preferences(Space, Len, X-Y, B) :-
|
||||
NLen is -Len,
|
||||
I0 := intvar(Space, NLen, Len),
|
||||
I := intvar(Space, 0, Len),
|
||||
B := boolvar(Space),
|
||||
Space += reify(B, 'RM_EQV', R),
|
||||
Space += linear([1,-1],[X,Y],'IRT_EQ', I0),
|
||||
Space += abs(I0, I),
|
||||
Space += rel(I, 'IRT_EQ', 1, R).
|
||||
|
||||
|
||||
db(s,[Alice,Bob,Carl,Deb,Evan], [Alice-Carl,
|
||||
Carl-Deb,
|
||||
Deb-Alice,
|
||||
Evan-Alice,
|
||||
Bob-Evan,
|
||||
Carl-Evan,
|
||||
Deb-Evan,
|
||||
Evan-Bob]).
|
||||
|
||||
db(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]).
|
67
packages/gecode/examples/queens.yap
Normal file
67
packages/gecode/examples/queens.yap
Normal file
@@ -0,0 +1,67 @@
|
||||
%% -*- 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)).
|
||||
:- use_module(library(maplist)).
|
||||
|
||||
% use alldiff constraints
|
||||
queens(N, Solution) :-
|
||||
Space := space,
|
||||
length(Queens, N),
|
||||
Queens := intvars(Space,N,1,N),
|
||||
Space += distinct(Queens),
|
||||
foldl(inc, Queens, Inc, 0, _),
|
||||
foldl(dec, Queens, Dec, 0, _),
|
||||
Space += distinct(Inc,Queens),
|
||||
Space += distinct(Dec,Queens),
|
||||
Space += branch(Queens, 'INT_VAR_SIZE_MIN', 'INT_VAL_MIN'),
|
||||
SolSpace := search(Space),
|
||||
Solution := val(SolSpace,Queens).
|
||||
|
||||
inc(_, I0, I0, I) :-
|
||||
I is I0+1.
|
||||
|
||||
dec(_, I0, I0, I) :-
|
||||
I is I0-1.
|
||||
|
||||
%
|
||||
% Using gecode linear constraints for diagonals.
|
||||
%
|
||||
lqueens(N, Solution) :-
|
||||
Space := space,
|
||||
length(Queens, N),
|
||||
Queens := intvars(Space,N,1,N),
|
||||
Space += distinct(Queens),
|
||||
lconstrain( Queens, Space, 0),
|
||||
Space += branch(Queens, 'INT_VAR_SIZE_MIN', 'INT_VAL_MIN'),
|
||||
SolSpace := search(Space),
|
||||
Solution := val(SolSpace,Queens).
|
||||
|
||||
lconstrain([], _, _).
|
||||
lconstrain( [Q|Queens], Space, I0) :-
|
||||
I is I0+1,
|
||||
foldl(constrain(Q, I0, Space), Queens, I, _),
|
||||
lconstrain( Queens, Space, I).
|
||||
|
||||
constrain(Q, I, Space, R, J, J1) :-
|
||||
% Q+I != R+J, Q-I != R-J <=> Q-R != J-I, Q-R != I-J,
|
||||
J1 is J+1,
|
||||
Sum is I-J,
|
||||
Diff is J-I,
|
||||
Space += linear([1,-1], [Q,R], 'IRT_NQ', Diff),
|
||||
Space += linear([1,-1], [Q,R], 'IRT_NQ', Sum).
|
41
packages/gecode/examples/send_more_money.yap
Normal file
41
packages/gecode/examples/send_more_money.yap
Normal file
@@ -0,0 +1,41 @@
|
||||
%% -*- 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)).
|
||||
|
||||
% S E N D
|
||||
% + M O R E
|
||||
% ---------
|
||||
% M O N E Y
|
||||
send_more_money(Solution) :-
|
||||
Space := space,
|
||||
Letters := intvars(Space,8,0,9),
|
||||
[S,E,N,D,M,O,R,Y] = Letters,
|
||||
Space += rel(M,'IRT_NQ',0),
|
||||
Space += rel(S,'IRT_NQ',0),
|
||||
Space += distinct(Letters),
|
||||
C = [1000, 100, 10, 1,
|
||||
1000, 100, 10, 1,
|
||||
-10000, -1000, -100, -10, -1],
|
||||
X = [S,E,N,D,
|
||||
M,O,R,E,
|
||||
M,O,N,E,Y],
|
||||
Space += linear(C, X, 'IRT_EQ', 0),
|
||||
Space += branch(Letters, 'INT_VAR_SIZE_MIN', 'INT_VAL_MIN'),
|
||||
SolSpace := search(Space),
|
||||
Solution := val(SolSpace,Letters).
|
45
packages/gecode/examples/send_most_money.yap
Normal file
45
packages/gecode/examples/send_most_money.yap
Normal file
@@ -0,0 +1,45 @@
|
||||
%% -*- 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)).
|
||||
|
||||
% S E N D
|
||||
% + M O S T
|
||||
% ---------
|
||||
% M O N E Y
|
||||
send_most_money(Solution,Amount) :-
|
||||
Space := space,
|
||||
Letters := intvars(Space,8,0,9),
|
||||
[S,E,N,D,M,O,T,Y] = Letters,
|
||||
Space += rel(M,'IRT_NQ',0),
|
||||
Space += rel(S,'IRT_NQ',0),
|
||||
Space += distinct(Letters),
|
||||
C = [1000, 100, 10, 1,
|
||||
1000, 100, 10, 1,
|
||||
-10000, -1000, -100, -10, -1],
|
||||
X = [S,E,N,D,
|
||||
M,O,S,T,
|
||||
M,O,N,E,Y],
|
||||
Space += linear(C, X, 'IRT_EQ', 0),
|
||||
Money := intvar(Space,0,99999),
|
||||
Space += linear([10000,1000,100,10,1],[M,O,N,E,Y],'IRT_EQ',Money),
|
||||
Space += maximize(Money),
|
||||
Space += branch(Letters,'INT_VAR_SIZE_MIN','INT_VAL_MIN'),
|
||||
SolSpace := search(Space),
|
||||
Solution := val(SolSpace,Letters),
|
||||
Amount := val(SolSpace,Money).
|
Reference in New Issue
Block a user