The following code is a naive way to find the smallest number whose square has n divisors (the minimum should be its log, and x_i should be powers in its simple factorization). If I look at the case n = 2000 and use ten variables instead of twenty, this uses somewhere around 600 MB of memory. With a value of n, I'm actually trying to find the answer, I need about 20 variables so as not to miss the actual solution, and it quickly uses up all available memory, and then replaces the trash.
n=8*10^6; a = Table[N[Log[Prime[i]]], {i, 20}]; b = Table[Subscript[x, i], {i, 20}]; cond = Fold[And, Product[2 Subscript[x, i] + 1, {i, 20}] > n, Table[Subscript[x, i] >= 0, {i, 20}]] && b \[Element] Integers; NMinimize[{ab, cond}, b, MaxIterations -> 1000]
It turns out that the problem is not at all related to whole programming, etc. (removing the limit on integers doesn't help).
My best guess is that the problem is that Mathematica is wasting all the memory expanding Product[2 Subscript[x, i] + 1, {i, 20}] . If I replaced the product with only Product[Subscript[x, i],{i,20}] and changed the restrictions to >= 1 , not 0 , I get results without fuss and without a kernel that uses more than 50 MB of memory. (Although this preserves the limitation of inequality and does not change the problem of minimizing the objective function, this gives rise to the requirement of integrality - I get even results that correspond to half-integers in the real problem.)
stack overflow.squite in their case, they had an objective function, which was evaluated symbolically with huge costs. They were able to fix this by forcing the function to accept only numerical input, effectively hiding it from Mathematica "I have an Expand [] hammer and everything looks like a nail trend. But you cannot hide the restriction of such a function (Mathematica will complain about the invalid restriction).
Any thoughts on how to fix this?
Edit: I know the correct answer - after my Mathematica code did not work, I used AMPL and the special MINLP solver to get it (pretty fast). I just want to know how I can ever hope that I can use Mathematica's built-in nonlinear optimization functions in the future, despite the crazy things that I think are related to my limitations when I introduced them in the only way I know .