Exemplo n.º 1
0
def watch(configs, gen, num_workers, output_dir, num_survive, fitness_direction):

    dump_dir = output_dir + "/to_master/" + str(gen)
    t_start = time.time()
    popn, num_finished, dir_checks = [], 0,0

    ids = [str(i) for i in range(1, num_workers + 1)]
    while (num_finished < num_workers):
        time.sleep(1)
        dir_checks+=1
        for root, dirs, files in os.walk(dump_dir):
            for f in files:
                if f in ids:
                        if (os.path.getmtime(root + "/" + f) + 1 < time.time()):
                            dump_file = output_dir + "/to_master/" + str(gen) + "/" + str(f)
                            with open(dump_file, 'rb') as file:
                                try:
                                    worker_pop = pickle.load(file)
                                    popn += worker_pop[:num_survive]
                                    num_finished += 1
                                    ids.remove(f)
                                except: pass

            #sort and delete some
            sorted_popn = fitness.eval_fitness(popn, fitness_direction)
            popn = sorted_popn[:num_survive]
            del sorted_popn
    assert (not ids)

    t_end = time.time()
    time_elapsed = t_end - t_start
    if (gen % 100 == 0): util.cluster_print(output_dir,"master finished extracting workers after " + str(time_elapsed) + " seconds, and making " + str(dir_checks) + " dir checks.")

    return popn
Exemplo n.º 2
0
def parse_worker_popn (num_workers, gen, output_dir, num_survive, fitness_direction):
    popn = []
    print('master.parse_worker_popn(): num workers = ' + str(num_workers) + " and gen " + str(gen))
    print("parse worker pop params: dir = " + str(output_dir) + ".")
    for w in range(1,num_workers+1): 
        dump_file = output_dir + "/to_master/" + str(gen) + "/" + str(w)
        with open(dump_file, 'rb') as file:
            worker_pop = pickle.load(file)
        i=0
        for indiv in worker_pop:
            popn.append(indiv)
            i+=1

    sorted_popn = fitness.eval_fitness(popn, fitness_direction)
    return sorted_popn[:num_survive]
Exemplo n.º 3
0
def evolve_minion(worker_file, gen, rank, output_dir):
    t_start = time.time()

    with open(str(worker_file), 'rb') as file:
        worker_ID, seed, worker_gens, pop_size, num_return, randSeed, curr_gen, configs = pickle.load(
            file)
        file.close()

    survive_fraction = float(configs['worker_percent_survive']) / 100
    num_survive = math.ceil(survive_fraction * pop_size)
    output_dir = configs['output_directory'].replace(
        "v4nu_minknap_1X_both_reverse/", '')
    #output_dir += str(worker_ID)
    max_gen = int(configs['max_generations'])
    control = configs['control']
    if (control == "None"): control = None
    fitness_direction = str(configs['fitness_direction'])

    node_edge_ratio = float(configs['edge_to_node_ratio'])

    random.seed(randSeed)
    population = gen_population_from_seed(seed, pop_size)
    start_size = len(seed.net.nodes())
    pressurize_time = 0
    mutate_time = 0

    for g in range(worker_gens):
        gen_percent = float(curr_gen / max_gen)
        if (g != 0):
            for p in range(num_survive, pop_size):
                population[p] = population[p % num_survive].copy()
                #assert (population[p] != population[p%num_survive])
                #assert (population[p].net != population[p % num_survive].net)

        for p in range(pop_size):
            t0 = ptime()
            mutate.mutate(configs, population[p].net, gen_percent,
                          node_edge_ratio)
            t1 = ptime()
            mutate_time += t1 - t0

            if (control == None):
                pressure_results = pressurize.pressurize(
                    configs, population[p].net, None
                )  # false: don't track node fitness, None: don't write instances to file
                population[p].fitness_parts[0], population[p].fitness_parts[
                    1], population[p].fitness_parts[2] = pressure_results[
                        0], pressure_results[1], pressure_results[2]

            else:
                util.cluster_print(
                    output_dir, "ERROR in minion(): unknown control config: " +
                    str(control))

        old_popn = population
        population = fitness.eval_fitness(old_popn, fitness_direction)
        del old_popn
        #debug(population,worker_ID, output_dir)
        curr_gen += 1
    write_out_worker(output_dir + "/to_master/" + str(gen) + "/" + str(rank),
                     population, num_return)

    # some output, probably outdated
    if (worker_ID == 0):
        orig_dir = configs['output_directory']
        end_size = len(population[0].net.nodes())
        growth = end_size - start_size
        output.minion_csv(orig_dir, pressurize_time, growth, end_size)
        #debug(population, worker_ID, output_dir)
        #if (worker_ID==0): util.cluster_print(output_dir,"Pressurizing took " + str(pressurize_time) + " secs, while mutate took " + str(mutate_time) + " secs.")

    t_end = time.time()
    time_elapsed = t_end - t_start
    if (rank == 1 or rank == 32 or rank == 63):
        util.cluster_print(
            output_dir, "Worker #" + str(rank) + " finishing after " +
            str(time_elapsed) + " seconds")
Exemplo n.º 4
0
def sort_popn(population, fitness_direction):
    old_popn = population
    population = fitness.eval_fitness(old_popn, fitness_direction)
    del old_popn
    return population