コード例 #1
0
    def construct(self, hyperparameters: Mapping, scenario: Mapping,
                  warm_startup_info: Mapping) -> None:
        problem_init_params = HashableDict(
            scenario['problem_initialization_parameters'])
        problem = self._get_problem(scenario['Problem'], problem_init_params)
        self.load_initial_solutions(warm_startup_info, problem)

        from jmetal.operator.mutation import PermutationSwapMutation

        termination_criterion_cls = self._get_class_from_module(
            name=scenario["Budget"]["Type"], module=termination)
        termination_criterion = termination_criterion_cls(
            scenario["Budget"]["Amount"])

        mh_name = hyperparameters["low level heuristic"].split(".")[1]
        if mh_name == "GeneticAlgorithm":
            from jmetal.algorithm.singleobjective.genetic_algorithm import (
                GeneticAlgorithm)
            from jmetal.operator.crossover import CXCrossover
            from jmetal.operator.selection import BinaryTournamentSelection
            self._llh_algorithm = GeneticAlgorithm(
                problem=problem,
                population_size=100,
                offspring_population_size=100,
                mutation=PermutationSwapMutation(0.5),
                crossover=CXCrossover(0.5),
                selection=BinaryTournamentSelection(),
                termination_criterion=termination_criterion,
                population_generator=self._solution_generator)

        elif mh_name == "SimulatedAnnealing":
            from jmetal.algorithm.singleobjective.simulated_annealing import (
                SimulatedAnnealing)
            self._llh_algorithm = SimulatedAnnealing(
                problem=problem,
                mutation=PermutationSwapMutation(0.5),
                termination_criterion=termination_criterion,
                solution_generator=self._solution_generator)

        elif mh_name == "EvolutionStrategy":
            from jmetal.algorithm.singleobjective.evolution_strategy import (
                EvolutionStrategy)
            self._llh_algorithm = EvolutionStrategy(
                problem=problem,
                mu=500,
                lambda_=500,
                elitist=False,
                mutation=PermutationSwapMutation(0.5),
                termination_criterion=termination_criterion,
                population_generator=self._solution_generator)
        else:
            self.logger.error(f"Wrong meta-heuristic name: {mh_name}")
コード例 #2
0
def evaluate(crosssover_algo, problem):
  alldata = []
  series = []
  for x in range(10):
    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=100,
        offspring_population_size=100,
        mutation=PolynomialMutation(1.0 / problem.number_of_variables, 20.0),
        crossover=crosssover_algo,
        selection=BinaryTournamentSelection(),
        termination_criterion=StoppingByEvaluations(max_evaluations=500000)
    )
    data = []
    dataobserver = DataObserver(1.0, data)
    algorithm.observable.register(observer=dataobserver)
    algorithm.run()
    result = algorithm.get_result().objectives[0]
    series.append(result) 
    alldata.append(data)

  numpy_array = np.array(alldata)
  transpose = numpy_array.T
  transpose_list = transpose.tolist()

    
  fig = plt.figure(figsize =(60, 42)) 
     
  ax = fig.add_axes([0, 0, 1, 1]) 
    
  bp = ax.boxplot(transpose_list) 
   
  plt.show()

  print(stats.kruskal(transpose_list[0],transpose_list[1],transpose_list[-1]))
  series = [series] 
  print(np.average(series))

  sp.posthoc_dunn([transpose_list[0],transpose_list[1],transpose_list[-1]], p_adjust = 'holm')
コード例 #3
0
 def __init__(self, islands_len, iterval, migration_rate, random_groups, random_destination, max_evaluations, problem):
     self.islands_len = islands_len
     self.iterval = iterval
     self.max_evaluations = max_evaluations
     self.problem = problem
     self.migration = Migration(
         migration_rate, random_groups, random_destination)
     for i in range(islands_len):
         # possible to override problem later self.problem
         self.islands[str(i)] = GeneticAlgorithm(
             problem=self.problem,
             population_size=100,
             offspring_population_size=100,
             mutation=UniformMutation(0.006, 20.0),
             crossover=SBXCrossover(0.3, 19.0),
             selection=BinaryTournamentSelection(),
             termination_criterion=StoppingByEvaluations(
                 max_evaluations=iterval)
         )
     print(self.islands)
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))
    print('Fitness: {}'.format(result.objectives[0]))
