def intermediate_gen(self):
     for n in range(self.pop.count):
         num = random.random()
         for i in range(self.pop.count):
             if num <= self.CDF[i]:
                 keep = i
                 break
         tmp_agent = agents.agent()
         tmp_agent.load_gene(self.pop.strings[keep].gene[:])
         self.intgen.append(tmp_agent)
 def mutation_random(self):  # randomly mutates swapping two locations
     for i in range(self.pop.count):
         temp = agents.agent()
         s = 0
         num = random.random()
         if num <= self.Pm:
             first = random.randint(0, self.pop.length - 1)
             second = random.randint(0, self.pop.length - 1)
             temp.gene[:] = self.newpop[i].gene[:]
             s = temp.gene[first]
             temp.gene[first] = temp.gene[second]
             temp.gene[second] = s
             self.newpop[i].gene[:] = temp.gene[:]
	def createAgent(self, agentType, strategy = 'TFT'):
		if agentType == 'Learner':
			iterations_exploring = 1000
			T = pow(.01, 1.0/iterations_exploring)
			alpha = .5
			gamma = .95
			numHistoryMoves = random.choice([1,2,3])
			numHistoryMoves = 1
			agentx = agent(numHistoryMoves, alpha, gamma, T, self.payoffMatrix)
			agentx.sneakStrategy = strategy
		elif agentType == 'Meta':
			agentx = metaAgent(self.learntPolicies, self.payoffMatrix)
		
		return agentx
    def initialization(self):
        for n in range(self.count):
            string = agents.agent()
            for i in range(self.length):
                if self.binary:
                    string.gene.append(random.randint(0, 1))
                else:
                    while len(string.gene) < self.length:
                        temp = random.randint(1, self.length)
                        if string.gene.count(temp) == 0:
                            string.gene.append(temp)

                            # just for testing (assign random values simulating a run of the game)
                            # string.moves = random.randint(4,21)
                            # string.result = random.randint(2,4) / 2.0

            self.strings.append(string)
 def mutation_improve(self):  # only mutates if the swapping will produce a better fitness
     for i in range(self.pop.count):
         changed = False
         count = 0
         highest = 0
         temp = agents.agent()
         s = 0
         num = random.random()
         if num <= self.Pm:
             while count < 10 and not changed:
                 first = random.randint(0, self.pop.length - 1)
                 second = random.randint(0, self.pop.length - 1)
                 temp.gene[:] = self.newpop[i].gene[:]
                 s = temp.gene[first]
                 temp.gene[first] = temp.gene[second]
                 temp.gene[second] = s
                 highest = self.newpop[i].fitness(self.pop.length)
                 if temp.fitness(self.pop.length) > highest:
                     self.newpop[i].gene[:] = temp.gene[:]
                     changed = True
                 count += 1
def runOneToOne():
	stra = 'TFT'
	opponentx = strategy(stra)

	iterations_exploring = 5000
	T = pow(.01, 1.0/iterations_exploring)
	alpha = .5
	gamma = .95
	numHistoryMoves = 2
	payoffMatrix = {"DD":1, "CC":3, "CD":0, "DC":5}
	agentx = agent(numHistoryMoves, alpha, gamma, T, payoffMatrix)

	for i in range(5000):
		agentsMove = agentx.agentsMove(i)
		responseMove = opponentx.next()

		agentx.updateAgent(agentsMove, responseMove)
		opponentx.addAgentsMove(agentsMove)
		
	agentx.interpretResults()
	iterations, diffs = zip(*agentx.agentsConvergence)
Exemple #7
0
import agents

test = agents.agent()

for i in range(1,10) :
	test.gene.append(i)

test.fitness(10)

