def aposteriori_FON(n_tests=20, n_dims=2):
    params = dict(
        init_sol=Solution(np.array([3.5] * n_dims)),
        problem=MOO_Problem.FON,
        constraints=[],  # no constraints for this problem
        step_size=0.05,
        neighborhood_size=20,
        max_iter=4000,
        M=100,
        tabu_list_max_length=60,
        max_loops=300,
        search_space_dimensions=n_dims,
        objective_space_dimensions=2,
        save=True,
        weights=[1. / n_dims] * 2)
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/aposteriori/FON/',
        filename='')
    for i in range(n_tests):
        save_options['filename'] = 'FON_test_' + str(i + 1) + '.pickle'
        params['save_options'] = save_options
        params['seed_value'] = i
        params['test_ID'] = 'FON_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        a = random.random()
        params['weights'] = [a, 1 - a]
        params['init_sol'] = Solution(
            np.random.uniform(low=-4, high=4, size=n_dims))
        SearchInstance = AposterioriWeightingMethod(**params)
        SearchInstance.search()
def aposteriori_BK1(n_tests=20):
    params = dict(init_sol=Solution(np.array([9, 9])),
                  problem=MOO_Problem.BK1,
                  constraints=[MOO_Constraints.BK1_constraint],
                  step_size=0.05,
                  neighborhood_size=15,
                  max_iter=2000,
                  M=100,
                  tabu_list_max_length=20,
                  max_loops=100,
                  search_space_dimensions=2,
                  objective_space_dimensions=2,
                  save=True,
                  weights=[0.5, 0.5])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/aposteriori/BK1/',
        filename='')
    for i in range(n_tests):
        save_options['filename'] = 'BK1_test_' + str(i + 1) + '.pickle'
        params['save_options'] = save_options
        params['seed_value'] = i
        params['test_ID'] = 'BK1_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        a = random.random()
        params['weights'] = [a, 1 - a]
        params['init_sol'] = Solution(
            np.array([random.uniform(-5, 10),
                      random.uniform(-5, 10)]))
        SearchInstance = AposterioriWeightingMethod(**params)
        SearchInstance.search()
def aposteriori_OSY(n_tests=20):
    def generate_feasible_sol():
        while True:  # 5k sample points
            x1 = random.uniform(0, 10)
            x2 = random.uniform(0, 10)
            x3 = random.uniform(1, 5)
            x4 = random.uniform(0, 6)
            x5 = random.uniform(1, 5)
            x6 = random.uniform(0, 10)
            if x1 + x2 - 2 < 0:
                continue
            if 6 - x1 - x2 < 0:
                continue
            if 2 - x2 + x1 < 0:
                continue
            if 2 - x1 + 3 * x2 < 0:
                continue
            if 4 - (x3 - 3)**2 - x4 < 0:
                continue
            if (x5 - 3)**2 + x6 - 4 < 0:
                continue
            return np.array([x1, x2, x3, x4, x5, x6])

    params = dict(
        init_sol=Solution(np.array(
            [9, 9, 4, 5, 4, 9])),  # unknown whether this solution is feasible.
        problem=MOO_Problem.OSY,
        constraints=[
            MOO_Constraints.OSY_constraint_1, MOO_Constraints.OSY_constraint_2,
            MOO_Constraints.OSY_constraint_3, MOO_Constraints.OSY_constraint_4,
            MOO_Constraints.OSY_constraint_5, MOO_Constraints.OSY_constraint_6,
            MOO_Constraints.OSY_constraint_7
        ],
        step_size=0.1,
        neighborhood_size=15,
        max_iter=2000,
        M=3000,
        tabu_list_max_length=20,
        max_loops=100,
        search_space_dimensions=6,
        objective_space_dimensions=2,
        save=True,
        weights=[0.5, 0.5])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/aposteriori/OSY/',
        filename='')
    for i in range(n_tests):
        save_options['filename'] = 'OSY_test_' + str(i + 1) + '.pickle'
        params['save_options'] = save_options
        params['seed_value'] = i
        params['test_ID'] = 'OSY_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        a = random.random()
        params['weights'] = [a, 1 - a]
        params['init_sol'] = Solution(generate_feasible_sol())
        SearchInstance = AposterioriWeightingMethod(**params)
        SearchInstance.search()
Пример #4
0
 def test_solution_comparisons(self):
     # this method tests whether a solution is in a list (tabu list)
     x1, x2, x3 = np.array([1, 2,
                            3]), np.array([1.0, 2.0, 3.0
                                           ]), np.array([1.00001, 2.0, 3.0])
     tabu_list = [Solution(x1), Solution(x2), Solution(x3)]
     sol = Solution(np.array([1.000000001, 2.00000001, 3.0]))
     self.assertTrue(sol in tabu_list)
