text:
import tensorflow as tf
import numpy as np
import gym
from gym import spaces
import matplotlib.pyplot as plt
import argparse
from tensorflow import keras
from tensorflow.keras import Input, Model, Sequential, layers
import datetime
from scipy.io import savemat

from cvxopt import matrix
from cvxopt import solvers

# print(tf.__version__)
# tf.test.gpu_device_name()

#Local Modules
from cbf.cbf import CBF_rl

#===================================
#     Actor and Critic DNNs
#===================================

class ActorNetwork(object):
    def __init__(self, state_dim, action_dim, action_bound, learning_rate, tau, batch_size, params_l1, params_l2):
        self.state_dim = state_dim
        self.action_dim = action_dim
        self.action_bound = action_bound
        self.learning_rate = learning_rate
        self.tau  = tau
        self.batch_size = batch_size
        self.params_l1 = params_l1
        self.params_l2 = params_l2
        self.optimizer = tf.keras.optimizers.Adam(self.learning_rate)

        #actor network
        self.inputs, self.out, self.scaled_out = self.create_actor_network()
        self.actor_model = keras.Model(inputs=self.inputs, outputs=self.scaled_out, name='actor_network')
        self.network_params = self.actor_model.trainable_variables

        #target actor network
        self.target_inputs, self.target_out, self.target_scaled_out = self.create_actor_network()
        self.target_actor_model = keras.Model(inputs=self.target_inputs, outputs=self.target_scaled_out, name='target_actor_network')
        self.target_network_params = self.target_actor_model.trainable_variables

        #initalizing the target params with then network params
        for i in range(len(self.target_network_params)):
            self.target_network_params[i].assign(self.network_params[i])

    def create_actor_network(self):
        inputs = Input(shape = (self.state_dim,), batch_size = None, name = "actor_input_state")
        w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)

        net = layers.Dense(self.params_l1, name = 'actor_dense_1', kernel_initializer = w_init)(inputs)
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation=tf.nn.relu)(net)

        net = layers.Dense(self.params_l2, name = 'actor_dense_2', kernel_initializer = w_init)(net)
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation='tanh')(net)

        out = layers.Dense(self.action_dim, activation='tanh', name = 'actor_dense_3', kernel_initializer = w_init)(net)
        scaled_out = tf.multiply(out, self.action_bound, name = "actions_scaling")
        return inputs, out, scaled_out

    def update_target_network(self):
        self.update_target_network_params = [self.target_network_params[i].assign(tf.multiply(self.network_params[i], self.tau) + tf.multiply(self.target_network_params[i], 1-self.tau)) for i in range(len(self.target_network_params))]

    def train(self, inputs, a_gradient):
        with tf.GradientTape(watch_accessed_variables=True) as tape:
            prediction = self.actor_model(inputs)
        unnormalized_actor_gradients = tape.gradient(prediction, self.network_params, output_gradients = -a_gradient)
        actor_gradients = list(map(lambda x: tf.math.divide(x, self.batch_size), unnormalized_actor_gradients))
        self.optimizer.apply_gradients(zip(actor_gradients, self.network_params))

    def predict(self, inputs):
        return self.actor_model(inputs)

    def predict_target(self, inputs):
        return self.target_actor_model(inputs)

##---------------------------------------------------------------------------------------------------------------
class CriticNetwork(object):
    def __init__(self, state_dim, action_dim, action_bound, learning_rate, tau, gamma, params_l1, params_l2):
        self.state_dim = state_dim
        self.action_dim = action_dim
        self.action_bound = action_bound
        self.learning_rate = learning_rate
        self.tau  = tau
        self.gamma = gamma
        self.params_l1 = params_l1
        self.params_l2 = params_l2
        self.optimizer = tf.keras.optimizers.Adam(self.learning_rate)

        #Critic Network and parameters
        self.inputs_state, self.inputs_action, self.out = self.create_critic_network()
        self.critic_model = keras.Model(inputs=[self.inputs_state, self.inputs_action], outputs=self.out, name='critic_network')
        self.network_params = self.critic_model.trainable_variables

        #Target Critic Network and parameters
        self.target_inputs_state, self.target_inputs_action, self.target_out = self.create_critic_network()
        self.target_critic_model = keras.Model(inputs=[self.target_inputs_state, self.target_inputs_action], outputs=self.target_out, name='target_critic_network')
        self.target_network_params = self.target_critic_model.trainable_variables

        #initalizing the target params with then network params
        for i in range(len(self.target_network_params)):
            self.target_network_params[i].assign(self.network_params[i])

        #gradients of Q function with respect to actions

    def create_critic_network(self):
        inputs_state = Input(shape = (self.state_dim,), batch_size = None, name = "critic_input_state")
        inputs_action = Input(shape = (self.action_dim,), batch_size = None, name = "critic_input_action")
        w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)

        #first hidden layer
        net_state = layers.Dense(self.params_l1, name = 'critic_dense_1', kernel_initializer = w_init)(inputs_state)
        net_state = layers.BatchNormalization()(net_state)
        net_state = layers.Activation(activation=tf.nn.relu)(net_state)

        # second hidden layer
        net_state = layers.Dense(self.params_l2, name = 'critic_dense_2_state', kernel_initializer = w_init)(net_state)
        net_action = layers.Dense(self.params_l2, name = 'critic_dense_2_action', kernel_initializer = w_init)(inputs_action)
        net = layers.Add()([net_state, net_action])
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation=tf.nn.relu)(net)

        #w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)
        out = layers.Dense(1, name = 'Q_val', kernel_initializer = w_init)(net)
        return inputs_state, inputs_action, out

    def update_target_network(self):
        self.update_target_network_params = [self.target_network_params[i].assign(tf.multiply(self.network_params[i], self.tau) + tf.multiply(self.target_network_params[i], 1-self.tau)) for i in range(len(self.target_network_params))]

    def train(self, input_state, input_actions, predicted_q_val):
        with tf.GradientTape(watch_accessed_variables=True) as tape:
            prediction = self.critic_model([input_state, input_actions])
            loss = tf.keras.losses.MSE(prediction, predicted_q_val)
        gradients = tape.gradient(loss, self.network_params)
        self.optimizer.apply_gradients(zip(gradients, self.network_params))
        return self.critic_model([input_state, input_actions])

    def action_gradient(self, input_state, input_actions):
        var = tf.constant(input_actions)
        with tf.GradientTape(watch_accessed_variables=False) as tape_a:
            tape_a.watch(var)
            prediction_a = self.critic_model([input_state, var])
        return tape_a.gradient(prediction_a, var)

    def predict(self, inputs_state, inputs_actions):
        return self.critic_model([inputs_state, inputs_actions])

    def predict_target(self, inputs_state, inputs_actions):
        return self.target_critic_model([inputs_state, inputs_actions])

##---------------------------------------------------------------------------------------------------------------
def train(env, test_env, args, actor, critic, actor_noise, reward_result, scaler, replay_buffer):
    writer = tf.summary.create_file_writer(logdir = args['summary_dir'])
    actor.update_target_network()
    critic.update_target_network()

    paths = list()

    for i in range(args['max_episodes']):
        test_env.reset()
        s = env.reset()
        ep_reward = 0
        ep_ave_max_q = 0
        obs, actions, rewards = [], [], []
        if args['scaling']:
            var, mean = scaler.get()
        else:
            var, mean = 1.0, 0.0
        if i<10:
            temp_a = np.random.normal(0, 0.1)
        for j in range(args['max_episode_len']):
            s_scaled = np.float32((s - mean) * var)

            #noise annealing
            noise = np.random.normal(0, args['noise_var']/((j/100)+1))
            a = actor.predict(np.reshape(s_scaled,(1,actor.state_dim))) + noise
            #print(a)
            #actor_noise()
            #print(j,a)
            if i<10:
                a = tf.constant([[temp_a + noise]])
                #print(a)
            # action should be inbetween -1 and 1
            a = np.clip(a, -args['action_bound'], args['action_bound'])

            s2, r, terminal, info = env.step(a[0])
            s2_scaled = np.float32((s2 - mean) * var)
            replay_buffer.add(
                np.reshape(s_scaled, (actor.state_dim,)), np.reshape(a, (actor.action_dim,)), r, terminal, np.reshape(s2_scaled, (actor.state_dim,)))
            if replay_buffer.size() > args['mini_batch_size']:
                s_batch, a_batch, r_batch, t_batch, s2_batch = replay_buffer.sample_batch(args['mini_batch_size'])
                target_q = np.array(critic.predict_target(s2_batch, actor.predict_target(s2_batch)))
                y = []
                for k in range(args['mini_batch_size']):
                    if t_batch[k]:
                        y.append(r_batch[k])
                    else:
                        y.append(r_batch[k] + critic.gamma * np.array(target_q[k]))

                temp = np.reshape(y, (args['mini_batch_size'], 1))

                predicted_q_value = critic.train(s_batch, a_batch, temp.astype('float32'))

                ep_ave_max_q += np.amax(predicted_q_value)

                a_outs = actor.predict(s_batch)
                grads = critic.action_gradient(s_batch, a_outs)

                actor.train(s_batch, grads)
                actor.update_target_network()
                critic.update_target_network()

            s = s2
            ep_reward += r
            obs.append(s)
            actions.append(a[0])
            rewards.append(r)

            if j+1 == args['max_episode_len']:
                with writer.as_default():
                    tf.summary.scalar("Reward", ep_reward, step = i)
                    tf.summary.scalar("Qmax Value", ep_ave_max_q / float(j), step = i)
                    writer.flush()
                print('| Reward: {:.4f} | Episode: {:d} | Qmax: {:.4f}'.format((ep_reward), i, (ep_ave_max_q / float(j))))
                reward_result[i] = ep_reward
                path = {
                    "Observation":np.concatenate(obs).reshape((args['max_episode_len'],2)),
                    "Action":np.concatenate(actions),
                    "Reward":np.asarray(rewards)
                    }
                paths.append(path)
                #env.plot()
                test_s = test_env.reset()
                # if i+1 == args['max_episodes']:
                #     env.plot()
                #     test_s = test_env.reset()
                #     for _ in range(1000):
                #         test_s_scaled = np.float32((test_s - mean) * var)
                #         test_a = actor.predict(np.reshape(test_s_scaled,(1,actor.state_dim)))
                #         test_s, r, terminal, info = test_env.step(test_a[0])
                #     test_env.plot()
                break
    return [paths, reward_result]