def train_fn(X_train_shards,
             Y_train_shards,
             X_test,
             Y_test,
             return_dict,
             mal_data_X=None,
             mal_data_Y=None):
    # Start the training process
    num_agents_per_time = int(args.C * args.k)
    simul_agents = gv.num_gpus * gv.max_agents_per_gpu
    simul_num = min(num_agents_per_time, simul_agents)
    alpha_i = 1.0 / args.k
    agent_indices = np.arange(args.k)
    if args.mal:
        mal_agent_index = gv.mal_agent_index
    guidance = {
        'gradient': (np.ones(args.k) * alpha_i, np.ones(args.k)),
        'data': (np.ones(args.k) * alpha_i, np.zeros(args.k))
    }
    unupated_frac = (args.k - num_agents_per_time) / float(args.k)
    t = 0
    mal_visible = []
    eval_loss_list = []
    loss_track_list = []
    lr = args.eta
    loss_count = 0
    if args.gar == 'krum':
        krum_select_indices = []

    while return_dict['eval_success'] < gv.max_acc and t < args.T:
        print('Time step %s' % t)

        process_list = []
        mal_active = 0
        # curr_agents = np.random.choice(agent_indices, num_agents_per_time,
        #                                replace=False)
        # if t == 0:
        #     curr_agents = np.random.choice(agent_indices, num_agents_per_time, replace=False)
        # else:
        curr_agents = np.random.choice(
            agent_indices,
            num_agents_per_time,
            p=np.exp(guidance['gradient'][0] / guidance['gradient'][1]) /
            np.sum(np.exp(guidance['gradient'][0] / guidance['gradient'][1]),
                   axis=0),
            replace=False)
        print('Set of agents chosen: %s' % curr_agents)

        if t == 0:
            if 'MNIST' in args.dataset:
                global_model = model_mnist(type=args.model_num)
            elif args.dataset == 'CIFAR-10':
                global_model = cifar_10_model()
            elif args.dataset == 'census':
                global_model = census_model_1()
            global_weights = global_model.get_weights()
            np.save(gv.dir_name + 'global_weights_t%s.npy' % t, global_weights)
        else:
            global_weights = np.load(gv.dir_name +
                                     'global_weights_t%s.npy' % t,
                                     allow_pickle=True)

        k = 0
        agents_left = 1e4
        while k < num_agents_per_time:
            # true_simul = min(simul_num,agents_left)
            true_simul = 1
            print('training %s agents' % true_simul)
            for l in range(true_simul):
                gpu_index = int(l / gv.max_agents_per_gpu)
                gpu_id = gv.gpu_ids[gpu_index]
                i = curr_agents[k]
                if args.mal is False or i != mal_agent_index:
                    # p = Process(target=agent, args=(i, X_train_shards[i],
                    #                             Y_train_shards[i], t, gpu_id, return_dict, X_test, Y_test,lr))
                    agent(i, X_train_shards[i], Y_train_shards[i], t, gpu_id,
                          return_dict, X_test, Y_test, lr)
                elif args.mal is True and i == mal_agent_index:
                    # p = Process(target=mal_agent, args=(X_train_shards[mal_agent_index],
                    #                                 Y_train_shards[mal_agent_index], mal_data_X, mal_data_Y, t, gpu_id, return_dict, mal_visible, X_test, Y_test))
                    mal_agent(X_train_shards[mal_agent_index],
                              Y_train_shards[mal_agent_index], mal_data_X,
                              mal_data_Y, t, gpu_id, return_dict, mal_visible,
                              X_test, Y_test)
                    mal_active = 1
                # print (return_dict[str(i)])
                guidance['gradient'][0][i] += np.sqrt(
                    np.array([(x**2).mean()
                              for x in return_dict[str(i)]]).mean())
                guidance['gradient'][1][i] += 1
                # p.start()
                # process_list.append(p)
                k += 1
            # for item in process_list:
            #     item.join()
            agents_left = num_agents_per_time - k
            print('Agents left:%s' % agents_left)

        if mal_active == 1:
            mal_visible.append(t)

        print('Joined all processes for time step %s' % t)

        if 'avg' in args.gar:
            if args.mal:
                count = 0
                for k in range(num_agents_per_time):
                    if curr_agents[k] != mal_agent_index:
                        if count == 0:
                            ben_delta = alpha_i * return_dict[str(
                                curr_agents[k])]
                            np.save(gv.dir_name + 'ben_delta_sample%s.npy' % t,
                                    return_dict[str(curr_agents[k])])
                            count += 1
                        else:
                            ben_delta += alpha_i * return_dict[str(
                                curr_agents[k])]

                np.save(gv.dir_name + 'ben_delta_t%s.npy' % t, ben_delta)
                global_weights += alpha_i * return_dict[str(mal_agent_index)]
                global_weights += ben_delta
            else:
                for k in range(num_agents_per_time):
                    global_weights += alpha_i * return_dict[str(
                        curr_agents[k])]

        elif 'krum' in args.gar:
            collated_weights = []
            collated_bias = []
            agg_num = int(num_agents_per_time - 1 - 2)
            for k in range(num_agents_per_time):
                # weights_curr, bias_curr = collate_weights(return_dict[str(curr_agents[k])])
                weights_curr, bias_curr = collate_weights(return_dict[str(k)])
                collated_weights.append(weights_curr)
                collated_bias.append(collated_bias)
            score_array = np.zeros(num_agents_per_time)
            for k in range(num_agents_per_time):
                dists = []
                for i in range(num_agents_per_time):
                    if i == k:
                        continue
                    else:
                        dists.append(
                            np.linalg.norm(collated_weights[k] -
                                           collated_weights[i]))
                dists = np.sort(np.array(dists))
                dists_subset = dists[:agg_num]
                score_array[k] = np.sum(dists_subset)
            print(score_array)
            krum_index = np.argmin(score_array)
            print(krum_index)
            global_weights += return_dict[str(krum_index)]
            if krum_index == mal_agent_index:
                krum_select_indices.append(t)
        elif 'coomed' in args.gar:
            # Fix for mean aggregation first!
            weight_tuple_0 = return_dict[str(curr_agents[0])]
            weights_0, bias_0 = collate_weights(weight_tuple_0)
            weights_array = np.zeros((num_agents_per_time, len(weights_0)))
            bias_array = np.zeros((num_agents_per_time, len(bias_0)))
            # collated_weights = []
            # collated_bias = []
            for k in range(num_agents_per_time):
                weight_tuple = return_dict[str(curr_agents[k])]
                weights_curr, bias_curr = collate_weights(weight_tuple)
                weights_array[k, :] = weights_curr
                bias_array[k, :] = bias_curr
            shape_size = model_shape_size(weight_tuple)
            # weights_array = np.reshape(np.array(collated_weights),(len(weights_curr),num_agents_per_time))
            # bias_array = np.reshape(np.array(collated_bias),(len(bias_curr),num_agents_per_time))
            med_weights = np.median(weights_array, axis=0)
            med_bias = np.median(bias_array, axis=0)
            num_layers = len(shape_size[0])
            update_list = []
            w_count = 0
            b_count = 0
            for i in range(num_layers):
                weights_length = shape_size[2][i]
                update_list.append(med_weights[w_count:w_count +
                                               weights_length].reshape(
                                                   shape_size[0][i]))
                w_count += weights_length
                bias_length = shape_size[3][i]
                update_list.append(med_bias[b_count:b_count +
                                            bias_length].reshape(
                                                shape_size[1][i]))
                b_count += bias_length
            assert model_shape_size(update_list) == shape_size
            global_weights += update_list

        # Saving for the next update
        np.save(gv.dir_name + 'global_weights_t%s.npy' % (t + 1),
                global_weights)

        # Evaluate global weight
        if args.mal:
            # p_eval = Process(target=eval_func, args=(
            #     X_test, Y_test, t + 1, return_dict, mal_data_X, mal_data_Y), kwargs={'global_weights': global_weights})
            eval_func(X_test,
                      Y_test,
                      t + 1,
                      return_dict,
                      mal_data_X,
                      mal_data_Y,
                      global_weights=global_weights)
        else:
            # p_eval = Process(target=eval_func, args=(
            #     X_test, Y_test, t + 1, return_dict), kwargs={'global_weights': global_weights})
            eval_func(X_test,
                      Y_test,
                      t + 1,
                      return_dict,
                      global_weights=global_weights)
        # p_eval.start()
        # p_eval.join()

        eval_loss_list.append(return_dict['eval_loss'])

        t += 1

    return t
                  validation_steps=valid_steps,
                  callbacks=[check_point, tensorboard])
    '''
    prune on vgg16, the layer index can be found by model.summary()
    '''
    state = 0
    base_model = vgg16
    base_model.evaluate(valid_gen, steps=valid_steps)
    new_model = Model(inputs=vgg_model.input, outputs=predict)
    layer_index = 17
    action_cnt = base_model.layers[layer_index].get_weights()[0].shape[-1]
    h = base_model.layers[layer_index].get_weights()[0].shape[:3]
    print("layer {} have {} filters".format(layer_index, action_cnt))

    ag = agent((action_cnt, h[0] * h[1] * h[2], 1),
               action_cnt,
               lr=1e-3,
               factor=0.01)
    env = environment(base_model,
                      new_model,
                      x_train,
                      y_train,
                      x_test,
                      y_test,
                      steps_per_epoch,
                      valid_steps,
                      batch_size=batch_size,
                      b=0.05,
                      layer_index=layer_index,
                      epochs=1)
    episode_len = 5
    writer = tf.summary.create_file_writer("./logs/mylogs_7")
