Bad behavior of NMinimize for discontinuous functions

Hello,

I have encountered a weird behavior of NMinimize for discontinuous functions. Consider the following piece of code:

f[x_] := Piecewise[{{0, 0 <= x <=  Pi/3}, {Pi/3, 
    Pi/3 <  x <=  2 Pi/3}, {2 Pi/3, 
    2 Pi/3 <  x <=  3 Pi/3} , {3 Pi/3, 
    3 Pi/3 <  x <= 4 Pi/3}, {4 Pi/3, 4 Pi/3 <  x <= 5 Pi/3}, {5 Pi/3, 
    5 Pi/3 <  x <= 6 Pi/3} }]
parameters = Flatten[Table[c[i], {i, 1, 5}]];
constraints = Flatten[Table[0 <= c[i] <=  2 Pi, {i, 1, 5}]];
CycleConstr = {c[1] == c[5]};

NMinimize[{Sum[
   Cos[c[i]] (Cos[f[c[i]]] - Cos[f[c[i + 1]]]) + 
    Sin[c[i]] (Sin[f[c[i]]] - Sin[f[c[i + 1]]]), {i, 1, 4}], 
  constraints, CycleConstr}, parameters]

The output:
{0., {c[1] → 3.47562, c[2] → 3.58266, c[3] → 4.15874,
c[4] → 3.52307, c[5] → 3.47562}}

However, it is easy to check that the minimum is -1, which the direct substitution shows

Simplify[Sum[
   Cos[c[i]] (Cos[f[c[i]]] - Cos[f[c[i + 1]]]) + 
    Sin[c[i]] (Sin[f[c[i]]] - Sin[f[c[i + 1]]]), {i, 1, 4}] /. {
   c[1] -> Pi/3,
   c[2] -> 2 Pi/3,
   c[3] -> 3 Pi/3,
   c[4] -> 5 Pi/3,
   c[5] -> Pi/3
   }]

I would like to play with the above example. So if anyone has any thoughts how to find the minimum with Mathematica functions, I would very much appreciate.

Try to apply NMinimize separately to each piece of f[x]. Then choose global minimum result.

It would be better if you put your Mathematica code into a code sample block, so it can be copied and pasted into Mathematica without error.

Thank you for your interest. That is my first post and I did not knew about a code sample block. I updated post accordingly.

I am not sure, what you mean. My function depends on 5 parameters that should be within [0,2Pi]. I am not sure how you propose to minimize for each interval separately

You might get better results with Method → “SimulatedAnnealing” or Method → “DifferentialEvolution”

Thank you! Results are not fantastic, but it gets closer to the minimum.

The two methods have a number of options which you can adjust for better results.

Thank you. This references is VERY helpful!

I’ve often wondered why the info in that reference isn’t in the NMinimize documentation.

It should be!