2
$\begingroup$

I would like to solve a maximization problem similar to

Maximize[{- E^x + E^y, 0 <= x <= 1 && 0 <= y <= 1}, {x, y}]

Mathematica correctly solves this problem. However, I have additional variables that are irrelevant to the maximization problem:

Maximize[{- E^x + E^y, 0 <= x <= 1 && 0 <= y <= 1}, {x, y, z}]

Mathematica cannot solve this problem. What can I do to still solve this problem? What if z additionally occurs in the constraints, as in the following line?

Maximize[{- E^x + E^y, 0 <= x <= 1 && 0 <= y <= 1 && x <= z}, {x, y, z}]

Interestingly, Mathematica can solve this:

Maximize[{-E^x + E^y + E^z, 0 <= x <= 1 && 0 <= y <= 1 && 0 <= z <= 1}, {x, y, z}]

Expected behavior: Because $z$ is irrelevant for my second example, I expect any value for $z$. If the maximization function is linear, this is what Mathematica does:

In : Maximize[{-x + y, 0 <= x <= 1 && 0 <= y <= 1}, {x, y, z}]
Out: {1, {x -> 0, y -> 1, z -> 0}}

In my third example, I expect a value for $z$ that satisfies the constraints. Again, a linear maximization function does achieve this:

In : Maximize[{-x + y, 0 <= x <= 1 && 0 <= y <= 1 && x + 20 <= z}, {x, y, z}]
Out: {1, {x -> 0, y -> 1, z -> 20}}

If the constraints are unsatisfiable, Mathematica should realize this. Again, the behavior is as expected for linear functions:

In : Maximize[{-x + y, 0 <= x <= 1 && 0 <= y <= 1 && z + 20 <= z}, {x, y,z}]
...  Maximize::infeas: There are no values of {x,y,z} for which the constraints 0<=x<=1&&0<=y<=1&&20+z<=z are satisfied and the objective function -x+y is real-valued.
$\endgroup$
  • $\begingroup$ is $z$ a variable that is somehow affecting $x$ and $y$ or just a parameter? can you express $x$ and $y$ as $x=x(z)$ and $y=y(z)$? $\endgroup$ – user42582 Oct 10 '17 at 13:15
  • $\begingroup$ No, x and y are independent of z (except in the third example, where x<=z because of the explicit constraint that enforces this). My four examples require no setup, you can just run them. Examples 1 and 3 are returned unevaluated, while examples 2 and 4 are solved. $\endgroup$ – Peter Oct 10 '17 at 14:55
  • $\begingroup$ Try NMaximize? NMaximize[{-E^x + E^y, 0 <= x <= 1 && 0 <= y <= 1}, {x, y, z}] This works for me if all you care about is getting max values for x and y $\endgroup$ – Matt Stein Oct 10 '17 at 15:13
  • $\begingroup$ @MattStein: NMaximize maximizes numerically, but I want the precise maximum. For this example, Maximize should give me a maximum of $E-1$, while NMaximize returns $1.71828$. $\endgroup$ – Peter Oct 11 '17 at 5:47
  • 1
    $\begingroup$ What does that mean " I have additional variables that are irrelevant to the maximization problem." If z occurs in the equation, it is relevant, and if z occurs not there , you can use any z you want. Show us your exact optmization problem. $\endgroup$ – Akku14 Oct 13 '17 at 6:15
4
+50
$\begingroup$

I have a problem like this at work where I am matching multiple models to the same data, trying to see which set of models (and parameters) gives the tightest confidence... so I need exactly this kind of parameter dropping.

dropParsMaximize[{f_,cons_|PatternSequence[]},pars:_List|_,
 dom_|PatternSequence[],opts:OptionsPattern[]]:=
 With[{p=Flatten@{pars}},With[{ps=Pick[p,!FreeQ[f,#]&/@p]},
 Maximize[{f,cons},ps,dom,opts]]]
dropParsMaximize[{-E^x+E^y,0<=x<=1&&0<=y<=1&&x<=z},{x,y,z}]
(*drops z from parameters because it isn't in the objective*)
(*Maximize[{-E^x+E^y,0<=x<=1&&0<=y<=1&&x<=z},{x,y}]*)    
dropConsParsMaximize[{f_,cons_},pars:_List|_,
 dom_|PatternSequence[],opts:OptionsPattern[]]:=
 Maximize@@Block[{Maximize=dropConsParsMaximizeInternal},
  dropParsMaximize[{f,cons},pars,dom,opts]]
dropConsParsMaximizeInternal[{f_,cons_},pars_List,
 dom_|PatternSequence[],opts:OptionsPattern[]]:=
 With[{cs=Pick[cons,Complement[Variables[#/.x_Symbol/;
  Context@x=!=Context[]:>List],pars]==={}&/@List@@cons]},
 {{f,cs},pars,dom,opts}]
dropConsParsMaximize[{-E^x+E^y,0<=x<=1&&0<=y<=1&&x<=z},{x,y,z}]
(*drops z from parameters and then drops any constraints with z*)
(*{-1+E,{x->0,y->1}}*)
dropConsParsMaximizeCheckCons[{f_,cons_},pars:_List|_,
 dom_|PatternSequence[],opts:OptionsPattern[]]:=
 With[{res=dropConsParsMaximize[{f,cons},pars,dom,opts]},Join[res,{cons}
  /.Last@res]]
dropConsParsMaximizeCheckCons[{-E^x+E^y,0<=x<=1&&0<=y<=1&&z+20<=z},{x,y,z}]
(*as above, but constraints are substituted with solution*)
(*{-1+E,{x->0,y->1},20+z<=z}*)
$\endgroup$
  • 1
    $\begingroup$ FWIW, I personally find it fairly (and sometimes prohibitively) difficult to read code with a lot of horizontal scrolling, and I hope you'll consider instead splitting such code into multiple lines. There are of course no hard and fast rules on this, and others might disagree with me, but I did find this meta answer from a few years ago (see point 4) which mentions avoiding horizontal scrolling. That said, thanks for your answers! (and +1) $\endgroup$ – jjc385 Oct 13 '17 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.