| 1 |
/*** |
| 2 |
!Test |
| 3 |
solvers: |
| 4 |
- gecode |
| 5 |
- cbc # Can't check as floats are imprecise |
| 6 |
- highs |
| 7 |
expected: !Result |
| 8 |
status: SATISFIED |
| 9 |
solution: !Solution |
| 10 |
P: !Approx 373.0277986476333 |
| 11 |
mortgage: [!Approx 260.3305785123967, !Approx 136.3636363636363, !Approx 0.0] |
| 12 |
***/ |
| 13 |
|
| 14 |
% Constraint Programming: Mortgage |
| 15 |
% |
| 16 |
% |
| 17 |
% This Minizinc program is written by Hakan Kjellerstrand, [email protected], |
| 18 |
% and is commented in the (swedish) blog post |
| 19 |
% Constraint Programming: Minizinc, Gecode/flatzinc och ECLiPSe/minizinc |
| 20 |
% http://www.hakank.org/webblogg/archives/001209.html |
| 21 |
% |
| 22 |
% See also my MiniZinc page: http://www.hakank.org/minizinc |
| 23 |
% |
| 24 |
% Marriot & Stuckey: Programming with Constraints, page 175f |
| 25 |
% (famous early example) |
| 26 |
% |
| 27 |
% Since Minizinc don't allow recursion, it is implemented as an array. |
| 28 |
% |
| 29 |
% This example shows the flexibility of Minizinc: |
| 30 |
% the same code can calculate P, R _or_ I with just a simple change of the declarations. |
| 31 |
% |
| 32 |
% |
| 33 |
% Calculates P, given I and R: |
| 34 |
% P: 373.02779864763335 |
| 35 |
% |
| 36 |
% Calculates R, given P and I: |
| 37 |
% R: 149.944102252456 (which is close to 150) |
| 38 |
% |
| 39 |
% Calculates I, given R and P: |
| 40 |
% I: 0.099995922287248337__0.10000424331679297 (close to 0.1) |
| 41 |
|
| 42 |
% Note: As of 2008-06-23 this don't work in version >= 0.8. |
| 43 |
% It may work with later version, though. |
| 44 |
|
| 45 |
int: T = 3; % time period |
| 46 |
|
| 47 |
% comment one of the initiations to calculate it: |
| 48 |
var 0.0..10000.0: I = 10.0/100.0; |
| 49 |
var 0.0..10000.0: R = 150.0; |
| 50 |
var 0.0..10000.0: P; % = 373.02779864763318; |
| 51 |
|
| 52 |
array[1..T] of var float: mortgage; |
| 53 |
|
| 54 |
solve satisfy; |
| 55 |
% solve minimize P; |
| 56 |
|
| 57 |
constraint |
| 58 |
% start value: |
| 59 |
mortgage[1] = P + (P * I) - R /\ |
| 60 |
forall(i in 2..T) ( |
| 61 |
% calculate the next value using a local variable |
| 62 |
let { |
| 63 |
var float: NP = mortgage[i-1] + (mortgage[i-1] * I) - R |
| 64 |
} |
| 65 |
in |
| 66 |
mortgage[i] = NP /\ NP >= 0.0 |
| 67 |
) |
| 68 |
; |
| 69 |
|
| 70 |
output [ |
| 71 |
"P: ", show(P), "\n", |
| 72 |
"I: ", show(I), "\n", |
| 73 |
"R: ", show(R), "\n", |
| 74 |
"mortgage: ", show(mortgage),"\n" % is not especially interesting |
| 75 |
]; |