2

I want to use NMaximize to solve a problem like this:

$\quad \quad \max_y f(y) \text{ subject to } \max_x g(x, y) \leq c.$

Rather than reproduce my (messier) problem, I have provided a very simple example of how I tried to solve this problem:

f[y_] := 1 - y
g[x_, y_] := x - y
NMaximize[{f[y], y >= 0, NMaximize[{g[x, y], 0 <= x <= 1}, x][1] <= 1/2}, y]

This shows my problem, which is that NMaximize does not nest in this way: It seems that rather than plug the same y in for both problems, Mathematica attempts to evaluate the inner function first and then complains that y is not a number:

NMaximize::nnum: The function value -0.0914636+y is not a number at {x} = {0.0914636}. >>

Is there an alternative approach that could solve a problem like this one?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Keith
  • 21
  • 1
  • Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the [faq]! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Aug 26 '15 at 23:21

1 Answers1

3

Perhaps this?

Clear[f, g, maxg];
f[y_] := 1 - y
g[x_, y_] := x - y
maxg[y_?NumericQ] := NMaxValue[{g[x, y], 0 <= x <= 1}, x]
NMaximize[{f[y], y >= 0 && maxg[y] <= 1/2}, y]

(*  {0.500001, {y -> 0.499999}}  *)

See this answer for more information about the use of NumericQ: What are the most common pitfalls awaiting new users?

There is some subtlety here that I think makes this question not a duplicate of it or its linked questions.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Thanks, this seems to work. Now I'll read up on this so that I understand why! –  Aug 26 '15 at 23:06