def run_example(): pso = PSO(save_sampled=False, verbose=True, num_proc=4) pso.set_cost_function(likelihood) pso.set_start_position(xnominal) pso.set_bounds(2) pso.set_speed(-.25, .25) pso.run(25, 100) display(pso.best)
def run_example(): pso = PSO(save_sampled=False, verbose=True, num_proc=4) pso.set_cost_function(likelihood) pso.set_start_position(xnominal) pso.set_bounds(lower=lower, upper=upper) pso.set_speed(-.25, .25) pso.run(25, 200) display(pso.best) np.save('calibrated_pars_pso1', pso.best)
def __call__(self, pso_kwargs=None, cost_type='norm_logpdf', custom_cost=None): """Call the SwarmIt instance to construct to instance of the NestedSampling object. Args: pso_kwargs (dict): Dictionary of any additional optional keyword arguments to pass to the PSO object constructor. Defaults to dict(). cost_type (str): Define the type of cost estimator to use. Options are 'norm_logpdf'=>Compute the cost using the normal distribution estimator, 'mse'=>Compute the cost using the negative mean squared error estimator, 'sse'=>Compute the cost using the negative sum of squared errors estimator. Defaults to 'norm_logpdf'. Returns: type: Description of returned object. """ if pso_kwargs is None: pso_kwargs = dict() # self.ns_version = ns_version self._pso_kwargs = pso_kwargs #population_size = pso_population_size if cost_type == 'mse': cost = self.mse_cost elif cost_type == 'sse': cost = self.sse_cost elif (cost_type == 'custom') and (custom_cost is not None): self.set_custom_cost(custom_cost) cost = self.custom_cost else: cost = self.norm_logpdf_cost # Construct the PSO if 'save_sampled' not in pso_kwargs.keys(): pso_kwargs['save_sampled'] = False if 'verbose' not in pso_kwargs.keys(): pso_kwargs['verbose'] = False pso = PSO(**pso_kwargs) pso.set_start_position(self._starting_position) pso.set_cost_function(cost) pso.set_bounds(lower=self._lower, upper=self._upper) pso.set_speed(-.25, .25) return pso
def run_example(): # Runs the cost function to calculate error between model and data print("Error at start = {}".format(likelihood(starting_position)[0])) # Displays the model with defaul positions display(starting_position, save_name='starting_position') # create PSO object pso = PSO(save_sampled=False, verbose=True, num_proc=4) pso.set_cost_function(likelihood) pso.set_start_position(starting_position) # allows particles to move +/- 2 orders of magnitude pso.set_bounds(2) # sets maximum speed that a particle can travel pso.set_speed(-.25, .25) pso.run(num_particles=25, num_iterations=50, stop_threshold=1e-5) display(pso.best, save_name='best_fit') np.savetxt("pso_fit_for_model.csv", pso.best)
def run_example(): # Runs the cost function to calculate error between model and data print("Error at start = {}".format(likelihood(starting_position)[0])) # Displays the model with defaul positions display(starting_position, save_name='starting_position') # create PSO object pso = PSO(save_sampled=False, verbose=True, num_proc=4) pso.set_cost_function(likelihood) pso.set_start_position(starting_position) # allows particles to move +/- 2 orders of magnitude pso.set_bounds(2) # sets maximum speed that a particle can travel pso.set_speed(-.25, .25) pso.run(num_particles=25, num_iterations=50, stop_threshold=1e-5) display(pso.best, save_name='best_fit') np.savetxt("pso_fit_for_model.csv", pso.best)
def run_pso(run, iterations, bd): pso = PSO(save_sampled=False, verbose=True, shrink_steps=False, num_proc=14) pso.set_cost_function(costfunction) pso.set_start_position(starting_position) pso.set_bounds(bd) pso.set_speed(-.1, .1) pso.run(num_particles=200, num_iterations=iterations, stop_threshold=1e-5) #print('best pos: ', pso.best.pos) print('history ', pso.history) print('run ', run) print('fit ', pso.best.fitness) print('all fitness ', pso.values) np.savetxt("H841_params_" + run + ".txt", 10**pso.history, delimiter=",") np.savetxt("H841_fit_" + run + ".txt", pso.values, delimiter=",")
def run_example_multiple(): best_pars = np.zeros((100, len(model.parameters))) counter = 0 for i in range(100): pso = PSO(save_sampled=False, verbose=False, num_proc=4) pso.set_cost_function(likelihood) nominal_random = xnominal + np.random.uniform(-1, 1, len(xnominal)) pso.set_start_position(nominal_random) pso.set_bounds(2.5) pso.set_speed(-.25, .25) pso.run(25, 100) if pso.best.fitness.values[0] < 0.066: Y = np.copy(pso.best) param_values[rates_of_interest_mask] = 10**Y best_pars[counter] = param_values counter += 1 print(i, counter) # display(pso.best) np.save('jnk3_noASK1_ncalibrated_pars_1h', best_pars)
# USER-Set: must appropriately update cost function! def cost(position): Y = np.copy(position) param_values[calibrate_mask] = 10**Y sim = solver.run(param_values=param_values).all logp_data = np.sum(like_data.logpdf(sim['observable'])) if np.isnan(logp_data): logp_data = np.inf return -logp_data, # Setup the particle swarm optimization run # Set the number of particles in the swarm. num_particles = 25 # Set the number of iterations for PSO run. num_iterations = 50 # Construct the optimizer pso = PSO(save_sampled=False, verbose=True, num_procs=1) pso.set_cost_function(cost) starting_position = swarm_param.centers() pso.set_start_position(starting_position) pso.set_bounds(lower=swarm_param.lower(), upper=swarm_param.upper()) # sets maximum speed that a particle can travel pso.set_speed(-.25, .25) # run it pso.run(num_particles, num_iterations, stop_threshold=1e-5) print("Best parameters: ", pso.best)