If that cost function is NP-complete or NP-hard (due to additional constraints often), you might want to take a look at OptaPlanner (Java, open source).
There is quite some commons-math documentation though.
Alternatively, there is also JavaOctave (but I doubt if that's 100% pure Java).