Ejemplo n.º 1
0
def optimize(cost, lower, upper):
    from mystic.tools import random_seed
    from pyina.launchers import Mpi as Pool
    random_seed(123)

    # generate a set of random starting points
    initial_values = samplepts(lower, upper, npts)

    # run optimizer for each grid point
    lb = [lower for i in range(len(initial_values))]
    ub = [upper for i in range(len(initial_values))]
    cf = [cost for i in range(len(initial_values))]
    # map:: params, energy, func_evals = local_optimize(cost,x0,lb,ub)
    results = Pool(nnodes).map(local_optimize, cf, initial_values, lb, ub)
    #print("results = %s" % results)

    # get the results with the lowest energy
    best = list(results[0][0]), results[0][1]
    func_evals = results[0][2]
    for result in results[1:]:
        func_evals += result[2]  # add function evaluations
        if result[1] < best[1]:  # compare energy
            best = list(result[0]), result[1]

    # return best
    print("solved: %s" % best[0])
    scale = 1.0
    diameter_squared = -best[1] / scale  #XXX: scale != 0
    return diameter_squared, func_evals
Ejemplo n.º 2
0
def UQ(start, end, lower, upper):
    #from pyina.launchers import Mpi as Pool
    from pathos.multiprocessing import ProcessingPool as Pool
    #from pool_helper import func_pickle  # if fails to pickle, try using a helper

    # run optimizer for each subdiameter
    lb = [lower + [lower[i]] for i in range(start, end + 1)]
    ub = [upper + [upper[i]] for i in range(start, end + 1)]
    nb = [nbins[:] for i in range(start, end + 1)]
    for i in range(len(nb)):
        nb[i][-1] = nb[i][i]
    cf = [costFactory(i) for i in range(start, end + 1)]
    #cf = [func_pickle(i)     for i in cf]
    #cf = [cost.name          for cost in cf]
    nnodes = len(lb)

    #construct cost function and run optimizer
    results = Pool(nnodes).map(optimize, cf, lb, ub, nb)
    #print "results = %s" % results

    results = zip(*results)
    diameters = list(results[0])
    function_evaluations = list(results[1])

    total_func_evals = sum(function_evaluations)
    total_diameter = sum(diameters)

    print "subdiameters (squared): %s" % diameters
    print "diameter (squared): %s" % total_diameter
    print "func_evals: %s => %s" % (function_evaluations, total_func_evals)

    return total_diameter
Ejemplo n.º 3
0
def optimize(cost, lb, ub):
    from pyina.launchers import Mpi as Pool
    from mystic.solvers import DifferentialEvolutionSolver2
    from mystic.termination import CandidateRelativeTolerance as CRT
    from mystic.strategy import Best1Exp
    from mystic.monitors import VerboseMonitor, Monitor
    from mystic.tools import random_seed

    random_seed(123)

    #stepmon = VerboseMonitor(100)
    stepmon = Monitor()
    evalmon = Monitor()

    ndim = len(lb)  # [(1 + RVend) - RVstart] + 1

    solver = DifferentialEvolutionSolver2(ndim, npop)
    solver.SetRandomInitialPoints(min=lb, max=ub)
    solver.SetStrictRanges(min=lb, max=ub)
    solver.SetEvaluationLimits(maxiter, maxfun)
    solver.SetEvaluationMonitor(evalmon)
    solver.SetGenerationMonitor(stepmon)
    solver.SetMapper(Pool().map)

    tol = convergence_tol
    solver.Solve(cost,termination=CRT(tol,tol),strategy=Best1Exp, \
                 CrossProbability=crossover,ScalingFactor=percent_change)

    print("solved: %s" % solver.bestSolution)
    scale = 1.0
    diameter_squared = -solver.bestEnergy / scale  #XXX: scale != 0
    func_evals = solver.evaluations
    return diameter_squared, func_evals
def optimize(cost, lower, upper, nbins):
    from mystic.tools import random_seed
    from pyina.launchers import TorqueMpi as Pool
    random_seed(123)

    # generate arrays of points defining a grid in parameter space
    grid_dimensions = len(lower)
    bins = []
    for i in range(grid_dimensions):
        step = abs(upper[i] - lower[i]) / nbins[i]
        bins.append([lower[i] + (j + 0.5) * step for j in range(nbins[i])])

    # build a grid of starting points
    from mystic.math.grid import gridpts
    from pool_helper import local_optimize
    from pool_helper import nnodes, queue, timelimit
    initial_values = gridpts(bins)

    # run optimizer for each grid point
    lb = [lower for i in range(len(initial_values))]
    ub = [upper for i in range(len(initial_values))]
    cf = [cost for i in range(len(initial_values))]
    # map:: params, energy, func_evals = local_optimize(cost,x0,lb,ub)
    config = {'queue': queue, 'timelimit': timelimit}
    results = Pool(nnodes, **config).map(local_optimize, cf, initial_values,
                                         lb, ub)
    #print("results = %s" % results)

    # get the results with the lowest energy
    best = list(results[0][0]), results[0][1]
    func_evals = results[0][2]
    for result in results[1:]:
        func_evals += result[2]  # add function evaluations
        if result[1] < best[1]:  # compare energy
            best = list(result[0]), result[1]

    # return best
    print("solved: %s" % best[0])
    scale = 1.0
    diameter_squared = -best[1] / scale  #XXX: scale != 0
    return diameter_squared, func_evals
Ejemplo n.º 5
0
    psow = VerboseMonitor(10)
    ssow = VerboseMonitor(10)

    random_seed(seed)
    print("first sequential...")
    solver = DifferentialEvolutionSolver2(ND,NP)  #XXX: sequential
    solver.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND)
    solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
    solver.SetGenerationMonitor(ssow)
    solver.Solve(ChebyshevCost, VTR(0.01), strategy=Best1Exp, \
                 CrossProbability=1.0, ScalingFactor=0.9, disp=1)
    print("")
    print_solution( solver.bestSolution )

    #'''
    random_seed(seed)
    print("\n and now parallel...")
    solver2 = DifferentialEvolutionSolver2(ND,NP)  #XXX: parallel
    solver2.SetMapper(Pool(NNODES).map)
    solver2.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND)
    solver2.SetEvaluationLimits(generations=MAX_GENERATIONS)
    solver2.SetGenerationMonitor(psow)
    solver2.Solve(ChebyshevCost, VTR(0.01), strategy=Best1Exp, \
                  CrossProbability=1.0, ScalingFactor=0.9, disp=1)
    print("")
    print_solution( solver2.bestSolution )
    #'''

# end of file
Ejemplo n.º 6
0
def test_pool():
    from pyina.launchers import MpiPool as Pool
    pool = Pool(nodes=4)
    check_sanity(pool)
    check_maps(pool, items, delay)
    check_dill(pool)
Ejemplo n.º 7
0
    # print m.ready()
    # print m.wait(0)
    tries = 0
    while not m.ready():
        time.sleep(delay)
        tries += 1
        if verbose: print "TRY: %s" % tries
        if tries >= maxtries:
            if verbose: print "TIMEOUT"
            break
#print m.ready()


#   print m.get(0)
    res = m.get()
    if verbose: print res
    z = [0] * len(x)
    assert res == map(squared, x)  # x, z)
    assert tries > 0
    assert maxtries > tries  #should be True, may not be if CPU is SLOW

if __name__ == '__main__':
    from pyina.launchers import MpiPool as Pool
    pool = Pool(nodes=4)
    test_sanity(pool)
    test_maps(pool, items, delay)
    test_dill(pool)
#test_ready( pool, maxtries, delay, verbose=False )

# EOF