def aposteriori_TNK(n_tests=20):
    def generate_feasible_sol():
        while True:
            x1 = random.uniform(0, math.pi)
            x2 = random.uniform(0, math.pi)
            if math.isclose(x1, 0):
                c1 = math.pow(x2, 2) - 1.1
            else:
                c1 = math.pow(x1, 2) + math.pow(
                    x2, 2) - 1 - 0.1 * math.cos(16 * math.atan(x2 / x1))
            c2 = math.pow(x1 - 0.5, 2) + math.pow(x2 - 0.5, 2) - 0.5
            if c1 >= 0 >= c2:
                return np.array([x1, x2])

    params = dict(
        init_sol=Solution(np.array(
            [2, 2])),  # unknown whether this solution is feasible.
        problem=MOO_Problem.TNK,
        constraints=[
            MOO_Constraints.TNK_constraint_1, MOO_Constraints.TNK_constraint_2,
            MOO_Constraints.TNK_constraint_3
        ],
        step_size=0.05,
        neighborhood_size=15,
        max_iter=2500,
        M=100,
        tabu_list_max_length=20,
        max_loops=100,
        search_space_dimensions=2,
        objective_space_dimensions=2,
        save=True,
        weights=[0.5, 0.5])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/aposteriori/TNK/',
        filename='')
    for i in range(n_tests):
        save_options['filename'] = 'TNK_test_' + str(i + 1) + '.pickle'
        params['save_options'] = save_options
        params['seed_value'] = i
        params['test_ID'] = 'TNK_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        a = random.random()
        params['weights'] = [a, 1 - a]
        params['init_sol'] = Solution(generate_feasible_sol())
        SearchInstance = AposterioriWeightingMethod(**params)
        SearchInstance.search()
def progressive_SCH1(n_tests=20, n_repetitions=10):
    search_params = dict(
        init_sol=Solution(np.array([10])),
        problem=MOO_Problem.SCH1,
        constraints=[],  # no constraints for this problem
        step_size=0.02,
        neighborhood_size=15,
        max_iter=100,
        M=5000,  # must be a greater value for progressive art.
        tabu_list_max_length=20,
        max_loops=50,  # must be a lesser value for progressive art.
        search_space_dimensions=1,
        objective_space_dimensions=2,
        save=False,  # important!
        dynamic_constraints=None,
        aspirations=[0, 0])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/progressive/SCH1/',
        filename='')
    IDM_params = dict(problem_name='IM1',
                      pen_val=500,
                      n_repetitions=n_repetitions,
                      save=True,
                      save_options=None,
                      which_csv_index='0')
    fifth = n_tests // 5
    which_csv = np.concatenate(
        (np.array([0] * fifth), np.array([1] * fifth), np.array([2] * fifth),
         np.array([3] * fifth), np.array([4] * fifth)))
    for i in range(n_tests):
        save_options['filename'] = 'SCH1_test_' + str(i + 1) + '.pickle'
        search_params['save_options'] = save_options
        IDM_params['save_options'] = save_options
        IDM_params['which_csv_index'] = str(which_csv[i])
        search_params['seed_value'] = i
        search_params['test_ID'] = 'SCH1_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        # Since the search space is 1D, pick initial point as either -10 or 10.
        if i % 2 is 0:
            search_params['init_sol'] = Solution(
                np.array([-5 + random.uniform(-1, 1)]))
        else:
            search_params['init_sol'] = Solution(
                np.array([5 + random.uniform(-1, 1)]))
        AgentInstance = IntelligentDM(IDM_params, search_params)
        AgentInstance.procedure()
