Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
@Nasser thank you but as you can easy see constraints are consistent. For example x1=0.0015 , x2=0.001 , x3 = 3. – tchronis Aug 7 '13 at 0:53
@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. – tchronis Aug 7 '13 at 1:32
@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. – 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).

share|improve this answer
So, @Nasser, this function x1^2+x2^2+x3^2 is not linear. – Ailton Andrade de Oliveira Aug 7 '13 at 13:52
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. – Ailton Andrade de Oliveira Aug 7 '13 at 13:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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