def simulation_random_walk(params): STPG = read_problem("datasets", "ORLibrary", params["dataset"]) crossover = CrossoverRandomWalkRST(STPG) evaluation = EvaluateEdgeSet(STPG) mutate = MutationReplaceByRandomEdge(STPG) output_data_dir = os.path.join("data", "test", "edgeset", STPG.name) tracker = DataTracker(params['runtrial'], target=output_data_dir) population = (GPopulation( chromosomes=[ gen_random_walk(STPG) for _ in range(params["population_size"]) ], eval_function=evaluation, maximize=True).evaluate().normalize( norm_function=normalize).callback(update_best)) evol = (Evolution().evaluate().normalize( norm_function=normalize).callback(update_best).callback( tracker.log_evaluation).select(selection_func=roullete).crossover( combiner=crossover).mutate( mutate_function=mutate, probability=params['tx_mutation']).callback( update_generation).callback(display, every=100)) with Stagnation(interval=params["stagnation_interval"]), \ BestKnownReached(global_optimum=params['global_optimum']): result = population.evolve(evol, n=params["n_iterations"]) tracker.log_simulation(params, STPG, result) tracker.report() print(result.stoppedby)
def simulation(simulation_name, params): datasets_folder = path.join("datasets", "ORLibrary") filename = path.join(datasets_folder, params["dataset"]) STPG = ReaderORLibrary().parser(filename) print("STPG information", '\n', 10 * '- ', '\n') print("Trial: ", parameters['runtrial']) print('Instance: ', STPG.name) print('Best Known cost: ', params['global_optimum']) print("Nro. Node:", STPG.nro_nodes) print("Nro. Edges:", STPG.nro_edges) print("Nro. Terminals:", STPG.nro_terminals) # print("Terminals: \n", STPG.terminals) output_folder = path.join("data", simulation_name, STPG.name) tracker = DataTracker(params['runtrial'], target=output_folder) generator = GenerateBasedPrimRST(STPG) evaluator = EvaluateTreeGraph(STPG) crossover = CrossoverPrimRST(STPG) prunner = Prunning(STPG) mut_prim = PrimBasedMutation(STPG) replace_random = ReplaceByRandomEdge(STPG) population = (GPopulation( chromosomes=[generator() for _ in range(params["population_size"])], eval_function=evaluator, maximize=True).evaluate().normalize( norm_function=normalize).callback(update_best)) evol = (Evolution().evaluate().normalize(norm_function=normalize).callback( update_best).callback(tracker.log_evaluation).select( selection_func=roullete).crossover(combiner=crossover).mutate( mutate_function=replace_random, probability=0.2).mutate( mutate_function=mut_prim, probability=0.2).mutate( mutate_function=prunner, probability=0.2).callback(update_generation).callback( display, every=100)) with Stagnation(interval=params["stagnation_interval"]), \ BestKnownReached(global_optimum=params['global_optimum']): result = population.evolve(evol, n=params["n_iterations"]) tracker.log_simulation(params, STPG, result) best_overall = result.documented_best test, response = is_steiner_tree(best_overall.chromosome, STPG) tracker.log_bestIndividual(best_overall, test, response) tracker.report()
def simulation(simulation_name, params: dict, get_evol: callable): STPG = read_problem("datasets", "ORLibrary", params["dataset"]) lenght = STPG.nro_nodes - STPG.nro_terminals tracker = DataTracker(params['runtrial'], target=os.path.join("data", simulation_name, STPG.name)) population = (GPopulation( chromosomes=[ random_binary(lenght) for _ in range(params["population_size"]) ], eval_function=EvaluateKruskalBased(STPG, penality_function=lambda k: (k - 1) * 100), maximize=True).evaluate().normalize( norm_function=normalize).callback(update_best)) evol = get_evol(STPG, tracker, params) # with Stagnation(interval=params["stagnation_interval"]), \ # with BestSteinerTreeReachead(global_optimum=params["global_optimum"], # STPG=STPG, # decoder=Coder(STPG).binary2treegraph, # ): with Stagnation(interval=params["stagnation_interval"]), \ BestKnownReached(global_optimum=params['global_optimum']): result = population.evolve(evol, n=params["n_iterations"]) tracker.log_simulation(params, STPG, result) tracker.report() print(result.stoppedby) return result