Example #1
0
    front = get_non_dominated_solutions(final)  #process[-1]
    process.append(front)
    smrprocessruns.append(process)
    reference += front

reference_front = get_non_dominated_solutions(reference)

plot_front = Plot(plot_title='Pareto front approximation',
                  axis_labels=['TRT', 'TDC'],
                  reference_front=None)
plot_front.plot(reference_front, label=' ', filename=fm, format='png')

# Save results to file
print_function_values_to_file(
    reference_front, 'FUN.' + algorithm.get_name() + "-" + problem.get_name())
print_variables_to_file(
    reference_front, 'VAR.' + algorithm.get_name() + "-" + problem.get_name())

# Normalization
timelist = []
costlist = []
for s in reference_front:
    timelist.append(s.objectives[0])
    costlist.append(s.objectives[1])
mintime = min(timelist)
maxtime = max(timelist)
mincost = min(costlist)
maxcost = max(costlist)
print("Time:", mintime, maxtime)
print("Cost:", mincost, maxcost)
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))
    print('Fitness: {}'.format(result.objectives[0]))
    print('Computing time: {}'.format(algorithm.total_computing_time))
Example #3
0
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm
from jmetal.operator import BinaryTournamentSelection, BitFlipMutation, SPXCrossover
from jmetal.problem.singleobjective.knapsack import Knapsack
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == "__main__":
    problem = Knapsack(
        from_file=True,
        filename="resources/Knapsack_instances/KnapsackInstance_50_0_0.kp")

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=100,
        offspring_population_size=1,
        mutation=BitFlipMutation(probability=0.1),
        crossover=SPXCrossover(probability=0.8),
        selection=BinaryTournamentSelection(),
        termination_criterion=StoppingByEvaluations(max_evaluations=25000),
    )

    algorithm.run()
    subset = algorithm.get_result()

    print("Algorithm: {}".format(algorithm.get_name()))
    print("Problem: {}".format(problem.get_name()))
    print("Solution: {}".format(subset.variables))
    print("Fitness: {}".format(-subset.objectives[0]))
    print("Computing time: {}".format(algorithm.total_computing_time))
    print(f"Problem Maximum Capacity: {problem.capacity}")