Parallel  the AI algorithm.

the code is a bit mess due to debuging the parallelizing algorithm.
I am going to tidy it up later.