# Time spent on a transformation exceeded 300 when solving the optimization problem

I have the code which shows below to solve an optimization problem, but it will show that it's over the time constraint of 300 seconds. Is there anyone who could help me solve it. Thanks.

m = 7;
n = 20;
coh = 5;
phi = 0.01/180*Pi;
r0 = 2;
segamax = 10;
segamay = 15;
seta[j_] := 2*Pi*(j - 1)/(2 n + 1);
c[k_] := c[k] /; RandomInteger < 10;
ce[j_] := Sum[c[k]*E^(-I*k*seta[j]), {k, m}];
dd[j_] := Sqrt[r^2*(E^(I*seta[j]) + ce[j])*(E^(-I*seta[j]) +Conjugate[ce[j]])]/r0;
ee[j_] := (E^(I*seta[j]) + ce[j])/Conjugate[E^(I*seta[j]) + ce[j]];
bb[j_] := coh*Cot[phi]/(1 - Sin[phi])*dd[j]^(2 Sin[phi]/(1 - Sin[phi]));
cc[j_] := coh*Cos[phi]/(1 - Sin[phi])*dd[j]^(2 Sin[phi]/(1 -Sin[phi]))*ee[j];
eee[j_, t_] := E^(-I*t*seta[j]);
ma = Table[eee[j, t], {j, 1, (2*n + 1)}, {t, n, 1, -1}];
mb = Table[Conjugate[eee[j, t]], {j, 1, (2*n + 1)}, {t, 1, n}];
iii = Transpose[{Table[1, {2*n + 1}]}];
haib = Table[Join[ma[[i]], iii[[i]], mb[[i]]], {i, (2*n + 1)}];
ihaib = Inverse[N[haib]];
fb = Table[{bb[j]}, {j, 2 n + 1}];
fc = Table[{cc[j]}, {j, 2 n + 1}];
d1 = ihaib.fb;
e1 = ihaib.fc;
es = e1[[m - 1 ;; n, 1 ;; 1]];
d11 = d1[[1 ;; n + 1, 1 ;; 1]];
e11 = e1[[1 ;; n + 1, 1 ;; 1]];
f[s_] := Which[s < m - 1, Sum[(m - s - k)*c[m - s - k]*f[m - k + 1], {k, m - s - 1}] + c[s], True, c[s]];
e2[k_] := Which[k == 0, -2*Sum[j*f[j + 1]*d[j],
{j, m - 1}] + (segamay - segamax)/2, k > m - 2, 0, True, -2*Sum[j*f[j + k + 1]*d[j], {j, m - k - 1}]];
e22 = Table[e2[k], {k, n, 0, -1}];
d[j_] := d1[[-j + n + 1, 1]];
e[j_] := e1[[-j + n + 1, 1]];
dx = 2 (segamax + segamay)/2 + coh*Cos[phi];
capf = Sum[Abs[es[[i, 1; 1]]], {i, n - m + 2}];
cons = {d - dx == 0, e - e2 == 0, Table[e11[[i]] == e22[[i]], {i, n + 1}]};
vars = {r, c, c, c, c, c, c, c};
sol = NMinimize[{capf, cons}, {r, c, c, c, c, c, c, c}]

• {LeafCount[capf],LeafCount[cons]} gives {242431,586827} constants and operators and symbols in those expressions. Not surprising it can't do that in 300 seconds. Can you see if you can Simplify parts or all of those before you give those to NMinimize? And get those down to a small fraction of those numbers? That might help you. – Bill Jul 23 '19 at 16:28
• @Bill This is invalid syntax and a misuse of Assuming. Assuming can't be used to set options. It just sets \$Assumptions. – Szabolcs Jul 24 '19 at 7:12
• @Szabolcs I apologize for my mistake. I had found another StackExchange answer which I though showed how to pass TimeConstraint information to Simplify used inside NMinimize but I have searched and haven't found that answer again. I must have misunderstood. Sorry. – Bill Jul 24 '19 at 8:41
• @Bill Maybe one could temporarily change the default option value using SetOptions[Simplify, TimeConstraint -> bigNumber] and also for FullSimplify. I have not checked whether this works or not. – Szabolcs Jul 24 '19 at 8:42
• Bill and Szabolcs, thank you so much for your help. I tried to decrease the value of 'm' & 'n' and also increase the TimeConstraint to Infinity. But it still cannot show results. @Bill May I ask how do you know the expression might need 250 pages to print? Thanks. – NWangCG Jul 26 '19 at 15:01