Пример #7
0
class TestSolution(unittest.TestCase):
    def setUp(self):
        Solution.eps = 0.000001
        self.sol1 = Solution()
        self.sol2 = Solution()

    def tearDown(self):
        self.sol1 = None
        self.sol2 = None

    def test_success_assignation_x(self):
        arr_1 = np.array([1, 2, 3])
        self.sol1.set_x(arr_1)
        self.assertTrue(
            np.all(
                np.isclose(arr_1,
                           self.sol1.x,
                           rtol=Solution.eps,
                           atol=Solution.eps)))

    @unittest.expectedFailure
    def test_fail_assignation_x(self):
        k = [1, 2, 3]
        self.sol1.set_x(k)

    def test_success_assignation_y(self):
        arr_1 = np.array([1, 2, 3])
        self.sol1.set_y(arr_1)
        self.assertTrue(
            np.all(
                np.isclose(arr_1,
                           self.sol1.y,
                           rtol=Solution.eps,
                           atol=Solution.eps)))

    @unittest.expectedFailure
    def test_fail_assignation_y(self):
        k = [1, 2, 3]
        self.sol1.set_y(k)

    def test_success_equality(self):
        self.sol1 = Solution(np.array([1, 2, 3, 4, 5]))
        self.sol2 = Solution(
            np.array([1.0000001, 2.00000001, 3.0000001, 4.0000001, 5.000001]))
        self.assertTrue(self.sol1 == self.sol2)

    @unittest.expectedFailure
    def test_fail_equality(self):
        self.sol1 = Solution(np.array([1, 2, 3, 4, 5]))
        self.sol2 = Solution(
            np.array([1.0000001, 2.00002, 3.0000001, 4.0000001, 5.000001]))
        self.assertTrue(self.sol1 == self.sol2)

    def test_success_nequality(self):
        self.sol1 = Solution(np.array([1, 2, 3, 4, 5]))
        self.sol2 = Solution(
            np.array([1.0000001, 2.00002, 3.0000001, 4.0000001, 5.000001]))
        self.assertTrue(self.sol1 != self.sol2)
def progressive_FON(n_tests=20, n_repetitions=10, n_dims=5):
    search_params = dict(
        init_sol=Solution(np.array([3.5] * n_dims)),
        problem=MOO_Problem.FON,
        constraints=[MOO_Constraints.FON_constraint
                     ],  # no constraints for this problem
        step_size=0.02,
        neighborhood_size=40,
        max_iter=200,
        M=5000,  # must be a greater value for progressive art.
        tabu_list_max_length=60,
        max_loops=70,  # must be a lesser value for progressive art.
        search_space_dimensions=n_dims,
        objective_space_dimensions=2,
        save=False,  # important!
        dynamic_constraints=None,
        aspirations=[0, 0])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/progressive/FON/',
        filename='')
    IDM_params = dict(problem_name='FON',
                      pen_val=500,
                      n_repetitions=n_repetitions,
                      save=True,
                      save_options=None,
                      which_csv_index='0')
    fifth = n_tests // 5
    which_csv = np.concatenate(
        (np.array([0] * fifth), np.array([1] * fifth), np.array([2] * fifth),
         np.array([3] * fifth), np.array([4] * fifth)))
    for i in range(n_tests):
        save_options['filename'] = 'FON_test_' + str(i + 1) + '.pickle'
        search_params['save_options'] = save_options
        IDM_params['save_options'] = save_options
        IDM_params['which_csv_index'] = str(which_csv[i])
        search_params['seed_value'] = i
        search_params['test_ID'] = 'FON_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        search_params['init_sol'] = Solution(
            np.random.uniform(low=-4, high=4, size=n_dims))

        AgentInstance = IntelligentDM(IDM_params, search_params)
        AgentInstance.procedure()
def aposteriori_SCH1(n_tests=20):
    params = dict(
        init_sol=Solution(np.array([10])),
        problem=MOO_Problem.SCH1,
        constraints=[],  # no constraints for this problem
        step_size=0.02,
        neighborhood_size=15,
        max_iter=2000,
        M=100,
        tabu_list_max_length=20,
        max_loops=100,
        search_space_dimensions=1,
        objective_space_dimensions=2,
        save=True,
        weights=[0.5, 0.5])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/aposteriori/SCH1/',
        filename='')
    for i in range(n_tests):
        save_options['filename'] = 'SCH1_test_' + str(i + 1) + '.pickle'
        params['save_options'] = save_options
        params['seed_value'] = i
        params['test_ID'] = 'SCH1_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        a = random.random()
        params['weights'] = [a, 1 - a]
        # Since the search space is 1D, pick initial point as either -10 or 10.
        if i % 2 is 0:
            params['init_sol'] = Solution(
                np.array([-5 + random.uniform(-1, 1)]))
        else:
            params['init_sol'] = Solution(np.array([5 + random.uniform(-1, 1)
                                                    ]))
        SearchInstance = AposterioriWeightingMethod(**params)
        SearchInstance.search()