##---------------------------------------------------------------------------------------------------------------
#------------------------------using RNN---------------------------------------------------------------
##---------------------------------------------------------------------------------------------------------------
class ActorNetwork_rnn(object):
    def __init__(self, state_dim, action_dim, action_bound, learning_rate, tau, batch_size, params_rnn, params_l1, params_l2, time_steps):
        self.state_dim = state_dim
        self.action_dim = action_dim
        self.action_bound = action_bound
        self.learning_rate = learning_rate
        self.tau  = tau
        self.batch_size = batch_size
        self.params_rnn = params_rnn
        self.params_l1 = params_l1
        self.params_l2 = params_l2
        self.time_steps = time_steps
        self.optimizer = tf.keras.optimizers.Adam(self.learning_rate)

        #actor network
        self.inputs, self.out, self.scaled_out = self.create_actor_network()
        self.actor_model = keras.Model(inputs=self.inputs, outputs=self.scaled_out, name='actor_network')
        self.network_params = self.actor_model.trainable_variables

        #target actor network
        self.target_inputs, self.target_out, self.target_scaled_out = self.create_actor_network()
        self.target_actor_model = keras.Model(inputs=self.target_inputs, outputs=self.target_scaled_out, name='target_actor_network')
        self.target_network_params = self.target_actor_model.trainable_variables

        #initalizing the target params with then network params
        for i in range(len(self.target_network_params)):
            self.target_network_params[i].assign(self.network_params[i])

    def create_actor_network(self):
        inputs = Input(shape = (self.time_steps, self.state_dim), batch_size = None, name = "actor_input_state")
        w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)
        conv = layers.Conv1D(filters=16, kernel_size = 2, activation='relu')(inputs)
        #maxpool = layers.MaxPooling1D(pool_size=2)(conv)
        lstm_net = layers.GRU(
            units= self.params_rnn,
            return_sequences=False,
            return_state=False,
            name = 'actor_rnn',
            #kernel_initializer = w_init
            )(conv)

        net = layers.Dense(self.params_l1, name = 'actor_dense_1', kernel_initializer = w_init)(lstm_net)
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation=tf.nn.relu)(net)

        net = layers.Dense(self.params_l2, name = 'actor_dense_2', kernel_initializer = w_init)(net)
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation='tanh')(net)

        out = layers.Dense(self.action_dim, activation='tanh', name = 'actor_dense_3', kernel_initializer = w_init)(net)
        scaled_out = tf.multiply(out, self.action_bound, name = "actions_scaling")
        return inputs, out, scaled_out

    def update_target_network(self):
        self.update_target_network_params = [self.target_network_params[i].assign(tf.multiply(self.network_params[i], self.tau) + tf.multiply(self.target_network_params[i], 1-self.tau)) for i in range(len(self.target_network_params))]

    def train(self, inputs, a_gradient):
        with tf.GradientTape(watch_accessed_variables=True) as tape:
            prediction = self.actor_model(inputs)
        unnormalized_actor_gradients = tape.gradient(prediction, self.network_params, output_gradients = -a_gradient)
        actor_gradients = list(map(lambda x: tf.math.divide(x, self.batch_size), unnormalized_actor_gradients))
        self.optimizer.apply_gradients(zip(actor_gradients, self.network_params))

    def predict(self, inputs):
        return self.actor_model(inputs)

    def predict_target(self, inputs):
        return self.target_actor_model(inputs)

##---------------------------------------------------------------------------------------------------------------
class CriticNetwork_rnn(object):
    def __init__(self, state_dim, action_dim, action_bound, learning_rate, tau, gamma, params_rnn, params_l1, params_l2, time_steps):
        self.state_dim = state_dim
        self.action_dim = action_dim
        self.action_bound = action_bound
        self.learning_rate = learning_rate
        self.tau  = tau
        self.gamma = gamma
        self.params_rnn = params_rnn
        self.params_l1 = params_l1
        self.params_l2 = params_l2
        self.time_steps = time_steps
        self.optimizer = tf.keras.optimizers.Adam(self.learning_rate)

        #Critic Network and parameters
        self.inputs_state, self.inputs_action, self.out = self.create_critic_network()
        self.critic_model = keras.Model(inputs=[self.inputs_state, self.inputs_action], outputs=self.out, name='critic_network')
        self.network_params = self.critic_model.trainable_variables

        #Target Critic Network and parameters
        self.target_inputs_state, self.target_inputs_action, self.target_out = self.create_critic_network()
        self.target_critic_model = keras.Model(inputs=[self.target_inputs_state, self.target_inputs_action], outputs=self.target_out, name='target_critic_network')
        self.target_network_params = self.target_critic_model.trainable_variables

        #initalizing the target params with then network params
        for i in range(len(self.target_network_params)):
            self.target_network_params[i].assign(self.network_params[i])

        #gradients of Q function with respect to actions

    def create_critic_network(self):
        inputs_state = Input(shape = (self.time_steps, self.state_dim), batch_size = None, name = "critic_input_state")
        inputs_action = Input(shape = ( self.action_dim,), batch_size = None, name = "critic_input_action")
        w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)

        #LSTM layer
        conv = layers.Conv1D(filters=16, kernel_size = 2, activation='relu')(inputs_state)
        #maxpool = layers.MaxPooling1D(pool_size=2)(conv)
        lstm_net = layers.GRU(units = self.params_rnn, return_sequences=False, return_state=False)(conv)

        #first hidden layer
        net_state = layers.Dense(self.params_l1, name = 'critic_dense_1', kernel_initializer = w_init)(lstm_net)
        net_state = layers.BatchNormalization()(net_state)
        net_state = layers.Activation(activation=tf.nn.relu)(net_state)

        # second hidden layer
        net_state = layers.Dense(self.params_l2, name = 'critic_dense_2_state', kernel_initializer = w_init)(net_state)
        net_action = layers.Dense(self.params_l2, name = 'critic_dense_2_action', kernel_initializer = w_init)(inputs_action)

        # net = layers.Add()([net_state, net_action])
        net = layers.concatenate([net_state, net_action])
        net = layers.Dense(self.params_l2, name = 'critic_dense_3_state', kernel_initializer = w_init)(net)
        net = layers.BatchNormalization()(net)
        net = layers.Activation(activation=tf.nn.relu)(net)

        #w_init = tf.random_uniform_initializer(minval=-0.03, maxval=0.03, seed=None)
        out = layers.Dense(1, name = 'Q_val', kernel_initializer = w_init)(net)
        return inputs_state, inputs_action, out

    def update_target_network(self):
        self.update_target_network_params = [self.target_network_params[i].assign(tf.multiply(self.network_params[i], self.tau) + tf.multiply(self.target_network_params[i], 1-self.tau)) for i in range(len(self.target_network_params))]

    def train(self, input_state, input_actions, predicted_q_val):
        with tf.GradientTape(watch_accessed_variables=True) as tape:
            prediction = self.critic_model([input_state, input_actions])
            #print('predicted_q_val', np.shape(predicted_q_val))
            #print('prediction', np.shape(prediction))
            loss = tf.keras.losses.MSE(prediction, predicted_q_val)
        gradients = tape.gradient(loss, self.network_params)
        self.optimizer.apply_gradients(zip(gradients, self.network_params))
        return self.critic_model([input_state, input_actions])

    def action_gradient(self, input_state, input_actions):
        var = tf.constant(input_actions)
        with tf.GradientTape(watch_accessed_variables=False) as tape_a:
            tape_a.watch(var)
            prediction_a = self.critic_model([input_state, var])
        return tape_a.gradient(prediction_a, var)

    def predict(self, inputs_state, inputs_actions):
        return self.critic_model([inputs_state, inputs_actions])

    def predict_target(self, inputs_state, inputs_actions):
        return self.target_critic_model([inputs_state, inputs_actions])

##---------------------------------------------------------------------------------------------------------------
def train_rnn(env, test_env, args, actor, critic, actor_noise, reward_result, scaler, replay_buffer, true_states):
    writer = tf.summary.create_file_writer(logdir = args['summary_dir'])
    actor.update_target_network()
    critic.update_target_network()
    time_steps = args['time_steps']

    paths = list()

    for i in range(args['max_episodes']):
        test_env.reset()
        s = env.reset()
        ep_reward = 0
        ep_ave_max_q = 0
        obs, obs_scaled, actions, rewards = [], [], [], []
        if args['scaling']:
            var, mean = scaler.get()
        else:
            var, mean = 1.0, 0.0
        # if i<10:
        #     temp_a = np.random.normal(0, 0.01)
        for _ in range(args['time_steps']-1):
            s_scaled = np.float32((s - mean) * var)
            obs_scaled.append(s_scaled)
            obs.append(s)
            s, r, terminal, info = env.step(np.array(env.action_space.sample(), dtype="float32"))
            actions.append(np.array(env.action_space.sample()))

        s_scaled = np.float32((s - mean) * var)
        obs_scaled.append(s_scaled)
        obs.append(s)
        actions.append(np.array(env.action_space.sample()))
        for j in range(args['max_episode_len']-args['time_steps']+1):

            S_0 = obs_scaled[-args['time_steps']: ]

            #noise annealing
            noise = np.random.normal(0, args['noise_var']/((j/100)+1))
            a = actor.predict(np.reshape(S_0, (1, args['time_steps'], args['state_dim']))) + noise

            # action should be inbetween -1 and 1
            #print(a)
            a = np.clip(a, -args['action_bound'], args['action_bound'])

            s2, r, terminal, info = env.step(a[0])
            s2_scaled = np.float32((s2 - mean) * var)

            #noise annealing
            obs_scaled.append(s2_scaled)
            S_2 = obs_scaled[-args['time_steps']: ]

            replay_buffer.add(S_0, np.reshape(a, (actor.action_dim,)), r, terminal, S_2)
            if replay_buffer.size() > args['mini_batch_size']:
                s_batch, a_batch, r_batch, t_batch, s2_batch = replay_buffer.sample_batch(args['mini_batch_size'])

                target_q = np.array(critic.predict_target(s2_batch, actor.predict_target(s2_batch)))
                y = []
                for k in range(args['mini_batch_size']):
                    if t_batch[k]:
                        y.append(r_batch[k])
                    else:
                        y.append(r_batch[k] + critic.gamma * np.array(target_q[k]))

                temp = np.reshape(y, (args['mini_batch_size'], 1))

                predicted_q_value = critic.train(s_batch, a_batch, temp.astype('float32'))

                ep_ave_max_q += np.amax(predicted_q_value)

                a_outs = actor.predict(s_batch)
                grads = critic.action_gradient(s_batch, a_outs)

                actor.train(s_batch, grads)
                actor.update_target_network()
                critic.update_target_network()

            s = s2
            ep_reward += r
            obs.append(s)
            actions.append(a[0])
            rewards.append(r)
            #print('j: {}, max episode len: {}, terminal: {}, steps-count: {}'.format(j,args['max_episode_len'], terminal, env.count_steps))
            #if j+args['time_steps'] == args['max_episode_len']:
            if terminal:
                with writer.as_default():
                    tf.summary.scalar("Reward", ep_reward, step = i)
                    tf.summary.scalar("Qmax Value", ep_ave_max_q / float(j), step = i)
                    writer.flush()
                print('| Reward: {:.4f} | Episode: {:d} | Qmax: {:.4f}'.format((ep_reward), i, (ep_ave_max_q / float(j))))
                reward_result[i] = ep_reward
                path = {
                    "Observation":np.concatenate(obs).reshape((args['max_episode_len']+1,2)),
                    "Action":np.concatenate(actions),
                    "Reward":np.asarray(rewards)
                    }
                paths.append(path)
                #env.plot(true_states, plot_original=False, savefig_filename = path+'/results/cbf_plot')
                #test_s = test_env.reset()
                # if i+1 == args['max_episodes']:
                #     env.plot()
                #     test_s = test_env.reset()
                #     for _ in range(1000):
                #         test_s_scaled = np.float32((test_s - mean) * var)
                #         test_a = actor.predict(np.reshape(test_s_scaled,(1,actor.state_dim)))
                #         test_s, r, terminal, info = test_env.step(test_a[0])
                #     test_env.plot()
                break
    return [paths, reward_result]
