# Problem with NMinimize - continuous and discrete variables

Dear Mathematica experts, I have a problem with the optimization of the objective Z depending on continuous and discrete variables. The error that occurs constantly is "The following constraints are not valid: ..... Constraints should be equalities, inequalities, or domain specifications involving the variables." I read a lot about this problem here, but as far as I understand each of the cases is individual. If the constraints are incompatible, shouldn't a message be displayed that they are not being satisfied? As far as I understand the mentioned error message, the constraints have not been entered correctly. Please, help me understand what the problem is. Thank you in advance.

PS: In vars list b and r are Integers.

Update: The minimal version of the corrected code is below the original code. The functions d and h are modified accroding to the @Hausdorff recomendations. There is an warning concerning the satisfaction of the constraints, but this is a question of adjustment and is not related to the original problem.

(*======================The original code================================*)
Mmin = 102500; Mmax = 193000; th2max = 430*Pi/180; th2min = 310*Pi/180;

(*Functions*)
d[b_] := Which[b == 1, 0.04, b == 2, 0.05, b == 3, 0.063, b == 4,
0.08, b == 5, 0.1, b == 6, 0.125, b == 7, 0.14, b == 8, 0.16,
b == 9, 0.18, b == 10, 0.2, b == 11, 0.22, b == 12, 0.25, b == 13,
0.28, b == 14, 0.32];

h[r_] := Which[r == 1, 0.15, r == 2, 0.30, r == 3, 0.45, r == 4, 0.60,
r == 5, 0.75, r == 6, 0.9, r == 7, 1.05, r == 8, 1.30, r == 9,
1.45, r == 10, 1.6, r == 11, 1.75, r == 12, 1.9, r == 13, 2.05,
r == 14, 2.2];

Lt[b_] := 0.291669 + 4.1388707*d[b];

F[b_, p_] := 2*p*Pi*d[b]^2/4;

S1max[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ,
ddelta1_?NumericQ] :=Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, S1max},
S1max = Sqrt[Abs[L1^2+L2^2+L3^2+2*L1*(L2*Sin[th2max + delta1]-L3*Cos[th2max + delta1])]]];

kmax[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ] :=
Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, Amax, Bmax,
ps1max, kmax},
Amax = (L1*Cos[delta1 + th2max] - L3)/S1max[L1, L2, L3, delta1];
Bmax = (L1*Sin[delta1 + th2max] + L2)/S1max[L1, L2, L3, delta1];
ps1max = ArcTan[Amax, Bmax];
kmax = -L1*Sin[delta1 + th2max - ps1max]];

S1min[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ,
ddelta1_?NumericQ] :=Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, S1min},
S1min =Sqrt[Abs[L1^2+L2^2+L3^2+2*L1*(L2*Sin[th2min+delta1]-L3*Cos[th2min+delta1])]]];

kmin[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ] :=
Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, Amin, Bmin,
ps1min, kmin},
Amin = (L1*Cos[delta1 + th2min] - L3)/S1min[L1, L2, L3, delta1];
Bmin = (L1*Sin[delta1 + th2min] + L2)/S1min[L1, L2, L3, delta1];
ps1min = ArcTan[Amin, Bmin];
kmin = -L1*Sin[delta1 + th2min - ps1min]];

maxM[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ,pp_?NumericQ, bb_?NumericQ] := Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, p = pp,b = bb, maxM},
maxM = F[b, p]*kmax[L1, L2, L3, delta1] - Mmax];

minM[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ,
pp_?NumericQ, bb_?NumericQ] :=Module[{L1=LL1, L2=LL2, L3=LL3, delta1=ddelta1, p=pp, b = bb, minM},
minM = F[b, p]*kmin[L1, L2, L3, delta1] - Mmin];

(*Objective*)
Z[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ,pp_?NumericQ, bb_?NumericQ, rr_?NumericQ,] :=
Module[{b=bb, r=rr, L1=LL1, L2=LL2, L3=LL3, p=pp, delta1=ddelta1, f1, f2, k1, k2, Z},
f1 = 2*h[r]*Pi*(d[b]^2)/4 ;
f2 = (maxM[L1,L2,L3,delta1,p,b]-Mmax)^2+(minM[L1,L2,L3,delta1,p,b]-Mmin)^2;
k1 = 0.5; k2 = 1 - k1;
Z = k1*f1 + k2*f2
];

(*Constraints*)
cons = {
b >= 1, b <= 14,
r >= 1, r <= 14,
L1 >= 3, L1 <= 4.1,
L2 >= 0.2, L2 <= 1.5,
L3 >= 0.2, L3 <= 1.5,
delta1 >= 14*Pi/180, delta1 <= 21*Pi/180,
p >= 6*10^6, p <= 30*10^6,
Abs[S1max[L1,L2,L3,delta1]-(Lt[b]+2*h[r])]<=0.01,
Abs[S1min[L1,L2,L3,delta1]-(Lt[b]+h[r])]<=0.01,
maxM[L1,L2,L3,delta1,p,b]>=0,
minM[L1,L2,L3,delta1,p,b]>=0
};

