% Benchmark Examples from Pierre Lim including stuff from other example files
% adapted for CHRs by Thom Fruehwirth 940308 for Eclipse CHR library
% the examples*math files are not yet adapted for Sicstus CHR library

/*
nb = not benchmarked
laplace5 = laplace5(M)
laplace7 = laplace7(M)
%laplace12 = laplace12
fact100 = fact
option1=go1
option2=go2
points = topb
holzbaur5 = top5
small_sched = small_sched
large_sched = bridge
*/

%:- nodbgcomp.
%:- set_flag(prefer_rationals,on).

timer(X):- 
	T1 is cputime, once(X), T is cputime-T1,writeln(X=T),nl,fail;true.

/*
:-
timer(laplace5(_)),
timer(laplace7(_)),
timer(chipfact(100,1,_)),
timer(fact(100,_)),
timer(solve1(_,_)),
timer(solve2(_,_)),
timer(topb(_,10)),
timer(top5(_)),  	
timer(small_sched),
timer(laplace12(_)),  
timer(bridge(_)).
*/

/*
:-['math-eager'].
:- set_flag(prefer_rationals,off).

laplace5([[0, 0, 0, 0, 0], [100, 57.1429, 47.3214, 57.1429, 100], [100, 81.25, 75.0, 81.25, 100], [100, 92.8571, 90.1786, 92.8571, 100], [100, 100, 100, 100, 100]]) = 0.6

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, 53.1313, 37.0775, 33.0575, 37.0775, 53.1313, 100], [100, 75.4477, 62.1212, 58.075, 62.1212, 75.4478, 100], [100, 86.5385, 77.8846, 75.0, 77.8846, 86.5385, 100], [100, 92.8215, 87.8788, 86.1558, 87.8788, 92.8215, 100], [100, 96.8687, 94.6533, 93.8656, 94.6533, 96.8687, 100], [100, 100, 100, 100, 100, 100, 100]]) = 5.51667

chipfact(100, 1, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 0.1

fact(100, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 5.13333

solve1(_g944, _g1364) = 0.283333

solve2(5.7, _g704) = 0.8

topb([_g14094, _g11438, _g9074, _g7002, _g5222, _g3734, _g2538, _g1634, _g1022, _g702], 10) = 6.66667

top5([_g830, _g814, _g798, _g782, _g766, _g910, _g894, _g878, _g862, _g846, _g964, _g1020, _g1130, _g1084, _g1068, _g948, ...]) = 3.63333

small_sched = 0.583334

laplace12([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [100, 50.9134, 32.1099, 23.882, 20.0481, 18.4822, 18.4822, 20.0481, 23.882, 32.1099, 50.9134, 100], [100, 71.5439, 53.644, 43.3701, 37.8282, 35.3984, 35.3984, 37.8282, 43.3701, 53.644, 71.5439, 100], [100, 81.6182, 67.5522, 58.1261, 52.4962, 49.885, 49.8849, 52.4962, 58.1261, 67.5522, 81.6182, 100], [100, 87.3765, 76.8207, 69.0858, 64.1456, 61.7602, 61.7602, 64.1456, 69.0858, 76.8207, 87.3765, ...], [100, 91.0673, 83.2681, 77.251, 73.2401, 71.2501, 71.2501, 73.2401, 77.251, 83.2681, ...], [100, 93.6245, 87.9333, 83.41, 80.3136, 78.7499, 78.7499, 80.3136, 83.41, ...], [100, 95.4976, 91.4305, 88.1422, 85.8544, 84.6861, 84.6861, 85.8544, ...], [100, 96.9352, 94.1489, 91.8739, 90.2758, 89.4541, 89.4541, ...], [100, 98.0944, 96.356, 94.9288, 93.9207, 93.4003, ...], [100, 99.0866, 98.2518, 97.5646, 97.0778, ...], [100, 100, 100, 100, ...]]) = 423.567

bridge(_g39340) = 57.45

*/

/*
:-['math-eager'].
:- set_flag(prefer_rationals,on).

laplace5([[0, 0, 0, 0, 0], [100, 400_7, 1325_28, 400_7, 100], [100, 325_4, 75_1, 325_4, 100], [100, 650_7, 2525_28, 650_7, 100], [100, 100, 100, 100, 100]]) = 0.716667

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, 5260_99, 63625_1716, 42545_1287, 63625_1716, 5260_99, 100], [100, 388405_5148, 2050_33, 149485_2574, 2050_33, 388405_5148, 100], [100, 1125_13, 2025_26, 75_1, 2025_26, 1125_13, 100], [100, 477845_5148, 2900_33, 221765_2574, 2900_33, 477845_5148, 100], [100, 9590_99, 162425_1716, 120805_1287, 162425_1716, 9590_99, 100], [100, 100, 100, 100, 100, 100, 100]]) = 7.6

chipfact(100, 1, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 0.1

fact(100, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 4.95

solve1(_g944, _g1364) = 0.3

solve2(5.7, _g704) = 0.799999

topb([_g15030, _g12174, _g9634, _g7410, _g5502, _g3910, _g2634, _g1674, _g1030, _g702], 10) = 6.66667

top5([_g830, _g814, _g798, _g782, _g766, _g910, _g894, _g878, _g862, _g846, _g964, _g1020, _g1116, _g1084, _g1068, _g948, ...]) = 3.85

small_sched = 0.6

laplace12([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [100, 38763048600_761351911, 4395212420897975_136880414671046, 1634489974393600_68440207335523, 1372096199788400_68440207335523, 1264924397859100_68440207335523, 1264924397859100_68440207335523, 1372096199788400_68440207335523, 1634489974393600_68440207335523, 4395212420897975_136880414671046, 38763048600_761351911, 100], [100, 9792959934395825_136880414671046, 40841980350_761351911, 5936514974674025_136880414671046, 2588970426900900_68440207335523, 2422676993788900_68440207335523, 2422676993788900_68440207335523, 2588970426900900_68440207335523, 5936514974674025_136880414671046, 40841980350_761351911, 9792959934395825_136880414671046, 100], [100, 5585964267837000_68440207335523, 9246577786852575_136880414671046, 44254407000_761351911, 7185702053378575_136880414671046, 3414136156606700_68440207335523, 3414136156606700_68440207335523, 7185702053378575_136880414671046, 44254407000_761351911, 9246577786852575_136880414671046, 5585964267837000_68440207335523, 100], [100, 5980067477171500_68440207335523, 5257621757814600_68440207335523, 9456496452702825_136880414671046, 48837352350_761351911, 8453760898683825_136880414671046, 8453760898683825_136880414671046, 48837352350_761351911, 9456496452702825_136880414671046, 5257621757814600_68440207335523, 5980067477171500_68440207335523, ...], [100, 6232663149482100_68440207335523, 5698882434309200_68440207335523, 5287073624341500_68440207335523, 10025129513623175_136880414671046, 108492743075_1522703822, 108492743075_1522703822, 10025129513623175_136880414671046, 5287073624341500_68440207335523, 5698882434309200_68440207335523, ...], [100, 6407681952895400_68440207335523, 6018171205598600_68440207335523, 5708599079893800_68440207335523, 10993360422971625_136880414671046, 119912830225_1522703822, 119912830225_1522703822, 10993360422971625_136880414671046, 5708599079893800_68440207335523, ...], [100, 6535872722948600_68440207335523, 6257521355296000_68440207335523, 12064942556298575_136880414671046, 65365434300_761351911, 11591873566035175_136880414671046, 11591873566035175_136880414671046, 65365434300_761351911, ...], [100, 6634266850050700_68440207335523, 12887140428975025_136880414671046, 69948379650_761351911, 12356983338933825_136880414671046, 6122253339814900_68440207335523, 6122253339814900_68440207335523, ...], [100, 13427207458428775_136880414671046, 73360806300_761351911, 12993891210812175_136880414671046, 6427948660645400_68440207335523, 6392331566960200_68440207335523, ...], [100, 75439738050_761351911, 13448744587591225_136880414671046, 6677341108375600_68440207335523, 6644025800748400_68440207335523, ...], [100, 100, 100, 100, ...]]) = 717.767

bridge(_g43116) = 59.9833

*/


