示例#1
0
def run():
    print("-----------------------------------------")
    print("local ssd")
    ssd = "X:/cam_front_center"
    process_folder(ssd)  # on a local ssd.
    print("-----------------------------------------")
    print("nas")
    with f.timing("get camera folders", units=1):
        folders, jsons, labels = f.get_camera_folders(f.get_path(),
                                                      root=f.get_root())
    print(len(folders), " camera folders")
    for folder in folders:
        process_folder(folder)
        if True:
            break
    print("-----------------------------------------")
    [print("local ssd")]
    process_folder(ssd)  # on a local ssd.ls ..

    print("-----------------------------------------")
    print("just read some files in a camera folder.")
    print("nas")
    with f.timing("get camera folders", units=1):
        folders, jsons, labels = f.get_camera_folders(f.get_path(),
                                                      root=f.get_root())
    print(len(folders), " camera folders")
    for folder in folders:
        process_folder(folder)
        if True:
            break
示例#2
0
def theme_path(request):
    smilies = Smilie.objects.all()
    return {'STATIC_THEME':settings.STATIC_URL+get_path(request.user)
            ,'BASE_PATH':get_path(request.user)+'base.html'
            ,'CURRENT_SITE':get_current_site(request)
            ,'THEME_PATH':get_path(request.user)
            ,'smilies':smilies}
示例#3
0
    def move_piece(self, piece, end):
        start = (piece.position[0], piece.position[1])

        # Check if selected move is a vertical, horizontal, or diagonal move.
        if val_end_pos(piece.position, end):

            # Check if what is being moved is actually a playable piece.
            if self.board[start[0]][start[1]] != None or self.board[start[0]][
                    start[1]] != 0:

                path = get_path(start, end)

                # Validate every square along the path the piece will move
                if self.validate_path(path) == True:

                    # Swap piece on board with empty square.
                    self.board[start[0]][start[1]], self.board[end[0]][
                        end[1]] = self.board[end[0]][end[1]], self.board[
                            start[0]][start[1]]

                    # Increase counter of total moves performed.
                    self.board[end[0]][end[1]].moves_performed += 1

        piece.position = end
        self.increment_time()
    def save_settings(self, env, agent, multi_agent=False, curricular=None):
        pathname = fc.get_path()

        # get settings from env and agent
        input_sequence_length, kind_cars, num_lines, capacity_lines, output_sequence_length, input_window_length = env.get_stats(
        )
        buffer_size, batch_size, update_every, gamma, tau, lr = agent.get_stats(
        )

        # check if folder exists. create it otherwise

        f = open(
            "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP_NoVal/test.settings",
            "a")
        f.write("Gamma " + str(self.gamma) + "\n")
        f.write("Buffer Size " + str(buffer_size) + "\n")
        f.write("Batch Size " + str(batch_size) + "\n")
        f.write("Update Every " + str(update_every) + "\n")
        f.write("Tau " + str(tau) + "\n")
        f.write("LR " + str(lr) + "\n")

        f.write("Kind cars " + str(self.kind_cars) + "\n")
        f.write("Input length " + str(input_sequence_length) + "\n")
        f.write("Output length " + str(output_sequence_length) + "\n")
        f.write("Capacity Lines " + str(capacity_lines) + "\n")
        f.write("Num Lines " + str(num_lines) + "\n")
        if multi_agent:
            f.write("Multi Agent\n")
        if curricular:
            f.write("Curricular - Front and Back same settings")

        f.close()
示例#5
0
def check_age(result, log_name, log_path, loginfo):
    directory_path = get_path()
    for filename, saved in loginfo[log_name + "-saved"].items():
        if (saved + 2592000) < time():
            remove((directory_path + log_path + filename))
        else:
            result[log_name + "-saved"][filename] = saved
    return result
