Ejemplo n.º 1
0
def main():
    global POPULATION_COUNT, ITERATIONS, MAX_FUNC_DEPTH
    file = open('data.txt', 'w')
    if len(sys.argv) == 3: ITERATIONS = int(sys.argv[1])
    if len(sys.argv) == 3: POPULATION_COUNT = int(sys.argv[2])

    f, data = create_test_data()
    write_init_data(file, f, data)

    population = create_population(POPULATION_COUNT, MAX_FUNC_DEPTH)
    normalizator = Normalizator(data, mistake)

    functions = FunctoionContainer(choose_parent_pairs2, choose_to_kill,
                                   create_children)
    options = Options(0.4)
    mutator = Mutator(population, normalizator, options, functions)

    print 'population: '
    #for s in population: print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f
    for i in range(ITERATIONS):
        print 'population ' + str(i) + ' : '
        for j in range(5):
            s = population[(j + 1) * (-1)]
            print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f
            if j == 1:  # ITERATION_NUMBER:MISTAKE:FUNCTION
                write_population_data(file, i, s, mistake(s.f, data))
        mutator.make_children()
        mutator.kill_unused()
        #	mutator.change_random()
        mutator.change_age()
        mutator.mutate()
        normalizator.compute_distribuante(population)
        population.sort()
    file.close()
Ejemplo n.º 2
0
def main():
    global POPULATION_COUNT, ITERATIONS, MAX_FUNC_DEPTH
    file = open('data.txt', 'w')
    if len(sys.argv) == 3: ITERATIONS = int(sys.argv[1])
    if len(sys.argv) == 3: POPULATION_COUNT = int(sys.argv[2])

    f, data = create_test_data()
    write_init_data(file, f, data)

    population = create_population(POPULATION_COUNT, MAX_FUNC_DEPTH)
    normalizator = Normalizator(data, mistake)

    functions = FunctoionContainer(choose_parent_pairs2, choose_to_kill, create_children)
    options = Options(0.4)	
    mutator = Mutator(population, normalizator, options, functions)


    print 'population: '
    #for s in population: print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f
    for i in range(ITERATIONS):
        print 'population ' + str(i) + ' : '
        for j  in range(5):
            s = population[(j+1)*(-1)]
            print 'm: ', mistake(s.f, data), '\t\ts.f: ', s.f
            if j == 1: # ITERATION_NUMBER:MISTAKE:FUNCTION
                write_population_data(file, i, s, mistake(s.f, data))
        mutator.make_children()
        mutator.kill_unused()
    #	mutator.change_random()
        mutator.change_age()
        mutator.mutate()
        normalizator.compute_distribuante(population)
        population.sort()
    file.close()
Ejemplo n.º 3
0
def worker_mutate(solveable, mutateable, log):
    m = Mutator()
    while True:
        fractals = []
        for n in range(8):
            fractals.append(mutateable.get(block=True))
        fractals = m.mutate(fractals)
        for f in fractals:
            solveable.put(f)
Ejemplo n.º 4
0
def mutation_test(file_name, tests):
    status = Status.UNEXPECTED
    local_changes_present = subprocess.call('git diff --quiet {0}'.format(file_name), shell=True)
    if local_changes_present == 1:
        status = Status.SKIPPED
        logging.warning("{0} has local changes, please commit/remove changes before running the test".format(file_name))
    else:
        strategies = list(get_strategies())
        while len(strategies):
            strategy = random.choice(strategies)
            strategies.remove(strategy)
            mutator = Mutator(strategy())
            mutated_line = mutator.mutate(file_name)
            if mutated_line != -1:
                logging.info("Mutated {0} at line {1}".format(file_name, mutated_line))
                logging.info("compiling mutant {0}:{1}".format(file_name, mutated_line))
                test_command = "python mach build --release"
                if subprocess.call(test_command, shell=True, stdout=DEVNULL):
                    logging.error("Compilation Failed: Unexpected error")
                    logging.error("Failed: while running `{0}`".format(test_command))
                    subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True)
                    status = Status.UNEXPECTED
                else:
                    for test in tests:
                        test_command = "python mach test-wpt {0} --release".format(test.encode('utf-8'))
                        logging.info("running `{0}` test for mutant {1}:{2}".format(test, file_name, mutated_line))
                        test_status = subprocess.call(test_command, shell=True, stdout=DEVNULL)
                        if test_status != 0:
                            logging.error("Failed: while running `{0}`".format(test_command))
                            logging.error("mutated file {0} diff".format(file_name))
                            subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True)
                            status = Status.SURVIVED
                        else:
                            logging.info("Success: Mutation killed by {0}".format(test.encode('utf-8')))
                            status = Status.KILLED
                            break
                logging.info("reverting mutant {0}:{1}\n".format(file_name, mutated_line))
                subprocess.call('git checkout {0}'.format(file_name), shell=True)
                break
            elif not len(strategies):
                # All strategies are tried
                logging.info("\nCannot mutate {0}\n".format(file_name))
    return status
