'group the results by minimizer and average over the runs'

def average_results(self):

    grouped_results = defaultdict(list)
    for res in self.results:
        grouped_results[res.name].append(res)
    averaged_results = dict()
    for (name, result_list) in grouped_results.items():
        newres = scipy.optimize.OptimizeResult()
        newres.name = name
        newres.mean_nfev = np.mean([r.nfev for r in result_list])
        newres.mean_njev = np.mean([r.njev for r in result_list])
        newres.mean_nhev = np.mean([r.nhev for r in result_list])
        newres.mean_time = np.mean([r.time for r in result_list])
        newres.ntrials = len(result_list)
        newres.nfail = len([r for r in result_list if (not r.success)])
        newres.nsuccess = len([r for r in result_list if r.success])
        try:
            newres.ndim = len(result_list[0].x)
        except TypeError:
            newres.ndim = 1
        averaged_results[name] = newres
    return averaged_results
