Message Boards Message Boards

0
|
1358 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

1/0 error when using NMaximize[]

Posted 1 year ago

(working code below)

I'm using NMaximize[] to solve an optimization problem comprising several projects with different costs. I have several constraints. When I solve for my variables as Elements in Reals I obtain a solution. When I change the domain to Integers (between 0 and 1) I get the following errors (mainly I get a 1/0 division error). Ideas? I'm not sure how to set starting conditions, etc., so I really don't know how to troubleshoot this.

Power::infy: Infinite expression 1/0 encountered

projCosts = {{40, 10, 20, 20, 0}, {65, 36, 30, 25, 30}, {6, 8, 10, 0, 
   0}, {20, 10, 20, 20, 0}}; yearlyFunds = {120, 40, 40, 55, 
  60}; totalPossibleFunds = {90, 65 + 36 + 30 + 25 + 30, 6 + 8 + 10, 
  20 + 10 + 20 + 20};

projScore = {.741, .845, .353, .457};

NMaximize[{projScore . {a, b, c, 
    d}, (projCosts[[#]][[1]] & /@ Range[Length[projScore]] . {a, b, c,
        d}) <= yearlyFunds[[1]] && (projCosts[[#]][[2]] & /@ 
       Range[Length[projScore]] . {a, b, c, d}) <= 
    yearlyFunds[[2]] && (projCosts[[#]][[3]] & /@ 
       Range[Length[projScore]] . {a, b, c, d}) <= 
    yearlyFunds[[3]] && (projCosts[[#]][[4]] & /@ 
       Range[Length[projScore]] . {a, b, c, d}) <= yearlyFunds[[4]] &&
    0 <= a <= 1 && 0 <= b <= 1 && 0 <= c <= 1 && 
   0 <= d <= 
    1 && ((totalPossibleFunds[[1]]*a + 
        totalPossibleFunds[[3]]*c)/(totalPossibleFunds[[1]]*a + 
        totalPossibleFunds[[3]]*c + totalPossibleFunds[[2]]*b + 
        totalPossibleFunds[[4]]*d)) <= 4/10}, {a, b, c, d} \[Element] 
  Integers]
POSTED BY: Michael Boehm
4 Replies
Posted 1 year ago

i finally found a solution, but not the book solution. I wonder why these optimization functions don't check all the boundary values. that is to say, why not check {alb,c,d}={1,0,0,0} and {0,1,0,0} and {0,0,1,0} and {0,0,0,1} and {1,1,0,0} etc. etc.

In[6]:= projCosts = {{40, 10, 20, 20, 0}, {65, 36, 30, 25, 30}, {6, 8,
    10, 0, 0}, {20, 10, 20, 20, 0}}; yearlyFunds = {120, 40, 40, 55, 
  60}; totalPossibleFunds = {90, 65 + 36 + 30 + 25 + 30, 6 + 8 + 10, 
  20 + 10 + 20 + 20};

In[7]:= projScore = {.741, .845, .353, .457};

In[17]:= NMaximize[{projScore . {a, b, c, 
    d}, ({projCosts[[1]][[1]], projCosts[[2]][[1]], 
       projCosts[[3]][[1]], projCosts[[4]][[1]]} . {a, b, c, d}) <= 
    yearlyFunds[[
     1]] && ({projCosts[[1]][[2]], projCosts[[2]][[2]], 
       projCosts[[3]][[2]], projCosts[[4]][[2]]} . {a, b, c, d}) <= 
    yearlyFunds[[
     2]] && ({projCosts[[1]][[3]], projCosts[[2]][[3]], 
       projCosts[[3]][[3]], projCosts[[4]][[3]]} . {a, b, c, d}) <= 
    yearlyFunds[[
     3]] && ({projCosts[[1]][[4]], projCosts[[2]][[4]], 
       projCosts[[3]][[4]], projCosts[[4]][[4]]} . {a, b, c, d}) <= 
    yearlyFunds[[
     4]] && ((totalPossibleFunds[[1]]*a + 
        totalPossibleFunds[[3]]*c)/(totalPossibleFunds[[1]]*a + 
        totalPossibleFunds[[3]]*c + totalPossibleFunds[[2]]*b + 
        totalPossibleFunds[[4]]*d)) <= 0.4 && 0 <= {a, b, c, d} <= 1 &&
    a + b + c + d > 0 && Element[{a, b, c, d}, Integers]}, {a, b, c, 
  d}, Method -> {"NelderMead"}]

Out[17]= {0.81, {a -> 0, b -> 0, c -> 1, d -> 1}}

(*this isn't the solution provided in the book solution set, but the \
cost of my solution is half that of the book's solution*)
POSTED BY: Michael Boehm
Posted 1 year ago

well, i included a constraint that a+b+c+d>0

and now i get the following error:

NMaximize::nnum: The function value Indeterminate is not a number at {a,b,c,d} = {0.106664,0.321896,0.053649,0.0900987}.

POSTED BY: Michael Boehm
Posted 1 year ago

ChatGPT is saying NMaximize[] isn't designed for Integers, just continuous variables. So which optimization works for Integers?

POSTED BY: Michael Boehm
Posted 1 year ago

thanks.

POSTED BY: Michael Boehm
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