Ejemplo n.º 5
0
def review():
    seed_from_form = request.form.get('seed_file')
    seed_from_url = request.args.get('seed_file')

    if seed_from_form:
        seed_file = seed_from_form
    elif seed_from_url:
        seed_file = seed_from_url
    else:
        raise AttributeError(
            'You must specify the seed file via POST or GET parameter.')

    # alter to allow submittal of seed files that already have relative path
    file_handler = FileHandler()
    seed_file_with_full_path = file_handler.find_seed_file_on_disk()
    seed_file_with_relative_path = file_handler.full_to_relative_path(
        seed_file_with_full_path)

    seed_melody = file_handler.filename_to_list(filename=seed_file)
    i = 0
    created_melodies = []
    created_files = []
    output_directory = file_handler.setup_output_directory()
    output_directory_seed_folder = os.path.join(output_directory, 'seed')
    shutil.copy2(seed_file_with_full_path, output_directory_seed_folder)
    mutator = Mutator(seed_melody=seed_melody, mutation_percentage=5)
    while i < 10:
        mutated_melody = mutator.mutate()
        if mutated_melody != seed_melody and mutated_melody not in created_melodies:
            midi_maker = MidiMaker(output_directory=output_directory,
                                   melody=mutated_melody)
            mutated_file = midi_maker.write()
            created_melodies.append(mutated_melody)
            created_files.append(
                file_handler.full_to_relative_path(mutated_file))
            i += 1
    # print(created_files)

    return render_template(
        'review.html',
        seed_file_no_path=seed_file,
        seed_file_with_relative_path=seed_file_with_relative_path,
        created_files=created_files)
Ejemplo n.º 6
0
def mutation_test(file_name, tests):
    status = Status.UNEXPECTED
    local_changes_present = subprocess.call('git diff --quiet {0}'.format(file_name), shell=True)
    if local_changes_present == 1:
        status = Status.SKIPPED
        logging.warning("{0} has local changes, please commit/remove changes before running the test".format(file_name))
    else:
        strategies = list(get_strategies())
        while len(strategies):
            strategy = random.choice(strategies)
            strategies.remove(strategy)
            mutator = Mutator(strategy())
            mutated_line = mutator.mutate(file_name)
            if mutated_line != -1:
                logging.info("Mutated {0} at line {1}".format(file_name, mutated_line))
                logging.info("compiling mutant {0}:{1}".format(file_name, mutated_line))
                if subprocess.call('python mach build --release', shell=True, stdout=DEVNULL):
                    logging.error("Compilation Failed: Unexpected error")
                    status = Status.UNEXPECTED
                else:
                    for test in tests:
                        test_command = "python mach test-wpt {0} --release".format(test.encode('utf-8'))
                        logging.info("running `{0}` test for mutant {1}:{2}".format(test, file_name, mutated_line))
                        test_status = subprocess.call(test_command, shell=True, stdout=DEVNULL)
                        if test_status != 0:
                            logging.error("Failed: while running `{0}`".format(test_command))
                            logging.error("mutated file {0} diff".format(file_name))
                            subprocess.call('git --no-pager diff {0}'.format(file_name), shell=True)
                            status = Status.SURVIVED
                        else:
                            logging.info("Success: Mutation killed by {0}".format(test.encode('utf-8')))
                            status = Status.KILLED
                            break
                logging.info("reverting mutant {0}:{1}\n".format(file_name, mutated_line))
                subprocess.call('git checkout {0}'.format(file_name), shell=True)
                break
            elif not len(strategies):
                # All strategies are tried
                logging.info("\nCannot mutate {0}\n".format(file_name))
    return status