/*
:-['math-lazy'].
:- set_flag(prefer_rationals,off).

laplace5([[0, 0, 0, 0, 0], [100, 57.1428, 47.3214, 57.1429, 100], [100, 81.2497, 75.0, 81.25, 100], [100, 92.8571, 90.1786, 92.8571, 100], [100, 100, 100, 100, 100]]) = 0.466667

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, 53.1315, 37.0777, 33.0576, 37.0775, 53.1313, 100], [100, 75.4484, 62.1216, 58.0751, 62.1213, 75.4478, 100], [100, 86.5385, 77.8847, 75.0, 77.8847, 86.5385, 100], [100, 92.8215, 87.8788, 86.1558, 87.8788, 92.8215, 100], [100, 96.8687, 94.6533, 93.8656, 94.6533, 96.8687, 100], [100, 100, 100, 100, 100, 100, 100]]) = 1.45

chipfact(100, 1, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 0.0999999

fact(100, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 4.78333

solve1(_g944, _g1370) = 0.25

solve2(5.7, _g704) = 0.683332

topb([_g16278, _g13132, _g10340, _g7902, _g5818, _g4088, _g2712, _g1690, _g1022, _g702], 10) = 4.4

top5([_g854, _g838, _g822, _g806, _g790, _g1096, _g1080, _g1064, _g1048, _g1032, _g1312, _g1512, _g1944, _g1718, _g1702, _g1296, ...]) = 1.7

small_sched = 0.349998

laplace12([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [100, 50.9134, 32.11, 23.8822, 20.0483, 18.4824, 18.4824, 20.0482, 23.8821, 32.1099, 50.9135, 100], [100, 71.5438, 53.6443, 43.3705, 37.8287, 35.3988, 35.399, 37.8284, 43.3702, 53.6441, 71.544, 100], [100, 81.6182, 67.5524, 58.1264, 52.4965, 49.8853, 49.8854, 52.4965, 58.1263, 67.5524, 81.6182, 100], [100, 87.3766, 76.8208, 69.086, 64.1458, 61.7605, 61.7605, 64.1458, 69.086, 76.8208, 87.3766, ...], [100, 91.0673, 83.2682, 77.2511, 73.2403, 71.2503, 71.2503, 73.2402, 77.2511, 83.2681, ...], [100, 93.6246, 87.9333, 83.4101, 80.3138, 78.7501, 78.7501, 80.3137, 83.4101, ...], [100, 95.4976, 91.4305, 88.1423, 85.8545, 84.6863, 84.6862, 85.8545, ...], [100, 96.9352, 94.1489, 91.874, 90.2758, 89.4541, 89.4541, ...], [100, 98.0944, 96.356, 94.9288, 93.9207, 93.4003, ...], [100, 99.0866, 98.2518, 97.5646, 97.0778, ...], [100, 100, 100, 100, ...]]) = 21.8

bridge(_g21512) = 21.4667

*/

