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
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
def test_calculate_methods(npts=2): upper_bounds = [1.0] lower_bounds = [0.0] # ------------------------------------- # generate initial coordinates, weights # ------------------------------------- # get a random distribution of points if disp: print("generate random points and weights") coordinates = samplepts(lower_bounds, upper_bounds, npts) D0 = D = [i[0] for i in coordinates] if disp: print("positions: %s" % D) # calculate sample range R0 = R = spread(D) if disp: print("range: %s" % R) # select weights randomly in [0,1], then normalize so sum(weights) = 1 wts = random_samples([0], [1], npts)[0] weights = normalize(wts, 0.0, zsum=True) if disp: print("weights (when normalized to 0.0): %s" % weights) assert almostEqual(sum(weights), 0.0, tol=1e-15) weights = normalize(wts, 1.0) assert almostEqual(sum(weights), 1.0, tol=1e-15) if disp: print("weights (when normalized to 1.0): %s" % weights) w = norm(weights) if disp: print("norm: %s" % w) assert almostEqual(w, sum(weights) / npts) # calculate sample mean m0 = m = mean(D, weights) if disp: print("mean: %s" % m) if disp: print("") # ------------------------------------- # modify coordinates, maintaining mean & range # ------------------------------------- # get new random distribution if disp: print("modify positions, maintaining mean and range") coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] # impose a range and mean on the points D = impose_spread(R, D, weights) D = impose_mean(m, D, weights) # print results if disp: print("positions: %s" % D) R = spread(D) if disp: print("range: %s" % R) assert almostEqual(R, R0) m = mean(D, weights) if disp: print("mean: %s" % m) assert almostEqual(m, m0) if disp: print("") # ------------------------------------- # modify weights, maintaining mean & norm # ------------------------------------- # select weights randomly in [0,1] if disp: print("modify weights, maintaining mean and range") wts = random_samples([0], [1], npts)[0] # print intermediate results #print("weights: %s" % wts) #sm = mean(D, wts) #print("tmp mean: %s" % sm) #print("") # impose mean and weight norm on the points D = impose_mean(m, D, wts) DD, weights = impose_weight_norm(D, wts) # print results if disp: print("weights: %s" % weights) w = norm(weights) if disp: print("norm: %s" % w) assert almostEqual(w, sum(weights) / npts) if disp: print("positions: %s" % DD) R = spread(DD) if disp: print("range: %s" % R) assert almostEqual(R, R0) sm = mean(DD, weights) if disp: print("mean: %s" % sm) assert almostEqual(sm, m0) sv = variance(DD, weights) if disp: print("var: %s" % sv) assert not almostEqual(sv, R) assert almostEqual(sv, 0.0, tol=.3) # ------------------------------------- # modify variance, maintaining mean # ------------------------------------- if disp: print("\nmodify variance, maintaining mean") DD = impose_variance(R, DD, weights) sm = mean(DD, weights) if disp: print("mean: %s" % sm) assert almostEqual(sm, m0) sv = variance(DD, weights) if disp: print("var: %s" % sv) assert almostEqual(sv, R)
def test_calculate_methods(npts=2): upper_bounds = [1.0] lower_bounds = [0.0] # ------------------------------------- # generate initial coordinates, weights # ------------------------------------- # get a random distribution of points print "generate random points and weights" coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] print "positions: %s" % D # calculate sample range R = spread(D) print "range: %s" % R # select weights randomly in [0,1], then normalize so sum(weights) = 1 wts = random_samples([0], [1], npts)[0] weights = normalize(wts, 0.0, zsum=True) print "weights (when normalized to 0.0): %s" % weights weights = normalize(wts) print "weights (when normalized to 1.0): %s" % weights w = norm(weights) print "norm: %s" % w # calculate sample mean m = mean(D, weights) print "mean: %s" % m print "" # ------------------------------------- # modify coordinates, maintaining mean & range # ------------------------------------- # get new random distribution print "modify positions, maintaining mean and range" coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] # impose a range and mean on the points D = impose_spread(R, D, weights) D = impose_mean(m, D, weights) # print results print "positions: %s" % D R = spread(D) print "range: %s" % R m = mean(D, weights) print "mean: %s" % m print "" # ------------------------------------- # modify weights, maintaining mean & norm # ------------------------------------- # select weights randomly in [0,1] print "modify weights, maintaining mean and range" wts = random_samples([0], [1], npts)[0] # print intermediate results #print "weights: %s" % wts #sm = mean(D, wts) #print "tmp mean: %s" % sm #print "" # impose mean and weight norm on the points D = impose_mean(m, D, wts) DD, weights = impose_weight_norm(D, wts) # print results print "weights: %s" % weights w = norm(weights) print "norm: %s" % w print "positions: %s" % DD R = spread(DD) print "range: %s" % R sm = mean(DD, weights) print "mean: %s" % sm sv = variance(DD, weights) print "var: %s" % sv # ------------------------------------- # modify variance, maintaining mean # ------------------------------------- print "\nmodify variance, maintaining mean" DD = impose_variance(R, DD, weights) sm = mean(DD, weights) print "mean: %s" % sm sv = variance(DD, weights) print "var: %s" % sv
def test_calculate_methods(npts=2): upper_bounds = [1.0] lower_bounds = [0.0] # ------------------------------------- # generate initial coordinates, weights # ------------------------------------- # get a random distribution of points if disp: print "generate random points and weights" coordinates = samplepts(lower_bounds, upper_bounds, npts) D0 = D = [i[0] for i in coordinates] if disp: print "positions: %s" % D # calculate sample range R0 = R = spread(D) if disp: print "range: %s" % R # select weights randomly in [0,1], then normalize so sum(weights) = 1 wts = random_samples([0],[1], npts)[0] weights = normalize(wts, 0.0, zsum=True) if disp: print "weights (when normalized to 0.0): %s" % weights assert almostEqual(sum(weights), 0.0, tol=1e-15) weights = normalize(wts) assert almostEqual(sum(weights), 1.0, tol=1e-15) if disp: print "weights (when normalized to 1.0): %s" % weights w = norm(weights) if disp: print "norm: %s" % w assert almostEqual(w, sum(weights)/npts) # calculate sample mean m0 = m = mean(D,weights) if disp: print "mean: %s" % m if disp: print "" # ------------------------------------- # modify coordinates, maintaining mean & range # ------------------------------------- # get new random distribution if disp: print "modify positions, maintaining mean and range" coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] # impose a range and mean on the points D = impose_spread(R, D, weights) D = impose_mean(m, D, weights) # print results if disp: print "positions: %s" % D R = spread(D) if disp: print "range: %s" % R assert almostEqual(R, R0) m = mean(D, weights) if disp: print "mean: %s" % m assert almostEqual(m, m0) if disp: print "" # ------------------------------------- # modify weights, maintaining mean & norm # ------------------------------------- # select weights randomly in [0,1] if disp: print "modify weights, maintaining mean and range" wts = random_samples([0],[1], npts)[0] # print intermediate results #print "weights: %s" % wts #sm = mean(D, wts) #print "tmp mean: %s" % sm #print "" # impose mean and weight norm on the points D = impose_mean(m, D, wts) DD, weights = impose_weight_norm(D, wts) # print results if disp: print "weights: %s" % weights w = norm(weights) if disp: print "norm: %s" % w assert almostEqual(w, sum(weights)/npts) if disp: print "positions: %s" % DD R = spread(DD) if disp: print "range: %s" % R assert almostEqual(R, R0) sm = mean(DD, weights) if disp: print "mean: %s" % sm assert almostEqual(sm, m0) sv = variance(DD, weights) if disp: print "var: %s" % sv assert not almostEqual(sv, R) assert almostEqual(sv, 0.0, tol=.2) # ------------------------------------- # modify variance, maintaining mean # ------------------------------------- if disp: print "\nmodify variance, maintaining mean" DD = impose_variance(R, DD, weights) sm = mean(DD, weights) if disp: print "mean: %s" % sm assert almostEqual(sm, m0) sv = variance(DD, weights) if disp: print "var: %s" % sv assert almostEqual(sv, R)
def test_calculate_methods(npts=2): upper_bounds = [1.0] lower_bounds = [0.0] # ------------------------------------- # generate initial coordinates, weights # ------------------------------------- # get a random distribution of points print "generate random points and weights" coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] print "positions: %s" % D # calculate sample range R = spread(D) print "range: %s" % R # select weights randomly in [0,1], then normalize so sum(weights) = 1 wts = random_samples([0],[1], npts)[0] weights = normalize(wts, 0.0, zsum=True) print "weights (when normalized to 0.0): %s" % weights weights = normalize(wts) print "weights (when normalized to 1.0): %s" % weights w = norm(weights) print "norm: %s" % w # calculate sample mean m = mean(D,weights) print "mean: %s" % m print "" # ------------------------------------- # modify coordinates, maintaining mean & range # ------------------------------------- # get new random distribution print "modify positions, maintaining mean and range" coordinates = samplepts(lower_bounds, upper_bounds, npts) D = [i[0] for i in coordinates] # impose a range and mean on the points D = impose_spread(R, D, weights) D = impose_mean(m, D, weights) # print results print "positions: %s" % D R = spread(D) print "range: %s" % R m = mean(D, weights) print "mean: %s" % m print "" # ------------------------------------- # modify weights, maintaining mean & norm # ------------------------------------- # select weights randomly in [0,1] print "modify weights, maintaining mean and range" wts = random_samples([0],[1], npts)[0] # print intermediate results #print "weights: %s" % wts #sm = mean(D, wts) #print "tmp mean: %s" % sm #print "" # impose mean and weight norm on the points D = impose_mean(m, D, wts) DD, weights = impose_weight_norm(D, wts) # print results print "weights: %s" % weights w = norm(weights) print "norm: %s" % w print "positions: %s" % DD R = spread(DD) print "range: %s" % R sm = mean(DD, weights) print "mean: %s" % sm sv = variance(DD, weights) print "var: %s" % sv # ------------------------------------- # modify variance, maintaining mean # ------------------------------------- print "\nmodify variance, maintaining mean" DD = impose_variance(R, DD, weights) sm = mean(DD, weights) print "mean: %s" % sm sv = variance(DD, weights) print "var: %s" % sv