from jmetal.util.observer import PrintObjectivesObserver
from jmetal.util.ranking import FastNonDominatedRanking
from jmetal.util.solutions.comparator import MultiComparator
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == '__main__':
    problem = TSP(instance='../../resources/TSP_instances/kroA100.tsp')

    print('Cities: ', problem.number_of_variables)

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=100,
        offspring_population_size=100,
        mutation=PermutationSwapMutation(1.0 / problem.number_of_variables),
        crossover=PMXCrossover(0.8),
        selection=BinaryTournamentSelection(
            MultiComparator([
                FastNonDominatedRanking.get_comparator(),
                CrowdingDistance.get_comparator()
            ])),
        termination_criterion=StoppingByEvaluations(max=2500000))

    algorithm.observable.register(observer=PrintObjectivesObserver(1000))

    algorithm.run()
    result = algorithm.get_result()

    print('Algorithm: {}'.format(algorithm.get_name()))
    print('Problem: {}'.format(problem.get_name()))
    print('Solution: {}'.format(result.variables))
Example #2
0
 def test_should_constructor_create_a_valid_operator(self):
     operator = PMXCrossover(0.5)
     self.assertEqual(0.5, operator.probability)
Example #3
0
 def test_should_constructor_create_a_non_null_object(self):
     solution = PMXCrossover(1.0)
     self.assertIsNotNone(solution)
Example #4
0
 def test_should_constructor_raises_an_exception_is_probability_is_higher_than_one(
         self) -> None:
     with self.assertRaises(Exception):
         PMXCrossover(1.01)
Example #5
0
 def test_should_constructor_raises_an_exception_is_probability_is_negative(
         self) -> None:
     with self.assertRaises(Exception):
         PMXCrossover(-1)
        metric.update_available_time(available_time)

        test_cases = vsc.get_testcases()
        IND_SIZE = len(test_cases)

        if (IND_SIZE > 1):
            # Run GA to find the best NAPFD in current commit
            problem = TCPCI(metric=metric, test_cases=test_cases,
                            number_of_variables=IND_SIZE)

            algorithm = GeneticAlgorithm(
                problem=problem,
                population_size=300,
                offspring_population_size=300,
                mutation=PermutationSwapMutation(0.01),
                crossover=PMXCrossover(0.9),
                selection=BinaryTournamentSelection(),
                termination_criterion=StoppingByEvaluations(max=60000)
            )

            # algorithm.observable.register(observer=PrintObjectivesObserver(1000))

            algorithm.run()
            result = algorithm.get_result()

            print(f"Commit: {t} - Fitness: {result.objectives[0]*-1} - Computing time: {algorithm.total_computing_time}")
        else:
            print(f"Commit: {t} - Fitness: {1}")
        # print('Algorithm: ' + algorithm.get_name())
        # print('Problem: ' + problem.get_name())
        # print('Solution: ' + str(result.variables[0]))