/*
:-['math-lazy'].
:- set_flag(prefer_rationals,on).

laplace5([[0, 0, 0, 0, 0], [100, 400_7, 1325_28, 400_7, 100], [100, 325_4, 75_1, 325_4, 100], [100, 650_7, 2525_28, 650_7, 100], [100, 100, 100, 100, 100]]) = 0.533333

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, 5260_99, 63625_1716, 42545_1287, 63625_1716, 5260_99, 100], [100, 388405_5148, 2050_33, 149485_2574, 2050_33, 388405_5148, 100], [100, 1125_13, 2025_26, 75_1, 2025_26, 1125_13, 100], [100, 477845_5148, 2900_33, 221765_2574, 2900_33, 477845_5148, 100], [100, 9590_99, 162425_1716, 120805_1287, 162425_1716, 9590_99, 100], [100, 100, 100, 100, 100, 100, 100]]) = 2.16667

chipfact(100, 1, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 0.0999999

fact(100, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 4.83333

solve1(_g944, _g1370) = 0.266666

solve2(5.7, _g704) = 0.683333

topb([_g17726, _g14260, _g11188, _g8510, _g6226, _g4336, _g2840, _g1738, _g1030, _g702], 10) = 4.73333

top5([_g854, _g838, _g822, _g806, _g790, _g1096, _g1080, _g1064, _g1048, _g1032, _g1312, _g1512, _g1958, _g1718, _g1702, _g1296, ...]) = 1.98333

small_sched = 0.366667

laplace12([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [100, 38763048600_761351911, 4395212420897975_136880414671046, 1634489974393600_68440207335523, 1372096199788400_68440207335523, 1264924397859100_68440207335523, 1264924397859100_68440207335523, 1372096199788400_68440207335523, 1634489974393600_68440207335523, 4395212420897975_136880414671046, 38763048600_761351911, 100], [100, 9792959934395825_136880414671046, 40841980350_761351911, 5936514974674025_136880414671046, 2588970426900900_68440207335523, 2422676993788900_68440207335523, 2422676993788900_68440207335523, 2588970426900900_68440207335523, 5936514974674025_136880414671046, 40841980350_761351911, 9792959934395825_136880414671046, 100], [100, 5585964267837000_68440207335523, 9246577786852575_136880414671046, 44254407000_761351911, 7185702053378575_136880414671046, 3414136156606700_68440207335523, 3414136156606700_68440207335523, 7185702053378575_136880414671046, 44254407000_761351911, 9246577786852575_136880414671046, 5585964267837000_68440207335523, 100], [100, 5980067477171500_68440207335523, 5257621757814600_68440207335523, 9456496452702825_136880414671046, 48837352350_761351911, 8453760898683825_136880414671046, 8453760898683825_136880414671046, 48837352350_761351911, 9456496452702825_136880414671046, 5257621757814600_68440207335523, 5980067477171500_68440207335523, ...], [100, 6232663149482100_68440207335523, 5698882434309200_68440207335523, 5287073624341500_68440207335523, 10025129513623175_136880414671046, 108492743075_1522703822, 108492743075_1522703822, 10025129513623175_136880414671046, 5287073624341500_68440207335523, 5698882434309200_68440207335523, ...], [100, 6407681952895400_68440207335523, 6018171205598600_68440207335523, 5708599079893800_68440207335523, 10993360422971625_136880414671046, 119912830225_1522703822, 119912830225_1522703822, 10993360422971625_136880414671046, 5708599079893800_68440207335523, ...], [100, 6535872722948600_68440207335523, 6257521355296000_68440207335523, 12064942556298575_136880414671046, 65365434300_761351911, 11591873566035175_136880414671046, 11591873566035175_136880414671046, 65365434300_761351911, ...], [100, 6634266850050700_68440207335523, 12887140428975025_136880414671046, 69948379650_761351911, 12356983338933825_136880414671046, 6122253339814900_68440207335523, 6122253339814900_68440207335523, ...], [100, 13427207458428775_136880414671046, 73360806300_761351911, 12993891210812175_136880414671046, 6427948660645400_68440207335523, 6392331566960200_68440207335523, ...], [100, 75439738050_761351911, 13448744587591225_136880414671046, 6677341108375600_68440207335523, 6644025800748400_68440207335523, ...], [100, 100, 100, 100, ...]]) = 61.05

bridge(_g25632) = 24.8167

*/


/*
:-['math-gauss'].
:- set_flag(prefer_rationals,off).

laplace5([[0, 0, 0, 0, 0], [100, _g1269, _g1301, _g2123, 100], [100, _g1333, _g2165, _g3283, 100], [100, _g4839, _g6857, _g9467, 100], [100, 100, 100, 100, 100]]) = 1.11667

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, _g910, _g926, _g958, _g990, _g1022, 100], [100, _g942, _g974, _g1006, _g1038, _g1054, 100], [100, _g1084, _g1114, _g1144, _g1174, _g1204, 100], [100, _g1234, _g1264, _g1294, _g1324, _g1354, 100], [100, _g1384, _g1414, _g1444, _g1474, _g1534, 100], [100, 100, 100, 100, 100, 100, 100]]) = 20.0333

calling an undefined procedure _g1770 =:= _g776 * _g1030 in module eclipse
....
topb([_g2601, _g2241, _g1917, _g1629, _g1377, _g1161, _g981, _g837, _g729, _g657], 10) = 0.0166664 
....	% no computation done with inequalities...
*/


/*
%:- nodbgcomp.  % get spurious interupt error if nodbgcomp compiled !!!
:-['math-fourier'].
:- set_flag(prefer_rationals,off).

laplace5([[0, 0, 0, 0, 0], [100, 57.1429, 47.3214, 57.1429, 100], [100, 81.2499, 75.0, 81.25, 100], [100, 92.8571, 90.1786, 92.8571, 100], [100, 100, 100, 100, 100]]) = 0.65

laplace7([[0, 0, 0, 0, 0, 0, 0], [100, 53.1311, 37.0772, 33.0574, 37.0775, 53.1313, 100], [100, 75.4463, 62.1207, 58.0749, 62.1212, 75.4477, 100], [100, 86.538, 77.8843, 74.9999, 77.8846, 86.5384, 100], [100, 92.8213, 87.8786, 86.1557, 87.8787, 92.8215, 100], [100, 96.8686, 94.6532, 93.8655, 94.6532, 96.8687, 100], [100, 100, 100, 100, 100, 100, 100]]) = 8.2

chipfact(100, 1, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 0.166667

fact(100, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) = 7.66667

solve1(_g1016, _g1446) = 0.466665

solve2(5.7, _g754) = 1.5

topb([P, P, P, _g3934, P, _g6800, _g3274, _g1968, _g5166, P], 10) = 4.21667

top5 seems to loop or take very long

small_sched = 3.05

laplace12 seems to loop or take very long

bridge seems to loop or take very long

*/



%============================================================================

% *************************************
% CLP(R) Version 1.1 - Example Programs
% *************************************
%
% Algebraic combinations of options transactions

% heaviside function
h(X, Y, Z) :- {Y < X, Z =:= 0}.
h(X, Y, Z) :- {Y >= X, Z =:= 1}. 

