def load(fname, gen_size, new_random, data, mutation_r=.1):
    load_brain = torch.load(fname)['model']
    p1 = car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle)
    p1.brain.load_state_dict(load_brain.state_dict().copy())
    agents = [car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle) for _ in range(gen_size)]
    agents[0] = p1

    for i in range(1, gen_size-new_random):
        agents[i].brain.load_state_dict(p1.brain.state_dict().copy())
        mutate(agents[i].brain, mutation_r)

    return agents
def new_gen(agents, data, gen_size = 1, mutation_r=.05, new_random=0):
    agents.sort(key=lambda x: x.score, reverse=True)
    best_brain = [agents[0].brain, agents[0].score]
    new_agents = []
    made = 0
    for i in range(int((gen_size-new_random)/10)):
        made += 1
        new_agent = car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle)
        new_agent.brain = agents[i].brain
        new_agents.append(new_agent)
        for j in range(int((gen_size-new_random-int((gen_size-new_random)/10))/10)):
            made += 1
            new_agent = car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle)
            new_agent.brain.load_state_dict(agents[i].brain.state_dict().copy())
            mutate(new_agent.brain, mutation_r)
            new_agents.append(new_agent)

    for i in range(gen_size - made):
        new_agents.append(car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle))
    return new_agents, best_brain
def reproduce(p1, p2, data, mutation_r=0):
    new_dict = {}
    w1 = p1.brain.state_dict()
    w2 = p2.brain.state_dict()
    for layer in w1:
        l1 = w1[layer].view(-1)
        l2 = w2[layer].view(-1)
        new_dict[layer] = torch.zeros(len(l1))
        for i in range(len(l1)):
            if np.random.rand() < mutation_r:
                new_dict[layer][i] = random.randrange(1000)/1000
            else:
                #new_dict[layer][i] = random.choice([l1[i],l2[i]]) * (1 + random.randrange(2/mutation_r)*mutation_r - mutation_r)
                new_dict[layer][i] = (l1[i] + l2[i])/2
        new_dict[layer] = new_dict[layer].view(w1[layer].shape)
    ret = car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle)
    ret.brain.load_state_dict(new_dict)
    return ret
Example #4
0
                                                                   '_') + "/"
    if not os.path.exists(brain_dir):
        os.mkdir(brain_dir)

    gameDisplay = pygame.display.set_mode((display_width, display_height))
    clock = pygame.time.Clock()
    pygame.display.set_caption('Car')
    font = pygame.font.SysFont('arialblack', 18)
    show_queue = []  # Queue of car state_dicts to show

    crashed = False
    running_time = 0.0
    batch_floor = 0
    batch_roof = batch_size
    gen = 0
    best_brain = (car_ai_instance().brain, 0.0)

    data = dataloader([roads.road2, roads.road3], 50)
    #agents = [car_ai_instance(pos=data.get_data().pos, angle=data.get_data().angle) for i in range(gen_size)]
    agents = load('brains/gen45s-1238393',
                  gen_size,
                  new_random,
                  data,
                  mutation_r=.1)

    key_pressed = {
        'right': False,
        'left': False,
        'up': False,
        'down': False,
        'space': False
    font = pygame.font.SysFont('arialblack', 18)

    crashed = False
    running_time = 0.0
    gen = 0

    data = dataloader([roads.road2, roads.road3], 50)
    files = os.listdir(dir)

    def get_gen(x):
        s = x.split('s')[0]
        return int(s[3:])

    files.sort(key=get_gen)

    agent = car_ai_instance(pos=data.get_data().pos,
                            angle=data.get_data().angle)
    agent.brain = torch.load(dir + '/' + files[0])['model']
    print('Showing:', files[0])

    key_pressed = {
        'right': False,
        'left': False,
        'up': False,
        'down': False,
        'space': False
    }

    while not crashed:
        gameDisplay.fill(gray)

        agent.tick(clock.get_time() / 1000,