Exemple #10
0
epsilon = 0.1
gamma = 0.9
target_replace_iter = 100
memory_capacity = 1000
n_actions = 7
n_states = 42

'''Runner'''
class runner():
    def __init__(self, env: environment, mode: str, total_episode: int):
        self.env = env
        self.mode = mode
        self.total_episode = total_episode

    def start(self):
        if self.mode == 'train':
            self.env.train(self.total_episode)
            self.env.test(trained_agent="none", total_episode_trained=self.total_episode)
            self.env.test(trained_agent="first_mover", total_episode_trained=self.total_episode)
            self.env.test(trained_agent="second_mover", total_episode_trained=self.total_episode)
            self.env.test(trained_agent="both agents", total_episode_trained=self.total_episode)

        self.env.com_as_player(com_as='first_mover', total_episode_trained=self.total_episode)
        self.env.com_as_player(com_as='second_mover', total_episode_trained=self.total_episode)

if __name__ == "__main__":
    first_mover = agent(1, total_episode, epsilon, learning_rate, gamma, batch_size, target_replace_iter, memory_capacity, n_actions, n_states)
    second_mover = agent(-1, total_episode, epsilon, learning_rate, gamma, batch_size, target_replace_iter, memory_capacity, n_actions, n_states)
    env = environment(first_mover, second_mover)
    runner = runner(env, mode, total_episode)
    runner.start()