示例#6
0
文件: mt0.py 项目: tayek/ss1
def get_files(units=None):
    images, jsons, labels = bf.get_camera_folders(bf.get_path(),
                                                  root=bf.get_root())
    print(len(images), ' folders for images')
    l = []
    for folder in images:
        l = get_pngs(folder)
        break  # just do one now.
    folder = images[0]
    l = l if units is None else l[:units]
    return l
 def __init__(self, checkpoint_name, kind_cars, gamma):
     self.kind_cars = kind_cars
     self.gamma = gamma
     self.best_score = -float("inf")
     pathname = fc.get_path()
     counter = 0
     dirname = checkpoint_name + "_" + str(counter)
     while dirname in os.listdir(pathname + '/results'):
         counter += 1
         dirname = checkpoint_name + "_" + str(counter)
     self.checkpoint_name = dirname
     self.run_number = counter
示例#8
0
def read_existing_notes():
    os.system('clear')
    path = functions.get_path()
    notes_list = functions.get_notes(path)
    for note in notes_list:
        print(note)
    choice = functions.get_choice()
    note = functions.get_the_note(choice, notes_list)
    page = functions.open_note(path, note)
    os.system('clear')
    functions.reading(page)
    reading_options(page)
    functions.save_and_close_note(page)
示例#9
0
def load_test_sequences(name):
    pathname = fc.get_path()

    f = open(pathname + 'testsequences/' + name + ".testsequence", "r")
    dim = f.readline().split()
    rows = int(dim[0])
    cols = int(dim[1])
    test_sequences = np.zeros((rows * cols), int)
    for i, line in enumerate(f):
        test_sequences[i] = int(line)
    f.close()
    test_sequences = test_sequences.reshape((rows, cols))
    test_sequences = torch.tensor(test_sequences).long()

    return test_sequences
示例#10
0
文件: usetff.py 项目: tayek/ss1
def run():
    print("--------------------------------------------")
    x,y,z=f.get_camera_folders(f.get_path(),root=f.get_root())
    title="cars by folder:"
    for i,folder in enumerate(x): 
        files=f.get_files(folder,"*.png")
        units=len(files)
        print(i,"folder: "+folder,"has:",len(files),"files.")
        with f.timing("folder: "+folder+" ["+str(i)+"] has: "+str(len(files))+" files.",units,title):
            ds=tff.make_tensor_slices_dataset_list(files) 
    print("----------------------------------------------")  
    for i,folder in enumerate(x): 
        files=f.get_files(folder,"*.png")
        units=len(files)
        print(i,"folder: "+folder,"has:",len(files),"files.")
        with f.timing("folder: "+folder+" ["+str(i)+"] has: "+str(len(files))+" files.",units,title):
            ds=tff.make_tensor_slices_dataset_list(files)   
            mapped=ds.map(tff.parse1and,tff.autotune)
            tff.do_enumeration(mapped,parse2=tff.write_file)
    print("--------------------------------------------")
示例#11
0
def create_test_sequences(number, length, KIND_CARS):
    name = 'N:' + str(number) + '_L:' + str(length) + '_KC:' + str(KIND_CARS)
    pathname = fc.get_path() + 'testsequences/'
    content = os.listdir(pathname)

    i = 1
    new_name = name
    while new_name + '.testsequence' in content:
        new_name = name + '_' + str(i)
        i += 1

    f = open(pathname + '/' + new_name + ".testsequence", "w")
    f.write(str(number) + ' ' + str(length) + "\n")

    for _ in range(number * length):
        f.write(str(random.randint(0, KIND_CARS - 1)) + "\n")

    f.close()

    return new_name
示例#12
0
        print("length is correct")
    #missing=set(['L:/a2d2/camera_lidar_semantic/20181016_095036/camera/cam_front_center','L:/a2d2/camera_lidar_semantic/20181204_191844/camera/cam_front_center'])
    missing_ = [
        '20181016_095036/camera/cam_front_center',
        '20181204_191844/camera/cam_front_center'
    ]
    missing = [join(path, part) for part in missing_]
    print("we expect problems with:", missing)
    # L:/a2d2/camera_lidar_semantic/20181016_095036/camera/cam_front_center
    # L:/a2d2/camera_lidar_semantic/20181204_191844/camera/cam_front_center
    return get_files_from_camera_folders(
        (camera_folders, ))  # do not remove the ','