% ramp function
r(X, Y, Z) :- {Y < X , Z =:= 0}.
r(X, Y, Z) :- {Y >= X, Z =:= Y - X}.

% option valuation
value(Type,Buy_or_Sell,S,C,P,I,X,B,Value) :-
	check_param(S,C,P,I,X,B),
	get_sign(Buy_or_Sell,Sign),
	lookup_option(Type,S,C,P,I,X,B,
		     B1,B2,H1,H2,R1,R2),
	h(B1,S,T1),h(B2,S,T2),r(B1,S,T3),r(B2,S,T4),
	{Value =:= Sign*(H1*T1 + H2*T2 + R1*T3 + R2*T4)}.

% safety check
check_param(S,C,P,I,X,B) :-
	{
	  S >= 0, C >= 0, P >= 0,
	  I >= 0, X >= 0, B >= 0 
	}.

% Buy or sell are just opposite
get_sign(buy,S) :-  {S =:= -1}.
get_sign(sell,S) :- {S =:= 1}.

% lookup option vector
lookup_option(Type,S,C,P,I,X,B,B1,B2,H1,H2,R1,R2) :- 
	table(Type,S,C,P,I,X,B,B1,B2,H1,H2,R1,R2).

% Table of values for B1,B2,H1,H2,R1,R2
% generic format - lookup_table(Type,Pos_neg,S,C,P,I,X,B,B1,B2,H1,H2,R1,R2).
% where K to R21 are obtained from the table
% M is a multiplier which is -1 or 1 depending on whether one
% is buying or selling the option
table(	stock,		S,	C,	P,	I,	X,	B,	0,	0,	S*(1+I),	0,	-1,	0).
table(	call,		S,	C,	P,	I,	X,	B,	0,	X,	C*(1+I),	0,	0,	-1).
table(	put,		S,	C,	P,	I,	X,	B,	0,	X,	P*(1+I)-X,	0,	1,	-1).
table(	bond,		S,	C,	P,	I,	X,	B,	0,	0,	B*(1+I),	0,	0,	0).


solve1(Wealth, Stockprice) :-
	{
	  Wealth =:= Wealth1 + Wealth2,
	  X =:= 99,
	  P =:= 10, C =:= 10,
	  I =:= 0
	},
	value(put, buy, Stockprice, _, P, I, X, _, Wealth1), 
	value(call, buy, Stockprice, C, _, I, X, _, Wealth2).
%	dump([Stockprice, Wealth]).

solve2(Wealth, Stockprice) :-
	{I =:= 0.1, P1 =:= 10, X1 =:= 20}, 
	value(put, sell, Stockprice, _, P1, I, X1, _, Wealth1), 
	{P2 =:= 18, X2 =:= 40}, 
	value(put, buy, Stockprice, _, P2, I, X2, _, Wealth2), 
	{C3 =:= 15, X3 =:= 60}, 
	value(call, buy, Stockprice, C3, _, I, X3, _, Wealth3), 
	{C4 =:= 10, X4 =:= 80}, 
	value(call, sell, Stockprice, C4, _, I, X4, _, Wealth4), 
	{Wealth =:= Wealth1 + Wealth2 + Wealth3 + Wealth4}.
%	dump([Stockprice, Wealth]).
/*
go1 :- solve1(Wealth, Stockprice), fail.
go1.

go2 :- solve2(Wealth, Stockprice), fail.
go2.

%?- printf("\n>>> Sample goals: go1/0, go2/0\n", []).

*/



%==============================================================================
%From lim@scorpio Thu Jun 17 14:09:36 1993
% thom fruehwirth 930617
% checked with eager.chr: rpoblem in last big equation (top5/n)
% fourier very slow, maybe loops with beale/1, opt1/1 for sure (opt2/1 works)

rmin(E):- {M=:=E}. % thom: no optimisation
rmax(E):- {(-M)=:=E}. % thom: no optimisation

%! nb
beale([X1,X2,X3,X4,X5,X6,X7]) :-
  {
    X1 + 1/4 * X4 - 8 * X5 - X6 + 9 * X7 =:= 0,
    X2 + 1/2 * X4 - 12 * X5 - 1/2 * X6 + 3 * X7 =:= 0,
    X3 + X6 =:= 1,
    X1 >= 0,
    X2 >= 0,
    X3 >= 0,
    X4 >= 0,
    X5 >= 0,
    X6 >= 0,
    X7 >= 0
  },
    rmin( - 3/4 * X4 + 20 * X5 - 1/2 * X6 + 6* X7).

%!
topb(L,N) :-
    topb1([],L,0,N).

topb1(Li,Li,I,I) :- !.
topb1(Li,Lo,I,N) :-
    insertb(P,Li,Lt),
    J is I+1,
    putcons(Lt,1,J),
    topb1(Lt,Lo,J,N).

insertb(P,[],[P]).
insertb(P,[A|B],[P,A|B]).
insertb(P,[A|B],[A|C]) :-
    insertb(P,B,C).

putcons(_,M,N) :-
    M > N,
    !.
putcons([P|R],M,N) :-
    M0 is M - 1,
%   bwriteln(P > M0/N),
%   bwriteln(P < M/N),
    { P > M0/N, P < M/N },
    M1 is M + 1,
    putcons(R,M1,N).

bwriteln(X) :-
    writeln(X).
bwriteln(X) :-
    writeln(delete(X)).

%!
fib(X,Y) :- {X =:= 0, Y =:= 1}.
fib(X,Y) :- {X =:= 1, Y =:= 1}.
fib(N,Z) :-
    {N > 1, Z =:= X1 + X2},
    fib(N-1,X1),
    fib(N-2,X2).


laplace([_, _]) :- !.
laplace([H1, H2, H3|T]):-
        laplace_vec(H1, H2, H3),
        laplace([H2, H3|T]).

laplace_vec([_, _], [_, _], [_, _]) :- !.
laplace_vec([_TL, T, TR|T1], [ML, M, MR|T2], [_BL, B, BR|T3]):-
        { B + T + ML + MR - 4 * M =:= 0 },
        laplace_vec([T, TR|T1], [M, MR|T2], [B, BR|T3]).