Exemple #11
0
    agentList = []
    wallList = []
    maplist = "pacman-large.txt", "huge.txt", "complex2.txt", "complex.txt", "default.txt", "empty-large.txt", "pacman.txt", "test.txt"
    i = random.randint(0, 7)
    chosenMap = "maps/" + maplist[i]
    c_map = maps.Map(chosenMap,
                     c_gameType=random.randint(0, 1))  #"maps/complex2.txt")

    for i in range(0, 10):
        r = random.randint(0, 10)
        if r == 0:
            _role = "runner"
            agentList.append(
                agents.agent(c_map=c_map,
                             c_agent_list=agentList,
                             c_alg="Reflex",
                             _role=_role,
                             _index=(len(agentList)),
                             _rand=2))
        elif r >= 1 and r <= 3:
            _role = "runner"
            agentList.append(
                agents.agent(c_map=c_map,
                             c_agent_list=agentList,
                             c_alg="BFS",
                             _role=_role,
                             _index=(len(agentList))))
        elif r > 3 and r < 6:
            _role = "runner"
            agentList.append(
                agents.agent(c_map=c_map,
                             c_agent_list=agentList,
Exemple #12
0
from crypto_fitness import *
import agents

temp = agents.agent()
temp.load_gene([22, 10, 16, 25, 20, 18, 2, 26, 11, 3, 19, 8, 24, 4, 23, 7, 14, 1, 9, 12, 15, 5, 17, 13, 6, 21])

print fitness(temp ,26)

fhCrypt = open("crypto.txt")
newAlpha = convert(temp.gene)
print flip(fhCrypt.readline(),newAlpha)

Exemple #13
0
for x in range(1, nooflocaion + 1):
    location_name = input("Enter location_name: ")
    merchant_Id = input("Enter merchant_Id: ")

    location = location(location_name, merchant_Id)
    locationmerchant_Id = insertlocationToDb(location)
    location.merchant_Id = locationmerchant_Id

    noofagents = int(input("Enter noofagents: "))

    agents = []
    for x in range(1, noofagents + 1):
        agent_name = input("Enter agent_name: ")
        location_Id = input("Enter location_Id: ")

        agents = agent(agent_name, location_Id)
        agentsagent_name = insertagentsToDb(agent)
        agents.agent_name = agentsagent_name

    nooftransactions = int(input("Enter nooftransactions: "))

    transactions = []
    for x in range(1, nooftransactions + 1):
        amount = int(input("Enter amount: "))
        balance = int(input("Enter balance: "))
        member_Id = input("Enter mermber_Id: ")
        clubcards_Id = input("Enter clubcards_Id: ")
        agents_Id = input("Enter agents_Id: ")

        transactions = transactions(amount, balance, member_Id, clubcards_Id,
                                    agents_Id)