Esempio n. 1
0
def main():
    # dsp = display_b.Display()
    dsp = display_b.Display()
    dsp.log("George's MK 52")

    kbd = keypad.Keypad(24, 23, 22, 21, 19, 18, 16, 15, 13, 12, 11, 10, 8, 7)

    with emulator.Emulator("ws://localhost:8080/",
                           on_display=dsp.show,
                           on_log=dsp.log) as em:
        for x, y, txt in kbd.get_key_presses():
            em.press_button(x, y)
            time.sleep(0.1)
Esempio n. 2
0
def configure(args):
    """create and configure an emulator"""

    emu = emulator.Emulator(args,
                            frequency=24 * 1000 * 1000
                            cpu="68070")
    emu.add_memory(base=0, size=512 * 1024, writable=False, from_file=args.rom)
    emu.add_memory(base=0x1000000, size=512 * 1024)
    devices.p90ce201.add_devices(args, emu)
    emu.add_device(args,
                   CompactFlash,
                   address=0xffe000)

    return emu
Esempio n. 3
0
 def __init__(self, repeat_num, apple, character, equipment, wait_time,
              delay_time, fight_turn, side_wait_time, emulator_name,
              use_rand_time, change_character, fight):
     self.repeat_num = repeat_num
     self.apple = apple
     self.character = character
     self.equipment = equipment
     self.wait_time = wait_time
     self.delay_time = delay_time
     self.fight_turn = fight_turn
     self.side_wait_time = side_wait_time
     self.emulator = emulator.Emulator(emulator_name, fgo_dict.keyboard_key,
                                       fgo_dict.mouse_key, use_rand_time)
     self.change_character = change_character
     self.fight = fight
     self.fight_list = self.read_fight()
     self.count_character = 0
Esempio n. 4
0
def main(_):
    #make environment of simple video game.
    env = emulator.Emulator()
    #env = gym.make('Breakout-v0')

    sess = tf.Session()

    #build network.
    q_network = Dqn(sess=sess, env=env, name="main")
    target_network = Dqn(sess=sess, env=env, name="target")

    sess.run(tf.global_variables_initializer())

    #initialize all variables, especially copy q_net weights to the target network
    copy_weights_op = train.copy_weights(q_scope_name="main", target_scope_name="target")
    sess.run(copy_weights_op)

    saver = tf.train.Saver()

    if FLAGS.model_dir != '':

        saver.restore(sess, FLAGS.model_dir + "model.ckpt")
        
        model_dir = FLAGS.model_dir
        
    else :
        print("TRAINING")
        start = time.time()
        
        train.train(env=env,
                    model=q_network,
                    target_net=target_network,
                    FLAGS=FLAGS)
        
        print_time(time.time() - start)
        
        send_message()
        
        # if not FLAGS.no_save:
        #     print("The model was not saved.")
        # else :
        model_dir = save_model.save(saver=saver, sess=q_network.sess)
Esempio n. 5
0
print(params_mean.shape)

# Set file and directory names
gptag = traintag + errtag + tag
res_dir = '../../clust/results_{}/'.format(statistic)
gperr = np.loadtxt(res_dir + "{}_error{}.dat".format(statistic, errtag))
training_dir = '{}training_{}{}/'.format(res_dir, statistic, traintag)
testing_dir = '../testing_results/'
hyperparams = "../training_results/{}_training_results{}.dat".format(
    statistic, gptag)

print("Building emulator")
emu = emulator.Emulator(statistic,
                        training_dir,
                        gperr=gperr,
                        hyperparams=hyperparams,
                        log=log,
                        mean=mean,
                        nhod=nhod,
                        kernel_name=kernel_name)
emu.build()
print("Emulator built")

y_pred = emu.predict(params_mean)

if statistic == 'wp':
    rmin = 0.1
    rmax = 50
    nbins = 9
    rbins = np.logspace(np.log10(rmin), np.log10(rmax),
                        nbins + 1)  # Note the + 1 to nbins
    r = 10**(0.5 * (np.log10(rbins)[1:] + np.log10(rbins)[:-1]))
Esempio n. 6
0
def run(chain_fn, mode='dynesty', overwrite=False):

    # TODO: this overwrite check doesn't work bc initialize_chain
    # creates chain_fn either way. fix!
    if not overwrite and os.path.exists(chain_fn):
        raise ValueError(f"ERROR: File {chain_fn} already exists! Set overwrite=True to overwrite.")
    f = h5py.File(chain_fn, 'r+')

    ### data params
    # required
    cosmo = f.attrs['cosmo']
    hod = f.attrs['hod']

    ### emu params
    # required
    statistics = f.attrs['statistic']
    traintags = f.attrs['traintag']
    testtags = f.attrs['testtag']
    errtags = f.attrs['errtag']
    tags = f.attrs['tag']
    kernel_names = f.attrs['kernel_name']
    # optional
    logs = f.attrs['log']
    means = f.attrs['mean']
    nhods = f.attrs['nhod']

    ### chain params
    # required
    param_names = f.attrs['param_names']
    # optional
    multi = f.attrs['multi']
    nwalkers = f.attrs['nwalkers']
    nburn = f.attrs['nburn']
    nsteps = f.attrs['nsteps']
    dlogz = float(f.attrs['dlogz'])
    print('dlogz:', f.attrs['dlogz'], dlogz)
    seed = f.attrs['seed']
    nbins = f.attrs['nbins']
    cov_fn = f.attrs['cov_fn']
    icov_fn = f.attrs['icov_fn']
    msg = 'Cannot give both cov_fn and icov_fn in config file! But must have one of them'
    using_cov = isinstance(cov_fn, str)
    using_icov = isinstance(icov_fn, str)
    #assert using_cov ^ using_icov, msg
    
    # Set file and directory names
    nstats = len(statistics)
    training_dirs = [None]*nstats
    testing_dirs = [None]*nstats
    hyperparams = [None]*nstats
    acctags = [None]*nstats
    gperrs = [None]*nstats
    ys = []
    cov_dir = '../../clust/covariances/'
    print("WARNING: train tag and test tag no longer used!")
    for i, statistic in enumerate(statistics):
        gptag = traintags[i] + errtags[i] + tags[i]
        acctags[i] = gptag + testtags[i]
        gperrs[i] = np.loadtxt(cov_dir+"error_aemulus_{}{}.dat".format(statistic, errtags[i]))
        training_dirs[i] = f'../../clust/results_aemulus_train/results_{statistic}/'
        testing_dirs[i] = f'../../clust/results_aemulus_test_mean/results_{statistic}/'
        hyperparams[i] = "../training_results/{}_training_results{}.dat".format(statistic, gptag)

        # actual calculated stat
        _, y = np.loadtxt(testing_dirs[i]+'{}_cosmo_{}_HOD_{}_mean.dat'.format(statistic, cosmo, hod), delimiter=',', unpack=True)
        y = y[:nbins]
        ys.extend(y)
    f.attrs['ys'] = ys

    # number of parameters, out of 11 hod + 7 cosmo
    num_params = len(param_names)
    cosmo_names = ['Omega_m', 'Omega_b', 'sigma_8', 'h', 'n_s', 'N_eff', 'w']
    cosmos_truth = np.loadtxt('../tables/cosmology_camb_test_box_full.dat')

    hod_names = ['M_sat', 'alpha', 'M_cut', 'sigma_logM', 'v_bc', 'v_bs', 'c_vir', 'f', 'f_env', 'delta_env', 'sigma_env']
    hods_truth = np.loadtxt('../tables/HOD_test_np11_n1000_new_f_env.dat')
    hods_truth[:, 0] = np.log10(hods_truth[:, 0])
    hods_truth[:, 2] = np.log10(hods_truth[:, 2])

    fixed_params = {}
    cosmo_truth = cosmos_truth[cosmo]
    hod_truth = hods_truth[hod]
    for (cn, ct) in zip(cosmo_names, cosmo_truth):
        fixed_params[cn] = ct
    for (hn, ht) in zip(hod_names, hod_truth):
        fixed_params[hn] = ht

    # remove params that we want to vary from fixed param dict and add true values
    truth = {}
    for pn in param_names:
        truth[pn] = fixed_params[pn]
        fixed_params.pop(pn)
    #can't store dicts in h5py, so make sure truths (for variable params) are in same order as param names 
    truths = [truth[pname] for pname in param_names]
    f.attrs['true_values'] = truths
    if len(fixed_params)>0:
        fixed_param_names = list(fixed_params.keys())
        fixed_param_values = [fixed_params[fpn] for fpn in fixed_param_names]
    else:
        fixed_param_names = []
        fixed_param_values = []
    f.attrs['fixed_param_names'] = fixed_param_names
    f.attrs['fixed_param_values'] = fixed_param_values
    # print chain file info
    print(f"h5 file attributes for chain_fn: {chain_fn}")
    for k, v in f.attrs.items():
        print(f'{k}: {v}')

    # Set up Covariance matrix
    #if using_cov:
    icov = None
    if True:
        if os.path.exists(cov_fn):
            cov = np.loadtxt(cov_fn)
        else:
            raise ValueError(f"Path to covmat {cov_fn} doesn't exist!")

        nbins_tot = 9 #the covmat should have been constructed with 9 bins per stat
        err_message = f"Cov bad shape! {cov.shape}, but nbins_tot={nbins_tot} and nstats={nstats}"
        assert cov.shape[0] == nstats*nbins_tot and cov.shape[0] == nstats*nbins_tot, err_message
        # delete rows/cols of covmat we don't want to use
        #nbins_tot = cov.shape[0]/nstats
        if nbins < nbins_tot:
            print(f"nbins={nbins}, while total in cov nbins_tot={nbins_tot}; removing {nbins_tot-nbins} bins")
        idxs_toremove = np.array([np.arange(nbins_tot-1, nbins-1, -1)+(ns*nbins_tot) for ns in range(nstats)]).flatten()
        # remove both row and col
        cov = np.delete(cov, idxs_toremove, axis=0)
        cov = np.delete(cov, idxs_toremove, axis=1)
        print("Covariance matrix:")
        print(cov)    
        print(cov.shape)
        print("Condition number:", np.linalg.cond(cov))
        f.attrs['covariance_matrix'] = cov

    #elif using_icov:
    if using_icov:
    #if True:
        if os.path.exists(icov_fn):
            icov = np.loadtxt(icov_fn)
        else:
            raise ValueError(f"Path to inverse covmat {icov_fn} doesn't exist!")
        print("Inverse covariance matrix:")
        print(icov)    
        print(icov.shape)
        f.attrs['inverse_covariance_matrix'] = icov
        #cov = icov # calling it cov so can pass this variable (this is bad i know)

    ### Set up chain datasets ###
    dsetnames = ['chain', 'lnprob', 'lnweight', 'lnevidence', 'varlnevidence']
    #for now will overwrite
    for dsn in dsetnames:
        if dsn in f.keys():
            del f[dsn]
    f.create_dataset('chain', (0, 0, len(param_names)), chunks = True, compression = 'gzip', maxshape = (None, None, len(param_names)))
    f.create_dataset('lnprob', (0, 0,) , chunks = True, compression = 'gzip', maxshape = (None, None, ))
    f.create_dataset('lnweight', (0, 0,), chunks = True, compression = 'gzip', maxshape = (None, None, ))
    f.create_dataset('lnevidence', (0, 0,), chunks = True, compression = 'gzip', maxshape = (None, None, ))
    f.create_dataset('varlnevidence', (0, 0,), chunks = True, compression = 'gzip', maxshape = (None, None, ))
    f.close()

    print("Building emulators")
    emus = [None]*nstats
    for i, statistic in enumerate(statistics):
        emu = emulator.Emulator(statistic, training_dirs[i], nbins=nbins,  fixed_params=fixed_params, gperr=gperrs[i], hyperparams=hyperparams[i], log=logs[i], mean=means[i], nhod=nhods[i], kernel_name=kernel_names[i])
        emu.build()
        emus[i] = emu
        print(f"Emulator for {statistic} built")

    ### Set up covariance matrix ###
    # stat_str = '_'.join(statistics)
    # #If all of the tags for the stats are the same, just use one of them (usually doing this now!)
    # if len(set(traintags))==1 and len(set(errtags))==1 and len(set(testtags))==1:
    #     tag_str = traintags[0] + errtags[0] + testtags[0]
    # else:
    #     # Otherwise will need to join them all
    #     print("Using acctags joined for emu")
    #     tag_str.join(acctags)
    # # for now, use performance covariance on aemulus test set (see emulator/words/error.pdf)
    # cov_emuperf_fn = f"{cov_dir}cov_emuperf_{stat_str}{tag_str}.dat"
    # if os.path.exists(cov_emuperf_fn):
    #     cov = np.loadtxt(cov_emuperf_fn)
    # else:
    #     raise ValueError(f"Path to covmat {cov_emuperf_fn} doesn't exist!")

    start = time.time()
    if mode=='emcee':
        res = chain.run_mcmc_emcee(emus, param_names, ys, cov, fixed_params=fixed_params, 
                             truth=truth, nwalkers=nwalkers, nsteps=nsteps, 
                             nburn=nburn, multi=multi, chain_fn=chain_fn)
    elif mode=='dynesty':
        res = chain.run_mcmc_dynesty(emus, param_names, ys, cov, icov=icov, using_icov=using_icov, fixed_params=fixed_params, 
                                     truth=truth, multi=multi, chain_fn=chain_fn,
                                     dlogz=dlogz, seed=seed)
    elif mode=='minimize':
        res = chain.run_minimizer([emu], param_names, [y], [cov], fixed_params=fixed_params, 
                                  truth=truth, chain_fn=chain_fn)
    else:
        raise ValueError(f"Mode {mode} not recognized!")
    end = time.time()
    print(f"Time: {(end-start)/60.0} min ({(end-start)/3600.} hrs) [{(end-start)/(3600.*24.)} days]")

    return res
Esempio n. 7
0
 def main():
     emulator.init(width=960, height=720)
     machine = emulator.Emulator(boot_rom='emulator/build/boot.rom')
     machine.run()
Esempio n. 8
0
 def setUp(self):
     self.chip_emulator = emulator.Emulator()
Esempio n. 9
0
nhod = 100 # used for training
kernel_name = 'M32ExpConst' # xi, upf, mcf
#kernel_name = 'M32ExpConst2' # wp
tag = '_log_k{}_{}hod'.format(kernel_name, nhod)
#tag = '_meansub_xrsq_k{}_{}hod'.format(kernel_name, nhod)
log = True
mean = False
meansub = False
xrsq = False
gptag = traintag + errtag + tag
acctag = gptag + testtag + testsavetag

res_dir = '../../clust/results_{}/'.format(statistic)
gperr = np.loadtxt("../../clust/covariances/error_aemulus_{}{}{}.dat".format(statistic, errtag, savetag))

training_dir = '{}training_{}{}/'.format(res_dir, statistic, traintag)
hyperparams = "../training_results/{}_training_results{}.dat".format(statistic, gptag)

predict_savedir = f"../testing_results/predictions_{statistic}{acctag}/"
os.makedirs(predict_savedir, exist_ok=True)

print("Initializing emu")
emu = emulator.Emulator(statistic, training_dir,
            gperr=gperr, testmean=testmean, hyperparams=hyperparams, log=log, 
            mean=mean, meansub=meansub, xrsq=xrsq, nhod=nhod, kernel_name=kernel_name)
print("Building emu")
emu.build()
print("Testing emu")
emu.test_glam4(predict_savedir)
print("Done!")
Esempio n. 10
0
def train(scenario,
          average_reward_episodes,
          rendering,
          hidden_layers,
          hidden_layers_size,
          memory_size,
          minibatch_size,
          optimizer_learning_rate,
          gamma,
          epsilon_decay_factor,
          maximum_episodes,
          model_file_name,
          converge_criteria=None,
          graphs_suffix='',
          seed=None,
          verbose=C_VERBOSE_NONE):
    '''
    Summary: 
        Trains a DQN model for solving the given OpenAI gym scenario.
    
    Args:
        scenario: string
            The OpenAI gym scenario to be solved.
        
        average_reward_episodes: int
            On how many concecutive episodes the averaged reward should be calculated.
        
        rendering: boolean
            If True, OpenAI gym environment rendering is enabled. 
        
        hidden_layers: int
            The number of hidden layers of the Deep Neural Network. Not including the first
            and last layer.
            
        hidden_layers_size: int
            The size of each hidden layer of the Neural Network.
        
        memory_size: int
            The size of the replay memory feature which will be used by the DQN.
                
        minibatch_size: int
            The minibatch size which will be retrieved randomly from the memory in each 
            iteration in the DQN.
        
        optimizer_learning_rate: float
            The Adam optimizer learning rate used in the DNN.
        
        gamma: float
                The discount factor to be used in the equation (3) of [1].
        
        epsilon_decay_factor: float
            The decay factor of epsilon parameter, for each iteration step.

        maximum_episodes: int
            The maximum number of episodes to be executed. If DQN converges earlier the training stops.
        
        model_file_name: string
            The file in which the DQN trained model (DNN Keras) should be saved.
            
        converge_criteria: int or None
            The DQN converge criteria (when for converge_criteria concecutive episodes average reward 
            is > 200, the DQN assumed that has been converged).
            If None, the training continues till the maximum_episodes is reached.
            
        graphs_suffix: string
            A suffix added in the graphs file names. To be used in case of multiple trains.
        
        seed: int
            Optional Seed to be used with the OpenAI gym environment, for results reproducability.
                
        verbose: int
            Verbose level (0: None, 1: INFO, 2: DEBUG)
                
    Raises:
        -
    
    Returns:
        convergence_episode: int
            In which episode the DQN convergences 
            
        convergence_time: string (time)
            On how much time the DQN convergences
            
        Rturns None if converge_criteria is None
            
    notes:
        -
    '''

    if verbose > C_VERBOSE_NONE:
        print('\nDQN Training Starts (scenario = ',
              scenario,
              ', average_reward_episodes = ',
              average_reward_episodes,
              ', rendering = ',
              rendering,
              ', hidden_layers = ',
              hidden_layers,
              ', hidden_layers_size = ',
              hidden_layers_size,
              ', memory_size = ',
              memory_size,
              ', minibatch_size = ',
              minibatch_size,
              ', optimizer_learning_rate = ',
              optimizer_learning_rate,
              ', gamma = ',
              gamma,
              ', epsilon_decay_factor = ',
              epsilon_decay_factor,
              ', maximum_episodes = ',
              maximum_episodes,
              ', model_file_name = ',
              model_file_name,
              ', converge_criteria = ',
              converge_criteria,
              ', graphs_suffix = ',
              graphs_suffix,
              ', seed = ',
              seed,
              ')',
              sep='')

    #If seed is given the apply it
    if seed is not None:
        applySeed(seed, verbose)

    #Create a Emulator object instance
    emulator = em.Emulator(scenario,
                           average_reward_episodes,
                           statistics=True,
                           rendering=rendering,
                           seed=seed,
                           verbose=verbose)

    #Create a Deep Neural Network object instance (Keras with Tensor Flow backend)
    dnn = deepNeuralNetwork.DeepNeuralNetwork(
        inputs=emulator.state_size,
        outputs=emulator.actions_number,
        hidden_layers=hidden_layers,
        hidden_layers_size=hidden_layers_size,
        optimizer_learning_rate=optimizer_learning_rate,
        seed=seed,
        verbose=verbose)

    #Create a DQN object instance (we start always from epsilon = 1.0, we control each value with the epsilon_decay_factor
    dqn = deepQNetwork.DeepQNetwork(emulator=emulator,
                                    dnn=dnn,
                                    states_size=emulator.state_size,
                                    actions_number=emulator.actions_number,
                                    memory_size=memory_size,
                                    minibatch_size=minibatch_size,
                                    gamma=gamma,
                                    epsilon=1.0,
                                    epsilon_decay_factor=epsilon_decay_factor,
                                    seed=seed,
                                    verbose=verbose)

    #Start measuring training time
    start_time = time.time()

    if converge_criteria is not None:
        #Holds how many concecutive episodes average reward is > 200
        convergence_counter = 0
        episodes_convergence_counter = [
        ]  #Holds the convergence_counter for all episodes
        convergence_episode = 0

    #Training starts here
    for i in range(maximum_episodes):
        current_state = emulator.start()

        #See Algorithm 1 in [1]
        while emulator.emulator_started:
            action = dqn.decideAction(current_state)

            #Experience [s, a, r, s']
            experience = emulator.applyAction(action)

            dqn.storeTransition(experience)
            dqn.sampleRandomMinibatch()

            #s = s' at the end of the step, before starting the new step
            current_state = experience[3]

        if converge_criteria is not None:
            #Check if convergence counter should be increased or to be reset
            if emulator.average_reward > 200:
                convergence_counter += 1
            else:
                convergence_counter = 0

            episodes_convergence_counter.append(convergence_counter)

            if verbose > C_VERBOSE_NONE:
                print('Convergence Counter: ', convergence_counter, sep='')

            #DQN model assumed that it has been converged
            if convergence_counter >= converge_criteria:
                convergence_episode = i
                break

    if converge_criteria is not None:
        convergence_time = time.time() - start_time

    if verbose > C_VERBOSE_NONE and converge_criteria is not None:
        print('\nDQN converged after ',
              convergence_episode,
              ' episodes in ',
              executionTimeToString(convergence_time),
              sep='')
    elif verbose > C_VERBOSE_NONE and converge_criteria is None:
        print('\nDQN trained for ',
              maximum_episodes,
              ' episodes in ',
              executionTimeToString(time.time() - start_time),
              sep='')

    #Create Graphs
    #1. Steps per Episode
    plt.plot(emulator.execution_statistics.values[:, 0],
             emulator.execution_statistics.values[:, 1],
             color='coral',
             linestyle='-')
    plt.grid(b=True, which='major', axis='y', linestyle='--')
    plt.xlabel('Episode', fontsize=12)
    plt.ylabel('Steps', fontsize=12)
    plt.title('Steps per Episode', fontsize=12)
    plt.savefig('Steps_Per_Episode' + graphs_suffix + '.png')
    plt.clf()

    #2. Total Reward per Training Episode
    plt.plot(emulator.execution_statistics.values[:, 0],
             emulator.execution_statistics.values[:, 2],
             color='coral',
             linestyle='-',
             label='Total Reward')
    plt.plot(emulator.execution_statistics.values[:, 0],
             emulator.execution_statistics.values[:, 3],
             color='midnightblue',
             linestyle='--',
             label='Episodes Reward Average')
    plt.grid(b=True, which='major', axis='y', linestyle='--')
    plt.xlabel('Episode', fontsize=12)
    plt.ylabel('Reward', fontsize=12)
    plt.title('Total Reward per Training Episode', fontsize=12)
    plt.legend(loc='lower right', fontsize=12)
    plt.savefig('Total_Reward_Per_Training_Episode' + graphs_suffix + '.png')
    plt.clf()

    #Save the trained model
    dnn.saveModel(model_file_name)

    if converge_criteria is not None:
        return convergence_episode
Esempio n. 11
0
def trial(model_file_name,
          scenario,
          number_of_trials,
          rendering=False,
          graphs_suffix='',
          verbose=C_VERBOSE_NONE):
    '''
    Summary: 
        Evaluate the trained DQN for a number of trials (number_of_trials).
    
    Args:
        model_file_name: string
            The saved trained DQN (Keras DNN h5 file).
            
        scenario: string 
            The OpenAI gym scenario to be loaded by the Emulator.
            
        number_of_trials: int
            How many trials to execute.
        
        rendering: boolean
            If True, OpenAI gym environment rendering is enabled.

        graphs_suffix: string
            A suffix added in the graphs file names. To be used in case of multiple trials.            
                
        verbose: int
            Verbose level (0: None, 1: INFO, 2: DEBUG)
                
    Raises:
        -

    Returns:
        trials_average_reward: float
            The average reward for the trial-episode (100 episodes)
            
    notes:
        -
    '''

    if verbose > C_VERBOSE_NONE:
        print('\nEvaluate the trained DQN in ',
              str(number_of_trials),
              ' trials (episodes).',
              sep='')
        print('- model_file_name = ',
              model_file_name,
              ', scenario = ',
              scenario,
              ', number_of_trials = ',
              number_of_trials,
              ', rendering = ',
              rendering,
              ', graphs_suffix = ',
              graphs_suffix,
              sep='')

    #Import Numpy for the trial
    import numpy as np

    #Create a Emulator object instance (without a seed)
    emulator = em.Emulator(scenario=scenario,
                           average_reward_episodes=number_of_trials,
                           statistics=True,
                           rendering=rendering,
                           seed=None,
                           verbose=verbose)

    #Create a Deep Neural Network object instance and load the trained model (model_file_name)
    dnn = deepNeuralNetwork.DeepNeuralNetwork(file_name=model_file_name,
                                              verbose=verbose)

    #Start measuring Trials time
    start_time = time.time()

    #Trials
    for i in range(number_of_trials):

        current_state = emulator.start()

        while emulator.emulator_started:
            action = np.argmax(dnn.predict(current_state))

            #Experience [s, a, r, s']
            experience = emulator.applyAction(action)
            current_state = experience[3]

    if verbose > C_VERBOSE_NONE:
        print('\nDQN ',
              str(number_of_trials),
              ' trials average = ',
              emulator.execution_statistics.values[-1, 3],
              ', in ',
              executionTimeToString(time.time() - start_time),
              sep='')

    return emulator.execution_statistics.values[-1, 3]
Esempio n. 12
0
def build_emus(f):

    #chain_fn = f'../chains/chains_{chaintag}.h5'
    #f = h5py.File(chain_fn, 'r')

    ### data params
    cosmo = f.attrs['cosmo']
    hod = f.attrs['hod']

    ### emu params
    statistics = f.attrs['statistic']
    traintags = f.attrs['traintag']
    testtags = f.attrs['testtag']
    errtags = f.attrs['errtag']
    tags = f.attrs['tag']
    kernel_names = f.attrs['kernel_name']
    logs = f.attrs['log']
    means = f.attrs['mean']
    nhods = f.attrs['nhod']

    ### chain params
    param_names = f.attrs['param_names']

    # Set file and directory names
    nstats = len(statistics)
    training_dirs = [None] * nstats
    testing_dirs = [None] * nstats
    hyperparams = [None] * nstats
    acctags = [None] * nstats
    gperrs = [None] * nstats
    ys = []
    cov_dir = '../../clust/covariances/'
    for i, statistic in enumerate(statistics):
        gptag = traintags[i] + errtags[i] + tags[i]
        acctags[i] = gptag + testtags[i]
        res_dir = '../../clust/results_{}/'.format(statistic)
        gperrs[i] = np.loadtxt(
            cov_dir + "error_aemulus_{}{}.dat".format(statistic, errtags[i]))
        training_dirs[i] = '{}training_{}{}/'.format(res_dir, statistic,
                                                     traintags[i])
        testing_dirs[i] = '{}testing_{}{}/'.format(res_dir, statistic,
                                                   testtags[i])
        hyperparams[
            i] = "../training_results/{}_training_results{}.dat".format(
                statistic, gptag)

    # number of parameters, out of 11 hod + 7 cosmo
    num_params = len(param_names)
    cosmo_names = ['Omega_m', 'Omega_b', 'sigma_8', 'h', 'n_s', 'N_eff', 'w']
    cosmos_truth = np.loadtxt('../tables/cosmology_camb_test_box_full.dat')

    hod_names = [
        'M_sat', 'alpha', 'M_cut', 'sigma_logM', 'v_bc', 'v_bs', 'c_vir', 'f',
        'f_env', 'delta_env', 'sigma_env'
    ]
    hods_truth = np.loadtxt('../tables/HOD_test_np11_n1000_new_f_env.dat')
    hods_truth[:, 0] = np.log10(hods_truth[:, 0])
    hods_truth[:, 2] = np.log10(hods_truth[:, 2])

    fixed_params = {}
    cosmo_truth = cosmos_truth[cosmo]
    hod_truth = hods_truth[hod]
    for (cn, ct) in zip(cosmo_names, cosmo_truth):
        fixed_params[cn] = ct
    for (hn, ht) in zip(hod_names, hod_truth):
        fixed_params[hn] = ht

    # remove params that we want to vary from fixed param dict and add true values
    truths = f.attrs['true_values']
    for pn in param_names:
        fixed_params.pop(pn)

    print("Building emulators")
    emus = [None] * nstats
    for i, statistic in enumerate(statistics):
        print(f"Rebuilding emulator for {statistic}")
        emu = emulator.Emulator(statistic,
                                training_dirs[i],
                                testing_dir=testing_dirs[i],
                                fixed_params=fixed_params,
                                gperr=gperrs[i],
                                hyperparams=hyperparams[i],
                                log=logs[i],
                                mean=means[i],
                                nhod=nhods[i],
                                kernel_name=kernel_names[i])
        emu.build()
        emus[i] = emu

    #return emus, statistics, param_names, fixed_params, truths, cosmo, hod
    return emus, fixed_params, gperrs
Esempio n. 13
0
def main():
    chaintag = 'upf_c4h4_fenv_med_nolog'
    chain_fn = f'../chains/chains_{chaintag}.h5'
    f = h5py.File(chain_fn, 'r')

    ### data params
    # required
    cosmo = f.attrs['cosmo']
    hod = f.attrs['hod']

    ### emu params
    # required
    statistic = f.attrs['statistic']
    traintag = f.attrs['traintag']
    testtag = f.attrs['testtag']
    errtag = f.attrs['errtag']
    tag = f.attrs['tag']
    # optional
    log = f.attrs['log']
    #log = True if log is None else log
    mean = f.attrs['mean']
    #mean = False if mean is None else mean

    ### chain params
    # required
    nwalkers = f.attrs['nwalkers']
    nburn = f.attrs['nburn']
    nsteps = f.attrs['nsteps']
    param_names = f.attrs['param_names']
    # optional
    multi = f.attrs['multi']
    #multi = True if multi is None else multi

    f.close()

    # Set file and directory names
    gptag = traintag + errtag + tag
    res_dir = '../../clust/results_{}/'.format(statistic)
    gperr = np.loadtxt(res_dir + "{}_error{}.dat".format(statistic, errtag))
    training_dir = '{}training_{}{}/'.format(res_dir, statistic, traintag)
    testing_dir = '{}testing_{}{}/'.format(res_dir, statistic, testtag)
    hyperparams = "../training_results/{}_training_results{}.dat".format(
        statistic, gptag)
    #plot_dir = '../plots/plots_2020-01-27'
    #plot_fn = f'{plot_dir}/prob_{statistic}{gptag}{savetag}.png'
    #chain_fn = f'../chains/chains_{statistic}{gptag}{savetag}.png'

    # actual calculated stat
    if 'parammean' in testtag:
        rad, y = np.loadtxt(f'../testing_results/{statistic}_parammean.dat',
                            delimiter=',',
                            unpack=True)
    else:
        rad, y = np.loadtxt(
            testing_dir +
            '{}_cosmo_{}_HOD_{}_mean.dat'.format(statistic, cosmo, hod))
    print('y:', y.shape, y)

    # number of parameters, ex 8 hod + 7 cosmo
    num_params = len(param_names)
    #means = np.random.rand(ndim)
    cosmo_names = ['Omega_m', 'Omega_b', 'sigma_8', 'h', 'n_s', 'N_eff', 'w']
    cosmos_truth = np.loadtxt('../tables/cosmology_camb_test_box_full.dat')

    hod_names = [
        'M_sat', 'alpha', 'M_cut', 'sigma_logM', 'v_bc', 'v_bs', 'c_vir', 'f',
        'f_env', 'delta_env', 'sigma_env'
    ]
    hods_truth = np.loadtxt('../tables/HOD_test_np11_n1000_new_f_env.dat')
    hods_truth[:, 0] = np.log10(hods_truth[:, 0])
    hods_truth[:, 2] = np.log10(hods_truth[:, 2])

    fixed_params = {}
    if 'parammean' in testtag:
        names = cosmo_names + hod_names
        params_mean = np.loadtxt("../testing_results/parammean.dat")
        for (name, pm) in zip(names, params_mean):
            fixed_params[name] = pm
    else:
        cosmo_truth = cosmos_truth[cosmo]
        hod_truth = hods_truth[hod]
        for (cn, ct) in zip(cosmo_names, cosmo_truth):
            fixed_params[cn] = ct
        for (hn, ht) in zip(hod_names, hod_truth):
            fixed_params[hn] = ht

    # remove params that we want to vary from fixed param dict and add true values
    truth = {}
    for pn in param_names:
        truth[pn] = fixed_params[pn]
        fixed_params.pop(pn)

    print("Stat:", statistic)
    print("True values:")
    print(truth)

    print("Building emulator")
    emu = emulator.Emulator(statistic,
                            training_dir,
                            fixed_params=fixed_params,
                            gperr=gperr,
                            hyperparams=hyperparams,
                            log=log,
                            mean=mean)
    emu.build()
    print("Emulator built")

    #diagonal covariance matrix from error
    cov = np.diag(emu.gperr)
    #cov *= 10
    print(cov)
    print(cov.shape)
    start = time.time()
Esempio n. 14
0
anntag = traintag + errtag + tag
acctag = anntag + testtag + testsavetag

res_dir = '../../clust/results_{}/'.format(statistic)
gperr = np.loadtxt("../../clust/covariances/error_aemulus_{}{}{}.dat".format(
    statistic, errtag, savetag))

training_dir = '{}training_{}{}/'.format(res_dir, statistic, traintag)
model_dir = "../training_ann_results/{}_training_results{}".format(
    statistic, anntag)

testing_dir = '{}testing_{}{}/'.format(res_dir, statistic, testtag)
predict_savedir = f"../testing_results/predictions_{statistic}{acctag}"
os.makedirs(predict_savedir, exist_ok=True)

emu = emulator.Emulator(statistic,
                        training_dir,
                        testing_dir=testing_dir,
                        gperr=gperr,
                        testmean=testmean,
                        log=log,
                        mean=mean,
                        meansub=meansub,
                        xrsq=xrsq,
                        nhod=nhod,
                        nhod_test=nhod_test,
                        model_dir=model_dir)
emu.build_ann()
emu.test_ann(predict_savedir)
#emu.build_ann_bybin()
#emu.test_ann_bybin(predict_savedir)
Esempio n. 15
0
import server, emulator, sys, threading
import tornado.ioloop


def usage():
    print('Invalid arguments: python %s <rom file>' % (sys.argv[0]))


def thread_function(core):
    core.run()


if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage()
        sys.exit(0)

    # Service instantiation.
    web_server = server.Server()
    core = emulator.Emulator(sys.argv[1], web_server)
    web_server.set_core(core)

    # Start both services.
    # Start the emulator service first and run in a background thread.
    emulator_thread = threading.Thread(target=thread_function, args=(core, ))
    emulator_thread.start()

    web_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
Esempio n. 16
0
def trial(model_file_name, scenario, number_of_trials, rendering=False, graphs_suffix='', verbose=C_VERBOSE_NONE,
          store_history=False, compute_saliency=False, history_save_path='./output/history_test.pkl'):
    """
    Summary:
        Evaluate the trained DQN for a number of trials (number_of_trials).

    Args:
        model_file_name: string
            The saved trained DQN (Keras DNN h5 file).

        scenario: string
            The OpenAI gym scenario to be loaded by the Emulator.

        number_of_trials: int
            How many trials to execute.

        rendering: boolean
            If True, OpenAI gym environment rendering is enabled.

        graphs_suffix: string
            A suffix added in the graphs file names. To be used in case of multiple trials.

        verbose: int
            Verbose level (0: None, 1: INFO, 2: DEBUG)

        store_history: bool
            Store history data or not.

        compute_saliency: bool
            Computes saliency or not.

        history_save_path: str
            Where to store the history file.

    Raises:
        -

    Returns:
        trials_average_reward: float
            The average reward for the trial-episode (100 episodes)

    notes:
        -
    """

    if verbose > C_VERBOSE_NONE:
        print('\nEvaluate the trained DQN in ', str(number_of_trials), ' trials (episodes).', sep='')
        print('- model_file_name = ', model_file_name, ', scenario = ', scenario, ', number_of_trials = ',
              number_of_trials,
              ', rendering = ', rendering, ', graphs_suffix = ', graphs_suffix, sep='')

        # Create a Emulator object instance (without a seed)
    emulator = em.Emulator(scenario=scenario, average_reward_episodes=number_of_trials, statistics=True,
                           rendering=rendering, seed=42, verbose=verbose)

    # Create a Deep Neural Network object instance and load the trained model (model_file_name)
    dnn = deepNeuralNetwork.DeepNeuralNetwork(file_name=model_file_name, verbose=verbose)

    # Start measuring Trials time
    start_time = time.time()

    history = {
        'trial': [],
        'state': [],
        'action': [],
        'reward': [],
        'next_state': [],
        'done': [],
        'q_values': []
    }
    if compute_saliency:
        history['saliency'] = []

    # Trials
    # used as baseline for perturbation
    # for each feature, apply a random noise of 0.2 * (max(feature) - min(feature))
    state_min = np.array([-0.354871, -0.10391249, -0.468456, -0.89336216, -0.15218297, -0.4017307, 0, 0])
    state_max = np.array([-0.00462484, 1.4088593, 0.12988918, 0.05392841, 0.5564749, 0.8584606, 1, 1])
    for i in range(number_of_trials):

        current_state = emulator.start()

        while emulator.emulator_started:
            q_values = dnn.predict(current_state)
            action = np.argmax(q_values)

            if compute_saliency:
                # compute saliency
                saliency = np.zeros(NUM_STATE)
                for _ in range(NUM_SALIENCY_TESTS):
                    for j in range(NUM_STATE):
                        # perturb state
                        perturbed_state = np.array(current_state)
                        if j < 6:  # numerical states
                            perturbed_state[j] = SALIENCY_PERTURBATION * np.random.rand() \
                                                 * (state_max[j] - state_min[j]) + state_min[j]
                        else:  # boolean states
                            perturbed_state = current_state.copy()
                            perturbed_state[j] = 1 - perturbed_state[j]
                        q_values_preturbed = dnn.predict(perturbed_state)

                        max_q = np.max(q_values)
                        q_values /= max_q
                        q_values_preturbed /= max_q

                        q_value_dict = {a: q_values[0, a].astype(np.float64) for a in range(4)}
                        q_value_preturbed_dict = {a: q_values_preturbed[0, a].astype(np.float64) for a in range(4)}
                        saliency[j] = sarfa_saliency.computeSaliencyUsingSarfa(action,
                                                                               q_value_dict,
                                                                               q_value_preturbed_dict)[0]
                saliency /= NUM_SALIENCY_TESTS

            # Experience [s, a, r, s']
            experience = emulator.applyAction(action)

            # save data
            if store_history:
                history['trial'].append(i)
                history['state'].append(current_state)
                history['action'].append(action)
                history['reward'].append(experience[2])
                if experience[3] is not None:
                    history['next_state'].append(experience[3])
                    history['done'].append(False)
                else:
                    history['next_state'].append(current_state)
                    history['done'].append(True)
                history['q_values'].append(q_values)
                if compute_saliency:
                    history['saliency'].append(saliency)

            current_state = experience[3]

    if store_history:
        for k in history.keys():
            history[k] = np.array(history[k])
        history_save_dir = os.path.split(history_save_path)[0]
        if not os.path.exists(history_save_dir):
            os.makedirs(history_save_dir)
        pd.to_pickle(history, history_save_path)

    if verbose > C_VERBOSE_NONE:
        print('\nDQN ', str(number_of_trials), ' trials average = ', emulator.execution_statistics.values[-1, 3],
              ', in ',
              executionTimeToString(time.time() - start_time), sep='')

    return emulator.execution_statistics.values[-1, 3]