コード例 #1
0
 def setUp(self):
     """Init"""
     set_seed(1)
     project_path = os.path.dirname(os.path.abspath(__file__))
     self.problem = Rmnk(project_path +
                         '/data/instances/rmnk_0_2_100_1_0.dat')
     self.solution = self.problem.generate_random_solution()
コード例 #2
0
class RmnkTest(unittest.TestCase):
    """Test the 'rmnk' problem."""
    def setUp(self):
        """Init"""
        project_path = os.path.dirname(os.path.abspath(__file__))
        self.problem = Rmnk(project_path +
                            '/../data/instances/rmnk_0_2_100_1_0.dat')

    def test_instance(self):
        """Test parameters"""
        self.assertEqual(self.problem.rho, 0)
        self.assertEqual(self.problem.m, 2)
        self.assertEqual(self.problem.n, 100)
        self.assertEqual(self.problem.k, 1)

    def test_generate_random_solution(self):
        """Test the function 'generate_random_solution'"""
        random_solution = self.problem.generate_random_solution()
        self.assertEqual(len(random_solution.decision_vector), 100)
        self.assertEqual(len(random_solution.F), 2)

        for item in random_solution.decision_vector:
            self.assertIn(item, [0, 1])

        for function_i in random_solution.F:
            self.assertLess(function_i, 0)
            self.assertGreater(function_i, -1)

    def test_generate_solution(self):
        """Test the function 'generate_solution'"""
        array = []

        for i in range(100):
            array.append(random.randint(0, 1))

        solution = self.problem.evaluate(array)
        self.assertEqual(len(solution.decision_vector), 100)
        self.assertEqual(len(solution.F), 2)

        for item in solution.decision_vector:
            self.assertIn(item, [0, 1])

        for function_i in solution.F:
            self.assertLess(function_i, 0)
            self.assertGreater(function_i, -1)

    def test_evaluation(self):
        """Test evaluation"""
        array = []

        for i in range(100):
            array.append(1)

        solution = self.problem.evaluate(array)

        self.assertEqual(solution.F[0], -0.4884468640000001)
        self.assertEqual(solution.F[1], -0.4930223456999998)
コード例 #3
0
 def setUp(self):
     """Init"""
     set_seed(1)
     project_path = os.path.dirname(os.path.abspath(__file__))
     self.rmnk = Rmnk(instance_file=project_path +
                      '/data/instances/rmnk_0_2_100_1_0.dat')
     self.rmnk3D = Rmnk(instance_file=project_path +
                        '/data/instances/rmnk_0_3_100_1_0.dat')
     self.number_of_objective = self.rmnk.number_of_objective
コード例 #4
0
    def setUp(self):
        """Init"""
        project_path = os.path.dirname(os.path.abspath(__file__))
        self.problem = Rmnk(project_path + '/data/instances/rmnk_0_2_100_1_0.dat')
        weight_file = project_path + "/data/weights/SOBOL-2objs-10wei.ws"
        self.weights = generate_weight_vectors(weight_file, False)

        array = []

        for i in range(100):
            array.append(1)

        self.solution = self.problem.evaluate(array)
        self.z = [1, 1]
コード例 #5
0
    def setUp(self):
        """Init"""
        random.seed(1)
        np.random.seed(1)

        self.number_of_evaluations = 100

        project_path = os.path.dirname(os.path.abspath(__file__))
        self.rmnk = Rmnk(instance_file=project_path +
                         '/data/instances/rmnk_0_2_100_1_0.dat')

        self.number_of_objective = self.rmnk.number_of_objective
        self.number_of_weight = 10
        self.number_of_weight_neighborhood = 10
        self.number_of_crossover_points = 4
        self.weight_file = project_path + "/data/weights/SOBOL-" \
                           + str(self.number_of_objective) \
                           + "objs-" + str(self.number_of_weight) \
                           + "wei.ws"

        self.moead = Moead(
            problem=self.rmnk,
            max_evaluation=self.number_of_evaluations,
            number_of_objective=self.number_of_objective,
            number_of_weight=self.number_of_weight,
            aggregation_function=Tchebycheff,
            number_of_weight_neighborhood=self.number_of_weight_neighborhood,
            number_of_crossover_points=self.number_of_crossover_points,
            weight_file=self.weight_file,
        )