コード例 #5
0
logging.basicConfig(level=logging.INFO, filename="Result20190123.log")

N_seeds = 50
run = 30
fm = 'NSGAII'

#if __name__ == '__main__':
logging.info(get_apps())
#Find AO seed: GA
problem = MADA()
max_evaluations = 20000
algorithm = GeneticAlgorithm(
    problem=problem,
    population_size=100,
    offspring_population_size=100,
    mutation=IntegerPolynomialMutation(probability=0.1),
    crossover=IntegerCrossover(probability=0.9),
    selection=BinaryTournamentSelection(),
    termination_criterion=StoppingByEvaluations(max=max_evaluations))

#algorithm.observable.register(observer=PrintObjectivesObserver(frequency=10))

algorithm.run()
aggseed = algorithm.get_result()
logging.info('Aggregated seed solution is {}: {}'.format(
    aggseed.variables, aggseed.objectives[0]))
#print(change)

#Find the min time, also SO time seed: GA
problem = MADT()
max_evaluations = 10000
コード例 #6
0
         1323, 1146, 5192, 6547, 343, 7584, 3765, 8660, 9318,
         5098, 5185, 9253, 4495, 892, 5080, 5297, 9275, 7515,
         9729, 6200, 2138, 5480, 860, 8295, 8327, 9629, 4212,
         3087, 5276, 9250, 1835, 9241, 1790, 1947, 8146, 8328,
         973, 1255, 9733, 4314, 6912, 8007, 8911, 6802, 5102,
         5451, 1026, 8029, 6628, 8121, 5509, 3603, 6094, 4447,
         683, 6996, 3304, 3130, 2314, 7788, 8689, 3253, 5920,
         3660, 2489, 8153, 2822, 6132, 7684, 3032, 9949, 59,
         6669, 6334]

    problem = SubsetSum(C, W)

    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))
コード例 #7
0
	for var in notes:
		combined += notes_directory.get_audio_note(var)
	combined.export('data/conc.wav', format = 'wav')


target_file = 'data/songs/ode_to_joy.wav'
number_of_notes = 16
problem = MusicProblem(target_file, number_of_notes)
population_size = 100
max_evaluations = 5000

algorithm = GeneticAlgorithm(
	problem = problem,
	mutation = UniformMutation(probability = 1.0 / problem.number_of_notes),
	crossover = UniformCrossover(1.0),
	selection = BinaryTournamentSelection(),
	population_size = population_size,
	termination_criterion = StoppingByEvaluations(max = max_evaluations),
	offspring_population_size = population_size
)

# initialize progress bar observer
algorithm.observable.register(observer = PrintObjectivesObserver())

# run algorithm and set results
algorithm.run()
front = algorithm.get_result()

# create audio file from the best result
generate_audio_file(front.variables[0])
print(front)
コード例 #8
0
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm
from jmetal.operator import BinaryTournamentSelection, PolynomialMutation, SBXCrossover
from jmetal.problem.singleobjective.unconstrained import Rastrigin
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == "__main__":
    problem = Rastrigin(10)

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=100,
        offspring_population_size=1,
        mutation=PolynomialMutation(1.0 / problem.number_of_variables, 20.0),
        crossover=SBXCrossover(0.9, 5.0),
        selection=BinaryTournamentSelection(),
        termination_criterion=StoppingByEvaluations(max_evaluations=100000),
    )

    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))
