Best Python code snippet using avocado_python
p4-2-Automater.py
Source:p4-2-Automater.py
1from problem import maxone,knapsack,vertex_cover2from solver.ga import GeneticSolver3from fn.ga import *4from fn.fitness import *5from utils import *6def select_problem(problem_name,config):7 if problem_name == 'maxone':8 problem = maxone.problem(16)9 config.fitness_fn = maxone_fitness10 config.feasibility_minimum = 011 config.best_possible_score = problem.N12 elif problem_name == 'knapsack':13 problem = knapsack.problem(99)14 config.fitness_fn = knapsack_fitness15 config.feasibility_minimum = 10016 config.best_possible_score = problem.total_value17 elif problem_name == 'vertex_cover':18 problem = vertex_cover.problem(99)19 config.fitness_fn = vertex_cover_fitness20 config.feasibility_minimum = 10021 config.best_possible_score = len(problem.variables)22 return problem23def main():24 bestConfigScore = 0;25 bestConfigList = [];26 configIterations = 0;27 # LISTS28 populationSizeList = [20,50]29 populationModelList = [(generational, "generational"), (choose_best, "choose_best")]30 31 parentSimilarityList = [0.9, 0.5]32 parentSelectionList = [ (fitness_proportionate, "fitness_proportionate") , (tournament_selection, "tournament_selection")]33 crossoverProbabilityList = [0.90, 0.60]34 crossoverTypeList = [(one_point_crossover,"one_point_crossover"), (two_point_crossover,"two_point_crossover"), (uniform_crossover, "uniform_crossover")]35 mutationProbabilityList = [0.3, 0.5]36 changekval2 = change_k_values(2)37 mutationTypeList = [(change_one_value, "change_one_value"), (changekval2, "changekval2"), (swap_two_values, "swap_two_values")]38 39 for popSize in populationSizeList:40 for popModel in populationModelList:41 for parentSim in parentSimilarityList:42 for parentSel in parentSelectionList: 43 for crossoverProb in crossoverProbabilityList:44 for crossoverType in crossoverTypeList:45 for mutationProb in mutationProbabilityList:46 for mutationType in mutationTypeList:47 config = Config()48 # problem_name = 'maxone'49 # problem_name = 'knapsack'50 problem_name = 'vertex_cover'51 problem = select_problem(problem_name,config)52 config.best_possible_score += config.feasibility_minimum53 config.initial_solution = 'random'54 config.max_parent_try = 50055 config.max_iterations = 20056 config.max_flat_iterations = 5057 config.random_seed = 12345678958 config.explain = False59 configList = []60 ################## CONFIG CHANGES STARTS HERE ########################################61 62 # POPULATION SIZE63 config.population_size = popSize # 20, 5064 configList.append(("popSize", popSize))65 # POPULATION MODEL66 config.replace_population = popModel[0]67 # config.replace_population = choose_best68 configList.append(("popModel", popModel[1]))69 70 # SELECTION (PARENT SIMILARITY)71 config.max_parent_similarity = parentSim # 0.9, 0.572 configList.append(("parentSim", parentSim))73 74 # SELECTION (PARENT SELECTION)75 config.select_parents = parentSel[0]76 # config.select_parents = tournament_selection77 configList.append(("parentSel", parentSel[1]))78 # TOURNAMENT (NO CHANGE)79 config.tournament_k = int(0.2 * config.population_size)80 # CROSSOVER PROBABILITY81 config.prob_crossover = crossoverProb # 0.90, 0.6082 configList.append(("crossoverProb", crossoverProb))83 # CROSSOVER TYPE84 config.crossover = crossoverType[0]85 # config.crossover = two_point_crossover86 # config.crossover =" uniform_crossover87 configList.append(("crossoverType", crossoverType[1]))88 # MUTATION PROBABILITY89 config.prob_mutate = mutationProb # 0.3, 0.590 configList.append(("mutationProb", mutationProb))91 # MUTATION TYPE92 config.mutate = mutationType[0]93 # config.mutate = change_k_values(2)94 # config.mutate = swap_two_values95 configList.append(("mutationType", mutationType[1]))96 ################## CONFIG CHANGES ENDS HERE ########################################97 98 print("\n\nSOLVING: ")99 for item in configList:100 print(str(item[0]) + ": " + str(item[1]))101 solver = GeneticSolver(problem,config)102 solver.solve()103 #Get max config104 currentConfigScore = solver.getBestScore()105 print("SCORE: " + str(currentConfigScore))106 if(currentConfigScore > bestConfigScore):107 bestConfigScore = currentConfigScore108 bestConfigList = configList109 configIterations+=1110 print("\n\n\n\n\nTESTS DONE!" + problem_name)111 print("CONFIGS TESTED: " + str(configIterations))112 print("BEST CONFIGURATION SCORE: " + str(bestConfigScore))113 for item in bestConfigList:114 print(str(item[0]) + ": " + str(item[1]))115if __name__ == '__main__':116 import time117 start = time.time()118 main()119 end = time.time()120 elapsed = end - start...
p4_automated.py
Source:p4_automated.py
1from problem import maxone,knapsack,vertex_cover2from solver.ga import GeneticSolver3from fn.ga import *4from fn.fitness import *5from utils import *6def select_problem(problem_name,config):7 if problem_name == 'maxone':8 problem = maxone.problem(16)9 config.fitness_fn = maxone_fitness10 config.feasibility_minimum = 011 config.best_possible_score = problem.N12 elif problem_name == 'knapsack':13 problem = knapsack.problem(99)14 config.fitness_fn = knapsack_fitness15 config.feasibility_minimum = 10016 config.best_possible_score = problem.total_value17 elif problem_name == 'vertex_cover':18 problem = vertex_cover.problem(99)19 config.fitness_fn = vertex_cover_fitness20 config.feasibility_minimum = 10021 config.best_possible_score = len(problem.variables)22 return problem23def main():24 # Initialization of parameters to be used for automation25 # POPULATION26 population_size = [20, 50]27 replace_population = [(generational, "generational"), (choose_best, "choose_best")]28 # SELECTION29 max_parent_similarity = [0.9, 0.5] 30 select_parents = [(fitness_proportionate, "fitness_proportionate"), (tournament_selection, "tournament_selection")]31 # CROSSOVER32 prob_crossover = [0.90, 0.60] 33 crossover = [(one_point_crossover, "one_point_crossover"), (two_point_crossover, "two_point_crossover"), (uniform_crossover, "uniform_crossover")]34 # MUTATION35 prob_mutate = [0.5, 0.3] 36 mutate = [(change_one_value, "change_one_value"), (change_k_values(2), "change_k_values"), (swap_two_values, "swap_two_values")]37 best_config_list = []38 best_score = 039 # Configuration starts40 config = Config()41 # problem_name = 'maxone'42 # problem_name = 'knapsack'43 problem_name = 'vertex_cover'44 problem = select_problem(problem_name,config)45 config.best_possible_score += config.feasibility_minimum46 config.initial_solution = 'random'47 config.max_parent_try = 50048 config.max_iterations = 20049 config.max_flat_iterations = 5050 config.random_seed = 12345678951 config.explain = False # Initially True. Set to False to avoid lots of printing in the terminal.52 # Automation starts53 count = 054 for populationSize in population_size:55 for replacePopulation in replace_population:56 for maxParentSimilarity in max_parent_similarity:57 for selectParents in select_parents:58 for probCrossover in prob_crossover:59 for Crossover in crossover:60 for probMutate in prob_mutate:61 for Mutate in mutate:62 # POPULATION63 config.population_size = populationSize # 20, 5064 config.replace_population = replacePopulation[0]65 # config.replace_population = choose_best66 # SELECTION67 config.max_parent_similarity = maxParentSimilarity # 0.9, 0.568 config.select_parents = selectParents[0]69 # config.select_parents = tournament_selection70 config.tournament_k = int(0.2 * config.population_size)71 # CROSSOVER72 config.prob_crossover = probCrossover # 0.90, 0.6073 config.crossover = Crossover[0]74 # config.crossover = two_point_crossover75 # config.crossover = uniform_crossover76 # MUTATION77 config.prob_mutate = probMutate # 0.3, 0.578 config.mutate = Mutate[0]79 # config.mutate = change_k_values(2)80 # config.mutate = swap_two_values81 config_list = [82 ("config.population_size",populationSize),83 ("config.replace_population",replacePopulation[1]),84 ("config.max_parent_similarity",maxParentSimilarity),85 ("config.select_parents",selectParents[1]),86 ("config.prob_crossover",probCrossover),87 ("config.crossover",Crossover[1]),88 ("config.prob_mutate",probMutate),89 ("config.mutate",Mutate[1])90 ]91 count+=192 # print(count)93 # print(config_list)94 solver = GeneticSolver(problem,config)95 solver.solve()96 score = solver.get_best_score()97 iterations = solver.get_iterations()98 99 if score >= best_score:100 best_score = score101 best_config_list = config_list102 print(count)103 print(best_config_list)104 print("Score:",score)105 print("Iterations:",iterations)106 print("\n")107 # display_solutions(problem,solver)108 109if __name__ == '__main__':110 import time111 start = time.time()112 main()113 end = time.time()114 elapsed = end - start...
configchanges.py
Source:configchanges.py
1 # POPULATION SIZE2 config.population_size = 20 # 20, 503 4 # POPULATION MODEL5 config.replace_population = generational6 # config.replace_population = choose_best7 # SELECTION (PARENT SIMILARITY)8 config.max_parent_similarity = 0.9 # 0.9, 0.59 10 11 # SELECTION (PARENT SELECTION)12 config.select_parents = fitness_proportionate13 # config.select_parents = tournament_selection14 # TOURNAMENT (NO CHANGE)15 config.tournament_k = int(0.2 * config.population_size)16 # CROSSOVER PROBABILITY17 config.prob_crossover = 0.90 # 0.90, 0.6018 # CROSSOVER TYPE19 config.crossover = one_point_crossover20 # config.crossover = two_point_crossover21 # config.crossover = uniform_crossover22 # MUTATION PROBABILITY23 config.prob_mutate = 0.5 # 0.3, 0.524 # MUTATION TYPE25 config.mutate = change_one_value26 # config.mutate = change_k_values(2)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!