コード例 #6
0
class RmnkTest(unittest.TestCase):
    """Test the 'rmnk' problem."""
    def setUp(self):
        """Init"""
        random.seed(1)
        np.random.seed(1)
        project_path = os.path.dirname(os.path.abspath(__file__))
        self.problem = Rmnk(project_path +
                            '/data/instances/rmnk_0_2_100_1_0.dat')
        self.solution = self.problem.generate_random_solution()

    def test_set_item(self):
        """Test set item"""
        # todo
        self.assertEqual(self.solution[0], -0.5107130364200001)
        self.solution[0] = -42
        self.assertEqual(self.solution[0], -42)

    def test_repr(self):
        """Test repr'"""
        self.assertEqual(repr(self.solution),
                         "[-0.5107130364200001, -0.5078202645999998]")

    def test_copy(self):
        """"Test the copy of solution"""
        copy_sol = copy(self.solution)
        # address are different
        self.assertNotEqual(copy_sol, self.solution)

        # solution are equals
        self.assertEqual(copy_sol.F, self.solution.F)
コード例 #7
0
    def setUp(self):
        """Init"""
        set_seed(1)
        self.number_of_evaluations = 100

        project_path = os.path.dirname(os.path.abspath(__file__))
        self.rmnk = Rmnk(instance_file=project_path + '/data/instances/rmnk_0_2_100_1_0.dat')

        self.number_of_objective = self.rmnk.number_of_objective
        self.number_of_weight = 10
        self.number_of_weight_neighborhood = 2
        self.number_of_crossover_points = 4
        self.weight_file = project_path + "/data/weights/SOBOL-" \
                           + str(self.number_of_objective) \
                           + "objs-" + str(self.number_of_weight) \
                           + "wei.ws"
        self.weight_file50 = project_path + "/data/weights/SOBOL-" \
                           + str(self.number_of_objective) \
                           + "objs-" + str(50) \
                           + "wei.ws"
コード例 #8
0
class ScalarizingTest(unittest.TestCase):
    """Test aggregation functions."""

    def setUp(self):
        """Init"""
        project_path = os.path.dirname(os.path.abspath(__file__))
        self.problem = Rmnk(project_path + '/data/instances/rmnk_0_2_100_1_0.dat')
        weight_file = project_path + "/data/weights/SOBOL-2objs-10wei.ws"
        self.weights = generate_weight_vectors(weight_file, False)

        array = []

        for i in range(100):
            array.append(1)

        self.solution = self.problem.evaluate(array)
        self.z = [1, 1]

    def test_tchebycheff(self):
        """Test Tchebycheff"""

        value1 = Tchebycheff().run(self.solution, 2, self.weights, 3, self.z)
        value2 = Tchebycheff().run(self.solution, 2, self.weights, 8, self.z)

        self.assertEqual(value1, 1.1197667592749998)
        self.assertEqual(value2, 1.3023910060000001)
        self.assertFalse(Tchebycheff().is_better(value1, value2))
        self.assertTrue(Tchebycheff().is_better(value2, value1))

    def test_weighted_sum(self):
        """Test Weighted Sum"""

        value1 = WeightedSum().run(self.solution, 2, self.weights, 3, self.z)
        value2 = WeightedSum().run(self.solution, 2, self.weights, 8, self.z)

        self.assertEqual(value1, -0.4918784752749999)
        self.assertEqual(value2, -0.4890187992125)
        self.assertFalse(WeightedSum().is_better(value1, value2))
        self.assertTrue(WeightedSum().is_better(value2, value1))
コード例 #9
0
from moead_framework.aggregation import Tchebycheff
from moead_framework.algorithm.combinatorial import Moead
from moead_framework.problem.combinatorial import Rmnk
from moead_framework.tool.result import save_population

###############################
#    Initialize the problem   #
###############################
# The file is available here : https://github.com/moead-framework/data/blob/master/problem/RMNK/Instances/rmnk_0_2_100_1_0.dat
# Others instances are available here : https://github.com/moead-framework/data/tree/master/problem/RMNK/Instances
instance_file = "rmnk_0_2_100_1_0.dat"
rmnk = Rmnk(instance_file=instance_file)

###############################
#  Initialize the algorithm   #
###############################
number_of_weight = 10
number_of_weight_neighborhood = 20
number_of_evaluations = 1000
# The file is available here : https://github.com/moead-framework/data/blob/master/weights/SOBOL-2objs-10wei.ws
# Others weights files are available here : https://github.com/moead-framework/data/tree/master/weights
weight_file = "SOBOL-" + str(
    rmnk.function_numbers) + "objs-" + str(number_of_weight) + "wei.ws"