コード例 #9
0
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm
from jmetal.operator import BinaryTournamentSelection, BitFlipMutation, SPXCrossover
from jmetal.problem import OneMax
from jmetal.util.observer import PrintObjectivesObserver
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == "__main__":
    problem = OneMax(number_of_bits=512)

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=40,
        offspring_population_size=40,
        mutation=BitFlipMutation(1.0 / problem.number_of_bits),
        crossover=SPXCrossover(1.0),
        selection=BinaryTournamentSelection(),
        termination_criterion=StoppingByEvaluations(max_evaluations=20000),
    )

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

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

    print("Algorithm: {}".format(algorithm.get_name()))
    print("Problem: {}".format(problem.get_name()))
    print("Solution: " + result.get_binary_string())
    print("Fitness:  " + str(result.objectives[0]))
    print("Computing time: {}".format(algorithm.total_computing_time))
コード例 #10
0
        result = 0
        x = solution.variables
        for i in range(solution.number_of_variables - 1):
            result += 100 * (x[i + 1] - x[i]**2)**2 + (1 - x[i])**2
        solution.objectives[0] = result
        return solution

    def get_name(self) -> str:
        return 'MyRosen'


problem = MyRosen(5)

algorithm = GeneticAlgorithm(
    problem=problem,
    population_size=100,
    offspring_population_size=50,
    mutation=SimpleRandomMutation(0.4),
    crossover=SBXCrossover(0.9, 20.0),
    selection=BestSolutionSelection(),
    termination_criterion=StoppingByEvaluations(max=5000))

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))
コード例 #11
0
        available_time = vsc.get_available_time()
        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())
コード例 #12
0
from jmetal.algorithm.singleobjective.genetic_algorithm import GeneticAlgorithm
from jmetal.operator import SPXCrossover, BitFlipMutation, BinaryTournamentSelection
from jmetal.problem import OneMax
from jmetal.util.observer import PrintObjectivesObserver
from jmetal.util.termination_criterion import StoppingByEvaluations

if __name__ == '__main__':
<<<<<<< HEAD
    problem = OneMax(number_of_bits=1024)

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=100,
        offspring_population_size=100,
=======
    problem = OneMax(number_of_bits=512)

    algorithm = GeneticAlgorithm(
        problem=problem,
        population_size=40,
        offspring_population_size=40,
>>>>>>> 52e0b172f0c6d651ba08b961a90a382f0a4b8e0f
        mutation=BitFlipMutation(1.0 / problem.number_of_bits),
        crossover=SPXCrossover(1.0),
        selection=BinaryTournamentSelection(),
        termination_criterion=StoppingByEvaluations(max=20000)
    )

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

    algorithm.run()
コード例 #13
0
ファイル: GA.py プロジェクト: ayushmankd/GeneticAlgorithms
    )
    new_solution.variables = random.sample(range(self.number_of_variables), k=self.number_of_variables)

    return new_solution
  def get_name(self):
        return 'Symmetric TSP'
algorithm = GeneticAlgorithm(
  problem=TSP(),
  population_size=4,
  offspring_population_size=4,
  # To Get a Different Combination by swapping any two positions
  mutation=PermutationSwapMutation(1.0 / 4),
  # Partially Mapped Crossover
  # Basically, parent 1 donates a swath of genetic material and the
  # corresponding swath from the other parent is sprinkled about in the child. 
  # Once that is done, the remaining alleles are copied direct from parent 2.
  crossover=PMXCrossover(0.8),
  # Compare any two Selection and then select the fittest
  selection=BinaryTournamentSelection(
    MultiComparator(
      [FastNonDominatedRanking.get_comparator(),
      CrowdingDistance.get_comparator()]
    )
  ),
  termination_criterion=StoppingByEvaluations(max=250)
)
algorithm.observable.register(observer=PrintObjectivesObserver(10))

algorithm.run()
result = algorithm.get_result()
# print('Algorithm: {}'.format(algorithm.get_name()))
print('Solution: {}'.format(result.variables))