def es(f_vals, pnts, dims, nr_of_funcs, nr_of_nodes, error_func, op_table, max_iter=500, nr_of_pars=0, reheat_iter=100, remaining_time=None, nr_of_children=4): """ Evolutionary Strategy. """ start_time = time() assert nr_of_funcs == len(op_table) # Create a starting function (solution) at random. current_sol = create_random_gene(dims+nr_of_pars, nr_of_funcs, nr_of_nodes) current_cgp = CGP(dims, op_table, current_sol, nr_of_parameters=nr_of_pars) (current_error, best_pars) = error_func(f_vals, pnts, dims, current_cgp, nr_of_pars, op_table) print(current_error) best_cgp = deepcopy(current_cgp) best_error = current_error for itr in range(max_iter): if itr % 50 == 0: print("iter:", itr," of", max_iter) # Do a small mutation to create a few new function (aka solution) children = [mutate(current_cgp, dims+nr_of_pars, nr_of_funcs) for _ in range(nr_of_children)] children_results = [error_func(f_vals, pnts, dims, child, nr_of_pars, op_table) for child in children] children_errors = [children_results[i][0] for i in range(nr_of_children)] children_pars = [children_results[i][1] for i in range(nr_of_children)] best_children_idx = children_errors.index(min(children_errors)) current_cgp = children[best_children_idx] current_error = children_errors[best_children_idx] if current_error < best_error: new_pars = children_pars[best_children_idx] print("best yet:", current_error) write_2_file(new_pars, itr, current_error, 'es',current_cgp.convert2str(parameters=new_pars)) current_cgp.print_function(parameters=new_pars) best_cgp = deepcopy(current_cgp) best_error = current_error best_pars = list(new_pars) if remaining_time!=None and time()-start_time >= remaining_time: break return (best_cgp, best_error, best_pars)
def sa(f_vals, pnts, dims, nr_of_funcs, nr_of_nodes, error_func, op_table, max_iter=500, nr_of_pars=0, reheat_iter=100, remaining_time=None): """ Simulated anneling is a simple way of doing compinatorial optimization without getting stuck in local minima. It basically works like this: 1) take the current solution and apply a small change to it 2) If this new solution is better, keep it. 3) There is a chance that the new solution is kept even if it is worse. This chance decreases as the iteration number grows, and the chance is small if the new solution is much worse than the old solution. 4) Repeate the process a bit, and return the best solution. The wiki page is rather good as well. """ start_time = time() assert nr_of_funcs == len(op_table) # Create a starting function (solution) at random. current_sol = create_random_gene(dims+nr_of_pars, nr_of_funcs, nr_of_nodes) current_cgp = CGP(dims, op_table, current_sol, nr_of_parameters=nr_of_pars) (current_error, best_pars) = error_func(f_vals, pnts, dims, current_cgp, nr_of_pars, op_table) print(current_error) #assert False best_cgp = deepcopy(current_cgp) best_error = current_error iterations_since_update = 0 temperature_itr = 0 for itr in range(max_iter): if itr % 50 == 0: print("iter:", itr," of", max_iter) temp = float(max_iter-temperature_itr)/max_iter # Do a small mutation to create a new function (aka solution) new_cgp = mutate(current_cgp, dims+nr_of_pars, nr_of_funcs) #cgp = CGP(dims, op_table, new_sol, nr_of_parameters=nr_of_pars) (new_error, new_pars) = error_func(f_vals, pnts, dims, new_cgp, nr_of_pars, op_table) temperature_itr += 1 if new_error < current_error or acceptance_prob(new_error, current_error, temp)<random(): #current_sol = new_sol current_cgp = new_cgp current_error = new_error if new_error < best_error: print("best yet:", new_error) write_2_file(new_pars, itr, current_error, 'sa',current_cgp.convert2str(parameters=new_pars)) new_cgp.print_function(parameters=new_pars) best_cgp = deepcopy(new_cgp) best_error = new_error best_pars = list(new_pars) else: iterations_since_update += 1 # If no change has been made in a while, then we set the temp to max again! if iterations_since_update == reheat_iter: temperature_itr = 0 iterations_since_update = 0 print("Reheating.") if remaining_time!=None and time()-start_time >= remaining_time: break return (best_cgp, best_error, best_pars)