You'll want to make this an optional parameter to maximize(). Do this by

def maximize(problem, numGradientSamples = 1) {