%!
laplace5(M) :-
    M = [
         [0,0,0,0,0],
         [100,R,S,T,100],
         [100,U,V,W,100],
         [100,X,Y,Z,100],
         [100,100,100,100,100]
         ],
    laplace(M).

% [chipc]: laplace7(X).
%
% X = [[0, 0, 0, 0, 0, 0, 0], [100, (5260/99), (63625/1716), (42545/1287), (63625/1716), (5260/99), 100], [100, (388405/5148), (2050/33), (149485/2574), (2050/33), (388405/5148), 100], [100, (1125/13), (2025/26), (75), (2025/26), (1125/13), 100], [100, (477845/5148), (2900/33), (221765/2574), (2900/33), (477845/5148), 100], [100, (9590/99), (162425/1716), (120805/1287), (162425/1716), (9590/99), 100], [100, 100, 100, 100, 100, 100, 100]]

%!
laplace7(M) :-
    M = [
         [0,0,0,0,0,0,0],
         [100,R11,R12,R13,R14,R15,100],
         [100,R21,R22,R23,R24,R25,100],
         [100,R31,R32,R33,R34,R35,100],
         [100,R41,R42,R43,R44,R45,100],
         [100,R51,R52,R53,R54,R55,100],
         [100,100,100,100,100,100,100]
        ],
    laplace(M).

laplace12(M) :-
    M = [
         [0,  0,0,0,0,0,0,0,0,0,0,0],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,_,_,_,_,_,_,_,_,_,_,100],
         [100,100,100,100,100,100,100,100,100,100,100,100]
        ],
    laplace(M).


% [chipc]: chipOpt(X,Y,Z).
% 
% X = (8/5)
% Y = (6/5)
% Z = (14/5)

%!
chipOpt(X1,X2,X3) :-
    {X1 + 2 * X2 =< 4,
     3 * X1 + X2 =< 6,
     X3 =:= X1 + X2},
    rmax(X3).

%! chipfact(n,1,N)
chipfact(X,Y,M) :-
    {X =:= 0},
    !,
    {Y =:= M}.
chipfact(X,Y,M) :-
    {X1 =:= X - 1,
     M1 =:= X * Y},
    chipfact(X1,M1,M).

%! order of magnitude slower than chipfact
fact(X,Y) :- {X =:= 0, Y =:= 1}.
fact(X,Y) :- {X =:= 1, Y =:= 1}.
fact(N,R) :- {1 < N, N =< R, M =:= N-1}, fact(M,T), {R =:= N * T}.

mg(P,T,I,B,MP):-
    {T > 0,
     T =< 1,
     B + MP =:= P * (1 + I/100)
    }.
mg(P,T,I,B,MP):-
    {T > 1,
     I1 =:= I / 100,
     T1 =:= T -1,
     P2 =:= P * (1 + I1) - MP
    },
    mg(P2, T1, I, B, MP).

mg1(X,Y,Z) :-
   {
    2 =:= T,
    1 =:= I,
    T > 1,
    I1 =:= I / 100,
    T1 =:= T -1,
    P2 =:= P * (1 + I1) - MP,
    T1 > 0,
    T1 =< 1,
    B + MP =:= P2 * (1 + I/100)
   }.

% [chipc]: top0(X,Y,Z).
%
% X = _r94 
% Y = (-9462212541120451001/1000000000000000000) * _r94  + (10462212541120451001/1000000000000000000) * _r90 
% Z = (101/100) * _r94  + (-1) * _r90      More? (;) 
%
% ---------------------------------------------------------
% [chipc]: top1(X,Y,Z).
%
% X = _r94 
% Y = (-101/100) * _r94  + (201/100) * _r90 
% Z = (101/100) * _r94  + (-1) * _r90      More? (;) 
% ---------------------------------------------------------
% [chipc]: top(X,Y,Z).
% 
% X = _r94 
% Y = (-2101900399479668244827490915525641902001/100000000000000000000000000000000000000) * _r94  + (2201900399479668244827490915525641902001/100000000000000000000000000000000000000) * _r90 
% Z = (101/100) * _r94  + (-1) * _r90      More? (;) 
%

%!
top0(P, B, MP) :- mg(P,10,1,B,MP).
top1(P, B, MP) :- mg(P,2,1,B,MP).
top(P, B, MP) :- mg(P,20,1,B,MP).
  
% Detection of Implied Equalities

%!                       
top4([A,B,C,D]) :-
	{
         A=<B, 
	 B=<C, 
	 C=<D, 
	 A>=D
	}. 

%     B = A,
%     C = A,
%     D = A  
/*
L = [A_m108, B_m128, C_m336, D_m660] 
 
Constraints:
eq0([B_m128 * 1, D_m660 * -1], 0)
eq0([C_m336 * 1, D_m660 * -1], 0)
eq0([A_m108 * -1, D_m660 * 1], 0)

*/ 

%
% X = [(1/3), (0), (13/3)]
%
%! nb
opt1([X1,X2,X3]) :-
 {
    X1 + X2 + 2 * X3 =< 9,
    X1 + X2 - X3     =< 2,
   -X1 + X2 + X3     =< 4,
    X1 >= 0,
    X2 >= 0,
    X3 >= 0
 },
    rmin(X1 + X2 - 4 * X3).

%
% X = [(0), (0)]
%
%! nb
opt2([X1,X2]) :-
 {
    X1 + 2 * X2 =< 4,
    X2 =< 1,
    X1 >= 0,
    X2 >= 0
 },
    rmin(X1 + X2).


% resistor example nb

available_res(10).
available_res(14).
available_res(27).
available_res(60).
available_res(100).
available_cell(10).
available_cell(20).

ohm(V,I,R) :-
%    bwriteln(V =:= I * R),
    {V =:= I * R}.

sum([],Z) :- 
%    bwriteln(Z =:= 0),
    {Z =:= 0}.
sum([H|T],N) :-
%    bwriteln(N =:= H + M),
    {N =:= H + M},
    sum(T,M).

