/* Mortgage in Picat. Marriot & Stuckey: Programming with Constraints, page 175f (famous early example) Note: Since Picat MIP solver (GLPK) don't support nonlinear constraints, both P and I cannot be decision variables. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import mip. main => go. % b=203.128769950000162 go => mortgage(1000.0,10,10.0/100.0,150.0,B), solve([B]), println(b=B), nl. % p=921.685065855701964 go2 => mortgage(P,10,10.0/100.0,150.0,0), solve([P]), println(p=P), nl. % This give p=0.0,r=0.0,b=0.0 % (book example: 0.3855*B + 6.1446*R) go3 => P :: 0.0..1000.0, R :: 0.0..1000.0, B :: 0.0..1000.0, R #>= 1, mortgage(P,10,10.0/100.0,R,B), solve([P,R,B]), println([p=P,r=R,b=B]), nl. go4 => B #= 10.0, mortgage(1234,10,10.0/100.0,R,B), solve([R,B]), println([r=R,b=B]), nl. % experiment... go5 => T :: 0..100, % B :: 1.0..1000.0, B #>= 100.0, B #<= 1000.0, mortgage(B,T,10.0/100.0,10,0), solve([B,T]), println([t=T,b=B]), nl. % P: principal (amount borrowed) % T: time periods % I: interest rate % R: repayment % B: balance (final amount owing) % mortgage(P,T,_I,_R,B) ?=> T #= 0, B #= P. % , % solve([P,T,I,R,B]). mortgage(P,T,I,R,B) => T #>= 0, NT #= T -1, NP #= P + P*I -R, mortgage(NP,NT,I,R,B).