##----------------------------------------------------------------------------------------------------------------------------
##-------------------------------------------With CBF---------------------------------------------------
##----------------------------------------------------------------------------------------------------------------------------
def train_rnn_cbf(env, test_env, args, actor, critic, actor_noise, reward_result, scaler, replay_buffer, true_states, CBF_rl):
    print('running - train_rnn_cbf')
    writer = tf.summary.create_file_writer(logdir = args['summary_dir'])
    actor.update_target_network()
    critic.update_target_network()
    time_steps = args['time_steps']
    delta_u = args['T_set_max_min'][1] - args['T_set_max_min'][0]

    paths = list()

    for i in range(args['max_episodes']):
        test_env.reset()
        s = env.reset()
        ep_reward = 0
        ep_ave_max_q = 0
        obs, obs_scaled, actions, rewards = [], [], [], []
        if args['scaling']:
            var, mean = scaler.get()
        else:
            var, mean = 1.0, 0.0
        # if i<10:
        #     temp_a = np.random.normal(0, 0.01)
        for _ in range(args['time_steps']-1):
            s_scaled = np.float32((s - mean) * var)
            obs_scaled.append(s_scaled)
            obs.append(s)
            #sampling a random input (-1,1)
            a_rl = env.action_space.sample()
            #rescaling the input to (u_min, u_max)
            T_rl = (a_rl + 1)*(delta_u/2) + args['T_set_max_min'][0]
            #Projection
            delta_cbf = CBF_rl(env, T_rl[0], T_min =args['T_max_min'][0], T_max=args['T_max_min'][1], eta_1 = 0.5, eta_2 = 0.5)
            T_cbf = T_rl + delta_cbf
            #rescaling the input to (-1, 1)
            a_cbf = (T_cbf - args['T_set_max_min'][0])*(2/delta_u) - 1

            #next step
            s, _,_,_ = env.step(a_cbf)
            actions.append(T_cbf)

        s_scaled = np.float32((s - mean) * var)
        obs_scaled.append(s_scaled)
        obs.append(s)
        actions.append(np.array(env.action_space.sample()))
        for j in range(args['max_episode_len']-args['time_steps']+1):

            S_0 = obs_scaled[-args['time_steps']: ]
            #print('i ={} , j={}'.format(i,j))
            #noise annealing
            noise = np.random.normal(0, args['noise_var']/((j/100)+1))
            a_rl = actor.predict(np.reshape(S_0, (1, args['time_steps'], args['state_dim']))) + noise
            # action should be inbetween -1 and 1
            #a_rl = np.clip(a_rl, -args['action_bound'], args['action_bound'])[0]
            #rescaling the input to (u_min, u_max)
            T_rl = (a_rl.numpy()[0] + 1)*(delta_u/2) + args['T_set_max_min'][0]
            #Projection
            delta_cbf = CBF_rl(env, T_rl[0], T_min =args['T_max_min'][0], T_max=args['T_max_min'][1], eta_1 = 0.5, eta_2 = 0.5)
            T_cbf = T_rl + delta_cbf
            #rescaling the input to (-1, 1)
            a_cbf = (T_cbf - args['T_set_max_min'][0])*(2/delta_u) - 1
            #print('a_rl = {}, T_rl = {}, delta_cbf = {}, T_cbf ={}, a_cbf = {}'.format(a_rl,T_rl,delta_cbf,T_cbf,a_cbf))
            s2, r, terminal, info = env.step(a_cbf)
            s2_scaled = np.float32((s2 - mean) * var)

            #noise annealing
            obs_scaled.append(s2_scaled)
            S_2 = obs_scaled[-args['time_steps']: ]

            replay_buffer.add(S_0, np.reshape(T_cbf, (actor.action_dim,)), r, terminal, S_2)
            if replay_buffer.size() > args['mini_batch_size']:
                s_batch, a_batch, r_batch, t_batch, s2_batch = replay_buffer.sample_batch(args['mini_batch_size'])

                target_q = np.array(critic.predict_target(s2_batch, actor.predict_target(s2_batch)))
                y = []
                for k in range(args['mini_batch_size']):
                    if t_batch[k]:
                        y.append(r_batch[k])
                    else:
                        y.append(r_batch[k] + critic.gamma * np.array(target_q[k]))

                temp = np.reshape(y, (args['mini_batch_size'], 1))

                predicted_q_value = critic.train(s_batch, a_batch, temp.astype('float32'))

                ep_ave_max_q += np.amax(predicted_q_value)

                a_outs = actor.predict(s_batch)
                grads = critic.action_gradient(s_batch, a_outs)

                actor.train(s_batch, grads)
                actor.update_target_network()
                critic.update_target_network()

            s = s2
            ep_reward += r
            obs.append(s)
            actions.append(a_cbf)
            rewards.append(r)
            #print('j: {}, max episode len: {}, terminal: {}, steps-count: {}'.format(j,args['max_episode_len'], terminal, env.count_steps))
            #print("episode reward", ep_reward, r)
            #if j+args['time_steps'] == args['max_episode_len']:
            if terminal:
                with writer.as_default():
                    tf.summary.scalar("Reward", ep_reward, step = i)
                    tf.summary.scalar("Qmax Value", ep_ave_max_q / float(j), step = i)
                    writer.flush()
                print('| Reward: {:.4f} | Episode: {:d} | Qmax: {:.4f}'.format((ep_reward), i, (ep_ave_max_q / float(j))))
                reward_result[i] = ep_reward
                path = {
                    "Observation":np.concatenate(obs).reshape((args['max_episode_len']+1,2)),
                    "Action":np.concatenate(actions),
                    "Reward":np.asarray(rewards)
                    }
                paths.append(path)
                #env.plot(true_states, plot_original=False)
                #test_s = test_env.reset()
                # if i+1 == args['max_episodes']:
                #     env.plot()
                #     test_s = test_env.reset()
                #     for _ in range(1000):
                #         test_s_scaled = np.float32((test_s - mean) * var)
                #         test_a = actor.predict(np.reshape(test_s_scaled,(1,actor.state_dim)))
                #         test_s, r, terminal, info = test_env.step(test_a[0])
                #     test_env.plot()
                break
    return [paths, reward_result]

max_stars_repo_name: asokraju/RL-buildings

max_issues_repo_name: asokraju/RL-buildings

max_forks_repo_name: asokraju/RL-buildings

reason: import numpy,from scipy


text:
import gym
import time
#  from keras.models import Sequential
#  from keras.layers import Dense, Activation
import collections
from matplotlib import pyplot as plt
import numpy as np
from math import sqrt
from timeit import default_timer as timer
import multiprocessing
import thread

# Suppress Warnings
ERROR = 40
gym.logger.set_level(ERROR)

class BlackBox:
    def __init__(self):
        self._init_model()

    def _init_model(self, shape=[2,2], actions=2):
        from keras.models import Sequential
        from keras.layers import Dense, Activation
        model = Sequential([
            Dense(2, input_dim=4, activation='relu'),
            Dense(actions, activation='softmax'),
            ])
        self.model = model
        self.shape = shape
        self.actions = actions
        return

    def flatten(self, weights):
        w = []
        for l in weights:
            if isinstance(l, collections.Iterable):
                w = w + self.flatten(l)
            else:
                w = w + [l]
        return w

    def unflatten(self, flat_weights):
        #  import tensorflow as tf
        print("here in unflatten")
        w = []
        i = 0
        for l, size in enumerate(self.shape):
            print("here in unflatten loop")
            layer = self.model.layers[l]
            print("here in unflatten loop2")
            layer = layer.get_weights()
            #  layer = layer.weights
            print("here in unflatten loop3")
            #  import tensorflow as tf
            #  layer = tf.keras.backend.batch_get_value(layer)
            print("here in unflatten loop4")
            params = layer[0]
            bias = layer[1]
            new_layer = []
            new_params = []
            new_bias = []
            for param in params:
                print("here in unflatten params")
                new_params.append(flat_weights[i:i+size])
                i += size
            for b in bias:
                new_bias.append(flat_weights[i])
                i += 1
            w.append(np.array(new_params))
            w.append(np.array(new_bias))
        return w

    def get_weights(self):
        return self.model.get_weights()

    def set_weights(self, weights):
        print("here in set weights")
        self.model.set_weights(weights)

    def get_flat_weights(self):
        return self.flatten(self.get_weights())

    def set_flat_weights(self, flat_weights):
        return self.set_weights(self.unflatten(flat_weights))

    def produce_action(self, state):
        inp = np.array([np.array(state).T])
        action_dist = self.model.predict(inp)
        action = np.random.choice(self.actions,1,p=action_dist[0])[0]
        #  print("state: ", state, "actions: ", action_dist, "take: ", action)
        return action

