def check_if_optimal_candidate_in_range_variables(mechanism_dir, model_dir, variables_all): evaluator = Evaluator([], [save_dir], 0, []) load_mechanism_dir(mechanism_dir) optimal_candidate = evaluator.get_optimal_candidate(model_dir, variables_all) for i in range(len(variables_all)): assert variables_all[i][0] <= optimal_candidate[i] <= variables_all[i][1]
def run_optimization(save_dir, normalize, return_init_pop, method, method_type, method_args, optimization_type): # load problem specification n_trials, pop_size, max_iterations, problem_dicts, seeds, init_pops = load_problem_specification(save_dir, normalize, return_init_pop) # load all different mechanisms beforehand mechanism_dirs = set([p['mechanism_dir'] for p in problem_dicts]) for m_dir in mechanism_dirs: load_mechanism_dir(m_dir) for trial in range(n_trials): # make directory for each method and trial save_dir_trial = save_dir + '/' + method + '/trial' + str(trial) + '/' if not os.path.exists(save_dir_trial): os.makedirs(save_dir_trial) # save additional method arguments with open(save_dir_trial + 'method_args.json', 'w') as f: json.dump(method_args, f, indent=4) # create problem problem_dicts[trial]['mechanism_dir'] = None problem = getattr(problem, problem_dicts[trial]['name'])(**problem_dicts[trial]) # open individuals file individuals_file = open(save_dir_trial+'/individuals_file.csv', 'w') # measure runtime start_time = time() # run optimization if optimization_type == 'scipy_based': optimize_scipy_based(pop_size, max_iterations, method, method_type, method_args, problem, individuals_file) elif optimization_type == 'bio_inspired': method_args['pop_size'] = pop_size method_args['max_generations'] = max_iterations seed = float(np.loadtxt(save_dir + 'specification/trial'+str(trial)+'/seed.txt')) optimize_bio_inspired(method, method_type, method_args, problem, individuals_file, seed) elif optimization_type == 'SA': method_args['pop_size'] = 1 method_args['max_generations'] = max_iterations seed = float(np.loadtxt(save_dir + 'specification/trial'+str(trial)+'/seed.txt')) optimize_SA(save_dir_trial, pop_size, method, method_type, method_args, problem, individuals_file, seed) elif optimization_type == 'random': seed = float(np.loadtxt(save_dir + 'specification/trial' + str(trial) + '/seed.txt')) optimize_random(pop_size, max_iterations, init_pops[trial], problem, seed, individuals_file) # print runtime end_time = time() print 'Runtime: ' + str(end_time - start_time) + ' sec' # close individuals file individuals_file.close()
def save_problem_specification(save_dir, n_trials, pop_size, max_iterations, problem_dicts, variables_all, generator=None): save_dir += 'specification/' # create save directory if os.path.exists(save_dir): print 'Saving directory already exists!' return False os.makedirs(save_dir) # save number of trials, population size, maximal number of iterations np.savetxt(save_dir+'/n_trials.txt', np.array([n_trials])) np.savetxt(save_dir+'/pop_size.txt', np.array([pop_size])) np.savetxt(save_dir+'/max_iterations.txt', np.array([max_iterations])) with open(save_dir+'/variables_all.json', 'w') as f: json.dump(variables_all, f) # load all different mechanism directories (needed for saving cell) mechanism_dirs = set([p['mechanism_dir'] for p in problem_dicts]) for m_dir in mechanism_dirs: load_mechanism_dir(m_dir) for trial in range(n_trials): # create directory for each trial save_dir_trial = save_dir + 'trial'+str(trial)+'/' os.makedirs(save_dir_trial) # save seeds seed = time() np.savetxt(save_dir_trial+'seed.txt', np.array([seed])) # save problem dict with open(save_dir_trial+'problem.json', 'w') as f: json.dump(problem_dicts[trial], f, indent=4) # save cell with open(save_dir_trial+'cell.json', 'w') as f: json.dump(Cell.from_modeldir(problem_dicts[trial]['model_dir']).get_dict(), f, indent=4) # save initial population if generator is not None: random = Random() random.seed(seed) initial_pop = list() lower_bound, upper_bound, path_variables = get_lowerbound_upperbound_path(problem_dicts[trial]['variables']) for i in range(pop_size): initial_pop.append(generator(random, lower_bound, upper_bound)) with open(save_dir_trial+'initial_pop.json', 'w') as f: json.dump(initial_pop, f, indent=4) return True
def __init__(self, variable_keys, errfun_name, fitfun_names, fitnessweights, model_dir, mechanism_dir, data_dir, simulation_params=None, args=None): self.variable_keys = variable_keys self.errfun_names = errfun_name self.fitfun_names = fitfun_names self.errfun = getattr(errfuns, errfun_name) self.fitfuns = [getattr(fitfuns, fitfun_name) for fitfun_name in fitfun_names] self.fitnessweights = fitnessweights self.args = args self.model_dir = model_dir self.mechanism_dir = mechanism_dir if mechanism_dir is not None: load_mechanism_dir(mechanism_dir) self.data_dir = data_dir self.data = pd.read_csv(data_dir) self.init_simulation_params = simulation_params if simulation_params is None: simulation_params = {} self.simulation_params = extract_simulation_params(self.data, **simulation_params) self.data_to_fit = [fitfun(self.data.v.values, self.data.t.values, self.data.i.values, self.args) for fitfun in self.fitfuns] self.cell = self.get_cell()
def load_mechanisms(self): with open(self.save_dirs[0] + '/specification/trial0/problem.json', 'r') as f: problem_dict = json.load(f) load_mechanism_dir(str(problem_dict['mechanism_dir']))