% From lim@scorpio Thu Jun 17 14:09:28 1993
% adapted for CHRs by thom fruehwirth 930617
% replaced $= by =:= and then removed '$'


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Rational Constraint Solver Source Module
%
% sccsid("@(#)data 1.00             92/06/29").
% sccscr("@(#)  Copyright 1992 ECRC GmbH ").
%
% IDENTIFICATION: examples
%
% AUTHOR:         Pierre Lim
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% X + Y =:= 4
% X - Y =:= 0
% Answer:
%
% X =:= 2, Y =:= 2
%

X + Y =:= 4,
X - Y =:= 0.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-8x + 5y + -1z =:= 18,
%x + -11z + -5y =:= 6,
%-1x + 5y + 5z =:= 0.
% Answer:
%
% x =:= -12/7, y =:= 23/35, z =:= -1

-8 * X + 5 * Y - Z =:= 18,
X - 11 * Z - 5 * Y =:= 6,
-X + 5 * Y + 5 * Z =:= 0. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-11z + -5y + x =:= -6,
%5z + -1x + 5y =:= 0.
%
% Answer:
% 
% z =:= 1, x =:= 5 * Y + 5, y =:= (unconstrained)
%
% Notes:
% CLP(R) compiler
% 
% Y =:= 0.2*X - 1
% Z =:= 1
%
% CHIP compiler
% Z =:= (1)
% X =:= (5) + (5) * _r80 
% Y =:= _r80 
%
% My rational constraint solver produces
% Z =:= 1
% X =:= 5 * _m277 + 5
% Y =:= 1 * _m277
%
%

-11*Z - 5*Y + X =:= -6,
5*Z - X + 5*Y =:= 0.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% X + -5*Y + -11 * Z =:= -6,
% -X + 5* Z + 5* Y =:= 0,
% X + 2* Z + -3* Y =:= 7,
% 8*X + Z + -5*Y + P =:= 18.
%
% Answer: z =:= 1.0, x =:= 5.0, y =:= 0.0, p =:= -23
%

X - 5*Y - 11 * Z =:= -6,
-X + 5* Z + 5* Y =:= 0,
X + 2* Z - 3* Y =:= 7,
8*X + Z - 5*Y + P =:= 18.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-8x + -1z + 5y =:= -18,
%x + -5y + -11z =:= -6,
%-x + 5z + y =:= 0,
%x + 2z + -3y =:= 7.
%
% Answer inconsistent
%

-8*X + -1*Z + 5*Y =:= -18,
X + -5*Y + -11*Z =:= -6,
-X + 5*Z + Y =:= 0,
X + 2*Z + -3*Y =:= 7.

%
% 0 =< X, X =< 10.
%

0 =< X, X =< 10, X =:= 11.              % inconsistent
0 =< X, X =< 10, X =:= 1.               % X =:= 1
X =:= (1/2)/(1/2).                        % X =:= 1


%
% Inequality example 1
/*
X1 + X2 >= 2,
-X1 + X2 >= 1,
X2 =< 3,
X1 >= 0,
X2 >= 0.
*/
%
%
% CHIP compiler
%
% X1 =:= (1/2) + (-1/2) * _rp105  + (1/2) * _rp78 
% X2 =:= (3/2) + (1/2) * _rp105  + (1/2) * _rp78 
%
%

X1 + X2 >= 2,
-X1 + X2 >= 1,
X2 =< 3,
X1 >= 0,
X2 >= 0.
% print_store.

%
%
% Answer: X =:= 5
%

X >= 5,
X =< 5.

%
% x1 + x2 =< 4,
% 2x1 + 3x2 >= 18,
% x1 >= 0,
% x2 >= 0.
%
% Answer: inconsistent

X1 + X2 =< 4,
2 * X1 + 3 * X2 >= 18,
X1 >= 0,
X2 >= 0.

%
%
/*
X1 =< 50,
X2 =< 200,
X1 + 0.2 * X2 =< 72,
150 * X1 + 25 * X2 =< 10000,
Z =:= 250 * X1 + 45 * X2.
*/
%
%
% Answer: CLP(R) compiler
%
% X1 =:= 0.004*Z - 0.18*X2
% Z =< 3.33333*X2 + 16666.7
% Z + 5*X2 =< 18000
% X2 =< 200
% Z =< 45*X2 + 12500
%
% Answer: CHIP compiler
%
% Z =:= (17000) + (-9/5) * _rp161  + (20) * _rp101 
% X1 =:= (50) + (-1) * _rp101 
% X2 =:= (100) + (-1/25) * _rp161  + (6) * _rp101 
%
% First 3 constraints
% X1 =:= (50) + (-1) * _rp67 
% X2 =:= (110) + (-5) * _rp105  + (5) * _rp67
%

X1 =< 50,
X2 =< 200,
X1 + 2/10 * X2 =< 72,
150 * X1 + 25 * X2 =< 10000,
Z =:= 250 * X1 + 45 * X2.
%,output.

/*
Eclipse input:

X1 =< 50,
X2 =< 200,
X1 + 2/10 * X2 =< 72,
150 * X1 + 25 * X2 =< 10000,
Z =:= 250 * X1 + 45 * X2.

*/

%
%
% X4 =< 1 + 3 * X3,
% X4 =< 4/13+18/13*X3,
% X4 >= -1/8+9/8*X3,
% X4 >= -2+6*X3,
% X4 >= -1/11+9/11*X3
/*
X4 - 3 * X3 =< 1,
X4 - 1.38462 * X3 =< 0.307692,
X4 - 1.125 * X3 >= -0.125,
X4 - 6 * X3 >= -2,
X4 - 0.818182 * X3 >= -0.0909091.

X4 - 3 * X3 =< 1,
X4 - 18/13 * X3 =< 4/13,
X4 - 9/8 * X3 >= -1/8,
X4 - 6 * X3 >= -2,
X4 - 9/11 * X3 >= -1/11.
*/
%
% CHIP Compiler
% X4 =:= (-2/7) + (-13/7) * _rp145  + (6/7) * _rp118 
% X3 =:= (-3/7) + (-13/21) * _rp145  + (13/21) * _rp118 
%
% CLP(R) Compiler
%
% 0.818182*X3 =< X4 + 0.0909091
% 6*X3 =< X4 + 2
% 1.125*X3 =< X4 + 0.125
% X4 =< 1.38462*X3 + 0.307692
% X4 =< 3*X3 + 1
% 
%

X4 =< 1+3*X3,
X4 =< 4/13+18/13*X3,
X4 >= -1/8+9/8*X3,
X4 >= -2+6*X3,
X4 >= -1/11+9/11*X3.

%
%
%
% CHIP Compiler
%
% X3 =:= (1/9) * _rp256  + (5/6) * _rp229  + (-4/9) * _rp202  + (-1/2) * _rp159 
% X4 =:= (2/3) * _rp229  + (-1/3) * _rp202 
% X1 =:= (1/9) * _rp256  + (1/3) * _rp229  + (-1/9) * _rp202  + (-1/3) * _rp159 
% X2 =:= (1) + (-1) * _rp256  + (-13/6) * _rp229  + (1/3) * _rp202  +
%       (3/2) * _rp159 
%
%
%example([X1,X2,X3,X4]) :-
  12*X1 +   X2 -  3*X3 +    X4 =< 1,
 -36*X1 - 2*X2 + 18*X3 - 11*X4 =< -2,
 -18*X1 -   X2 +  9*X3 -  7*X4 =< -1,
  45*X1 + 4*X2 - 18*X3 + 13*X4 =< 4,
     X1                        >= 0,
            X2                 >= 0.

