Hello guys!
I'll give you the big picture of what I'm doing. I'm solving a combinatorial optimization problem. I find an initial solution and then try to improve it with some heuristics. One of them involves some local search techniques. Everything works well except that I encounter a memory problem. My local search works only for small instances of the problem (ie up to 600 nodes). For larger instances I get an OutOfMemoryError exception. The exception occures in the method that finds the "neighbors" of the current solution. You can think of this method as one that finds all possible permutations of a list (all possible triplets or quadraplets of arcs, more precisely).
Each neighbor-permutation is saved in an array (since I know its length) and the whole neighborhood in an ArrayList.
My thought is to find the number of permutations P(n,r) so that I can use an array for the neighborhood or at least an ArrayList of initial size -since the exception occurs while the ArrayList is increasing its size, ie copies its internal array to a bigger one, and I think it will avoid that if it has an initial size.
I implemented a recursive method to calculate P(n,r)
public static long factorial(long n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } //P(n,r) = n! / (n - r)! public static long permutations(long n, long r) { long arithmitis = factorial(n); //n! long paron = factorial(n - r); //(n - r)! return arithmitis / paron; }
This method works fine for a relatively small n (r is always 3 or 4) but when I try to find P(120,3) for example -which is near to what I will have to find for my program- it crashes, because the values of factorial(n) factorial(n-r) are very large and cannot be held in the long variables.
Here are my questions:
Firstly, do you think that saving the neighborhood in an array or an ArrayList with initial size would improve my memory problem or I'm just wasting my time?
And secondly, any thoughts on how to make the permutations(long n, long r) method to work?
Thanks for your time.
PS:I have used the -Xmx option to increase the VM's heap space with some good results.