kirchoff(L) :-
    sum(L,0).

% X = [(200/37), (540/37)]     More? (;) 
% 
% X = [(140/37), (600/37)]     More? (;) 
% 
% X = [(540/127), (2000/127)]     More? (;) 

%!
ohm_example([V1,V2]) :-
    {29/2 < V2, V2 < 65/4},
    available_res(R1),
    available_res(R2),
    available_cell(V),
    ohm(V1,I1,R1), ohm(V2,I2,R2),
    kirchoff([I1,-I2]), kirchoff([-V,V1,V2]).

% X = [10, 27, 20]     More? (;) 
% 
% X = [14, 60, 20]     More? (;) 
% 
% X = [27, 100, 20]     More? (;) 
%!
ohm_example1([R1,R2,V]) :-
    {29/2 < V2, V2 < 65/4},
    available_res(R1),
    available_res(R2),
    available_cell(V),
    ohm(V1,I1,R1), ohm(V2,I2,R2),
    kirchoff([I1,-I2]), kirchoff([-V,V1,V2]).

%
% X = [(14), (60), (20)]
%
%!
ohm1([A,B,C]) :-
 {
    A =:= 14,
    B =:= 60,
    C =:= 20,
    29/2 < V2, V2 < 65/4,
    V1/A - V2/B =:= 0,
    V1 + V2 =:= C
 }.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%!
example( [X0,X1,X2,X3,X4]) :-
{
        87*X0  +52*X1  +27*X2  -54*X3  +56*X4 =<  -93,
        33*X0  -10*X1  +61*X2  -28*X3  -29*X4 =<   63,
       -68*X0   +8*X1  +35*X2  +68*X3  +35*X4 =<  -85,
        90*X0  +60*X1  -76*X2  -53*X3  +24*X4 =<  -68,
       -95*X0  -10*X1  +64*X2  +76*X3  -24*X4 =<   33,
        43*X0  -22*X1  +67*X2  -68*X3  -92*X4 =<  -97,
        39*X0   +7*X1  +62*X2  +54*X3  -26*X4 =<  -27,
        48*X0  -13*X1   +7*X2  -61*X3  -59*X4 =<   -2,
        49*X0  -23*X1  -31*X2  -76*X3  +27*X4 =<    3,
       -50*X0  +58*X1   -1*X2  +57*X3  +20*X4 =<    6,
       -13*X0  -63*X1  +81*X2   -3*X3  +70*X4 =<   64,
        20*X0  +67*X1  -23*X2  -41*X3  -66*X4 =<   52,
       -81*X0  -44*X1  +19*X2  -22*X3  -73*X4 =<  -17,
       -43*X0   -9*X1  +14*X2  +27*X3  +40*X4 =<   39,
        16*X0  +83*X1  +89*X2  +25*X3  +55*X4 =<   36,
        +2*X0  +40*X1  +65*X2  +59*X3  -32*X4 =<   13,
       -65*X0  -11*X1  +10*X2  -13*X3  +91*X4 =<   49,
        93*X0  -73*X1  +91*X2   -1*X3  +23*X4 =<  -87
}.



top2 :- example( [X0,X1,X2,X3,X4]).

%     X3<=-5/4-35/68*X2-2/17*X1+X0-35/68*X4,
%     X3<=68/53-76/53*X2+60/53*X1+90/53*X0+24/53*X4,
%     X3<=-1/2-31/27*X2-7/54*X1-13/18*X0+13/27*X4,
%     X3<=17/22+19/22*X2-2*X1-81/22*X0-73/22*X4,
%     X3<=33/76-16/19*X2+5/38*X1+5/4*X0+6/19*X4,
%     X3>=87+91*X2-73*X1+93*X0+23*X4,
%     X3>=-3/76-31/76*X2-23/76*X1+49/76*X0+27/76*X4,
%     X3<=13/9-14/27*X2+1/3*X1+43/27*X0-40/27*X4,
%     X3<=2/19+1/57*X2-58/57*X1+50/57*X0-20/57*X4 

top3 :- example( [X0,_,_,_,X4]).

%     X0>=477804/40409+6973307/969816*X4,
%     X0>=7357764/4517605-5006476/13552815*X4,
%     X0>=58416/36205-4659804/12418315*X4,
%     X0>=3139326/1972045-745308/1972045*X4,
%     X0>=67158/43105-16394/43105*X4,
%     X0>=1327097/6210451-2619277/6210451*X4,
%     X0<=-688135/1217232-2174029/811488*X4 

% [chipc]: top5(X).
% 
% X = [(0), (0), (0), (1000), (0), (0), (50), (200/9) + (-1/9) * _rp522 , (1000/3), (5350/9) + (1/9) * _rp522 , (4000), (0), (7450/3), (0), (0), (5000), (250), (600), (1000/9) + (-5/9) * _rp522 , (0)]
% yes.

top5([X11,X12,X13,X14,X15,X21,X22,X23,X24,X25,Y21,Y22,Y23,Y24,Y25,Z21,Z22,Z23,Z24,Z25]) :-
  {   
              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,
             -4*X14 - 5*X24 + Y21 - Y22 - Y23 + Y25
                            + Z21 - Z22 - Z23 + Z25 =:= 0,
             -4*X15 - 5*X25 + Y23 - Y24 + Z23 - Z24 =:= 0,
            
              7*X11 + 9*X21 >= 0,
              7*X12 + 9*X22 =< 3000,
              7*X13 + 9*X23 =< 200,
              7*X14 + 9*X24 =< 10000,
              7*X15 + 9*X25 =< 7000,
            
              Z21 =< 5000,
              Z22 =< 250,
              Z23 =< 600,
              Z24 =< 7000,
              Z25 =< 4000,
            
              X11 >= 0,
              X12 >= 0,
              X13 >= 0,
              X14 >= 0,
              X15 >= 0,
              X21 >= 0, X22 >= 0, X23 >= 0, X24 >= 0, X25 >= 0,
            
              Y21 >= 0, Y22 >= 0, Y23 >= 0, Y24 >= 0, Y25 >= 0,
            
              Z21 >= 0, Z22 >= 0, Z23 >= 0, Z24 >= 0, Z25 >= 0,
            
              M =:= 99999,
              - Min =:= 99999 * X11 + 99999 * X21 + 4 * Y21 + 7 * Y22 + 
			3 * Y23 +  8*Y24 + 5*Y25
  },
              rmax(Min).