root = f.get_root()
path = f.get_path()
(filename, image_filename, json_filename,
 label_filename) = f.get_my_filenames()
image_filename = filename
actual = []
if os.path.exists(image_filename):
    print(image_filename + " exists")
    print("reading:", image_filename)
    actual = f.read_filenames_from_disk(image_filename)
else:
    print(image_filename + " does not exist.")
    print("examining camera folder for files")
    good = get_filenames_from_camera_folders(path, root=root)
    print(type(good), len(good), "file names")
    print(good[0])
    images = good
示例#13
0
                bot.sendMessage(
                    user, json_answers["notification_cam"].format(
                        json_answers["cams"][int(notification)]))
                take_snapshot(bot, user, int(notification))
    jsonfile["notify"] = {}
    dump_json_file("allowed_users.json", jsonfile)


## SATRTUP FUNCTION (well, better call it code than function)
if __name__ == "__main__":
    config_log = load_json_file("config.json")
    bot = telepot.Bot(config_log["telegram-token"])

    json_file = load_json_file("lang-" + config_log["main-language"] + ".json")
    json_answers = json_file["answers"]
    json_commands = json_file["commands"]

    ph = PlayListHandler(get_path() + "playlists.json")
    ph.main_folder = config_log["music-folder"] + "/"

    #STARTS BOT, AND INFINITE LOOP TO KEEP IT RUNNING
    MessageLoop(bot, handle).run_as_thread()

    while True:
        for _ in range(10):
            sleep(1)
            ph.update()

        send_notifications()
        ph.save_playlists()
示例#14
0
文件: dscat.py 项目: tayek/ss1
with f.timing("import tensorflow",1):
    import tensorflow as tf
print(tf.__version__)
import tffunctions as tff
x,y,z=f.get_lists_of_filenames()
print("got (",len(x),len(y),len(z),") files.",flush=True)
first=x[0]
print("first file:",first,os.path.exists(first))
path=f.path_head(first)
filename=f.path_leaf(first)
print(path,filename)
maybe='L:/ss/sem/20180807_145028/camera/cam_front_center/20180807145028_camera_frontcenter_000000091.png'
if maybe==first:
    print("maybe =")
print("maybe file:",maybe,os.path.exists(maybe))
x,y,z=f.get_camera_folders(f.get_path(),root=f.get_root())
title="cars by folder:"
    #with f.timing("folder: "+folder+" ["+str(i)+"] has: "+str(len(files))+" files.",units,title):
    #    ds=tff.make_tensor_slices_dataset_list(files) 
filesets=f.getFilesets(x,"*.png")
print(len(filesets),"filesets.")
with f.timing("make datasets from filename sets.",len(x),title):
    datasets=tff.makeDatasets(filesets)
print(len(datasets),"datasets, type:",type(datasets))
tff.time_enumeration(datasets,units=len(datasets)) # was 1892?
# this will make the 44 datasets, one for each camera folder
# naybe this is enough for this file.
exit()
print("---------------------------------------")
def concat_datasets(datasets):
    ds0 = tf.data.Dataset.from_tensors(datasets[0])
示例#15
0
# https://adventofcode.com/2019/day/3
from functions import get_path, fewest_steps_intersection

wires = open('input').read().splitlines()
paths = [{}, {}];

# wires = [
# 	'R8,U5,L5,D3',
# 	'U7,R6,D4,L4'
# ]

for wire in range(len(wires)):
	instructions = wires[wire].split(',')
	paths[wire] = get_path(instructions)

fewest_steps = fewest_steps_intersection(paths[0], paths[1])

print('Fewest steps: {}'.format(fewest_steps))
示例#16
0
文件: add.py 项目: MountainWhale/warp
import os
import sqlite3
import click
import termcolor as tc
from functions import get_path

path = get_path()
db = sqlite3.connect(os.path.realpath(path))
cursor = db.cursor()


@click.command()
@click.argument('name', default='thing')
def add(name):
    try:
        cursor.execute("INSERT INTO warp_points (name, path) VALUES (?, ?)",
                       (name, os.getcwd()))
        db.commit()
        print(tc.colored(f"Successfully added {name} warp point", "cyan"))
    except:
        print(tc.colored(f"{name} warp point already exists", "red"))


if __name__ == "__main__":
    add()
示例#17
0
    others=list(set(maybies)-set(files))
    printffo(files,folders,others)
    return (files,folders,others)
def printijo(imageFiles,jsonFiles,otherFiles):
    print("image_files:",imageFiles[:3])
    print("json_files:",jsonFiles[:3])
    print("others:",otherFiles[:3])
def getijo(l:list): # images, jsons, others - should only be given files.
    imageFiles=[e for e in l if '.png' in e]
    maybeFiles=list(set(l)-set(imageFiles))
    jsonFiles=[e for e in maybeFiles if '.json' in e]
    otherFiles=list(set(maybeFiles)-set(jsonFiles))
    if len(maybeFiles)!=0: print("maybies:",maybeFiles[:3])
    printijo(imageFiles,jsonFiles,otherFiles)
    return imageFiles,jsonFiles,otherFiles
path=op.join(f.get_root(),f.get_path()) # these will be sorted
(folders,y,z)=f.get_camera_folders(path)
print('--------------------------')
f.ppl0(folders,name="folders from get camera folders",print_=True)
print('--------------------------')
files,folders,others=getffo(path)
print('--------------------------')
for folder in folders:
    (files2,folders2,others2)=getffo(op.join(path,folder))
    for folder2 in folders2:
        print("process folder:",folder2)
        files3,folders3,others3=getffo(op.join(path,folder,folder2)) # may need camera or lifar sun directory?
        for folder3 in folders3:
            print("process folder:",folder2)
            files4,folders4,others4=getffo(op.join(path,folder,folder2,folder3)) # may need camera or lifar sun directory?
            (imageFiles,jsonFiles,otherFiles)=getijo(files4)