class CartPoleES:
    def __init__(self,
            env = 'CartPole-v1',
            pop_size=200,
            alpha=0.1,
            sigma=3,
            max_generations=50,
            convergence_reward=195,
            convergence_trials=100,
            max_steps=200,
            device='cpu',
            num_cores=1,
            verbose=1,
            ):
        self.pop_size=pop_size
        self.alpha=alpha
        self.sigma=sigma
        self.max_generations=max_generations
        self.convergence_reward=convergence_reward
        self.convergence_trials=convergence_trials
        self.max_steps=max_steps
        self.device=device
        self.num_cores=num_cores
        self.env = env
        self.verbose = verbose
        return

    def test_brain(self, brain, bb):
        local_env = gym.make(self.env)
        observation = local_env.reset()
        bb.set_flat_weights(brain)
        agent_fitness = 0
        steps = 0
        # An agent gets a single episode to determine its fitness.
        # In CartPole, fitness is equal to number of steps the the
        # pole remains balanced
        for step in range(self.max_steps):
            steps += 1
            action = bb.produce_action(np.array(list(observation)))
            observation, reward, done, info = local_env.step(action)
            agent_fitness += reward
            # If the agents loses balance of the pole, the episode ends
            if done:
                break
        return agent_fitness, steps

    def eval_population(self, population):
        #  import tensorflow as tf
        #  import keras
        bb = BlackBox()
        F = []
        trials = 0
        for brain in population:
            agent_fitness, steps = self.test_brain(brain, bb)
            print("Agent Fitness: ", agent_fitness)
            F.append(agent_fitness)
            trials += 1
        return F, trials, steps

    def train(self, num_tests=1, num_processes=2):
        print("Using {} processes.".format(num_processes))
        history = []
        for test in range(num_tests):
            wall_time_start = timer()
            total_trials = 0
            total_steps = 0
            total_generations = 0
            generation_times = []
            converged = False
            bbs = [BlackBox() for t in range(num_processes)]
            weights = bbs[0].get_flat_weights()
            convergence_test = collections.deque(maxlen=int(sqrt(self.convergence_trials)))
            fitness_history = []
            for generation in range(self.max_generations):
                total_generations = generation+1
                generation_time_start = timer()
                mutations = np.random.randn(self.pop_size, len(weights))
                population = weights + self.sigma*mutations
                sub_population_size = len(population) // len(bbs)
                jobs = []
                for process in range(num_processes):
                    #  bb = bbs[process]
                    start_index = sub_population_size * process
                    sub_population = population[start_index:start_index+sub_population_size]
                    print("Spawning subprocess for sub_population: ", start_index, start_index+sub_population_size)
                    p = multiprocessing.Process(target=self.eval_population, args=(sub_population,))
                    jobs.append(p)
                    p.start()
                print(jobs)
                results = [j.join() for j in jobs]
                F = [r[0] for r in results]
                F = [item for sublist in F for item in sublist]
                weights = weights + self.alpha*(1/(self.pop_size*self.sigma))*(mutations.T*F).T.sum(axis=0)
                generation_time_end = timer()
                current_fitness = self.test_weights(weights)
                gen_time = generation_time_end - generation_time_start
                if self.verbose:
                    print(
                        'Gen: ', generation,
                        '| Generation Time: ' + str(gen_time),
                        '| Fitness: %.1f\n' % current_fitness,
                        )
                fitness_history.append(current_fitness)
                convergence = fitness_history[-int(sqrt(self.convergence_trials)):]
                if (sum(convergence) / len(convergence)) >= self.convergence_reward:
                    converged = True
                    wall_time_end = timer()
                    wall_time = wall_time_end - wall_time_start
                    print("Convergence Reached after {0} Generations, in {1} seconds.".format(generation+1, wall_time))
                    break
                if generation == (self.max_generations - 1):
                    converged = False
                    wall_time_end = timer()
                    wall_time = wall_time_end - wall_time_start
                    print("Convergence Not Reached after {0} Generations, in {1} seconds.".format(generation+1, wall_time))

            self.trained_weights = weights
            hist = {
                    'wall_time': wall_time,
                    'fitness_history': fitness_history,
                    'total_generations': total_generations,
                    'generation_times': generation_times,
                    'total_trials': total_trials,
                    'total_steps': total_steps,
                    'converged': converged,
                    'test_id': test,
                    }
            history.append(hist)
        return history

    def save_weights(self, filename="ES-CartPole-v1.hdf5", experiment_id=None):
        bb.set_flat_weights(self.trained_weights)
        if experiment_id:
            filename = str(experiment_id) + filename
        bb.model.save(filename)

    def test_weights(self, w):
        bb = BlackBox()
        bb.set_flat_weights(w)
        env = gym.make(self.env)
        num_trials = int(sqrt(self.convergence_trials))
        average_fitness = 0
        for i in range(num_trials):
            fitness = 0
            observation = env.reset()
            for t in range(self.max_steps):
                action = bb.produce_action(np.array(list(observation)))
                observation, reward, done, info = env.step(action)
                fitness += reward
                if done:
                    break
            average_fitness += fitness / num_trials
        return average_fitness

ext: py

reason: import numpy


text:
import numpy as np
np.random.seed(1337)  # for reproducibility, needs to be the first 2 lines in a script

import logging

from keras.models import Model, load_model
from train import prepare_data, predict_and_eval
from keras import metrics
from utils import *
import json

max_sequence_size = 2000

batch_size = 500

functions_map = ['0046872', '0003676', '0008270', '0003677', '0005524']

def get_sequences_with_unkonwn_annotations(sequences_file, functions_file):
    # input protein-function map
    protein_function_map = {}
    with open(functions_file) as fn_file:
        protein_function_map = json.load(fn_file)

    proteins_of_interest = []
    # open sequences file. Has:  protein_id,seq
    i = 0
    with open(sequences_file) as f:
        for line in f:
            ln = line.split(',')
            protein_id = ln[0].strip()
            seq = ln[1].strip()

            if protein_id in protein_function_map:
                continue # have annotation. Don't need it.

            seq_x = sequence_to_indices(seq)
            seq_x = sequence.pad_sequences([seq_x], maxlen=max_sequence_size)
            seq_x = seq_x[0]
            proteins_of_interest.append((protein_id, seq, seq_x))

            i += 1
            # if i > 2: break

    return proteins_of_interest

def predict(model, proteins_of_interest, out_file):

    # get sequences only
    seqs = np.array([i[2] for i in proteins_of_interest])
    predictions = model.predict(seqs, verbose=1)

    out_str = ''
    for protein_index, pred in enumerate(predictions):
        for i, val in enumerate(pred):
            if val >= 0.5:
                out_str += proteins_of_interest[protein_index][0] + ' GO:' + functions_map[i] + ";\n"

    with open(out_file, "w") as text_file:
        text_file.write(out_str)

if __name__ == '__main__':
    model_filename = "../results/00061-saved-model.h5"
    sequences_file = "../data/protein-seqs-2017-01-23-203946.txt"
    functions_file = "../data/protein-functions-2017-01-23-203946.txt"
    out_file = "../data/predictions-unknowns-00002"

    proteins_of_interest = get_sequences_with_unkonwn_annotations(sequences_file, functions_file)
    # print seqs

    model = load_model(model_filename)
    predict(model, proteins_of_interest, out_file)

max_stars_repo_name: recluze/deepseq

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_name: recluze/deepseq

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_name: recluze/deepseq

max_forks_repo_licenses: MIT Unlicense


text:
from collections import defaultdict
from PIL import ImageFont
import numpy as np
import argparse
import math
import cv2

FPS=1.0

def get_parser():
    parser = argparse.ArgumentParser(description='RSP Solution Visualization')
    parser.add_argument('rnet_file')
    parser.add_argument('prob_file')
    parser.add_argument('sol_file')
    parser.add_argument('-o', '--out', metavar='OUTPUT', help='output video path (default: a.mp4)',
                        default='a.mp4')
    return parser

class Visualizer:
    map_scale = 5000
    output_file = None
    draw_inactive = False
    # colors from https://materializecss.com/color.html
    # and colorbrewer2.org
    load_color_map = [(180,117,69),(209,173,116),(233,217,171),(248,243,224),
            (144,224,254),(97,174,253),(67,109,244),(39,48,215)]
    #load_color_map = [(196, 203, 128), (172, 182, 77), (154, 166, 38), (136, 150, 0),
    #                  (123, 137, 0), (107, 121, 0), (92, 105, 0), (64, 77, 0)]  # teal
    background_color = (56, 50, 38)  # dark-green darken-4
    vehicle_finish_color = (139, 125, 96)  # cyan
    customer_timeout_color = (158, 158, 158)  # grey
    customer_waiting_color = (7, 193, 255)  # amber
    customer_assigned_color = (74, 195, 139)  # light green
    customer_dropped_color = (139, 125, 96)  # cyan
    # road_color = (224, 224, 224)  # light grey
    road_color = (117, 117, 117)  # grey darken-1
    text_color = (224, 224, 224)  # grey lighten-2
    customer_size = 4
    vehicle_size = 8
    vehicle_border_size = 0
    road_size = 1
    text_size = 1

    node = {}
    edge = defaultdict(list)
    long_upper = 0
    long_lower = 0
    lat_upper = 0
    lat_lower = 0
    video_width = 0
    video_height = 0
    screen = None

    vehicle = {}
    vehicle_count = 0
    vehicle_map = {}
    customer = {}
    customer_count = 0
    show_vehicle_route = []

    def __init__(self, rnet_file, prob_file, sol_file, output_file, map_scale=5000,
                 draw_inactive=True, fps=FPS):
        self.output_file = output_file
        self.map_scale = map_scale
        self.draw_inactive = draw_inactive
        self.fps = fps
        self.text_size = self.text_size * map_scale / 8000
        self.get_background(rnet_file)
        self.get_problem(prob_file)
        self.run_solution(sol_file)

    def get_background(self, rnet_file):
        """ Generate road network background from rnet_file """
        min_long = 10000
        min_lat = 10000
        max_long = -10000
        max_lat = -10000
        try:
            with open(rnet_file) as f_rnet:
                for line in f_rnet:
                    col = line.strip().split()
                    # get picture bounds
                    longitude = float(col[3])
                    latitude = float(col[4])
                    if longitude < min_long:
                        min_long = longitude
                    if longitude > max_long:
                        max_long = longitude
                    if latitude < min_lat:
                        min_lat = latitude
                    if latitude > max_lat:
                        max_lat = latitude
                    self.node[int(col[1])] = key = (longitude, latitude)
                    longitude = float(col[5])
                    latitude = float(col[6])
                    if longitude < min_long:
                        min_long = longitude
                    if longitude > max_long:
                        max_long = longitude
                    if latitude < min_lat:
                        min_lat = latitude
                    if latitude > max_lat:
                        max_lat = latitude
                    self.node[int(col[2])] = value = (longitude, latitude)
                    self.edge[key].append(value)

            # video padding
            self.long_upper = max_long + 0.005
            self.long_lower = min_long - 0.005
            self.lat_upper = max_lat + 0.005
            self.lat_lower = min_lat - 0.005 - 100 / self.map_scale
            print('Longitude bounds:', self.long_lower, self.long_upper)
            print('Latitude bounds:', self.lat_lower, self.lat_upper)
            self.video_width = math.floor((self.long_upper - self.long_lower) * self.map_scale)
            self.video_height = math.floor((self.lat_upper - self.lat_lower) * self.map_scale)
            self.screen = np.full((self.video_height, self.video_width, 3), self.background_color,
                                  dtype=np.uint8)
            print('Video resolution:', self.video_width, self.video_height)

            # draw road network
            for k, v_list in self.edge.items():
                x_k = self.long2x(k[0])
                y_k = self.lat2y(k[1])
                for v in v_list:
                    x_v = self.long2x(v[0])
                    y_v = self.lat2y(v[1])
                    cv2.line(self.screen, (x_k, y_k), (x_v, y_v), self.road_color, self.road_size,
                             cv2.LINE_AA)

            # draw legends
            x_start = 20
            y_start = self.video_height - 80
            text = 'Vehicle'
            # font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=1)
            # print(cv2.getTextSize(text, fontFace=font, fontScale=1))
            # print(cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=.5, thickness=1))
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20
            for i in range(0, 8):
                cv2.rectangle(self.screen, (x_start, y_start),
                              (x_start + self.vehicle_size * 2 + 1,
                               y_start + self.vehicle_size * 2 + 1),
                              self.load_color_map[i], cv2.FILLED)
                x_start += self.vehicle_size * 2 + 1
            x_start += 10
            text = 'load(0~7)'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20
            cv2.rectangle(self.screen, (x_start, y_start),
                          (x_start + self.vehicle_size * 2 + 1,
                           y_start + self.vehicle_size * 2 + 1),
                          self.vehicle_finish_color, cv2.FILLED)
            x_start += self.vehicle_size * 2 + 11
            text = 'inactive'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)

            x_start = 20
            y_start += text_height + 20
            text = 'Customer'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20

            # draw waiting
            cv2.circle(self.screen, (x_start, y_start + text_height // 2), self.customer_size,
                       self.customer_waiting_color, cv2.FILLED, cv2.LINE_AA)
            x_start += self.customer_size + 10
            text = 'waiting'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20

            # draw assigned
            cv2.circle(self.screen, (x_start, y_start + text_height // 2), self.customer_size,
                       self.customer_assigned_color, cv2.FILLED, cv2.LINE_AA)
            x_start += self.customer_size + 10
            text = 'assigned'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20

            # draw dropped
            cv2.circle(self.screen, (x_start, y_start + text_height // 2), self.customer_size,
                       self.customer_dropped_color, cv2.FILLED, cv2.LINE_AA)
            x_start += self.customer_size + 10
            text = 'dropped'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20

            # draw timeout
            cv2.circle(self.screen, (x_start, y_start + text_height // 2), self.customer_size,
                       self.customer_timeout_color, cv2.FILLED, cv2.LINE_AA)
            x_start += self.customer_size + 10
            text = 'timeout'
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_TRIPLEX,
                                                                  self.text_size, 1)
            cv2.putText(self.screen, text, (x_start, y_start + text_height),
                        cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1, cv2.LINE_AA)
            x_start += text_width + 20
            # cv2.imwrite('a.png', self.screen, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
        except BaseException:
            print('Reading rnet file failed')
            raise

    def get_problem(self, prob_file):
        """ Load vehicles and customers from problem instance """
        try:
            with open(prob_file) as f_prob:
                lines = f_prob.readlines()[6:]
                for line in lines:
                    col = line.split()
                    id, o, d, q, e = int(col[0]), int(col[1]), int(col[2]), int(col[3]), int(col[4])
                    if q < 0:
                        # early time, origin, destination, load, route, position
                        self.vehicle[id] = Vehicle(e, o, d)
                        # self.vehicle_map[id] = len(self.vehicle) - 1
                    else:
                        # early time, origin, destination, status
                        self.customer[id] = Customer(e, o, d)
                self.vehicle_count = len(self.vehicle)
                self.customer_count = len(self.customer)
        except BaseException:
            print('Reading prob file failed')
            raise

    def update(self, action, data):
        if action == 'R':
            vid = int(data[0])
            self.vehicle[vid].r.clear()
            for index in range(1, len(data)):
                self.vehicle[vid].r.append(int(data[index]))
        elif action == 'V':
            for index in range(0, len(data), 2):
                self.vehicle[int(data[index])].p = int(data[index + 1])
        elif action == 'P':
            for cid in data:
                cid = int(cid)
                self.customer[cid].s = 2
                self.vehicle[self.customer[cid].m].l += 1
        elif action == 'D':
            for cid in data:
                cid = int(cid)
                self.customer[cid].s = 3
                self.vehicle[self.customer[cid].m].l -= 1
        elif action == 'T':
            for cid in data:
                self.customer[int(cid)].s = 4
        elif action == 'M':
            vid = int(data[0])
            for index in range(1, len(data)):
                cid = int(data[index])
                if cid < 0:
                    self.customer[cid].m = 0
                    self.customer[cid].s = 0
                    # TODO: remove
                else:
                    self.customer[cid].m = vid
                    self.customer[cid].s = 1
                    self.vehicle[vid].m.append(cid)
                    if len(self.vehicle[vid].m) == 6:
                        # print(vid)
                        self.show_vehicle_route.append(vid)
        elif action == 'A':
            for vid in data:
                vid = int(vid)
                self.vehicle[vid].s = 2
                if vid in self.show_vehicle_route:
                    self.show_vehicle_route.remove(vid)

    def run_solution(self, log_file):
        """ Run solution simulation and write it to output video """
        out = cv2.VideoWriter(self.output_file, cv2.VideoWriter_fourcc(*'MP4V'), self.fps,
                              (self.video_width, self.video_height))
        with open(log_file) as f_log:
            time_count = 0
            # for line in lines:
            line = f_log.readline()
            while line:
                line = line.strip()
                col = line.split(' ')
                if len(col) is 0:
                    break
                while col[0].isdigit() and int(col[0]) == time_count:
                    self.update(col[1], col[2:])
                    line = f_log.readline()
                    col = line.split(' ')

                canvas = self.screen.copy()
                # canvas = self.screen

                # draw routes
                for vid in self.show_vehicle_route:
                    route = self.vehicle[vid].r
                    if len(route) is 0:
                        continue
                    node_1 = route[0]
                    for index in range(1, len(route)):
                        node_2 = route[index]
                        a_x = self.long2x(self.node[node_1][0])
                        a_y = self.lat2y(self.node[node_1][1])
                        b_x = self.long2x(self.node[node_2][0])
                        b_y = self.lat2y(self.node[node_2][1])
                        cv2.line(canvas, (a_x, a_y), (b_x, b_y), (255, 255, 255), 1, cv2.LINE_AA)
                        node_1 = node_2

                # draw customers
                for cid in self.customer:
                    if time_count >= self.customer[cid].e:
                        status = self.customer[cid].s
                        o_x = self.long2x(self.node[self.customer[cid].o][0])
                        o_y = self.lat2y(self.node[self.customer[cid].o][1])
                        d_x = self.long2x(self.node[self.customer[cid].d][0])
                        d_y = self.lat2y(self.node[self.customer[cid].d][1])
                        if status == 0:
                            cv2.circle(canvas, (o_x, o_y), self.customer_size,
                                       self.customer_waiting_color, cv2.FILLED, cv2.LINE_AA)
                        elif status == 1:
                            cv2.circle(canvas, (o_x, o_y), self.customer_size,
                                       self.customer_assigned_color, cv2.FILLED, cv2.LINE_AA)
                        elif status == 2:
                            pass
                        elif status == 3:
                            cv2.circle(canvas, (d_x, d_y), self.customer_size,
                                       self.customer_dropped_color, cv2.FILLED, cv2.LINE_AA)
                        elif status == 4:
                            if self.draw_inactive:
                                cv2.circle(canvas, (o_x, o_y), self.customer_size,
                                           self.customer_timeout_color, cv2.FILLED, cv2.LINE_AA)

                # draw vehicles
                for vid in self.vehicle:
                    if time_count >= self.vehicle[vid].e:
                        status = self.vehicle[vid].s
                        if status == 2:
                            if self.draw_inactive:
                                d_x = self.long2x(self.node[self.vehicle[vid].d][0])
                                d_y = self.lat2y(self.node[self.vehicle[vid].d][1])
                                cv2.circle(canvas, (d_x, d_y), self.vehicle_size+self.vehicle_border_size,
                                           (255,255,255), cv2.FILLED, cv2.LINE_AA)
                                cv2.circle(canvas, (d_x, d_y), self.vehicle_size,
                                           self.vehicle_finish_color, cv2.FILLED, cv2.LINE_AA)
                                #cv2.rectangle(canvas,
                                #              (d_x - self.vehicle_size, d_y - self.vehicle_size),
                                #              (d_x + self.vehicle_size, d_y + self.vehicle_size),
                                #              self.vehicle_finish_color, cv2.FILLED)
                        else:
                            x = self.long2x(self.node[self.vehicle[vid].p][0])
                            y = self.lat2y(self.node[self.vehicle[vid].p][1])
                            if vid in self.show_vehicle_route:
                                cv2.circle(canvas, (x, y), self.vehicle_size+self.vehicle_border_size,
                                           (255,255,255), cv2.FILLED, cv2.LINE_AA)
                                cv2.circle(canvas, (x, y), self.vehicle_size,
                                           (0, 0, 255), cv2.FILLED, cv2.LINE_AA)
                                #cv2.rectangle(canvas,
                                #              (x - self.vehicle_size, y - self.vehicle_size),
                                #              (x + self.vehicle_size, y + self.vehicle_size),
                                #              (0, 0, 255), cv2.FILLED)
                            else:
                                cv2.circle(canvas, (x, y), self.vehicle_size+self.vehicle_border_size,
                                           (255,255,255), cv2.FILLED, cv2.LINE_AA)
                                cv2.circle(canvas, (x, y), self.vehicle_size,
                                           self.load_color_map[self.vehicle[vid].l], cv2.FILLED, cv2.LINE_AA)
                                #cv2.rectangle(canvas,
                                #              (x - self.vehicle_size, y - self.vehicle_size),
                                #              (x + self.vehicle_size, y + self.vehicle_size),
                                #              self.load_color_map[self.vehicle[vid].l], cv2.FILLED)

                # draw time
                text = 'Time: %d' % time_count
                (text_width, text_height), baseline = cv2.getTextSize(text,
                                                                      cv2.FONT_HERSHEY_TRIPLEX,
                                                                      self.text_size, 1)
                cv2.putText(canvas, text, (5, 5 + text_height),
                            cv2.FONT_HERSHEY_TRIPLEX, self.text_size, self.text_color, 1,
                            cv2.LINE_AA)

                # display and write to video
                # cv2.imshow('Visualizer', canvas)
                # cv2.waitKey(10)
                out.write(canvas)
                if time_count % 500 == 0:
                    print('processing ' + str(time_count))
                time_count += 1

    def long2x(self, longitude):
        """ longitude to x coordinate in the video """
        return math.floor((longitude - self.long_lower) * self.map_scale)

    def lat2y(self, latitude):
        """ latitude to y coordinate in the video """
        return self.video_height - math.floor((latitude - self.lat_lower) * self.map_scale)

class Vehicle:
    def __init__(self, e, o, d):
        super(Vehicle, self)
        self.e = e  # early time
        self.o = o  # origin
        self.d = d  # destination
        self.l = 0  # load
        self.r = list()  # route
        self.p = o  # position
        self.s = 0  # status 0 idle 1 running 2 finish
        self.m = []  # matched customers

class Customer:
    def __init__(self, e, o, d):
        super(Customer, self)
        self.e = e  # early time
        self.o = o  # origin
        self.d = d  # destination
        self.s = 0  # status 0 waiting 1 matched 2 picked 3 dropped 4 timeout
        self.m = 0  # matched vehicle

if __name__ == '__main__':
    args = vars(get_parser().parse_args())
    v = Visualizer(args['rnet_file'], args['prob_file'], args['sol_file'], args['out'],
                   map_scale=8000, draw_inactive=False)

max_stars_repo_name: jamjpan/libezrs

max_issues_repo_name: jamjpan/libezrs

max_forks_repo_name: jamjpan/libezrs


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 17 20:19:43 2020

@author: albert
"""

import cv2
import numpy as np
from darknet import darknet
import time
import os

def detectorFilterClasses(detections, validClasses, aliasClasses): # Keep only relevant objects
    filtered = []
    for i in detections:
        if i[0] in aliasClasses:
            alias = aliasClasses[i[0]]
            filtered.append((alias, i[1], i[2]))
        elif i[0] in validClasses:
            filtered.append(i)
    return filtered

def detectorGetBest(detections): # Keep only relevant objects
    temp = 0
    best = []
    if len(detections) > 0 and detections is not None:
        for i in detections:
            if i[1] >= temp:
                temp = i[1]
                best = i
                print("Best: " + str(best))
        return [best]
    else:
        return best

def detectorInitialise():
    print("Initialising detector")
    darknet.performDetect(
                        configPath    = detectorConfigPath,
                        weightPath    = detectorWeightPath,
                        metaPath      = detectorMetaPath,
                        initOnly      = True)  # Only initialize globals. Don't actually run a prediction.

def detectorDetect(imageRGB, timing = False):
    ### Do detection
    prev_time = time.time() # Time
    print("Performing detection of RGB image")
    detections = darknet.performDetectAS(
        imageInRGB    = imageRGB, # MUST BE RGB
        thresh        = detectorThresh,
        configPath    = detectorConfigPath,
        weightPath    = detectorWeightPath,
        metaPath      = detectorMetaPath,
        showImage     = False, # Shows image using scikit - halts execution
        makeImageOnly = False, # If showImage is True, this won't actually *show* the image, but will create the array and return it.
        initOnly      = False) # Only initialize globals. Don't actually run a prediction.
    if timing:
        print("Detected in " + str((time.time()-prev_time)*1000) + " ms")
    return detections

def detectorDetectBGR(imageBGR, timing = False):
    ### Do detection
    imageRGB = cv2.cvtColor(imageBGR, cv2.COLOR_BGR2RGB)
    prev_time = time.time() # Time
    print("Performing detection of BGR image")
    detections = darknet.performDetectAS(
        imageInRGB    = imageRGB, # MUST BE RGB
        thresh        = detectorThresh,
        configPath    = detectorConfigPath,
        weightPath    = detectorWeightPath,
        metaPath      = detectorMetaPath,
        showImage     = False, # Shows image using scikit - halts execution
        makeImageOnly = False, # If showImage is True, this won't actually *show* the image, but will create the array and return it.
        initOnly      = False) # Only initialize globals. Don't actually run a prediction.
    if timing:
        print("Detected in " + str((time.time()-prev_time)*1000) + " ms")
    return detections

def detectorDetectFile(filepath, timing = False):
    prev_time = time.time() # Time
    print("Performing detection of " + detectorFilePath)
    detections = darknet.performDetect(
                            imagePath     = detectorFilePath,
                            thresh        = detectorThresh,
                            configPath    = detectorConfigPath,
                            weightPath    = detectorWeightPath,
                            metaPath      = detectorMetaPath,
                            showImage     = False, # Shows image using scikit - halts execution
                            makeImageOnly = False, # If showImage is True, this won't actually *show* the image, but will create the array and return it.
                            initOnly      = False) # Only initialize globals. Don't actually run a prediction.
    if timing:
        print("Detected in " + str((time.time()-prev_time)*1000) + " ms")
    return detections

def drawObject(img, text, color, x, y, w, h):
    # Function to draw bounding box on image
    # x, y is center coordinates
    thickness_box = 4
    thickness_text = 2

    # Define points
    x0 = int(x - w/2) # top left
    y0 = int(y - h/2)
    x1 = int(x + w/2) # bottom right
    y1 = int(y + h/2)

    # Draw
    cv2.rectangle(img, (x0, y0), (x1, y1), color, thickness_box)
    cv2.putText(img, text, (x0, y0 - 5), cv2.FONT_HERSHEY_PLAIN, 1, color, thickness_text)

    return img

# Assuming file is being run from wdir='home/bla/bla/31392_project'
detectorConfigPath = "detector/cfg/yolov3-spp_31392.cfg"
#detectorConfigPath = "detector/cfg/yolov3-spp.cfg"
detectorWeightPath = "detector/weights/yolov3-spp_31392_final.weights"
#detectorWeightPath = "detector/weights/yolov3-spp_final.weights"
detectorMetaPath = "detector/cfg/yolo31392.data"
detectorThresh = 0.10 # Open Images weights without our own training occasionally has confidence around 0.03 on some sample boxes...

validClasses = ["Cup", "Book", "Box"]
aliasClasses = {"Mug": "Cup", "Coffee cup": "Cup"}

### Initialise
print("--------------------- INITIALISING ---------------------")
detectorInitialise()

print("--------------------- TESTING ---------------------")
### Prep image
detectorFilePath = "data/course/sample_stereo_conveyor_without_occlusions/left/left_0042.png"
imBGR = cv2.imread(detectorFilePath) # BGR
imRGB = cv2.cvtColor(imBGR, cv2.COLOR_BGR2RGB)

### Do detection
# RGB image
detections1 = detectorDetect(imRGB, timing = True)
print(detections1)

# BGR image
detections2 = detectorDetectBGR(imBGR, timing = True)
print(detections2)

# File
detections3 = detectorDetectFile(detectorFilePath, timing = True)
print(detections3)

# Filter detections
detections = detectorFilterClasses(detections1, validClasses, aliasClasses)
print(detections)

print("--------------------- DONE TESTING ---------------------")

debug = True
writeLabel = False

# Define constants
classNameID = {"Box": 269, "Book": 68, "Cup": 56,}
classIDName = {269: "Box", 68: "Book", 56: "Cup"}
classIDColor = {269: (255, 0, 0), 68: (0, 255, 0), 56: (0, 0, 255)}

path = "data/train/cup/cup00001"
path = "data/course/Stereo_conveyor_without_occlusions/left"

# Find content at path
content = sorted(os.listdir(path))
imgs = []
for string in content:
    if (string[-4:] == ".jpg") or (string[-4:] == ".png"): # Choose images
        imgs.append(string)

i = 0
while True:
    # Read image
    filename_img = imgs[i]
    path_current_img = os.path.join(path, filename_img)
    img = cv2.imread(path_current_img)

    # Read label
    filename_label = imgs[i][:-4] + ".txt"
    path_current_label = os.path.join(path, filename_label)
    if debug:
        print("\n" + path_current_img)

    # Detect anf filter
    detections_all = detectorDetectBGR(img, timing = True)
    detections_filter = detectorFilterClasses(detections_all, validClasses, aliasClasses)
    detections = detectorGetBest(detections_filter)
    if debug:
        print("All detections:")
        print(detections_all)
        print("Filtered detections:")
        print(detections_filter)
        print("Best detection:")
        print(detections)

    img_labels = []
    for cnt, det in enumerate(detections): # Go through each line
        try:
            # Read label information

            ID = int(classNameID[det[0]]) # Class ID
            x = float(det[2][0]) # x center
            y = float(det[2][1]) # y center
            w = float(det[2][2]) # width
            h = float(det[2][3]) # height
            conf = float(det[1])

            # Add label information to list
            this_label = [ID, conf, x, y, w, h]
            img_labels.append(this_label)

        except ValueError:
            print("Something went wrong with the label")

#    ID = int(detections[0]) # Class ID
#    x = float(detections[1]) # x center
#    y = float(detections[2]) # y center
#    w = float(detections[3]) # width
#    h = float(detections[4]) # height

    # Get image dimensions
    img_dim = img.shape
    img_h = img_dim[0]
    img_w = img_dim[1]
    img_c = img_dim[2]

    # Draw bounding box
    for bbox in img_labels:
        bbox_name = classIDName[bbox[0]] # Get name and color form ID
        bbox_color = classIDColor[bbox[0]]
        bbox_conf = bbox[1]
        bbox_x = bbox[2] # Absolute
        bbox_y = bbox[3]
        bbox_w = bbox[4]
        bbox_h = bbox[5]
#        bbox_x = bbox[2] * img_w # Go from relative to absolute
#        bbox_y = bbox[3] * img_h
#        bbox_w = bbox[4] * img_w
#        bbox_h = bbox[5] * img_h
        bbox_text = "{0} {1:.2f}".format(bbox_name, bbox_conf*100)
        img = drawObject(img, bbox_text, bbox_color, bbox_x, bbox_y, bbox_w, bbox_h)

    if writeLabel:
        if not os.path.exists(path_current_label):
            if len(img_labels) > 0:
                try:
                    f = open(path_current_label, "w") # Open file
                    for cnt, line in enumerate(img_labels): # Go through each line
                        try:
                            f.write("{} {} {} {} {}".format(line[0], line[2]*img_w, line[3]*img_h, line[4]*img_w, line[5]*img_h))

                        except ValueError:
                            print("Something went wrong writing the label")
                        finally:
                            f.close()
                except ValueError:
                    print("Couldn't open file: " + path_current_label)
        else:
            print("Label file exists or not accessible: " + path_current_label)

    # Put path on image
    cv2.putText(img, path_current_img, (20, 20), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

    # Show image with bounding box
    cv2.imshow("LabelChecker", img)

    # Wait for keypress
    keyPressed = cv2.waitKey(1) & 0xFF

    # Handle keypress
    if keyPressed == 27 or i == len(imgs) - 1: # ESC - Stop running
        cv2.destroyAllWindows()
        break

    # Go to next image
    if debug:
        print("Next")
    if not i >= len(imgs) - 1:
            i += 1

#    if keyPressed == ord('k'): # Left - Previous
#        if debug:
#            print("Previous")
#
#        if not i <= 0:
#            i -= 1
#
#    elif keyPressed == ord('l'): # Right - Next
#        if debug:
#            print("Next")
#
#        if not i >= len(imgs) - 1:
#            i += 1
#
#    elif keyPressed == ord('x'): # X - Delete / move
#        if debug:
#            print("Delete/move")
#
#        # Remove image from list and get filenames
#        filename_img = imgs.pop(i)
#        filename_label = filename_img[:-4] + ".txt"
#
#        # Create delete folder if it doesn't exist
#        if not os.path.exists(path_delete):
#            os.mkdir(path_delete)
#
#        # Get paths
#        path_current_img = os.path.join(path, filename_img)
#        path_current_label = os.path.join(path, filename_label)
#        path_delete_img = os.path.join(path_delete, filename_img)
#        path_delete_label = os.path.join(path_delete, filename_label)
#
#        # Move image and label
#        os.replace(path_current_img, path_delete_img)
#        os.replace(path_current_label, path_delete_label)
#        print("Moved " + filename_img)
#
#        # Correct index
#        if  i <= 0:
#            i = 0
#        elif i > len(imgs) - 1:
#            i = len(imgs) - 1

max_stars_repo_name: albertsseligmann/UsefulMachineLearning

max_issues_repo_name: albertsseligmann/UsefulMachineLearning

max_forks_repo_name: albertsseligmann/UsefulMachineLearning


text:
#! /usr/bin/env python
# coding=utf-8
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import logging

import numpy as np
import pandas as pd

from ludwig.utils.data_utils import read_csv

def concatenate(train_csv, vali_csv, test_csv, output_csv):
    concatenated_df = concatenate_csv(train_csv, vali_csv, test_csv)

    logging.info('Saving concatenated csv..')
    concatenated_df.to_csv(output_csv, encoding='utf-8', index=False)
    logging.info('done')

def concatenate_csv(train_csv, vali_csv, test_csv):
    logging.info('Loading training csv...')
    train_df = read_csv(train_csv)
    logging.info('done')

    logging.info('Loading validation csv..')
    vali_df = read_csv(vali_csv) if vali_csv is not None else None
    logging.info('done')

    logging.info('Loading test csv..')
    test_df = read_csv(test_csv) if test_csv is not None else None
    logging.info('done')

    logging.info('Concatenating csvs..')
    concatenated_df = concatenate_df(train_df, vali_df, test_df)
    logging.info('done')

    return concatenated_df

def concatenate_df(train_df, vali_df, test_df):
    train_size = len(train_df)
    vali_size = len(vali_df) if vali_df is not None else 0
    test_size = len(test_df) if test_df is not None else 0
    concatenated_df = pd.concat([train_df, vali_df, test_df], ignore_index=True)
    split = np.array(
        [0] * train_size + [1] * vali_size + [2] * test_size,
        dtype=np.int8
    )
    concatenated_df = concatenated_df.assign(split=pd.Series(split).values)
    return concatenated_df

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Concatenate train validation and test set'
    )

    parser.add_argument(
        '-train',
        '--train_csv',
        help='CSV containing the training set'
    )
    parser.add_argument(
        '-vali',
        '--vali_csv',
        help='CSV containing the validation set'
    )
    parser.add_argument(
        '-test',
        '--test_csv',
        help='CSV containing the test set'
    )

    parser.add_argument('-o', '--output_csv', help='output csv')
    args = parser.parse_args()

    concatenate(args.train_csv, args.vali_csv, args.test_csv, args.output_csv)

max_stars_repo_name: rajputakhil/ludwig

max_issues_repo_name: rajputakhil/ludwig

max_forks_repo_name: rajputakhil/ludwig


text:
import multiprocessing
import os
import itertools
import importlib
import time

import cache as cachelib

import numpy as np
import random
from multiprocessing import Pool, cpu_count
from io import StringIO
import statistics
import argparse
import sys
import json

parser = argparse.ArgumentParser(description="Run the Prisoner's Dilemma simulation.")
parser.add_argument(
    "-n",
    "--num-runs",
    dest="num_runs",
    type=int,
    default=100,
    help="Number of runs to average out",
)

parser.add_argument(
    "--skip-slow",
    dest="use_slow",
    action="store_false",
    help="Skip slow strategies for better performance",
)

parser.add_argument(
    "-s",
    "--strategies",
    dest="strategies",
    nargs="+",
    help="If passed, only these strategies will be tested against each other. If only a single strategy is passed, every other strategy will be paired against it.",
)

cacheparser = parser.add_argument_group("Cache")

cacheparser.add_argument(
    "--no-cache",
    dest="cache",
    action="store_false",
    default=True,
    help="Ignores the cache."
)

cacheparser.add_argument(
    "--delete-cache",
    "--remove-cache",
    dest="delete_cache",
    action="store_true",
    default=False,
    help="Deletes the cache."
)

cacheparser.add_argument(
    "-k",
    "--cache-backend",
    dest="cache_backend",
    type=str,
    default="sqlite",
    help="Specifies which cache backend to use. (sqlite or json)"
)

cacheparser.add_argument(
    "--cache-file",
    dest="cache_file",
    type=str,
    default="",
    help="Specifies the cache file to use."
)

parser.add_argument(
    "--no-weights",
    dest="weights",
    action="store_false",
    default=True,
    help="Ignores weights set in weights.json."
)

parser.add_argument(
    "-j",
    "--num-processes",
    dest="processes",
    type=int,
    default=cpu_count(),
    help="Number of processes to run the simulation with. By default, this is the same as your CPU core count.",
)

args = parser.parse_args()

STRATEGY_FOLDERS = [
    "exampleStrats",
    "valadaptive",
    "nekiwo",
    "edward",
    "misc",
    "saffron",
    "aaaa-trsh",
    "phoenix",
    "l4vr0v",
    "smough",
    "dratini0",
    "decxjo",
    "Nobody5050",
    "Taro1z"
    "sanscipher"
]
if args.use_slow:
    STRATEGY_FOLDERS.append("slow")
RESULTS_FILE = "results.txt"
RESULTS_HTML = "results.html"
RESULTS_JSON = "results.json"
SUMMARY_FILE = "summary.txt"
NUM_RUNS = args.num_runs

pointsArray = [
    [1, 5],
    [0, 3],
]  # The i-j-th element of this array is how many points you receive if you do play i, and your opponent does play j.
moveLabels = ["D", "C"]
# D = defect,     betray,       sabotage,  free-ride,     etc.
# C = cooperate,  stay silent,  comply,    upload files,  etc.

def strategyMove(move):
    if type(move) is str:
        defects = ["defect", "tell truth"]
        return 0 if (move in defects) else 1
    else:
        return move

def runRound(pair):
    moduleA = importlib.import_module(pair[0])
    moduleB = importlib.import_module(pair[1])
    memoryA = None
    memoryB = None

    # The games are a minimum of 200 turns long.
    # The np.log here guarantees that every turn after the 200th has an equal (low) chance of being the final turn.
    LENGTH_OF_GAME = int(
        200 - 40 * np.log(1-random.random())
    )
    history = np.zeros((2, LENGTH_OF_GAME), dtype=int)
    historyFlipped = np.zeros((2,LENGTH_OF_GAME),dtype=int)

    for turn in range(LENGTH_OF_GAME):
        # Copy history so that players cannot rewrite it
        playerAmove, memoryA = moduleA.strategy(history[:,:turn].copy(),memoryA)
        playerBmove, memoryB = moduleB.strategy(historyFlipped[:,:turn].copy(),memoryB)
        history[0, turn] = strategyMove(playerAmove)
        history[1, turn] = strategyMove(playerBmove)
        historyFlipped[0,turn] = history[1,turn]
        historyFlipped[1,turn] = history[0,turn]

    return history

def tallyRoundScores(history):
    scoreA = 0
    scoreB = 0
    roundLength = history.shape[1]
    for turn in range(roundLength):
        playerAmove = history[0, turn]
        playerBmove = history[1, turn]
        scoreA += pointsArray[playerAmove][playerBmove]
        scoreB += pointsArray[playerBmove][playerAmove]
    return scoreA / roundLength, scoreB / roundLength

def outputRoundResults(f, pair, roundHistory, scoresA, scoresB, stdevA, stdevB):
    f.write(f"{pair[0]} (P1)  VS.  {pair[1]} (P2)\n")
    for p in range(2):
        for t in range(roundHistory.shape[1]):
            move = roundHistory[p, t]
            f.write(moveLabels[move] + " ")
        f.write("\n")
    f.write(f"Final score for {pair[0]}: {scoresA} ± {stdevA}\n")
    f.write(f"Final score for {pair[1]}: {scoresB} ± {stdevB}\n")
    f.write("\n")

def pad(stri, leng):
    result = stri
    for i in range(len(stri), leng):
        result = result + " "
    return result

def progressBar(width, completion):
    numCompleted = round(width * completion)
    return f"[{'=' * numCompleted}{' ' * (width - numCompleted)}]"

def runRounds(pair):
    # If round results are cached, return the cached results instead
    if args.cache:
        cache = cachelib.get_backend(args, lock=lock)
        r = cache.get(pair)
        if r:
            cache.close()
            return True, *r

    allScoresA = []
    allScoresB = []
    firstRoundHistory = None
    for i in range(NUM_RUNS):
        roundHistory = runRound(pair)
        scoresA, scoresB = tallyRoundScores(roundHistory)
        if i == 0:
            firstRoundHistory = roundHistory
        allScoresA.append(scoresA)
        allScoresB.append(scoresB)
    avgScoreA = statistics.mean(allScoresA)
    avgScoreB = statistics.mean(allScoresB)

    # Standard deviation throws an error with <2 data points (run with -n1).
    # In that case, set it to 0 instead.
    stdevA = statistics.stdev(allScoresA) if len(allScoresA) > 1 else 0
    stdevB = statistics.stdev(allScoresB) if len(allScoresB) > 1 else 0

    roundResults = StringIO()
    outputRoundResults(
        roundResults, pair, firstRoundHistory, scoresA, scoresB, stdevA, stdevB
    )
    roundResults.flush()
    roundResultsStr = roundResults.getvalue()
    roundResults.close()

    if args.cache:
        cache.insert(pair, avgScoreA, avgScoreB, stdevA, stdevB, firstRoundHistory, roundResultsStr)
        cache.close()

    return False, avgScoreA, avgScoreB, stdevA, stdevB, firstRoundHistory, roundResultsStr

def pool_init(l):
    global lock
    lock = l

def runFullPairingTournament(inFolders, outFile, summaryFile):
    startTime = time.time()
    print("Starting tournament, reading files from " + ", ".join(inFolders))
    if args.delete_cache:
        try:
            cache = cachelib.get_backend(args)
            file = args.cache_file
            os.remove(file if file != "" else cache.default)
        except FileNotFoundError:
            pass

    if args.cache:
        cache = cachelib.get_backend(args)
        cache.setup()

    scoreKeeper = {}
    STRATEGY_LIST = []
    for inFolder in inFolders:
        for file in os.listdir(inFolder):
            if file.endswith(".py"):
                STRATEGY_LIST.append(f"{inFolder}.{file[:-3]}")

    if args.strategies is not None and len(args.strategies) > 1:
        STRATEGY_LIST = [strategy for strategy in STRATEGY_LIST if strategy in args.strategies]

    if len(STRATEGY_LIST) < 2:
        raise ValueError('Not enough strategies!')

    for strategy in STRATEGY_LIST:
        scoreKeeper[strategy] = 0

    mainFile = open(outFile, "w+")
    summaryFile = open(summaryFile, "w+")

    combinations = list(itertools.combinations(STRATEGY_LIST, r=2))

    if args.strategies is not None and len(args.strategies) == 1:
        combinations = [pair for pair in combinations if pair[0] == args.strategies[0] or pair[1] == args.strategies[0]]

    numCombinations = len(combinations)
    allResults = []
    with Pool(args.processes, initializer=pool_init, initargs=(multiprocessing.Lock(),)) as p:
        hits = 0
        for i, result in enumerate(
            zip(p.imap(runRounds, combinations), combinations), 1
        ):
            (
                cached,
                avgScoreA,
                avgScoreB,
                stdevA,
                stdevB,
                firstRoundHistory,
                roundResultsStr,
            ) = result[0]

            if cached:
                hits += 1

            sys.stdout.write(
                f"\r{i}/{numCombinations} pairings ({NUM_RUNS} runs per pairing, {hits} hits, {i-hits} misses) {progressBar(50, i / numCombinations)}"
            )
            sys.stdout.flush()
            (nameA, nameB) = result[1]
            scoresList = [avgScoreA, avgScoreB]

            allResults.append(
                {
                    "playerA": {
                        "name": nameA,
                        "avgScore": avgScoreA,
                        "stdev": stdevA,
                        "history": firstRoundHistory[0].tolist()
                    },
                    "playerB": {
                        "name": nameB,
                        "avgScore": avgScoreB,
                        "stdev": stdevB,
                        "history": firstRoundHistory[1].tolist()
                    }
                }
            )
            mainFile.write(roundResultsStr)
            scoreKeeper[nameA] += avgScoreA
            scoreKeeper[nameB] += avgScoreB
    sys.stdout.write("\n")
    sys.stdout.flush()

    with open(RESULTS_JSON, "w+") as j:
        j.write(json.dumps(allResults))

    scoresNumpy = np.zeros(len(scoreKeeper))
    for i in range(len(STRATEGY_LIST)):
        scoresNumpy[i] = scoreKeeper[STRATEGY_LIST[i]]
    rankings = np.argsort(scoresNumpy)
    invRankings = [len(rankings) - int(ranking) - 1 for ranking in np.argsort(rankings)]

    with open("viewer-template.html", "r+") as t:
        jsonStrategies = [
            {
                "name": name,
                "rank": rank,
                "score": score,
                "avgScore": score / (len(STRATEGY_LIST) - 1),
            }
            for (name, rank, score) in zip(STRATEGY_LIST, invRankings, scoresNumpy)
        ]
        jsonResults = json.dumps({"results": allResults, "strategies": jsonStrategies})
        templateStr = t.read()
        with open(RESULTS_HTML, "w+") as out:
            out.write(templateStr.replace("$results", jsonResults))

    mainFile.write("\n\nTOTAL SCORES\n")
    for rank in range(len(STRATEGY_LIST)):
        i = rankings[-1 - rank]
        score = scoresNumpy[i]
        scorePer = score / (len(STRATEGY_LIST) - 1)
        scoreLine = f"#{rank + 1}: {pad(STRATEGY_LIST[i] + ':', 16)}{score:.3f}  ({scorePer:.3f} average)\n"
        mainFile.write(scoreLine)
        summaryFile.write(scoreLine)

    mainFile.flush()
    mainFile.close()
    summaryFile.flush()
    summaryFile.close()
    print(f"Done with everything! ({time.time() - startTime}) Results file written to {RESULTS_FILE}")

if __name__ == "__main__":
    runFullPairingTournament(STRATEGY_FOLDERS, RESULTS_FILE, SUMMARY_FILE)


text:
# -*- coding: utf-8 -*-
"""
Description: Using different networks to solve basic logical problems.
"""

import numpy as np  #numerical python package for scientific computing
import pcn #class for one-layer perceptron
import mlpcn #class for two-layer perceptron
import rbf #class for radial basic function network

"""
#logical AND
inputs = np.array([[1,1],[1,0],[0,1],[0,0]])
targets = np.array([[1],[0],[0],[0]])
"""

"""
#logical OR
inputs = np.array([[1,1],[1,0],[0,1],[0,0]])
targets = np.array([[1],[1],[1],[0]])
"""

#logical XOR
inputs = np.array([[1,1],[1,0],[0,1],[0,0]])
targets = np.array([[0],[1],[1],[0]])

"""
#identity matrix
inputs = np.array([[1,1],[1,0],[0,1],[0,0]])
targets = np.array([[1,1],[1,0],[0,1],[0,0]])
"""

#use one-layer perceptron
p = pcn.pcn(inputs,targets,0.2,'linear','batch')
p.pcntrain(10000)
p.confmat(inputs,targets)

#use rbf network
p = rbf.rbf(inputs,targets,4,0,1,0.2,'linear','batch')
p.rbftrain(10000)
p.confmat(inputs,targets)

#use two-layer perceptron
p = mlpcn.mlpcn(inputs,targets,4,0.2,'linear','batch')
p.mlptrain(10000)
p.confmat(inputs,targets)

max_stars_repo_name: RaoulMa/NeuralNets

max_issues_repo_name: RaoulMa/BasicNeuralNets

max_forks_repo_name: RaoulMa/BasicNeuralNets


text:
import os
import yaml
import cv2
import numpy as np
from glob import glob
import sys
import copy

def create_label(raw_image_path, label_image_path, param_dict):

    saving_directory_mask = label_image_path.replace("all_labels", "all_masks")
    saving_directory_array = label_image_path.replace("all_labels", "all_arrays")
    raw_image = cv2.imread(raw_image_path)
    label_image = cv2.imread(label_image_path)
    final_mask = np.zeros(label_image.shape, dtype=np.uint8)
    mask_color = np.zeros(label_image.shape, dtype=np.uint8)
    mask_array = np.zeros((label_image.shape[0], label_image.shape[1]), dtype=np.uint8)
    image_width = raw_image.shape[0]
    image_height = raw_image.shape[1]
    # print("pixel value: ", label_image[10][30][:])
    for i in range(image_width):
        for j in range(image_height):
            if np.array_equal(label_image[i][j][:], [128, 128, 128]) or np.array_equal(label_image[i][j][:], [0, 0, 0]) or np.array_equal(label_image[i][j][:], [255, 255, 255]):
                mask_color[i][j] = [0, 0, 0]
                mask_array[i][j] = 0
            elif np.array_equal(label_image[i][j][:], [255, 255, 0]):
                mask_color[i][j] = [75, 75, 75]
                mask_array[i][j] = 1
            elif np.array_equal(label_image[i][j][:], [255, 0, 0]):
                mask_color[i][j] = [150, 150, 150]
                mask_array[i][j] = 2
            elif np.array_equal(label_image[i][j][:], [0, 0, 255]):
                mask_color[i][j] = [225, 225, 225]
                mask_array[i][j] = 3
    final_mask = mask_color
    colors = np.unique(label_image.reshape(400*400, 3), axis=0)
    tag = "{} regions:\n{}".format(len(colors), colors)
    # print(tag)
    frame = np.concatenate((raw_image, label_image), axis=1)
    font = cv2.FONT_HERSHEY_PLAIN
    frame = cv2.putText(frame, str(len(colors))+" regions", (450, 50), font, 1.5, [250,50,0], 2, cv2.LINE_AA)
    # frame = np.concatenate((frame, final_mask), axis=1)
    cv2.imshow("Input & Label", frame)
    cv2.imshow("Mask", final_mask.astype(np.uint8))
    base = os.path.basename(saving_directory_mask)
    mask_file_name = os.path.splitext(base)[0].replace("_label", "_mask")
    # print("mask: ", os.path.dirname(os.path.realpath(saving_directory_mask)), mask_file_name)
    file_name = os.path.dirname(os.path.realpath(saving_directory_mask)) + "/" + mask_file_name + ".png"
    # print(file_name)
    cv2.imwrite(file_name, final_mask.astype(np.uint8))
    base = os.path.basename(saving_directory_array)
    mask_file_name = os.path.splitext(base)[0].replace("_label", "_array")
    # print("array: ", mask_file_name, os.path.dirname(os.path.realpath(saving_directory_array)), mask_file_name)
    file_name = os.path.dirname(os.path.realpath(saving_directory_array)) + "/" + mask_file_name + ".npy"
    np.save(file_name, mask_array)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        return True
    # cv2.destroyAllWindows()
    return True

def all_labels(image_directory, label_directory, param_dict):
    result = False
    images_paths = sorted(glob(os.path.join(image_directory, '*png')))
    i = 0
    for image_path in images_paths:
        print("Running Label Generation for: {} ID: {}/{}".format(os.path.basename(image_path), i, len(images_paths)))
        base = os.path.basename(image_path)
        image_file_name = os.path.splitext(base)[0]
        files_missing = []
        label_image_file_name = label_directory + "/" + image_file_name + "_label.png"
        input_label_image_file_name = label_directory + "/" + image_file_name + "_label_&_input.png"
        image_files = [label_image_file_name, input_label_image_file_name]
        for image_file in image_files:
            if not os.path.exists(image_file):
                files_missing.append(os.path.basename(image_file))
                print("Image File {} not found!".format(image_file))
        if len(files_missing) > 0:
            print("One or more file missing!!", files_missing)
            continue
        i = i + 1

        result = create_label(raw_image_path=image_path,
                              label_image_path=label_image_file_name,
                              param_dict=param_dict)
    cv2.destroyAllWindows()
    return result

def parse_pipeline_parameters(parameters_fpath):
    param_dict = dict()
    with open(parameters_fpath, "r") as stream:
        param_dict = yaml.safe_load(stream)
    return param_dict

if __name__ == '__main__':

    # File Path for the yaml file
    parameters_fpath = os.getcwd() + "/annotation_parameters.yaml"
    param_dict = parse_pipeline_parameters(parameters_fpath)
    image_directory = param_dict['directory']['image_directory']
    parent_label_directory = param_dict['directory']['label_directory']
    print(param_dict)
    parent_directory = "/hdd01/eye_images/Final_eye_images/Bates_labels/all_labels/"
    directory_contents = ["/hdd01/eye_images/2021_07_05_13_09_00/",
                          "/hdd01/eye_images/2021_07_13_11_34_04/",
                          "/hdd01/eye_images/2021_07_13_14_32_23/",
                          "/hdd01/eye_images/2021_07_13_14_37_32/",
                          "/hdd01/eye_images/2021_07_13_14_43_36/",
                          "/hdd01/eye_images/2021_07_14_17_00_32/",
                          "/hdd01/eye_images/2021_07_16_12_29_06/",
                          "/hdd01/eye_images/2021_07_16_13_00_43/",
                          "/hdd01/eye_images/2021_07_21_13_47_43/",
                          "/hdd01/eye_images/2021_07_22_14_04_09/",
                          "/hdd01/eye_images/2021_07_22_14_49_47/",
                          "/hdd01/eye_images/2021_07_22_15_25_59/"
                          ]
    for image_directory in directory_contents:
        label_directory = parent_label_directory + os.path.basename(os.path.normpath(image_directory))
        print("Label Directory:", label_directory)
        all_labels(image_directory=image_directory,
                   label_directory=label_directory,
                   param_dict=param_dict)
    sys.exit(0)