def main(): # defining initial population pop = toolbox.MST_pop(n=nDisks) # clean up old files if os.path.exists('./stoppingRate.txt'): os.remove('stoppingRate.txt') if os.path.exists('./highEElectrons.txt'): os.remove('highEElectrons.txt') # clean up old result directories shutil.rmtree('runOutput', ignore_errors=True) print('Cleared run output') shutil.rmtree('evalOut', ignore_errors=True) print("Cleared evaluation output") # making new directories if not os.path.exists('./runOutput'): os.mkdir("./runOutput/") if not os.path.exists('./evalOut'): os.mkdir("./evalOut") print("-- Generation 1 --") # creating SIMG4 directory for the first iteration simG4Dir = '/vols/comet/users/sy5118/newBuild/OfflineProject/packages/SimG4/geometry_macros/GA_MST/generated_MST1' if not os.path.exists(simG4Dir): os.makedirs(simG4Dir) j = 0 for mst in pop: baby_MST = MST(nDisksOnAxis, mst, gen=1, id=j) baby_MST.write_MST(simG4Dir, j) j = j + 1 fitnesses = list( map(toolbox.evaluate, np.ones(nDisks), range(1, nDisks + 1))) storage = np.array([pop[1]]) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit # CXPB is the probability with which two individuals # are crossed # # MUTPB is the probability for mutating an individual CXPB, MUTPB = 0.5, 0.2 # Extracting all the fitnesses of fits = [ind.fitness.values[0] for ind in pop] # counter g = 1 # Begin the evolution while max(fits) < 1 and g < 5: g = g + 1 # A new generation simG4Dir = '/vols/comet/users/sy5118/newBuild/OfflineProject/packages/SimG4/geometry_macros/GA_MST/generated_MST' + str( g) if not os.path.exists(simG4Dir): os.makedirs(simG4Dir) print("-- Generation %i --" % g) # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values j = 1 for pop in offspring: baby_MST = MST(nDisksOnAxis, pop, gen=g, id=j) baby_MST.write_MST(simG4Dir, j) pop.fitness.values = toolbox.evaluate(g, j) j = j + 1 # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit pop = offspring # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop]
from k_means import * from convex_hull import * N = 10000 # points quantity sample_N = 100 # points for testing data K = 2 # clusters amount mass = [] edges = [] mst = [] center = [[130, 125], [700, 300]] # test centers of generated clusters gen = data_gen(center, N, K) # data generator dr = data_drawer(K) ch = convex_hull() m = MST(100, K) #mass = gen.normal_gen() #mass = gen.nested_data() mass = gen.get_random_data() # all points #mass = gen.get_real_random_data() #mass = gen.strip_data() sample = mass[:sample_N] smpl = data_gen(center, sample_N, K) smpl.mass = sample s_edges = smpl.get_edges() s_edges.sort() mst = m.get_MST(s_edges) km_obj = k_means(mass, K) #mass = gen.strip_data() #convex = ch.convex_hull(result[0]) #dr.draw_MST(mst, mass)