Exemplo n.º 1
0
def optimize(solver, mapper, nodes, target='rosen', **kwds):
    if target == 'rosen': # 3d-rosenbrock
        ndim = 3
        actual_coeffs = [1.0] * ndim
        pprint = list
    else: # 4th-order chebyshev
        from poly import chebyshev4coeffs as actual_coeffs
        ndim = len(actual_coeffs)
        from mystic.math import poly1d as pprint

    # number of trials
    N = nodes
    print("Number of trials: %s" % N)
    print("===============")

    # initial guess
    import random
    x0 = ([random.uniform(-100,100) for i in range(ndim)] for i in range(N))

    # minimize the function
    results = mapper(nodes).map(solver, x0)

    # find the results with the lowest energy
    from optimize_helper import best_results
    solution = best_results(results)

    print("===============")
    print("Actual params:\n %s" % pprint(actual_coeffs))
    print("Solved params:\n %s" % pprint(solution[0]))
    print("Function value: %s" % solution[1])
    print("Total function evals: %s" % solution[4])
    return 
Exemplo n.º 2
0
def optimize(solver, mapper, nodes, target="rosen", **kwds):
    if target == "rosen":  # 3d-rosenbrock
        # Rosenbrock function
        from dejong import rosen as the_model

        ndim = 3
        actual_coeffs = [1.0] * ndim
        pprint = list
    else:  # 4th-order chebyshev
        # Chebyshev cost function
        from poly import chebyshev4cost as the_model
        from poly import chebyshev4coeffs as actual_coeffs

        ndim = len(actual_coeffs)
        from mystic.math import poly1d as pprint

    # number of trials
    N = nodes
    print "Number of trials: %s" % N
    print "==============="

    # initial guess
    import random

    x0 = ([random.uniform(-100, 100) for i in xrange(ndim)] for i in xrange(N))
    model = (the_model for i in xrange(N))

    # minimize the function
    results = mapper(nodes).map(the_solver, model, x0)

    # find the results with the lowest energy
    from optimize_helper import best_results

    solution = best_results(results)

    print "==============="
    print "Actual params:\n %s" % pprint(actual_coeffs)
    print "Solved params:\n %s" % pprint(solution[0])
    print "Function value: %s" % solution[1]
    print "Total function evals: %s" % solution[4]
    return
Exemplo n.º 3
0
def optimize(solver, nodes, target='rosen', **kwds):
    if target == 'rosen': # 3d-rosenbrock
        # Rosenbrock function
        from dejong import rosen as the_model
        ndim = 3
        actual_coeffs = [1.0] * ndim
        pprint = list
    else: # 4th-order chebyshev
        # Chebyshev cost function
        from poly import chebyshev4cost as the_model
        from poly import chebyshev4coeffs as actual_coeffs
        ndim = len(actual_coeffs)
        from mystic.math import poly1d as pprint

    # number of trials
    N = nodes
    print "Number of trials: %s" % N
    print "==============="

    # initial guess
    import random
    x0 = ([random.uniform(-100,100) for i in xrange(ndim)] for i in xrange(N))
    model = (the_model for i in xrange(N))

    # minimize the function
    from itertools import imap                   # itertools
    results = imap(the_solver, model, x0, **kwds)

    # find the results with the lowest energy
    from optimize_helper import best_results
    solution = best_results(results)

    print "==============="
    print "Actual params:\n %s" % pprint(actual_coeffs)
    print "Solved params:\n %s" % pprint(solution[0])
    print "Function value: %s" % solution[1]
    print "Total function evals: %s" % solution[4]
    return