def minimize_linregr(expected_xs_dict, err_func, stall_precision, max_stalled_in_row, ga_options,
                     return_classify = False, test_classification = False):
    error_f = lambda coefs: sum([ err_func(linregr(coefs, xs), expected) 
                                    for expected, xss in expected_xs_dict.iteritems() 
                                    for xs in xss
                                ])
    res = ga.minimize_until_stall(error_f, stall_precision, max_stalled_in_row, ga_options)
    
    res_ = (res, )
    
    if return_classify or test_classification:
        classify = linregr_classifier(expected_xs_dict.keys(), res['best'][0])
        res_ = res_ + (classify, )
    
    if test_classification:
        test_results = [ (clazz, [classify(xs) == clazz for xs in xss])
                          for clazz, xss in expected_xs_dict.iteritems()
                          ]
        
        test_count   = map(lambda (clazz,rs): (clazz, count(identity, rs), len(rs)), test_results)
        
        test_results = map(lambda (clazz,c,n): (clazz, str(c) + " of " + str(n)),
                           test_count
                           )
        
        test_results_total = reduce(lambda (ac,an), (_,c,n): (ac+c, an+n), test_count, (0,0))
        
        test_results.sort(key=lambda(c,_): c)
        
        res_ = res_ + (test_results, test_results_total)
        
    
    return res_
def ga_test_s_2():
    _print_2()
    res   = ga.minimize_until_stall(_fit_f_2, 1e-5, 40, _opts_2)
    print "Result:"
    print res
def ga_test_s_1():
    _print_1()
    res = ga.minimize_until_stall(_fit_f_1, 1e-5, 40, _opts_1) # fitness_func, stall_precision, options, max_iter = None)
    print "Result:"
    print res