# Evaluate NMinimize/NMaximize on a grid

I'm working on a minimization problem with moderate number of variables (~10-20). I don't need several significant digits, so I have already set my WorkingPrecision->4, which does help speed up the program, but I also don't need infinite granularity of the variables being optimized. Is there some way to set up NMinimize so that it minimizes all variables on a grid, and doesn't worry about improving the calculation by using a finer grain than the grid spacing? I had an idea to use a Mod constraint (shown below), but it doesn't seem to work and freezes up NMinimize for a larger number of variables. Any advice would be appreciated.

A very barebones code sample of what I tried, here with just a single variable for simplicity (with a grid spacing off 0.001), though it doesn't seem to be working as I would expect - monitoring the tested variables with Reap/Sow shows they often do not lie on the grid itself.

Reap[NMaximize[{Sin[x], 0 < x < 4, Mod[x, 0.001] == 0}, x, Method -> {"DifferentialEvolution"}, StepMonitor :> Sow[x]]]

## 1 Answer

Your question is somewhat unclearly formulated. If you are interested in the maximum value on the grid, then the rescaling does the job:

NMaximize[{Sin[x/1000],1 <= x &&x <= 3999 && x \[Element] Integers},x, WorkingPrecision->20]
(*{0.99999997925861283316, {x -> 1571}*)


That approach works in higher dimensions too, e.g.

 NMaximize[{Sin[x/1000 + y/1000], 1 <= x && x <= 3999 && x \[Element] Integers && 1 <= y &&

y <= 3999 && y \[Element] Integers}, {x, y}, WorkingPrecision -> 20]

(*{0.99999999983134455336, {x -> 3967, y -> 3887}}*)


Let us verify the obtained result by

N[Max[Table[Sin[x/500 + y/500], {x, 1, 1999}, {y, 1, 1999}]], 20]
(*0.99999999983134455336*)


which is not bad.

• That's exactly what I was looking for - thanks! – KHAAAAAAAAN Apr 4 '20 at 19:47