Ejemplo n.º 7
0
class EvoAgent(object):
    def __init__(self):

        self.benchmark_agents = []
        self.agents = []
        self.epoch_results = []

        if not os.path.exists(AGENT_DIR):
            os.makedirs(AGENT_DIR)

    '''
    Run by the main function to produce the top agents.
    Function that runs NUM_EPOCHS epochs and writes the parameter results of the 
    top three evolved agents to file
    '''

    def produce_agents(self, nagents):
        print "Producing %d top agents of evolution:\n\
                %d epochs\n\
                %d agents\n\
                %d players per game group\n\
                coevolution: %d\n"                                   % \
                (nagents, NUM_EPOCHS, NUM_AGENTS, NUM_GAME_PLAYERS, COEVOLVE)
        sys.stdout.flush()

        self.run_epochs(TO_MUTATE, TO_KEEP)

        # get the parameters of the top agent
        for i in xrange(min(nagents, TO_KEEP)):
            print "Top agent %d ID: %s" % (i, self.top_agents[i])
        sys.stdout.flush()

    '''
    Runs num_epoch epochs, each of which plays num_game games.
    After the end of each epoch, agents are ranked in order of their success in winning games.

    The top to_mutate agents are then mutated/crossed-over to generate the next population of 
    agents for the next epoch. The top to_keep agents from each generation are kept in the next generation
    as benchmarks for the next generation and to ensure that the evolution never loses progress.
    '''

    def run_epochs(self, to_mutate, to_keep):
        self.init_agents()
        for i in xrange(NUM_EPOCHS):
            # set the gameplaying groups
            self.init_agent_gameplaying_groups()

            # maps from player to list player scores in games played during
            # the epoch
            self.epoch_results = {}

            p = Pool(args.nthreads)
            all_game_scores = p.map(play_epoch, self.game_groups)

            # get the results of each epoch for each gameplaying group
            # don't record scores for benchmark
            for game_scores in all_game_scores:
                for player, scores in game_scores.iteritems():
                    if player != "benchmark":
                        self.epoch_results[player] = self.epoch_results.get(
                            player, []) + scores

            # sort the agents in order of rank
            self.agents = self.rank_agents()

            # get which agents to mutate
            self.mutate_agents = self.agents[:TO_MUTATE]
            # mutate the agents for the next generation
            self.mutator = Mutator(self.mutate_agents, AGENT_DIR)
            NUM_AGENTS_in_mutate_groups = (NUM_AGENTS - TO_KEEP) / 3
            crossovers = self.mutator.crossover(NUM_AGENTS_in_mutate_groups)
            mutated = self.mutator.mutate(NUM_AGENTS_in_mutate_groups)
            combinations = self.mutator.combo(NUM_AGENTS - TO_KEEP -
                                              2 * NUM_AGENTS_in_mutate_groups)

            # get the top to_keep agents
            self.top_agents = self.agents[:TO_KEEP]

            for aid in self.top_agents:
                print "---------------------------------------\n"
                print "Evaluating top agent %s from epoch %d: " % (aid, i)
                print "\tTotal Winnings: %d" % sum(self.epoch_results[aid])
                print "\tGames Won: %d/%d" % (len(
                    filter(lambda x: x >= 0, self.epoch_results[aid])),
                                              len(self.epoch_results[aid]))
                sys.stdout.flush()

            # set the agents for the next epoch
            self.agents = crossovers + mutated + combinations + self.top_agents
            self.epoch_results = {}

    '''
    Sorts agents based upon the results from the epoch 
    Top agents are placed first
    '''

    def rank_agents(self):
        sum_scores = []
        for aid, scores in self.epoch_results.iteritems():
            sum_scores.append((sum(scores), aid))
            sum_scores.sort(reverse=True)
        sorted_list = [aid for (_, aid) in sum_scores]
        return sorted_list

    ''' 
    Sets self.agents to be the initial list of agents as a dictionary
    mapping from an agent ID to the agent parameters
    '''

    def init_agents(self):
        self.agents = []
        noise = 0.001
        for i in xrange(NUM_AGENTS):
            aid = str(uuid.uuid4())
            initial_params = []
            with np.load(os.path.join(AGENT_DIR,
                                      "initial-poker-params.npz")) as data:
                for i in range(len(data.keys())):
                    initial_params.append(data["arr_%d" % i] +
                                          np.random.normal(0, noise, 1)[0])
            agent_params = Params(aid=aid,
                                  agent_dir=AGENT_DIR,
                                  params_list=initial_params)
            self.agents.append(aid)

    '''
    Inits the game_groups list of lists indicating (by agent ID) which agents
    are playing which other agents

    If not coevolving the agents, game_groups indicates (by agent ID) the agent 
    in the population that is playing the game
    '''

    def init_agent_gameplaying_groups(self):
        agent_game_groups = []

        if COEVOLVE:
            agents_indices = range(len(self.agents))

            for _ in range(4):
                random.shuffle(agents_indices)
                i = 0
                while (i < len(self.agents)):
                    group = []
                    for _ in xrange(NUM_GAME_PLAYERS):
                        group.append(self.agents[i])
                        i += 1
                    agent_game_groups.append(group)
        else:
            for agent in self.agents:
                agent_game_groups.append([agent])
                # the other agents will be benchmark agent programs
        self.game_groups = agent_game_groups