%
%
% Small Scheduling Problem
% CHIP Compiler
%
% SA =:= (-7) + (1) * _r218  + (-1) * _rp211 
% SB =:= _r218 
% SD =:= (1) * _r218  + (1) * _rp238  + (-1) * _rp211 
% SC =:= (3) + (1) * _r218  + (1) * _rp264 
% SF =:= (8) + (1) * _r218  + (1) * _rp407  + (1) * _rp238  + (-1) * _rp211 
% SH =:= (9) + (1) * _r218  + (1) * _rp471  + (1) * _rp407  + (1) * _rp238  +
%      (-1) * _rp211 
% SG =:= (8) + (1) * _r218  + (1) * _rp374  + (1) * _rp238  + (-1) * _rp211 
% SE =:= (8) + (1) * _r218  + (1) * _rp314  + (1) * _rp238  + (-1) * _rp211 
% SJ =:= (12) + (1) * _r218  + (1) * _rp506  + (1) * _rp471  + (1) * _rp407  +
%      (1) * _rp238  + (-1) * _rp211 
% Send =:= (15) + (1) * _r218  + (1) * _rp674  + (1) * _rp607  + (1) * _rp506  +
%        (1) * _rp471  + (1) * _rp407  + (1) * _rp238  + (-1) * _rp211 
% SK =:= (14) + (1) * _r218  + (1) * _rp607  + (1) * _rp506  + (1) * _rp471  +
%      (1) * _rp407  + (1) * _rp238  + (-1) * _rp211 
%
%

SB >= SA + 7,
SD >= SA + 7,
SC >= SB + 3,
SE >= SC + 1,
SE >= SD + 8,
SG >= SC + 1,
SG >= SD + 8,
SF >= SD + 8,
SF >= SC + 1,
SH >= SF + 1,
SJ >= SH + 3,
SK >= SG + 1,
SK >= SE + 2,
SK >= SJ + 2,
Send >= SK + 1.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
% Example from paper by De Backer and Beringer
% ``A CLP language handling disjunctions of linear constraints''
%
% ConstrainedMin[ x, {3x -2y =< 4, 3x+2y =< 28, 5 =< y},{x,y}]
% {0, {x -> 0, y -> 5}}
% ConstrainedMax[ x, {3x -2y =< 4, 3x+2y =< 28, 5 =< y},{x,y}]
%  16        16
% {--, {x -> --, y -> 6}}
%  3         3
% ConstrainedMin[y, {3x -2y =< 4, 3x+2y =< 28, 5 =< y},{x,y}]
%           14
% {5, {x -> --, y -> 5}}
%           3
% ConstrainedMax[ y, {3x -2y =< 4, 3x+2y =< 28, 5 =< y},{x,y}]
% {14, {x -> 0, y -> 14}}


3*X - 2*Y =< -4, 3*X + 2*Y =< 28, 5 =< Y. %, rmax(X).
%3*X - 2*Y >= -4, 3*X + 2*Y =< 28, 5 =< Y.%, rmin(X).
%3*X - 2*Y =< -4, 3*X + 2*Y =< 28, 5 =< Y.%, rmax(Y).
%3*X - 2*Y =< -4, 3*X + 2*Y =< 28, 5 =< Y.%, rmin(Y).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

X11 + X12 + X13 + X14 + X15 =:= 1000,
X21 + X22 + X23 + X24 + X25 =:= 1000,

4*X11 + 5*X21 - Y21 - Z21 =< 0,
-4*X12 - 5*X22 + Y22 + Z22 =:= 0,
-4*X13 - 5*X23 + Y24 - Y25 + Z24 - Z25 =:= 0,
Y21 + Z21 =:= 0.

X1>=0,%positive(X1),
X2>=0,%positive(X2),
Y1>=0,%positive(Y1),
Y2>=0,%positive(Y2),
Y1 =:= X1 - X2,
Y2 =:= X2 - X1.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

U1-Z+W =:= 0,
U2 +Z-V =:= 0,
U3 -W + V =:= 0,
U1 >=0,
U2 >= 0,
U3 >= 0,
Z >= 0,
V >= 0,
W >= 0.

U1-Z+2*W =:= 0,
U2 +2*Z-V =:= 0,
U3-W+2*V =:= 0,
U1 >= 0,
U2 >= 0,
U3 >= 0,
Z >= 0,
V >= 0,
W >= 0.

X + 2*Z >=0,
-Z +Y >= 1,
-Y >= 2.