def progressive_OSY(n_tests=20, n_repetitions=10):

    # Copied from goal programming
    def generate_feasible_sol():
        while True:  # 5k sample points
            x1 = random.uniform(0, 10)
            x2 = random.uniform(0, 10)
            x3 = random.uniform(1, 5)
            x4 = random.uniform(0, 6)
            x5 = random.uniform(1, 5)
            x6 = random.uniform(0, 10)
            if x1 + x2 - 2 < 0:
                continue
            if 6 - x1 - x2 < 0:
                continue
            if 2 - x2 + x1 < 0:
                continue
            if 2 - x1 + 3 * x2 < 0:
                continue
            if 4 - (x3 - 3)**2 - x4 < 0:
                continue
            if (x5 - 3)**2 + x6 - 4 < 0:
                continue
            return np.array([x1, x2, x3, x4, x5, x6])

    search_params = dict(
        init_sol=Solution(np.array(
            [9, 9, 4, 5, 4, 9])),  # unknown whether this solution is feasible
        problem=MOO_Problem.OSY,
        constraints=[
            MOO_Constraints.OSY_constraint_1, MOO_Constraints.OSY_constraint_2,
            MOO_Constraints.OSY_constraint_3, MOO_Constraints.OSY_constraint_4,
            MOO_Constraints.OSY_constraint_5, MOO_Constraints.OSY_constraint_6,
            MOO_Constraints.OSY_constraint_7
        ],
        step_size=0.1,
        neighborhood_size=15,
        max_iter=300,
        M=5000,  # must be a greater value for progressive art.
        tabu_list_max_length=20,
        max_loops=80,  # must be a lesser value for progressive art.
        search_space_dimensions=6,
        objective_space_dimensions=2,
        save=False,  # important!
        dynamic_constraints=None,
        aspirations=[0, 0])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/progressive/OSY/',
        filename='')
    IDM_params = dict(
        problem_name='OSY',
        pen_val=1000,  # must be greater for OSY
        n_repetitions=n_repetitions,
        save=True,
        save_options=None,
        which_csv_index='0')
    fifth = n_tests // 5
    which_csv = np.concatenate(
        (np.array([0] * fifth), np.array([1] * fifth), np.array([2] * fifth),
         np.array([3] * fifth), np.array([4] * fifth)))
    for i in range(n_tests):
        save_options['filename'] = 'OSY_test_' + str(i + 1) + '.pickle'
        search_params['save_options'] = save_options
        IDM_params['save_options'] = save_options
        IDM_params['which_csv_index'] = str(which_csv[i])
        search_params['seed_value'] = i
        search_params['test_ID'] = 'OSY_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        search_params['init_sol'] = Solution(generate_feasible_sol())
        AgentInstance = IntelligentDM(IDM_params, search_params)
        AgentInstance.procedure()
def progressive_TNK(n_tests=20, n_repetitions=10):

    # Copied from goal programming
    def generate_feasible_sol():
        while True:
            x1 = random.uniform(0, math.pi)
            x2 = random.uniform(0, math.pi)
            if math.isclose(x1, 0):
                c1 = math.pow(x2, 2) - 1.1
            else:
                c1 = math.pow(x1, 2) + math.pow(
                    x2, 2) - 1 - 0.1 * math.cos(16 * math.atan(x2 / x1))
            c2 = math.pow(x1 - 0.5, 2) + math.pow(x2 - 0.5, 2) - 0.5
            if c1 >= 0 >= c2:
                return np.array([x1, x2])

    search_params = dict(
        init_sol=Solution(np.array(
            [2, 2])),  # unknown whether this solution is feasible
        problem=MOO_Problem.TNK,
        constraints=[
            MOO_Constraints.TNK_constraint_1, MOO_Constraints.TNK_constraint_2,
            MOO_Constraints.TNK_constraint_3
        ],  # no constraints for this problem
        step_size=0.05,
        neighborhood_size=15,
        max_iter=150,
        M=5000,  # must be a greater value for progressive art.
        tabu_list_max_length=20,
        max_loops=50,  # must be a lesser value for progressive art.
        search_space_dimensions=2,
        objective_space_dimensions=2,
        save=False,  # important!
        dynamic_constraints=None,
        aspirations=[0, 0])
    save_options = dict(
        path=
        '/home/kemal/Programming/Python/Articulation/data/pickles/progressive/TNK/',
        filename='')
    IDM_params = dict(problem_name='TNK',
                      pen_val=500,
                      n_repetitions=n_repetitions,
                      save=True,
                      save_options=None,
                      which_csv_index='0')
    fifth = n_tests // 5
    which_csv = np.concatenate(
        (np.array([0] * fifth), np.array([1] * fifth), np.array([2] * fifth),
         np.array([3] * fifth), np.array([4] * fifth)))
    for i in range(n_tests):
        save_options['filename'] = 'TNK_test_' + str(i + 1) + '.pickle'
        search_params['save_options'] = save_options
        IDM_params['save_options'] = save_options
        IDM_params['which_csv_index'] = str(which_csv[i])
        search_params['seed_value'] = i
        search_params['test_ID'] = 'TNK_test_' + str(i + 1)
        random.seed(i)
        np.random.seed(i)
        search_params['init_sol'] = Solution(generate_feasible_sol())
        AgentInstance = IntelligentDM(IDM_params, search_params)
        AgentInstance.procedure()
