Could anybody explain to me why the following code fails ?

 NMaximize[{x1^2 + x2^2 + x3^2, 0.001 <= x1 <= 0.002, 
   0 <= x2 <= 0.002, 1 <= x3 <= 10, x3 \[Element] Integers }, {x1, x2,
    x3}, Method -> "SimulatedAnnealing", 
  EvaluationMonitor :> Sow[{x1, x2, x3}]]

In fact it outputs :

{{-\[Infinity], {x1 -> Indeterminate, x2 -> Indeterminate, 
   x3 -> Indeterminate}}, {}}

If i put x1's lower bound to 0 then it works but it uses only 0 values for x1

I suppose it has something to do with the integer x3.

Thanks in advance.

  • $\begingroup$ @Nasser thank you but as you can easy see constraints are consistent. For example x1=0.0015 , x2=0.001 , x3 = 3. $\endgroup$ – tchronis Aug 7 '13 at 0:53
  • $\begingroup$ @Nasser I think that the optimization function is not the problem here. I have focused on the use of the Tolerance parameter and the AccuracyGoal. $\endgroup$ – tchronis Aug 7 '13 at 1:32
  • $\begingroup$ @Nasser thanks. Maybe i wasn't so clear in my initial post. My purpose is not to solve this exact problem but to understand why it outputs that the constraints are not consistent.In my very complex problem (that i cannot state here) i need both continuous and discrete variables. $\endgroup$ – tchronis Aug 7 '13 at 17:55

By now, Mathematica is unable to do exact optimization with mixed integer and real variables.

Luckily, in your case, removing the constraint about x3 being an integer but rewriting the problem to fit into the exact optimization approach gives an optimum in which x3 is an exact integer.

Maximize[{x1^2 + x2^2 + x3^2, 
          1/1000 <= x1 <= 2/1000 && 0 <= x2 <= 2/1000 && 1 <= x3 <= 10},
          {x1, x2, x3}]

And this gives {12500001/125000, {x1 -> 1/500, x2 -> 1/500, x3 -> 10}} as a solution.


Maximize[{x1^2 + x2^2 + x3^2, 
          1/1000 <= x1 <= 2/1000 && 0 <= x2 <= 2/1000 && 1 <= x3 <= 10 &&
                  Element[x3, Integers]},
          {x1, x2, x3}]

Results in error:

Maximize::mixdom: Exact optimization with mixed real and integer variables is not yet implemented.

I think that the "N" functions (NMaximize, NMinimize, NSolve, etc) only deal with finite precision approximate numbers so they wouldn't "respect" integers (they are supposed to replace them with an approximate number).

  • $\begingroup$ So, @Nasser, this function x1^2+x2^2+x3^2 is not linear. $\endgroup$ – Ailton Andrade de Oliveira Aug 7 '13 at 13:52
  • 1
    $\begingroup$ I am only guessing here, but I suspect that the linearity restriction is present, because in this case we can use approximate numbers and still being able to "honor" restrictions to integers with an arbitrary precision. I agree with you that there is a need of clarification in the documentation as information is spread along several parts and there is a lot of space open to guesses. I think it could be improved by making clearer the case about integer optimization, for example. $\endgroup$ – Ailton Andrade de Oliveira Aug 7 '13 at 13:59

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.