Message Boards Message Boards

FindMaximum::nrnum: The function value is not a real number

Posted 1 month ago

Hi everyone,

I'm working on an optimization problem with three decision variables: n (shipment frequency), x (replenishment cycle in days), and g (preservation technology investment cost). To find the optimal values for these variables, I fix n at various values and use FindMaximum to solve for x and g. Then, I compare the total profit for different n values to determine the optimal solution.

However, I’m encountering the following error when I try to run my code :

FindMaximum::nrnum: The function value -7.43115002564108910^1001-2.42719779476497110^1017 I is not a real number at {x,g} = {2.15602,-2.3412}.

Here is the code :

Clear["Global`*"]
L = 7;
w = 0.064;
ww = 2;
R = 250;
Kf = 50;
Km = 0.5;
cf = 0.1;
cg = 0.01;
m = 0.2;
ml = 0.01;
Kp = 30;
Kj = 0.3;
hp = 0.05;
ht = 0.2;
Kr = 10;
Ks = 1;
hr = 0.1;
hs = 0.07;
j = 0.001;
k = 6.87;
q = 0.1; 
a = 80;
b = 0.2;
Pv = 1;
pw = 0.5;
pf = 0.7;
pp = 2;
pr = 10;
c = 0.8;
S = 2;
Sv = 3;
Sr = 1;
T = 0.5;
v = 0.15;
vp = 1;
y = 400;
z = 100;
f = 0.9;
l = 0.3;
Ik = 0.1;
M = 0.5;
bk = 1 - f;
n = 1;
Tf = - (Log[1/z (k/ww - 1)]/q);
TP[x_, g_] = 
  pr/x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(1 - b)) - (
    hr + c hs + g + c j)/
    x (2 x (-((a (-1 + b) (1 - E^(-y g)) x^2)/L))^(1/(1 - b))) - (Kr +
      c Ks)/x - (Kp + Kf + c Km + c Kj)/(
   n x) - (c pp + c pf)/x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(
    1/(1 - b)) - (S + c T)/
   x^2 - (hp + c ht + g + c j)/(
    2 R x) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(2/(
    1 - b)) - ((hp + c ht + g + c j) (n - 1))/(
    2 x) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(2/(
    1 - b)) (x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^1/(b - 1) - 1/
      R) - (Sv + c Sr)/(n x)^2 - ((vp + c v)/(
     n x)) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
    1 - b)) - ((Pv + c pw) w)/( 
    x f ww) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
    1 - b)) - (cf f + c cg f + m bk + c ml bk)/( 
    x f ww ) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
    1 - b)) (k Tf + k/q (Log[1 + z E^(-q Tf)] - Log[1 + z])) + 
   pr/x l ((
      a E^(-g y) (-1 + E^(g y)) x^3 ((a (1 - b) (1 - E^(-g y)) T^2)/
        L)^(b/(1 - b)) (3 (-1 + 2^(1/(-1 + b))) (-1 + b) - 2 ))/(
      3 L) + ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
       1 - b)) (M - x));
FindMaximum[{TP[x, g]}, {x, g}]

Any insights on why this might be happening and how I can solve this problem?

POSTED BY: Shafa Hananta
2 Replies
Posted 1 month ago

As Gianluca Gorni said, your function has complex number outputs. This is because your variable g is treated as an exponent in your function expression, which can cause complex output. Complex function is not optimizable. So you must need to convert it to real number.

Some suggestion: 1. use function such as Abs to convert your output to real number domain; 2. fix the variable g at integers and optimize the x only.

POSTED BY: Yifa Tian

Your function is complex-valued when g<0, as you can see from

Reduce[FunctionDomain[TP[x, g], {x, g}],
 {x, g}, Reals]

From a plot

Plot3D[TP[x, g], {x, 0, 1}, {g, 0, 10},
 PlotRange -> {-300, 0}]

I am tempted to guess that your function has no optimal point for x,g>0, or that the optimum is at x=Infinity, g=0. My suggestion is to give sensible constraints to x and g.

POSTED BY: Gianluca Gorni
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