this repo has no description
at develop 1.3 kB view raw
1/* 2 * Function fzn_piecewise_linear_base creates 3 * interval representation of x, 4 * which is reusable if we have several pwls 5 * on the same set of breakpoints 6 */ 7function array[int, int] of var 0.0..1.0: fzn_piecewise_linear_base( 8 var float: x, array[int] of float: xi) ::promise_total = 9 let { 10 set of int: is = index_set(xi), 11 set of int: is_1 = is diff { max(is) }, 12 array[is_1] of var 0.0..1.0: s, %% Segment variables 13 array[is_1] of var 0..1: f, 14 constraint 1 == sum(f), 15 constraint forall(i in is_1)(f[i] >= s[i]), 16 constraint x == sum(i in is_1)(xi[i] * f[i] + (xi[i+1]-xi[i]) * s[i]), 17 } in 18 array2d(is_1, 1..2, f++s); 19 20predicate fzn_piecewise_linear(var float: x, var float: y, 21 array[int] of float: xi, array[int] of float: vi) = 22 let { 23 set of int: is = index_set(xi), 24 constraint assert(is == index_set(vi) /\ 0<card(is), 25 "fzn_pwl: index sets unequal or empty"), 26 set of int: is_1 = is diff { max(is) }, 27 array[int, int] of var float: fs = fzn_piecewise_linear_base(x, xi), 28 } in 29 y == sum(i in is_1)(vi[i] * fs[i, 1] + (vi[i+1]-vi[i]) * fs[i, 2]) 30 ; 31 32%-----------------------------------------------------------------------------%