%!
top5a(List) :-
	List = [X11,X12,X13,X14,X15,X21,X22,X23,X24,X25,Y21,Y22,Y23,Y24,Y25,Z21,Z22,Z23,Z24,Z25],
  {
              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,
             -4*X14 - 5*X24 + Y21 - Y22 - Y23 + Y25
                            + Z21 - Z22 - Z23 + Z25 =:= 0,
             -4*X15 - 5*X25 + Y23 - Y24 + Z23 - Z24 =:= 0,
            
              7*X11 + 9*X21 >= 0,
              7*X12 + 9*X22 =< 3000,
              7*X13 + 9*X23 =< 200,
              7*X14 + 9*X24 =< 10000,
              7*X15 + 9*X25 =< 7000,
            
              Z21 =< 5000,
              Z22 =< 250,
              Z23 =< 600,
              Z24 =< 7000,
              Z25 =< 4000,
            
              X11 >= 0,
              X12 >= 0,
              X13 >= 0,
              X14 >= 0,
              X15 >= 0,
              X21 >= 0, X22 >= 0, X23 >= 0, X24 >= 0, X25 >= 0,
            
              Y21 >= 0, Y22 >= 0, Y23 >= 0, Y24 >= 0, Y25 >= 0,
            
              Z21 >= 0, Z22 >= 0, Z23 >= 0, Z24 >= 0, Z25 >= 0,
            
              M =:= 99999,
	      Min = 23450,
              - Min =:= 99999 * X11 + 99999 * X21 + 4 * Y21 + 7 * Y22 + 
			3 * Y23 +  8*Y24 + 5*Y25
  }.

%       M = 99999,
%       Min = 23450,
%       X11 = 0,
%       X12 = 0,
%       X13 = 0,
%       X14 = 1000,
%       X15 = 0,
%       X21 = 0,
%       X22 = 50,
%       X23 = 1850/3-X25,
%       X24 = 1000/3,
%       Y21 = 4000,
%       Y22 = 0,
%       Y23 = 7450/3,
%       Y24 = 0,
%       Y25 = 0,
%       Z21 = 5000,
%       Z22 = 250,
%       Z23 = 600,
%       Z24 = 9250/3-5*X25,
%       Z25 = 0,
%       X25>=5350/9,
%       X25<=1850/3 



%=============================================================================
%From lim@scorpio Thu Jun 17 14:09:28 1993
% thom fruehwirth 930617

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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
%