(*Variables*)
vars = {L1,L2,L3,delta1,p,b\[Element]Integers,r\[Element]Integers};

(*Optimization*)
opt = NMinimize[{Z[L1,L2,L3,delta1,p,b,r],cons},vars,MaxIterations->500]

(*==============The minimal updated and working code, =====================*)

Mmax = 100; th2max = 430*Pi/180;

(*Functions*)
d[b_] := Which[b == 1, 0.04, b == 2, 0.05, b == 3, 0.063, b == 4,
0.08, b == 5, 0.1, b == 6, 0.125, b == 7, 0.14, True, 0];

h[r_] := Which[r == 1, 0.15, r == 2, 0.30, r == 3, 0.45, r == 4, 0.60,
r == 5, 0.75, r == 6, 0.9, r == 7, 1.05, True, 0];

Lt[b_] := 0.291669 + 4.1388707*d[b];

F[b_, p_] := 2*p*Pi*d[b]^2/4;

S1max[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ,
ddelta1_?NumericQ] := Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, S1max},
S1max = Sqrt[Abs[L1^2 + L2^2 + L3^2 + 2*L1*(L2*Sin[th2max + delta1] - L3*Cos[th2max + delta1])]]];

kmax[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ] :=
Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, Amax, Bmax,
ps1max, kmax},
Amax = (L1*Cos[delta1 + th2max] - L3)/S1max[L1, L2, L3, delta1];
Bmax = (L1*Sin[delta1 + th2max] + L2)/S1max[L1, L2, L3, delta1];
ps1max = ArcTan[Amax, Bmax];
kmax = -L1*Sin[delta1 + th2max - ps1max]];

maxM[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ,
pp_?NumericQ, bb_?NumericQ] :=
Module[{L1 = LL1, L2 = LL2, L3 = LL3, delta1 = ddelta1, p = pp,
b = bb, maxM},
maxM = F[b, p]*kmax[L1, L2, L3, delta1] - Mmax];

(*Objective*)
Z[LL1_?NumericQ, LL2_?NumericQ, LL3_?NumericQ, ddelta1_?NumericQ,
pp_?NumericQ, bb_?NumericQ, rr_?NumericQ] :=
Module[{b = bb, r = rr, L1 = LL1, L2 = LL2, L3 = LL3, p = pp,
delta1 = ddelta1, f1, f2, k1, k2, Z}, f1 = 2*h[r]*Pi*(d[b]^2)/4;
Z = (maxM[L1, L2, L3, delta1, p, b] - Mmax)^2];

(*Constraints*)
cons = {b >= 1, b <= 14, r >= 1, r <= 14, L1 >= 3, L1 <= 4.1,
L2 >= 0.2, L2 <= 1.5, L3 >= 0.2, L3 <= 1.5, delta1 >= 14*Pi/180,
delta1 <= 21*Pi/180, p >= 6*10^6, p <= 30*10^6,
Abs[S1max[L1, L2, L3, delta1] - (Lt[b] + 2*h[r])] <= 0.01};

(*Variables*)
vars = {L1, L2, L3, delta1, p, b \[Element] Integers,
r \[Element] Integers};

(*Optimization*)
opt = NMinimize[{Z[L1, L2, L3, delta1, p, b, r], cons}, vars]


• The problem is caused by the Which statements in d, h, which do not return numeric values for b and r outside the interval $[1,14]$. As a quick fix you can add a default case, by adding for example True, 0 to the list of cases (the actual value should not matter because of your constraint) Commented Jan 23, 2021 at 13:06
• Thanks for the attention. But both b and r are constrained by the following inequalities: b >= 1, b <= 14, r >= 1, r <= 14. How is it possible for them to go beyond the defined interval? As a quick fix you can add a default case, by adding for example True, 0 to the list of cases. - would you be kind enough to give me some example of how to do this. Commented Jan 23, 2021 at 14:28
• My guess is that Mathematica checks whether the constraints are sensible within the parameter domains, which for p and r is all Integers. You can add the default case in the Which statements by appending True, 0 to the list of cases like this: d[b_] := Which[b == 1, 0.04, b == 2, 0.05, b == 3, 0.063, b == 4, 0.08, b == 5, 0.1, b == 6, 0.125, b == 7, 0.14, b == 8, 0.16, b == 9, 0.18, b == 10, 0.2, b == 11, 0.22, b == 12, 0.25, b == 13, 0.28, b == 14, 0.32, True, 0]; For h[r_] you do exactly the same. Commented Jan 23, 2021 at 14:43
• Thanks, now the error is: NMinimize::nnum: The function value Z[3.74365,1.17879,0.690019,0.332994,1.90502*10^7,11,10] is not a number at {b,delta1,L1,L2,L3,p,r} = {10.8409,0.332994,3.74365,1.17879,0.690019,1.90502*10^7,10.4059}. I guess there are problems with the functions. Thank you very much. Commented Jan 23, 2021 at 14:56
• The code works. The problem is exactly the one you indicated with the integers. Such a problem is not described anywhere, I hope your advice will be useful to many other users. Thank you again. Commented Jan 23, 2021 at 15:15