Message Boards Message Boards

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, 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.

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]
POSTED BY: Rosen MITREV
Posted 3 years ago

Please note when you've simultaneously posted the same question on different forums: https://mathematica.stackexchange.com/questions/238671/problem-with-nminimize-continuous-and-discrete-variables.

POSTED BY: Jim Baldwin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract