<jupyter_start><jupyter_text>Phylogenetic Analysis<jupyter_code>import numpy as np
import pickle as pkl
import os, sys
import gzip
cwd = os.getcwd()
folder = os.path.basename(cwd)
cwd = os.path.dirname(cwd)
folder = os.path.basename(cwd)
ROOT = os.path.join(cwd)

sys.path.append(ROOT)

from analysis import utils
from parameters import params<jupyter_output>Root directory /home/giuseppe/src/cmans<jupyter_text>Load Data<jupyter_code>env_type = 'RW'

if env_type == 'RW':
    env = 'Walker2D'
elif env_type == 'Dummy':
    env = 'Dummy'
elif env_type == 'CB':
    env = 'CollectBall'
elif env_type == 'AM':
    env = 'AntMaze'
elif env_type == 'arm':
    env = 'NDofArm'
else:
    raise ValueError('Wrong environment type given: {}'.format(env_type))

EXP_ROOT = '/mnt/7e0bad1b-406b-4582-b7a1-84327ae60fc4/cmans/'
    
exp_path = os.path.join(ROOT, 'experiment_data')
# exp_path = EXP_ROOT
exp_types = ['NS', 'NSGA-II', 'CMA-NS', 'CMA-NS_prob']
paths = []

avail_exps = []
for exp_type in exp_types:
#     path = os.path.join(exp_path, "{}/{}_{}".format(env, env, exp_type))
    path = os.path.join(exp_path, "{}_{}".format(env, exp_type))

    if not os.path.exists(path):
        path = None
        print("No {} experiment for {} env.".format(exp_type, env))
        continue
    paths.append(path)
    avail_exps.append(exp_type)
exp_types = avail_exps
grid_parameters = registered_envs[env]['grid']
best_run = {}
for exp in exp_types:
    best_run[exp] = 0
trees = {}
for exp, path in zip(exp_types, paths):
    runs = utils.get_runs_list(path)
    run_tree = []
    
    for run in runs:
        tree_path = os.path.join(path, run, 'analyzed_data/phyl_tree.pkl')
        
        if os.path.exists(tree_path):
            with gzip.open(tree_path) as f: 
                indivs_by_id, indivs_by_generation = pkl.load(f)    
            run_tree.append({'id':indivs_by_id, 'gen': indivs_by_generation})
            
        else:
            print("{} does not exists.".format(tree_path))
        
        trees[exp] = run_tree<jupyter_output>/home/giuseppe/src/cmans/experiment_data/Walker2D_NSGA-II/2020_09_24_17:31_169786/analyzed_data/phyl_tree.pkl does not exists.
/home/giuseppe/src/cmans/experiment_data/Walker2D_CMA-NS/2020_10_02_13:46_375388/analyzed_data/phyl_tree.pkl does not exists.
/home/giuseppe/src/cmans/experiment_data/Walker2D_CMA-NS/2020_10_02_13:59_875427/analyzed_data/phyl_tree.pkl does not exists.<jupyter_text>Find rewarding lineages<jupyter_code>for exp in trees:
    for tree in trees[exp]:
        lineages = {}
        for agent_id in tree['id']:
            agent = tree['id'][agent_id]
            if agent['reward']>0:
                parent = agent['parent']
#                 print("{}: {}".format(agent_id, parent))
                lineage = [agent_id]
                
                while True:
                    if parent is None:
                        break
                    lineage.append(parent)
                    agent = tree['id'][parent]
                    parent = agent['parent']
                
                if agent['id'] not in lineages:
                    lineages[agent['id']] = [lineage]
                else:
                    lineages[agent['id']].append(lineage)
                
                
lineages
tree_id<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12793497892835642, 'fraction_numerical': 0.024081878386514148, 'mean_word_length': 2.557815845824411, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '331155', 'license': 'MIT', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1172, 'n_words': 216, 'path': 'analysis/.ipynb_checkpoints/Phylogenetic Analysis-checkpoint.ipynb', 'repo_name': 'GPaolo/SERENE', 'size': '3322'}