示例#18
0
    def train(self, env, agent, checkpoint_length, n_episodes=8000, eps_start=1.0, eps_end=0.0001,
                 eps_decay=0.999, show_picture = False, valid_actions_only = False, load_param = False):

        # initialise saving spots for better reading in the code
        run_number = self.save_settings(env, agent)
        pathname = fc.get_path()

        best_score_weights_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP/bestscore_32.pth"
        weights_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP/weights_32.pth"
        scores_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP/scores.txt"
        best_score_optim_file = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP/bestscore_optim_32.pth"


        # status variables and plots
        scores = []  # list containing scores from each episode
        means = []
        scores_window = deque(maxlen=checkpoint_length)  # last 100 scores
        eps = eps_start  # initialize epsilon

        episode = 1
        if load_param :
            checkpoint = torch.load("/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP/bestscore_32_c.pth")
            eps = checkpoint['eps']
            episode = checkpoint['episode']
            agent.qnetwork_local.load_state_dict(checkpoint['model_state_dict'])
            agent.optimizer.load_state_dict(checkpoint['optim_state_dict'])
            agent.qnetwork_local.train()

        for i_episode in range(1, n_episodes + 1):
            env.reset(i_episode)
            state = env.get_state()
            state = self.linearize(state)
            score = 0
            option_lengths = {opt:[] for opt in range(NUM_OPTIONS)} 
            greedy_option  = agent.greedy_option(state)
            current_option = 0
            option_termination = True
            curr_op_len = 0
            done = False
            
            counter = 0
            while not done:
                if option_termination:
                    option_lengths[current_option].append(curr_op_len)
                    current_option = np.random.choice(NUM_OPTIONS) if np.random.rand() < eps else greedy_option
                    curr_op_len = 0
                action, logp, entropy = agent.act(state, eps, current_option, eval_mode = valid_actions_only, pa = env.possible_actions())
                #action, logp, entropy = agent.act(state, eps, current_option, eval_mode = valid_actions_only)
                reward, next_state, done = env.step(action) # send the action to the environment and observe
                next_state = self.linearize(next_state)
                agent.step(state, current_option, reward, next_state, done, logp, entropy)
            
                state = next_state
                score += reward
                option_termination, greedy_option = agent.predict_option_termination(state, current_option)
                if done:
                    break
                    
            scores_window.append(score)
            scores.append(score)
            eps = max(eps_end, eps_decay * eps)
            means.append(np.mean(scores_window))
            if i_episode % checkpoint_length == 0:

                score = np.mean(scores_window)
                # if current score is better, save the network weights and update best seen score
                if score > self.best_score:
                    self.best_score = score
                    torch.save({ 'episode' : i_episode,
                                 'eps' : eps,
                                'model_state_dict' : agent.qnetwork_local.state_dict(),
                                'optim_state_dict' : agent.optimizer.state_dict()
                        }, best_score_weights_filename)
                    #torch.save(agent.qnetwork_local.state_dict(), best_score_weigths_filename)
                    #torch.save(agent.optimizer.state_dict(),best_score_optim_file)
                #if show_picture:
                
                #    clear_output(wait=True)
                    #plt.scatter(range(len(scores)), scores, label='Scores', color='c', alpha=0.8, s=1)
                #    plt.plot(np.arange(len(means)), means, label='Mean', color='r')
                #    plt.ylabel('Score')
                #    plt.xlabel('Episode #')
                #    plt.show()
                print('\rEpisode {}\tAverage Score: {:.2f}\tBest Score: {:.2f}'.format(i_episode, score, self.best_score))

                f = open(scores_filename, "a")
                for score in scores_window:
                    f.write(str(score) + '\n')
                f.close()

        torch.save(agent.qnetwork_local.state_dict(), weights_filename)
        return scores
    def curricular_train(self,
                         env,
                         back_agent,
                         front_agent,
                         n_iterations,
                         checkpoint_length,
                         n_episodes=8000,
                         eps_start=1.0,
                         eps_end=0.0001,
                         eps_decay=0.999,
                         valid_actions_only=False):

        self.save_settings(env, back_agent, curricular='Back')
        self.save_settings(env, front_agent, curricular='Front')

        pathname = fc.get_path()

        ## Wrapping training of one learning agent against fixed wagent ##
        def train(learning_agent, wrapper, wrapper_first,
                  best_score_weights_file, weights_file, scores_file, itr):

            agent_name = 'Back' if 'Back' in weights_file else 'Front'

            # if we train the back agent, a1 should be None, because back agent starts.
            ###  maw = MAW(env, None, wrapper)
            # if we train the front agent, a2 should be None, for the same reason
            ### maw = MAW(env, wrapper, None)
            a1, a2 = [wrapper, None][::wrapper_first]
            maw = MAW(env, a1, a2)

            # status variables and plots
            scores = []  # list containing scores from each episode
            means = []
            scores_window = deque(maxlen=checkpoint_length)  # last 100 scores
            eps = eps_start  # initialize epsilon

            # flush the replay buffer of previous training experiences
            learning_agent.reset_memory()

            for i_episode in range(1, n_episodes + 1):
                maw.reset()
                state = maw.get_state()
                state = self.linearize(state)
                score = 0
                done = False
                while not done:
                    action = learning_agent.act(state,
                                                eps,
                                                eval_mode=valid_actions_only,
                                                pa=env.possible_actions())
                    reward, next_state, done = maw.step(
                        action
                    )  # send the action to the environment and observe
                    next_state = self.linearize(next_state)
                    learning_agent.step(state, action, reward, next_state,
                                        done)
                    state = next_state
                    score += reward
                    if done:
                        break
                scores_window.append(score)
                scores.append(score)
                eps = max(eps_end, eps_decay * eps)

                means.append(np.mean(scores_window))

                if i_episode % checkpoint_length == 0:

                    score = np.mean(scores_window)

                    # if current score is better, save the network weights and update best seen score
                    if score > self.best_score:
                        self.best_score = score
                        torch.save(learning_agent.qnetwork_local.state_dict(),
                                   best_score_weights_file)
                    print(
                        '\rCC Iteration {} ({}) Episode {}\tAverage Score: {:.2f}\tBest Score: {:.2f}'
                        .format(itr, agent_name, i_episode, score,
                                self.best_score))

                    f = open(scores_file, "a")
                    for score in scores_window:
                        f.write(str(score) + '\n')
                    f.close()

            torch.save(learning_agent.qnetwork_local.state_dict(),
                       weights_file)

        ## Perform Curricular Training ##

        # store scores of all iteration as list of tuples [(score_back_1,score_back_2), ..., (score_back_niter, score_front_niter)]
        all_scores = []

        # initially use a random front agent
        front_wrapper = fc.RandomPlayer()

        for iteration in range(1, n_iterations + 1):

            # filenames for this iteration
            best_score_weights_filename_front = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + "_ITR:{itr}_Front_highScore.pth".format(
                itr=iteration)
            weights_filename_front = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_ITR:{itr}_Front.pth'.format(
                itr=iteration)

            best_score_weights_filename_back = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_ITR:{itr}_Back_highScore.pth'.format(
                itr=iteration)
            weights_filename_back = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_ITR:{itr}_Back.pth'.format(
                itr=iteration)

            scores_filename_back = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_ITR:{itr}_Back.scores'.format(
                itr=iteration)
            scores_filename_front = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_ITR:{itr}_Front.scores'.format(
                itr=iteration)

            # train back with fixed front
            scores_back = train(back_agent, front_wrapper, 1,
                                best_score_weights_filename_back,
                                weights_filename_back, scores_filename_back,
                                iteration)
            back_wrapper = fc.Agent_wrapper(back_agent.qnetwork_local,
                                            env.kind_cars)

            # train front with fixed back
            scores_front = train(front_agent, back_wrapper, -1,
                                 best_score_weights_filename_front,
                                 weights_filename_front, scores_filename_front,
                                 iteration)
            front_wrapper = fc.Agent_wrapper(front_agent.qnetwork_local,
                                             env.kind_cars)

            all_scores.append((scores_back, scores_front))

        return all_scores
    def train(self,
              env,
              agent,
              checkpoint_length,
              n_episodes=8000,
              eps_start=1.0,
              eps_end=0.0001,
              eps_decay=0.999,
              show_picture=False,
              valid_actions_only=False):

        # initialise saving spots for better reading in the code
        run_number = self.save_settings(env, agent)
        pathname = fc.get_path()

        #best_score_weigths_filename = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name  + '_highScore.pth'
        #weights_filename = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name +  '.pth'
        #scores_filename = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '.scores'

        best_score_weights_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP_NoVal/bestscore_32.pth"
        weights_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP_NoVal/weights_32.pth"
        scores_filename = "/media/shadowwalker/DATA/study/RIL1/code/carmanufacturing/test/results/CP_NoVal/scores.txt"

        # status variables and plots
        scores = []  # list containing scores from each episode
        means = []
        scores_window = deque(maxlen=checkpoint_length)  # last 100 scores
        eps = eps_start  # initialize epsilon

        for i_episode in range(1, n_episodes + 1):
            env.reset(i_episode)
            state = env.get_state()
            state = self.linearize(state)
            score = 0
            done = False
            while not done:
                action = agent.act(state,
                                   eps,
                                   eval_mode=valid_actions_only,
                                   pa=env.possible_actions())
                reward, next_state, done = env.step(
                    action)  # send the action to the environment and observe
                next_state = self.linearize(next_state)
                agent.step(state, action, reward, next_state, done)
                state = next_state
                score += reward
                if done:
                    break
            scores_window.append(score)
            scores.append(score)
            eps = max(eps_end, eps_decay * eps)

            means.append(np.mean(scores_window))

            if i_episode % checkpoint_length == 0:

                score = np.mean(scores_window)

                # if current score is better, save the network weights and update best seen score
                if score > self.best_score:
                    self.best_score = score
                    torch.save(agent.qnetwork_local.state_dict(),
                               best_score_weights_filename)
                if show_picture:
                    clear_output(wait=True)
                    #plt.scatter(range(len(scores)), scores, label='Scores', color='c', alpha=0.8, s=1)
                    plt.plot(np.arange(len(means)),
                             means,
                             label='Mean',
                             color='r')
                    plt.ylabel('Score')
                    plt.xlabel('Episode #')
                    plt.show()
                print(
                    '\rEpisode {}\tAverage Score: {:.2f}\tBest Score: {:.2f}'.
                    format(i_episode, score, self.best_score))

                f = open(scores_filename, "a")
                for score in scores_window:
                    f.write(str(score) + '\n')
                f.close()

        torch.save(agent.qnetwork_local.state_dict(), weights_filename)
        return scores
    def multi_train(self,
                    env,
                    agent_front,
                    agent_back,
                    checkpoint_length,
                    n_episodes=8000,
                    eps_start=1.0,
                    eps_end=0.0001,
                    eps_decay=0.999,
                    show_picture=False,
                    valid_actions_only=False):
        self.save_settings(env, agent_front, multi_agent=True)
        pathname = fc.get_path()

        best_score_weigths_filename_front = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + "_Front_highScore.pth"
        weights_filename_front = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_Front.pth'

        best_score_weigths_filename_back = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_Back_highScore.pth'
        weights_filename_back = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '_Back.pth'

        scores_filename = pathname + "/results/" + self.checkpoint_name + "/" + self.checkpoint_name + '.scores'

        scores = []  # list containing scores from each episode
        means = []
        scores_window = deque(maxlen=checkpoint_length)  # last 100 scores
        eps = eps_start  # initialize epsilon

        # init figure
        fig = plt.figure()
        # ax = fig.add_subplot(111)

        for i_episode in range(1, n_episodes + 1):
            env.reset()
            state = env.get_state()
            score = 0
            done = False
            while not done:
                state = self.linearize(env.get_state())
                player = env.get_player()
                if player == 1:
                    action = agent_front.act(state,
                                             eps,
                                             eval_mode=valid_actions_only,
                                             pa=env.possible_actions())
                else:
                    action = agent_back.act(state,
                                            eps,
                                            eval_mode=valid_actions_only,
                                            pa=env.possible_actions())

                reward, next_state, done = env.step(
                    action)  # send the action to the environment and observe
                next_state = self.linearize(next_state)

                if player == 1:
                    agent_front.step(state, action, reward, next_state, done)
                else:
                    agent_back.step(state, action, reward, next_state, done)

                score += reward
                if done:
                    break

            scores_window.append(score)
            scores.append(score)
            eps = max(eps_end, eps_decay * eps)

            means.append(np.mean(scores_window))

            if i_episode % checkpoint_length == 0:

                score = np.mean(scores_window)

                # if current score is better, save the network weights and update best seen score
                if score > self.best_score:
                    self.best_score = score
                    torch.save(agent_front.qnetwork_local.state_dict(),
                               best_score_weigths_filename_front)
                    torch.save(agent_back.qnetwork_local.state_dict(),
                               best_score_weigths_filename_back)
                if show_picture:
                    plt.scatter(range(len(scores)),
                                scores,
                                label='Scores',
                                color='c',
                                alpha=0.8,
                                s=1)
                    plt.plot(np.arange(len(means)),
                             means,
                             label='Mean',
                             color='r')
                    plt.ylabel('Score')
                    plt.xlabel('Episode #')
                    plt.show()
                print(
                    '\rEpisode {}\tAverage Score: {:.2f}\tBest Score: {:.2f}'.
                    format(i_episode, score, self.best_score))

                f = open(scores_filename, "a")
                for score in scores_window:
                    f.write(str(score) + '\n')
                f.close()

        torch.save(agent_front.qnetwork_local.state_dict(),
                   weights_filename_front)
        torch.save(agent_back.qnetwork_local.state_dict(),
                   weights_filename_back)
        return scores