Пример #12
0
    def search(self):
        random.seed(self.seed_value)
        np.random.seed(self.seed_value)
        start = time.process_time()
        # Evaluate initial solution.ž
        self.evaluate_objectives(self.init_sol)
        self.evaluate_solution(self.init_sol)
        self.curr_sol = copy.deepcopy(self.init_sol)
        self.global_best_sol = copy.deepcopy(self.curr_sol)

        self.search_history = []
        it = 0
        prev_sol = None
        last_global_sol_improvement = 0
        while 1:
            self.search_history.append(copy.deepcopy(self.curr_sol))
            prev_sol = copy.deepcopy(self.curr_sol)
            self.generate_neighborhood_x_vectors()
            self.neighborhood = [
                Solution(self.neighborhood_x_vector[i])
                for i in range(self.neighborhood_size)
            ]
            for sol in self.neighborhood:
                self.evaluate_objectives(sol)
                self.evaluate_solution(sol)

            self.neighborhood.sort(key=Solution.get_val)

            # purge neighborhood from Tabu elements
            # I did not cover the case where theoretically every element from the neighborhood is in the tabu list.
            while 1:
                if self.neighborhood[0] in self.tabu_list:
                    del self.neighborhood[0]
                else:
                    break

            # Update current solution
            self.curr_sol = copy.deepcopy(self.neighborhood[0])

            # Update global best solution if necessary
            if self.global_best_sol.get_val() > self.curr_sol.get_val():
                self.global_best_sol = copy.deepcopy(self.curr_sol)
                last_global_sol_improvement = it

            # Update tabu-list
            # As an optimization trick, remove the 5 first entries in the TL, instead of just the oldest element.
            if len(self.tabu_list) > self.TL_max_length:
                self.tabu_list = self.tabu_list[5:]
            self.tabu_list.append(copy.deepcopy(prev_sol))

            it = it + 1
            # Check termination conditions:

            # Maximum iterations exceeded?
            if it > self.max_iter:
                self.time_elapsed = time.process_time() - start
                print(
                    "-------------------------------------------------------------------"
                )
                print(
                    'Terminating because max iterations were exceeded, it = ',
                    it)
                print('Time elapsed: ', self.time_elapsed)
                print(
                    "-------------------------------------------------------------------"
                )
                self.last_iter = it
                self.termination_reason = 'Maximum iters exceeded.'
                if self.save is True:
                    self.save_search_results()
                return_dict = dict(search_history=self.search_history,
                                   termination_reason='max iter exceeded',
                                   last_iter=it,
                                   global_best_sol=self.global_best_sol,
                                   time_elapsed=self.time_elapsed)
                return return_dict

            # No progress made for max_loops iterations already?
            if it - last_global_sol_improvement > self.max_loops:
                self.time_elapsed = time.process_time() - start
                print(
                    "-------------------------------------------------------------------"
                )
                print('Terminating after iteration number ', it,
                      ' because the algorithm hasn'
                      't progressed in ', it - last_global_sol_improvement,
                      ' iterations')
                print('Time elapsed: ', self.time_elapsed)
                print(
                    "-------------------------------------------------------------------"
                )
                self.last_iter = it
                self.termination_reason = 'No performance improvement.'
                if self.save is True:
                    self.save_search_results()
                return_dict = dict(search_history=self.search_history,
                                   termination_reason='no progress',
                                   last_iter=it,
                                   global_best_sol=self.global_best_sol,
                                   time_elapsed=self.time_elapsed)
                return return_dict
Пример #13
0
 def test_success_nequality(self):
     self.sol1 = Solution(np.array([1, 2, 3, 4, 5]))
     self.sol2 = Solution(
         np.array([1.0000001, 2.00002, 3.0000001, 4.0000001, 5.000001]))
     self.assertTrue(self.sol1 != self.sol2)
Пример #14
0
 def setUp(self):
     Solution.eps = 0.000001
     self.sol1 = Solution()
     self.sol2 = Solution()