test40:-
{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

test8518:-
{-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
%
%

test1156:-
{-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
%

test5116:-
{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
%

test815:-
{-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.
%

test01011:-
{0 =< X, X =< 10, X =:= 11}.              % inconsistent
test0101:-
{0 =< X, X =< 10, X =:= 1}.               % X =:= 1
test1212:-
{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 
%
%

test112:-
{X1 + X2 >= 2,
-X1 + X2 >= 1,
X2 =< 3,
X1 >= 0,
X2 >= 0}.
% print_store.

%
%
% Answer: X =:= 5
%

test55:-
{X >= 5,
X =< 5}.

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

test114:-
{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
%

test50200:-
{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
% 
%

test13:-
{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 
%
%
test12131:-
{
  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 
%
%

small_sched:-
{
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}}

test324:-
{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).

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

test111213:-
{
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}.

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

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

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

test1232:-
{
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}.

test1233:-
{X + 2*Z >=0,
-Z +Y >= 1,
-Y >= 2}.



%==============================================================================
%From lim@scorpio Tue Mar  8 11:57:12 1994

:- setval(limit, -1).

bridge(Ende) :- 
 	setup(K,Ende,Disj),
        min_max(p(Disj,K,Ende),Ende,100,200).

min_max(G,E,L,U) :-
	call(G),
	!,
        rmin(E).

extract_vars(Term , Vars) :-
	extract_vars(Term, Vars0, []),
	=(Vars0, Vars).

extract_vars(X, [X|T], T) :- var(X), !.
extract_vars(X, [X|T], T) :- rvar(X), !.
extract_vars(X, [X|T], T) :- pvar(X), !.
extract_vars(X, T, T) :- atomic(X), !.
extract_vars([X|Xs], T, T0) :-
	!,
	extract_vars(X, T, T1),
	extract_vars(Xs, T1, T0).
extract_vars(X, T, T0) :-
	X =.. Xl,
	extract_vars(Xl, T, T0).

setup(K,Ende,Disj):-
	jobs(L),
	make_vars(L,K),
	el([stop,_,Ende],K),
	precedence(M),
	make_precedence(M,K),
	max_nf(M1),
	make_max_nf(M1,K),
	max_ef(M2),
	make_max_ef(M2,K),
	min_af(M3),
	make_min_af(M3,K),
	min_sf(M4),
	make_min_sf(M4,K),
	min_nf(M5),
	make_min_nf(M5,K),
	resources(R),
	make_disj(R,K,[],Disj1),
	reverse(Disj1,Disj).

p(Disj,K,Ende):-
	disjunct(Disj,0).

make_vars([],[]).
make_vars([H|T],[[H,D,A]|R]):-
	duration(H,D),
	make_vars(T,R).

make_precedence([],_).
make_precedence([[A,B]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
	mgreatereqc(Ba,Aa,Ad),
	make_precedence(R,L).

mgreatereqc(X,Y,Z) :-
	{X >= Y + Z}.

smallereqd(X,Y,Z) :-
	{X =< Y + Z}.

make_max_nf([],_).
make_max_nf([[A,B,C]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
        C1 is C + Ad,
	smallereqd(Ba,Aa,C1),
	make_max_nf(R,L).

make_max_ef([],_).
make_max_ef([[A,B,C]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
        C1 is Ad + C - Bd,
	smallereqd(Ba,Aa,C1),
	make_max_ef(R,L).

make_min_af([],_).
make_min_af([[A,B,C]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
	mgreatereqc(Ba,Aa,C),
	make_min_af(R,L).

make_min_sf([],_).
make_min_sf([[A,B,C]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
        C1 is C - Bd,
	smallereqd(Ba,Aa,C1),
	make_min_sf(R,L).

make_min_nf([],_).
make_min_nf([[A,B,C]|R],L):-
	el([A,Ad,Aa],L),
	el([B,Bd,Ba],L),
        C1 is C + Ad,
	mgreatereqc(Ba,Ad,C1),
	make_min_nf(R,L).

make_disj([],R,D,D).
make_disj([[H,R]|T],K,Din,Dout):-
	el_list(R,K,R1),
	make_disj1(R1,Din,D1),
	make_disj(T,K,D1,Dout).

make_disj1([],D,D).
make_disj1([H|T],Din,Dout):-
	make_disj2(H,T,Din,D1),
	make_disj1(T,D1,Dout).

make_disj2(H,[],D,D).
make_disj2([A,B],[[C,D]|S],Din,Dout):-
	make_disj2([A,B],S,[[A,B,C,D]|Din],Dout).

el_list([],_,[]).
el_list([H|T],L,[[A,D]|S]):-
	el([H,D,A],L),
	el_list(T,L,S).

disjunct([],_).
/*
disjunct([[A,B,C,D]|R],N):-
        writeln(disjunct(N)),
        N1 is N + 1,
        (N == 7 ->
            stop
        ;
	    true
	),
	disj(A,B,C,D),
	disjunct(R,N1).
*/
disjunct([[A,B,C,D]|R],N):-
        N1 is N + 1,
	getval(limit, L),
        (N == L ->
	    stop,
	    disj(A,B,C,D),
            true
        ;
	    disj(A,B,C,D),
	    disjunct(R,N1)
	).

disj(Aa,Ad,Ba,Bd):-
	mgreatereqc(Ba,Aa,Ad).
disj(Aa,Ad,Ba,Bd):-
	mgreatereqc(Aa,Ba,Bd).

reverse(L,K):-
	rev(L,[],K).

rev([],L,L).
rev([H|T],L,K):-
	rev(T,[H|L],K).

el(X,[X|R]).
el(X,[Y|R]):-
	el(X,R).


/*


		DATA


*/

jobs([start,a1,a2,a3,a4,a5,a6,p1,p2,ue,s1,s2,s3,s4,s5,s6,
	b1,b2,b3,b4,b5,b6,ab1,ab2,ab3,ab4,ab5,ab6,m1,m2,m3,m4,m5,m6,
	l1,t1,t2,t3,t4,t5,ua,v1,v2,k1,k2,stop]).

duration(start,0).
duration(a1,4).
duration(a2,2).
duration(a3,2).
duration(a4,2).
duration(a5,2).
duration(a6,5).
duration(p1,20).
duration(p2,13).
duration(ue,10).
duration(s1,8).
duration(s2,4).
duration(s3,4).
duration(s4,4).
duration(s5,4).
duration(s6,10).
duration(b1,1).
duration(b2,1).
duration(b3,1).
duration(b4,1).
duration(b5,1).
duration(b6,1).
duration(ab1,1).
duration(ab2,1).
duration(ab3,1).
duration(ab4,1).
duration(ab5,1).
duration(ab6,1).
duration(m1,16).
duration(m2,8).
duration(m3,8).
duration(m4,8).
duration(m5,8).
duration(m6,20).
duration(l1,2).
duration(t1,12).
duration(t2,12).
duration(t3,12).
duration(t4,12).
duration(t5,12).
duration(ua,10).
duration(v1,15).
duration(v2,10).
duration(k1,0).
duration(k2,0).
duration(stop,0).

precedence([[start,a1],[start,a2],[start,a3],[start,a4],[start,a5],
	[start,a6],[start,ue],[a1,s1],[a2,s2],[a5,s5],
	[a6,s6],[a3,p1],[a4,p2],[p1,s3],[p2,s4],
	[p1,k1],[p2,k1],[s1,b1],[s2,b2],
	[s3,b3],[s4,b4],[s5,b5],[s6,b6],[b1,ab1],
	[b2,ab2],[b3,ab3],[b4,ab4],[b5,ab5],[b6,ab6],
	[ab1,m1],[ab2,m2],[ab3,m3],[ab4,m4],[ab5,m5],
	[ab6,m6],[m1,t1],[m2,t1],[m2,t2],[m3,t2],
	[m3,t3],[m4,t3],[m4,t4],[m5,t4],[m5,t5],
	[m6,t5],[m1,k2],[m2,k2],[m3,k2],[m4,k2],
	[m5,k2],[m6,k2],[l1,t1],[l1,t2],[l1,t3],
	[l1,t4],[l1,t5],[t1,v1],[t5,v2],[t2,stop],
	[t3,stop],[t4,stop],[v1,stop],[v2,stop],[ua,stop],
	[k2,stop]]).

max_nf([[start,l1,30],[a1,s1,3],[a2,s2,3],[a5,s5,3],
	[a6,s6,3],[p1,s3,3],[p2,s4,3]]).

min_sf([[ua,m1,2],[ua,m2,2],[ua,m3,2],[ua,m4,2],
		[ua,m5,2],[ua,m6,2]]).

max_ef([[s1,b1,4],[s2,b2,4],[s3,b3,4],[s4,b4,4],[s5,b5,4],[s6,b6,4]]).

min_nf([[start,l1,30]]).

min_af([[ue,s1,6],[ue,s2,6],[ue,s3,6],[ue,s4,6],[ue,s5,6],[ue,s6,6]]).

resources([[kran,[l1,t1,t2,t3,t4,t5]],
	[maurer,[m1,m2,m3,m4,m5,m6]],
	[schal,[s1,s2,s3,s4,s5,s6]],
	[bagger,[a1,a2,a3,a4,a5,a6]],
	[ramme,[p1,p2]],
	[pumpe,[b1,b2,b3,b4,b5,b6]],
	[walze,[v1,v2]]]).

b.

stop.