Esempio n. 1
0
    def experiment_rhc_1(self):
        print('restart vary')
        init_state = None
        restart_lengths = np.arange(10, 800, 100)
        result = np.zeros((len(self.rand_seeds), len(restart_lengths)))
        #best_state = np.zeros((len(self.rand_seeds), len(restart_lengths)))
        print(self.problem_type)
        for i in range(len(self.rand_seeds)):
            rand_state = self.rand_seeds[i]
            prob_length = 20
            for j in range(len(restart_lengths)):
                restart_length = restart_lengths[j]
                max_iter = np.inf
                #max_attempts is varied by trial and error
                max_attempts = 100
                fl = CustomProblem(prob_length, self.problem_type)
                problem = fl.create_problem()
                alg = RHC(problem, init_state, rand_state, max_attempts,
                          max_iter, restart_length.item())
                best_state, best_fitness = alg.optimize()
                result[i][j] = best_fitness

        print('best fitness')
        print(str(result))
        print('best state')
        print(best_state)
        avg_result = np.mean(result, axis=0)
        print('avg result for varying input size' + str(avg_result))
        title = self.problem_type + ' with RHC - # of Restarts Variation'
        plot_curve(restart_lengths, avg_result, title, '# of Restarts',
                   'Best Score')
 def experiment(self):
     se = SixPeaks(length = 20)
     problem  = se.create_problem()
     rhc_a = RHC(problem)
     best_state ,best_fitness = rhc_a.optimize()
     print(best_fitness)
     print(best_state)
Esempio n. 3
0
    def experiment_rhc_4(self):
        init_state = None
        t_pcts = np.arange(0.1, 1, 0.1)
        result = np.zeros((len(self.rand_seeds), len(t_pcts)))
        best_score = None
        max_iter = np.inf
        for i in range(len(self.rand_seeds)):
            restarts = 400
            rand_state = self.rand_seeds[i]
            for j in range(len(t_pcts)):
                prob_length = 20
                restarts = 400
                max_attempt = 50
                t_pct = t_pcts[j].item()
                fl = SixPeaks(prob_length, t_pct)
                problem = fl.create_problem()

                alg = RHC(problem, init_state, rand_state, max_attempt,
                          max_iter, restarts)
                best_score, best_fitness = alg.optimize()
                result[i][j] = best_fitness

        avg_result = np.mean(result, axis=0)
        print('avg result ' + str(avg_result))
        print('best score')
        print(best_score)
        title = self.problem_type + ' with RHC - Threshold Variation'
        plot_curve(t_pcts, avg_result, title, 'Threshold', 'Best Score')
Esempio n. 4
0
    def experiment_rhc_3(self):
        init_state = None
        max_iters = np.arange(100, 5000, 100)
        result = np.zeros((len(self.rand_seeds), len(max_iters)))

        for i in range(len(self.rand_seeds)):
            rand_state = self.rand_seeds[i]
            for j in range(len(max_iters)):
                prob_length = 20
                fl = CustomProblem(prob_length, self.problem_type)
                problem = fl.create_problem()
                max_attempt = 200
                restarts = 150
                max_iter = max_iters[j].item()
                alg = RHC(problem, init_state, rand_state, max_attempt,
                          max_iter, restarts)
                best_score, best_fitness = alg.optimize()
                result[i][j] = best_fitness

        avg_result = np.mean(result, axis=0)
        print('avg result ' + str(avg_result))
        print('best score')
        print(best_score)
        title = self.problem_type + ' with RHC - Max Iterations Variation'
        plot_curve(max_iters, avg_result, title, 'Max Iterations',
                   'Best Score')
Esempio n. 5
0
    def experiment_rhc_22(self):
        init_state = None
        max_attempts = np.array(
            [5, 10, 15, 30, 40, 50, 60, 80, 100, 200, 300, 350])
        result = np.zeros((len(self.rand_seeds), len(max_attempts)))
        best_score = None
        for i in range(len(self.rand_seeds)):
            restarts = 0
            rand_state = self.rand_seeds[i]
            for j in range(len(max_attempts)):
                prob_length = 20
                fl = CustomProblem(prob_length, self.problem_type)
                problem = fl.create_problem()
                max_attempt = max_attempts[j].item()
                max_iter = np.inf
                alg = RHC(problem, init_state, rand_state, max_attempt,
                          max_iter, restarts)
                best_score, best_fitness = alg.optimize()
                result[i][j] = best_fitness

        avg_result = np.mean(result, axis=0)
        print('avg result ' + str(avg_result))
        print('best score')
        print(best_score)
        title = self.problem_type + ' with RHC - Max Attempts Variation - 0 restart'
        plot_curve(max_attempts, avg_result, title, 'Max Attempts',
                   'Best Score')
Esempio n. 6
0
 def experiment_optimal_rhc(self):
     prob_length = 20
     max_attempt = 100
     restarts = 100
     max_iter = 1000
     rand_state = 42
     init_state = None
     fl = CustomProblem(prob_length, self.problem_type)
     problem = fl.create_problem()
     start = time.time()
     alg = RHC(problem, init_state, rand_state, max_attempt, max_iter,
               restarts)
     best_score, best_fitness = alg.optimize()
     end = time.time()
     diff = abs(end - start)
     print('time taken for RHC- Knapsack: ' + str(diff))
Esempio n. 7
0
    def experiment_rhc_11(self):
        init_state = None
        prob_lengths = np.arange(7, 30)
        result = np.zeros((len(self.rand_seeds), len(prob_lengths)))
        print(self.problem_type)
        for i in range(len(self.rand_seeds)):
            rand_state = self.rand_seeds[i]
            for j in range(len(prob_lengths)):
                prob_length = prob_lengths[j]
                fl = CustomProblem(prob_length.item(), self.problem_type)
                problem = fl.create_problem()
                alg = RHC(problem, init_state, rand_state, 10, 1000)
                best_fitness = alg.optimize()
                result[i][j] = best_fitness

        print(str(result))
        avg_result = np.mean(result, axis=0)
        print('avg result for varying input size' + str(avg_result))
        title = self.problem_type + ' with RHC - Input Size Variation'
        plot_curve(prob_lengths, avg_result, title, 'Input Size', 'Best Score')