###############################
#    Execute the algorithm    #
###############################
moead = Moead(
    problem=rmnk,
    max_evaluation=number_of_evaluations,
    number_of_weight_neighborhood=number_of_weight_neighborhood,
コード例 #10
0
 def setUp(self):
     """Init"""
     project_path = os.path.dirname(os.path.abspath(__file__))
     self.problem = Rmnk(project_path +
                         '/../data/instances/rmnk_0_2_100_1_0.dat')
コード例 #11
0
class ToolsTest(unittest.TestCase):
    """Test implemented algorithms."""
    def setUp(self):
        """Init"""
        random.seed(1)
        np.random.seed(1)
        project_path = os.path.dirname(os.path.abspath(__file__))
        self.rmnk = Rmnk(instance_file=project_path +
                         '/data/instances/rmnk_0_2_100_1_0.dat')
        self.rmnk3D = Rmnk(instance_file=project_path +
                           '/data/instances/rmnk_0_3_100_1_0.dat')
        self.number_of_objective = self.rmnk.number_of_objective

    def test_PF(self):
        """Test get Non dominated"""
        solutions = []
        for i in range(20):
            solutions.append(self.rmnk.generate_random_solution())

        non_dominated_pop = get_non_dominated(population=solutions)
        non_dominated = []
        for s in non_dominated_pop:
            non_dominated.append(s.F)

        test = [[-0.5571641596999998, -0.4450894643],
                [-0.5483895551199999, -0.48251202308000013],
                [-0.5428439510999998, -0.48726908450000017],
                [-0.5107130364200001, -0.5078202645999998],
                [-0.5070678622999999, -0.5411136235],
                [-0.4660221059999998, -0.5569167061000001],
                [-0.4493039854199998, -0.5587430658000001]]

        self.assertEqual(len(non_dominated), len(test))

        for elt in test:
            self.assertIn(elt, non_dominated)

    def test_population_size_without_duplicate(self):
        """test the function population_size_without_duplicate"""
        population = []

        for i in range(10):
            population.append(self.rmnk.generate_random_solution())

        # duplicate two solutions in the population
        population.append(population[4])
        population.append(population[2])

        self.assertEqual(len(population), 12)
        self.assertEqual(
            population_size_without_duplicate(population=population), 10)

    def test_hypervolume3D(self):
        """test the hypervolume in 3D"""
        population = []

        for i in range(10):
            population.append(self.rmnk3D.generate_random_solution())

        self.assertEqual(compute_hypervolume(population, [0, 0, 0]),
                         0.16108679722159508)  # test the hypervolume value

    def test_compute_crowding_distance(self):
        """test the function compute_crowding_distance"""
        population = []

        for i in range(10):
            population.append(self.rmnk.generate_random_solution())

        # distance equal to 0 before computing
        self.assertEqual(population[0].distance, 0)

        # compute the crowding distance
        pop_with_distance = compute_crowding_distance(population)

        self.assertEqual(population[0].distance, 0.07806191187999945)
        self.assertEqual(pop_with_distance[0].distance, 0.07806191187999945)

    def test_save_population(self):
        """test the function to save the population"""
        population = []

        for i in range(10):
            population.append(self.rmnk.generate_random_solution())

        save_population("test_save_population.txt", population)
        f = open("test_save_population.txt", "r")

        index_pop = 0
        for line in f:
            f1 = float(line.replace("\n", "").split(" ")[0])
            f2 = float(line.replace("\n", "").split(" ")[1])
            self.assertEqual(f1, population[index_pop].F[0])
            self.assertEqual(f2, population[index_pop].F[1])
            index_pop += 1

        f.close()
        os.remove("test_save_population.txt")

    def test_save_population_full(self):
        """test the function to save the population with all decision variables"""
        population = []

        for i in range(10):
            population.append(self.rmnk.generate_random_solution())

        save_population_full("test_save_population_full.txt", population)
        f = open("test_save_population_full.txt", "r")

        index_pop = 0
        for line in f:
            f1 = float(
                line.replace("\n", "").replace(" ", "_", 2).split("_")[0])
            f2 = float(
                line.replace("\n", "").replace(" ", "_", 2).split("_")[1])
            solution = line.replace("\n", "").replace(" ", "_",
                                                      2).split("_")[2]

            self.assertEqual(f1, population[index_pop].F[0])
            self.assertEqual(f2, population[index_pop].F[1])
            self.assertEqual(solution,
                             str(population[index_pop].solution.tolist()))
            index_pop += 1

        f.close()
        os.remove("test_save_population_full.txt")