Ask Your Question

Revision history [back]

Transition from Mathematica to SageMath... I need help with a minimize function with several constraints

Hi !

Due to the high cost of the Mathematica license, I would like to use SageMath. However, getting started is not easy and I would need help with my first program.

Here is the current Mathematica program:

S[0] = 2000;Cp = 1;Ch = 0.1;Cs = 5;Subscript[U, max] = 16*8;Subscript[U, min] = 0;\[CapitalDelta]t = 30;H = 12;
demande = {9786, 15159, 12805, 12758, 13003, 6775, 8585, 9612, 10797, 7261, 14946, 7359};
Table[Subscript[d, k] = demande[[k]], {k, 1, H}];
S[k_] := S[k - 1] + U[k] \[CapitalDelta]t - Subscript[d, k];
FOprod[k_] := Ch \[CapitalDelta]t (Max[0, S[k - 1]] + (U[k] \[CapitalDelta]t)/2) - Cs Min[0, S[k]] + Cp U[k] \[CapitalDelta]t
rep = NMinimize[{\!\(\*UnderoverscriptBox[\(\[Sum]\), \(k = 1\), \(H\)]\(FOprod[k]\)\), Table[Subscript[U, min] <= U[k] <= n Subscript[U, max] && U[k] \[Element] Integers, {k, 1, H}]}, Table[U[k], {k, 1, H}],  MaxIterations -> 2000]

*Result :* {323450., {U[1] -> 260, U[2] -> 505, U[3] -> 427, U[4] -> 425, U[5] -> 433, U[6] -> 226, U[7] -> 286, U[8] -> 321, U[9] -> 360, U[10] -> 242, U[11] -> 498, U[12] -> 245}}

With SageMath, it's hard (for me) to obtain the same program... Should I use the function sage.numerical.optimize.minimize_constrained?

Here is the beginning of the program but I think the last two instructions are already wrong.

k=var('k')
list1 = [False for j in range(6)]; list1[0]=2000;
print list1
Cp = 1; Ch = 0.1; Cs = 5; Umax = 16*8; Umin = 0;  Deltat = 30;H = 12;
demande = [9786, 15159, 12805, 12758, 13003, 6775, 8585, 9612, 10797, 7261, 14946, 7359]; demande
S(k)=S[k-1] + U[k] * Deltat - demande[k]; 'It is not correct but...'
FOprod(k) = Ch Deltat (max[0, S[k - 1]] + (U[k] Deltat)/2) - Cs min[0, S[k]] + Cp U[k] Deltat 'It is not correct but...'
...

Best regards.