def test_can_get_stuck(self): f = (lambda x: x**3 - x) x, y, _ = hill_climbing(f, np.array([6]), np.array([-1]), goal_delta=0.01, seed=42) self.assertNotAlmostEqual(x[0], 2) self.assertNotAlmostEqual(y[0], 4) x, y, _ = hill_climbing(f, np.array([6]), np.array([4]), goal_delta=0.01, seed=42) np.testing.assert_array_almost_equal(x, np.array([2]), decimal=2) np.testing.assert_array_almost_equal(y, np.array([6]), decimal=2)
def test_linear(self): f = (lambda x: 2*x) x, y, _ = hill_climbing(f, np.array([3]), np.array([0]), goal_delta=0.01, seed=42) np.testing.assert_array_almost_equal(x, np.array([1.5]), decimal=2) np.testing.assert_array_almost_equal(y, np.array([3]), decimal=2) x, y, _ = hill_climbing(f, np.array([3]), np.array([4]), goal_delta=0.01, seed=42) np.testing.assert_array_almost_equal(x, np.array([1.5]), decimal=2) np.testing.assert_array_almost_equal(y, np.array([3]), decimal=2)
def test_hill_climbing_mountain_from_right(self): arr = range(0,10) + range(10,-1,-1) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds = (0,20)) pos, score = hill_climbing(eval_funct, move_funct, 0) self.assertEquals(score, 10) self.assertAlmostEqual(round(pos), 10)
def test_hill_climbing_left_peak_first(self): arr = range(0,10) + range(10,-1,-1) + range(1, 6) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds = (0,25)) pos, score = hill_climbing(eval_funct, move_funct, 20) self.assertTrue(score == 10 or score == 5) self.assertTrue(round(pos) == 10 or round(pos) == 25)
def learning(sample, method, parameters): if method == "cpc": binNumber, alpha = parameters learner = otagr.ContinuousPC(sample, binNumber, alpha) ndag = learner.learnDAG() TTest = otagr.ContinuousTTest(sample, alpha) jointDistributions = [] for i in range(ndag.getSize()): d = 1 + ndag.getParents(i).getSize() if d == 1: bernsteinCopula = ot.Uniform(0.0, 1.0) else: K = TTest.GetK(len(sample), d) indices = [int(n) for n in ndag.getParents(i)] indices = [i] + indices bernsteinCopula = ot.EmpiricalBernsteinCopula( sample.getMarginal(indices), K, False) jointDistributions.append(bernsteinCopula) bn = named_dag_to_bn(ndag) elif method == "elidan": #print(sample.getDescription()) max_parents, n_restart_hc = parameters copula, dag = hc.hill_climbing(sample, max_parents, n_restart_hc)[0:2] #bn = dag_to_bn(dag, Tstruct.names()) bn = dag_to_bn(dag, sample.getDescription()) else: print("Wrong entry for method argument !") return bn
def fit_hill_climbing(): """ Uses Hill Climbing (HC) to fit the ball parameters. :return theta: array containing the initial speed and the acceleration factor due to rolling friction. :rtype theta: numpy.array. :return history: history of points visited by the algorithm. :rtype history: list of numpy.array. """ # Hyperparameters used for computing the neighbors delta = 2.0e-3 num_neighbors = 8 def neighbors(theta): """ Returns 8-connected neighbors of point theta. The neighbors are sampled around a circle of radius "delta". Equally spaced (in terms of angle) "num_neighbors" neighbors are sampled. :param theta: current point. :type theta: numpy.array. :return: neighbors of theta. :rtype: list of numpy.array. """ # Creates neighbors_list neighbors_list = [ theta + np.array([cos(i * pi / 4), sin(i * pi / 4)]) * delta for i in range(num_neighbors) ] return neighbors_list theta, history = hill_climbing(cost_function, neighbors, np.array([0.0, 0.0]), 1.0e-10, 1000) return theta, history
def fit_gesture_padded(tutor, songmodel, gesture_index, conf): measure = conf['measure_obj'] comp = conf['comp_obj'] nb_iter = conf['iter_per_train'] nb_pad = conf.get('nb_pad', 1) temp = conf.get('temperature', None) rng = conf.get('rng', None) prev_igest = max(0, gesture_index - nb_pad) start_tutor = songmodel.gestures[prev_igest][0] next_igest = min(len(songmodel.gestures) - 1, gesture_index + nb_pad) end_tutor = songmodel.gesture_end(next_igest) goal = measure(tutor[start_tutor:end_tutor]) x, dummy_y, score = hill_climbing( function=lambda x: measure(_padded_gen_sound( songmodel, range(prev_igest, next_igest+1), gesture_index, x)), goal=goal, guess=deepcopy(songmodel.gestures[gesture_index][1]), guess_deviation=np.diag(conf['dev']), max_iter=nb_iter, comparison_method=comp, temp_max=temp, verbose=False, rng=rng) return x, score
def fit_func(goal, f, params, dev, mins, maxs): """ Fit params of so that f(i) is the closest to goal[i]. Find the params so that the function f fits the best the goal by minimizing square error, return the best params. goal - An array of the value that the function must fit. Assume f(i) = goal[i] f - The function to fit. must be of the signature f(i, params) with params being an ndarray. params - Either guesses for the params as an ndarray, or the number of params to fit. returns best_params, sq_error """ try: nb_params = len(params) guess = copy.copy(params) except TypeError: nb_params = params guess = np.ones(nb_params) params_out, y, score = hill_climbing( lambda param: f(np.arange(len(goal)), param), goal, guess, guess_deviation=dev, comparison_method=lambda g, c: fastdtw(g, c, dist=2)[0], max_iter=10000, guess_min=np.array(mins), guess_max=np.array(maxs), temp_max=2) return params_out, score
def test_hill_climbing_neg_slope(self): arr = range(9, -1, -1) eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=1, bounds = (0,9)) pos, score = hill_climbing(eval_funct, move_funct, 9) self.assertEquals(score, 9) self.assertAlmostEqual(round(pos), 0)
def test_hill_climbing_aapl_data(self): aapl = quandl_stocks('AAPL') close = aapl['WIKI/AAPL - Adj. Close'] arr = close.values eval_funct = lambda x: arr[int(round(abs(x)))] move_funct = lambda X, i: maximum_number_move(X, i, step_size=5, bounds = (0,len(arr))) pos, score = hill_climbing(eval_funct, move_funct, 6) plot_stock_data(aapl['WIKI/AAPL - Adj. Close'], close.index[pos], score)
def main(): print("BIA 2019/2020 - BED0111") # Blind search # get best point and show in 3d graph searched_points = blind_search.blind_search(FUNCTION, NUM_GENERETED_POINTS) show_3D_graph.show_graph_with_searched_point_3D(FUNCTION, searched_points) ############################### # (4/4) # hill climbing with recursion x = hill_climbing.hill_climbing(FUNCTION, 5, 5, 0.8, 50, 15) show_3D_graph.show_graph_with_searched_point_3D(FUNCTION, x) # simulated annealing with temperature x = simulated_annealing(FUNCTION, 5, 5, 0.8, 1, 200) show_3D_graph.show_graph_with_searched_point_3D(FUNCTION, x)
def fit_hill_climbing(): """ Uses Hill Climbing (HC) to fit the ball parameters. :return theta: array containing the initial speed and the acceleration factor due to rolling friction. :rtype theta: numpy.array. :return history: history of points visited by the algorithm. :rtype history: list of numpy.array. """ # Hyperparameters used for computing the neighbors delta = 2.0e-3 num_neighbors = 8 def neighbors(theta): """ Returns 8-connected neighbors of point theta. The neighbors are sampled around a circle of radius "delta". Equally spaced (in terms of angle) "num_neighbors" neighbors are sampled. :param theta: current point. :type theta: numpy.array. :return: neighbors of theta. :rtype: list of numpy.array. """ neighbors_list = [] # Todo: Implement i = 0 while i < num_neighbors: neighbors_list.append( np.array([ theta[0] + delta * cos(2 * pi * i / num_neighbors), theta[1] + delta * sin(2 * pi * i / num_neighbors) ])) i = i + 1 return neighbors_list theta, history = hill_climbing(cost_function, neighbors, np.array([0.0, 0.0]), 1.0e-10, 1000) return theta, history
def fit_gesture_whole(measured_tutor, songmodel, gesture_index, conf): measure = conf['measure_obj'] comp = conf['comp_obj'] nb_iter = conf['iter_per_train'] temp = conf.get('temperature', None) rng = conf.get('rng', None) goal = measured_tutor x, dummy_y, score = hill_climbing( function=lambda x: measure(_padded_gen_sound( songmodel, range(0, len(songmodel.gestures)), gesture_index, x)), goal=goal, guess=deepcopy(songmodel.gestures[gesture_index][1]), guess_deviation=np.diag(conf['dev']), max_iter=nb_iter, comparison_method=comp, temp_max=temp, verbose=False, rng=rng) return x, score
def run_test_hillclimbing(verbose): opt_value = [] # Lista para os valores encontrados opt_time = [] # Lista para os tempos encontrados i = 0 for problem in problems: # Executando o algoritmo state, size, value, time = hill_climbing(max_size=problem[0], values=problem[1], max_time=300) # Print caso queira acompanhar if verbose: print('Problem', problem_name[i], 'finished with (opt_value, opt_size, time) equals', (value, size, time)) # Salvando os melhores valores opt_value.append(value) opt_time.append(time) i = i + 1 mean_value = np.mean(opt_value) std_value = np.std(opt_value) mean_time = np.mean(opt_time) std_time = np.std(opt_time) # Print caso queira acompanhar if verbose: print('The mean and std for the values found were:', mean_value, '+-', std_value) print('The mean and std for the times found were:', mean_time, '+-', std_time) return opt_value, opt_time, [mean_value, std_value, mean_time, std_time]
def fit_gesture_hill(gesture, conf, prior): """Find the parameters to fit to a gesture.""" measure = conf['measure_obj'] comp = conf['comp_obj'] nb_iter = conf['iter_per_train'] temp = conf.get('temperature', None) rng = conf.get('rng', None) size = len(gesture) goal = measure(gesture) x, dummy_y, score = hill_climbing( function=lambda x: measure(gen_sound( x, size, falpha=lambda x, p: only_sin(x, p, nb_sin=3), fbeta=lambda x, p: only_sin(x, p, nb_sin=1), falpha_nb_args=13)), goal=goal, guess=np.array(prior), guess_deviation=np.diag(conf['dev']), max_iter=nb_iter, comparison_method=comp, temp_max=temp, verbose=False, rng=rng) return x, score
n_nodes.append(i) n_arc = int(density * (i - 1)) bn = generator.generate(i, n_arc) TNdag = otagr.NamedDAG(bn.dag(), bn.names()) data = ut.generate_gaussian_data(TNdag, sample_size, float(args.correlation)) learner = otagr.ContinuousPC(data, mcss, alpha) start = time.time() LNdagCPC = learner.learnDAG() end = time.time() times_cpc.append(end - start) start = time.time() LNdagElidan = hc.hill_climbing(data, max_parents, n_restart_hc)[1] end = time.time() times_elidan.append(end - start) #LNdagCPC = [[ut.named_dag_to_bn(LNdagCPC)]] #LNdagElidan = [[ut.dag_to_bn(LNdagElidan, data.getDescription())]] #cpc_scores = ut.structural_scores(ut.named_dag_to_bn(TNdag), LNdagCPC) #elidan_scores = ut.structural_scores(ut.named_dag_to_bn(TNdag), LNdagElidan) n_nodes = np.reshape(n_nodes, (len(n_nodes), 1)) times_cpc = np.reshape(times_cpc, (len(times_cpc), 1)) times_elidan = np.reshape(times_elidan, (len(times_elidan), 1)) results = np.concatenate((n_nodes, times_cpc, times_elidan), axis=1) header = "N_nodes, Times_cpc, Times_elidan"
Loglikelihoods = [] Structures = [] sizes = np.linspace(100, 500, n_samples, dtype=int) for size in sizes: print(size) sample = data[np.random.choice(np.arange(0, len(data)), size=size, replace=False)] kf = KFold(n_splits=k, shuffle=True) list_loglikelihoods = [] list_structures = [] for train_index, test_index in kf.split(sample): train, test = sample[train_index], sample[test_index] c, g, s = hc.hill_climbing(ot.Sample(train), max_parents, n_restart) list_loglikelihoods.append( sc.log_likelihood(ot.Sample(test), c, g) / test.shape[0]) list_structures.append(g) Loglikelihoods.append(list_loglikelihoods) Structures.append(list_structures) Loglikelihoods = np.array(Loglikelihoods, dtype=float) ll_mean = np.mean(Loglikelihoods, axis=1, keepdims=True) ll_std = Loglikelihoods.std(axis=1, keepdims=True) ll_mean = ll_mean.reshape(n_samples, 1) ll_std = ll_std.reshape(n_samples, 1) sizes = sizes.reshape(n_samples, 1) results = np.concatenate((sizes, ll_mean, ll_std), axis=1)
# first representation kk_result = kk(this_loop_list[:]) kk_results.append(kk_result[0]) kk_times.append(kk_result[1]) kk_df['time'][loop] = kk_result[1] kk_df['result'][loop] = kk_result[0] print "kk_result " + str(kk_result[0]) rr_result = repeated_random(this_loop_list[:], iterations) rr_results.append(rr_result[0]) rr_times.append(rr_result[1]) rr_df['time'][loop] = rr_result[1] rr_df['result'][loop] = rr_result[0] print "rr_result " + str(rr_result[0]) hc_result = hill_climbing(this_loop_list[:], iterations) hc_results.append(hc_result[0]) hc_times.append(hc_result[1]) hc_df['time'][loop] = hc_result[1] hc_df['result'][loop] = hc_result[0] print "hc_result " + str(hc_result[0]) sa_result = simulated_annealing(this_loop_list[:], iterations) sa_results.append(sa_result[0]) sa_times.append(sa_result[1]) sa_df['time'][loop] = sa_result[1] sa_df['result'][loop] = sa_result[0] print "sa_result " + str(sa_result[0]) # second representation rr_resultPP = repeated_randomPP(this_loop_list[:], iterations)
from hill_climbing import hill_climbing, h_n import sys import timeit from puzzle_utils import file_input, print_hill_climbing import math if __name__ == '__main__': start, goal = file_input(sys.path[0], sys.argv) choice = int( input(''' 1. Displced tiles Heuristic. 2. Manhattan distance Heuristic. 3. Displaced tile heuristic with blank tile cost included 4. Manhattan distance heuristic with blank tile cost included 5. Manhattan distance + displaced tile heuristic Enter choice: ''')) if choice > 5 or choice < 1: print("Invalid choice bc.") else: start_time = timeit.default_timer() puzzle_start = Puzzle(start, 0, h_n(start, goal, choice)) closed_list, parent_list, optimal_path_cost, string_to_matrix_mapping, monotonic_satisfied = hill_climbing( puzzle_start, goal, choice) stop_time = timeit.default_timer() print_hill_climbing(start, goal, parent_list, optimal_path_cost, string_to_matrix_mapping, str(len(closed_list))) print(f'Time taken: {stop_time -start_time}') print("Is monotonic restriction followed: %s" % (str(monotonic_satisfied)))
def test3(problem=None): if not problem: import test_problems problem = test_problems.alfiles() for step in hill_climbing(problem, steps=100000, restarts=4): print(step)
# Fix schedule such as the time for sleeping schedule.put_act("Sleep", "00:00", "08:00") schedule.put_act("Lunch", "12:00", "13:00") schedule.put_act("Nap", "15:00", "16:00") schedule.put_act("Dinner", "18:00", "20:00") # Take in tasks tasks = set() tasks.add( Task("Essay", priority=3, end_date="2018-01-14", estimated_time=8 * 60, category="Assignment")) tasks.add( Task("Programming", priority=3, end_date="2018-01-19", estimated_time=4 * 60, category="Assignment")) tasks.add( Task("Writing", priority=3, end_date="2018-01-30", estimated_time=8 * 60, category="Assignment")) # Generate the Schedule and check with Machine Learning algorithm hill_climbing(schedule, tasks) schedule = anneal(schedule, list(tasks), 0.1, 0.1, 0.9) print(schedule)
import numpy as np import openturns as ot import hill_climbing as hc from scipy.stats import random_correlation ot.RandomGenerator.SetSeed(42) np.random.seed(42) M = 10000 # Size of the dataset N = 4 # Dimension of the random vector # Correlation matrix definition R = ot.CorrelationMatrix(N) R[0,1] = 0.3 R[2,3] = -0.3 # Sampling from standard normal D = ot.Normal([0] * N, [1] * N, R).getSample(M) # Switch to rank space D_r = (D.rank()+1)/(D.getSize()+2) C, G, S = hc.hill_climbing(D_r,2) print("C", C) print("G: ", G) print("S: ", S)
Tstruct_file = "struct_1.txt" Tstruct_file_name = Tstruct_file.split('.')[0] data = np.loadtxt(data_directory + data_file, delimiter=',', skiprows=1) Tstruct = load_struct(path.join(struct_directory, Tstruct_file)) # Computing scores sizes = np.linspace(start_size, end_size, n_samples, dtype=int) list_structures = [] for size in sizes: print(size) list_restart = [] for i in range(n_restart): sample = data[np.random.randint(0, len(data), size=size)] sample = ot.Sample(sample) c, g, s = hc.hill_climbing(sample, max_parents, n_restart_hc) list_restart.append(g) list_structures.append(list_restart) precision = [] recall = [] fscore = [] for l in list_structures: list_fscore = [] list_recall = [] list_precision = [] for s in l: bn = dag_to_bn(s, Tstruct.names()) comparison = GraphicalBNComparator(Tstruct, bn) scores = comparison.scores() list_precision.append(scores['precision'])
ar[0] = int_round(non_neg(ar[0])) # number_of_molecules --> round + non-negative ar[1] = int_round(non_neg(ar[1])) # monomer_pool --> round + -1? ar[2] = int_round(monom(ar[2])) # p_growth --> between 0 and 1 + non-negative ar[3] = prob(non_neg(ar[3])) # p_death --> between 0 and 1 + non-negative ar[4] = prob(non_neg(ar[4])) # p_dead_react --> between 0 and 1 + non-negative ar[5] = prob(non_neg(ar[5])) # l_exponent --> non-negative ar[6] = non_neg(ar[6]) # d_exponent --> non-negative ar[7] = non_neg(ar[7]) # l_naked --> lol ar[8] = ar[8] # kill_spawns_new --> boolean ar[9] = 0 return ar def process_arguments(arguments): arguments = clip(arguments) # print(arguments) return arguments if __name__ == '__main__': hill_climbing(diff.get_difference, process_arguments)
def test_mult_dim(self): f = (lambda x: np.array((2 * (x[0] * x[1]), 3 * (-x[1])))) x, y, _ = hill_climbing(f, np.array([1, 2]), np.array([-1, 4]), goal_delta=0.01, seed=42) np.testing.assert_array_almost_equal(x, np.array([-3/4, -2/3]), decimal=2) np.testing.assert_array_almost_equal(y, np.array([1, 2]), decimal=2)
debug = False for x in equipes: print("nombre équipes ", x) list_mean = [] for i in range(num_pass): start_time = time.time() if method == 't': has_finished, _ = tabu.tabu(x, max_iteration, show_graph, debug) elif method == 'r': has_finished, _ = recuit.recuit_simule(x, max_iteration, show_graph, debug) elif method == 'h': has_finished, _ = hill_climbing.hill_climbing( x, max_iteration, show_graph, debug) elif method == 'rw': has_finished, _ = random_walk.ranomd_walk(x, max_iteration, show_graph, debug) if (has_finished): elapsed_time = time.time() - start_time list_mean.append(elapsed_time) print(list_mean) if len(list_mean) > 0: all_result[x] = np.mean(list_mean) print(all_result) print("END") print(all_result)
def test1(problem=None): if not problem: import test_problems problem = test_problems.eggholder() for step in hill_climbing(problem, steps=100000): print(step)