예제 #1
0
def create_connectivity(agents, p, type='undirected_random'):
    if type == 'directed_random':
        conn = gg.random_undirected_graph(agents, p)
    elif type == 'spatial_random':
        conn = gg.spatial_random_graph(agents, p)
    elif type == 'hierarchy':
        conn = gg.hierarchy_graph(agents)
        for agent in agents[1:]:
            agent.uses_knowledge = False
        if p==1: ##team leaders can do filtering
            for agent in agents[1:5]:
                agent.uses_knowledge = True
    elif type == 'collaborative':
        conn = gg.collaborative_graph(agents)
        for agent in agents[1:]:
            agent.uses_knowledge = False
        if p==1: ##team leaders can do filtering
            for agent in agents[1:5]:
                agent.uses_knowledge = True
    else:
        conn = gg.random_directed_graph(agents, p)
        
    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))
    
    if type in ['hierarchy', 'collaborative']:
        return (1, len(agents))
    else:
        cc_conn = nx.connected_components(conn)
        ## return the number of connected components and 
        ## the size of the largest connected component
        return (len(cc_conn), len(cc_conn[0]))
예제 #2
0
def create_connectivity(agents, p, type='undirected_random'):
    if type == 'directed_random':
        conn = gg.random_undirected_graph(agents, p)
    elif type == 'spatial_random':
        conn = gg.spatial_random_graph(agents, p)
    elif type == 'hierarchy':
        conn = gg.hierarchy_graph(agents)
        for agent in agents[1:]:
            agent.uses_knowledge = False
        if p == 1:  ##team leaders can do filtering
            for agent in agents[1:5]:
                agent.uses_knowledge = True
    elif type == 'collaborative':
        conn = gg.collaborative_graph(agents)
        for agent in agents[1:]:
            agent.uses_knowledge = False
        if p == 1:  ##team leaders can do filtering
            for agent in agents[1:5]:
                agent.uses_knowledge = True
    else:
        conn = gg.random_directed_graph(agents, p)

    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))

    if type in ['hierarchy', 'collaborative']:
        return (1, len(agents))
    else:
        cc_conn = nx.connected_components(conn)
        ## return the number of connected components and
        ## the size of the largest connected component
        return (len(cc_conn), len(cc_conn[0]))
예제 #3
0
def main(nodes, numberOfTests):
    for k in range(0, numberOfTests):
        gen = GraphGen(nodes)
        g = gen.randomMaxDeg4Graph()
        partition = Alg.localCutAlg(g)
        result = Verifier.partitionCheck(g, partition)
        if len(result[1]) > 1:
            Verifier.graphDisplay(g, partition, result[1])
예제 #4
0
def create_connectivity(agents, p, type='undirected_random'):
    properties = {'graph_type' : type, 'connection_probability' : p}
    conn = gg.create_graph_type(agents, properties)[0]
        
    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))
    
    if type in ['hierarchy', 'collaborative']:
        return (1, len(agents))
    else:
        cc_conn = nx.connected_components(conn)
        ## return the number of connected components and 
        ## the size of the largest connected component
        return (len(cc_conn), len(cc_conn[0]))
예제 #5
0
def main():
    nodes = 5
    gen = GraphGen.GraphGen(nodes)
    g = gen.random4RegularGraph()

    (m, b, mArr, bArr, x) = create_mbx(g)
    c = createC(g)
    checkIP(mArr, bArr, x)

    f = open('ip.m', 'w') #write mathematica script
    f.write("Print[LinearProgramming[" + c + ", " + m
            + ", " + b + ", " + "0" + ", " + "Integers]]")
    f.close()

    solution = executeMathematicaScript()

    gNewWeights = makeNewG(g, solution)

    print(checkNewG(gNewWeights, g))
예제 #6
0
if __name__ =="__main__":
    p = {}
    gtypes = [('erdos_renyi_graph', 20, 0.15, 3)]
    #gtypes = [('random', 20, 0.15, 3), \
              #('random', 200, 0.03, 3), \
              #('watts_strogatz_graph', 20, 0.1, 5), \
              #('watts_strogatz_graph', 200, 0.01, 8), \
              #('barabasi_albert_graph', 20, 0.1, 10), \
              #('barabasi_albert_graph', 200, 0.01, 20), \
    #]

    #gtypes = [('watts_strogatz_graph', 40, 0.1, 3), \
              #('watts_strogatz_graph', 40, 0.1, 8), \
              #('watts_strogatz_graph', 40, 0.1, 15), \
    #]

    for (g,x,y,z) in gtypes:
        p['graph_type'] = g
        p['num_agents'] = x
        p['connection_probability'] = y
        p['num_nodes_to_attach'] = z


        ag = range(p['num_agents'])

        (conn, stats) = gg.create_graph_type(ag,p)
        print (p)
        print ("Connected components:", stats['num_cc'])
        histogram(stats, 20*(1+int(p['num_agents']/60)))

예제 #7
0
    #---------------------- Import Patrick-Generated Nodes ----------------------#

    # terrain = Terrain(800, 150)
    # p("terrain created")

    # nodes_all, dim = Utils.importNodes_PatrickSim("../inputs/marathon2_2000_2.scb", node_type="hon")

    # val_time_1 = 243
    # val_pos_1 = (700,125)
    # val_rad = 40

    #--------------------------------- Analysis ---------------------------------#

    p("input loaded")
    graph_gen = GraphGen(nodes_all, val_time_1, val_pos_1, val_rad)
    p("graph made")
    nodes_val = graph_gen.nodes_val
    comm_plan_original, key2idx, potential_conns = graph_gen.genCommPlan()
    p("comm plan made")
    comm_plan_modified = Adversary.impersonation(nodes_val, comm_plan_original)
    p("impersonation done")
    sim_conns_original = graph_gen.genSimuConns(comm_plan_modified)
    p("connections simulated")
    sim_conns_modified = Adversary.dissemination(nodes_val, comm_plan_original,
                                                 sim_conns_original)
    p("dissemination done")
    id2edges = graph_gen.formEdges(sim_conns_modified, potential_conns,
                                   key2idx)
    p("edges formed")
    results = SybilDetection.runDetectionAlgorithms(nodes_val, id2edges)
예제 #8
0
def run_simulation_one_graph(properties, outfile):
    facts = range(properties["num_facts"] + properties["num_noise"])
    agents = []
    for i in xrange(properties["num_agents"]):
        agents.append(
            Agent.Agent(
                properties["willingness"],
                properties["competence"],
                properties["num_facts"],
                properties["num_noise"],
                properties["spamminess"],
                properties["selfishness"],
                properties["trust_used"],
                properties["inbox_trust_used"],
                properties["trust_filter_on"],
            )
        )

    ## Create agent graph
    conn, stats = gg.create_graph_type(agents, properties)

    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))

    nodes_to_try = top_nodes(stats)
    # print "Nodes", nodes_to_try

    for current_agent in nodes_to_try:
        # print "New node", current_agent
        agents[current_agent].capacity = 50  ##set one agent to high capacity
        node_stats = current_agent_stats(current_agent, stats)

        ## Distribute facts to agents
        for i in facts:
            for j in xrange(properties["agent_per_fact"]):
                ## find a random agent, and distribute fact i
                k = random.randint(0, properties["num_agents"] - 1)
                agents[k].add_fact(i)

        ## Initialize agents to send everything that they think is valuable
        ## in their outbox
        for agent in agents:
            agent.init_outbox()

        action_list = []
        all_stats = ss.SimulationStats(
            properties["num_facts"], properties["num_noise"], stats["num_cc"], stats["largest_cc"]
        )

        ##actual simulation starts here
        for i in xrange(properties["num_steps"]):
            x = one_step_simulation(agents)
            action_list.append(x)
            if i % 100 == 0:
                all_stats.update_stats(agents, i)

        summary_results = all_stats.process_sa()

        results = {}
        results["setup"] = properties
        results["total_filtered"] = summary_results["total_filtered"]
        results["num_cc"] = summary_results["num_cc"]
        results["size_lcc"] = summary_results["size_lcc"]
        results["summary_results"] = summary_results
        results["all_sa"] = all_stats.sa
        results["all_comm"] = all_stats.comm
        results["all_sa0"] = all_stats.sa0
        results["all_comm0"] = all_stats.comm0
        results["steps"] = all_stats.steps
        results["node_stats"] = node_stats

        add_to_output(results, outfile)

        for agent in agents:
            agent.clear()
            agent.capacity = 1
    relevant_sentences = sorted(list(total))

    dep_props = {'annotators': 'depparse', 'pipelineLanguage': 'en'}
    depparsed = {}
    for sentence_idx in tqdm(relevant_sentences):
        depparsed[sentence_idx] = [
            sent[dep_type] for sent in json.loads(
                nlp.annotate(corpus[sentence_idx], properties=dep_props))
            ['sentences']
        ]

    annotator = lambda x: json.loads(nlp.annotate(x, properties=dep_props))
    for qdata in tqdm(data):
        qdata['edges'] = graph.get_edge_list(qdata,
                                             depparse,
                                             annotator,
                                             depth=depth,
                                             n_neighbors=top_k_neighbors,
                                             dep_type=dep_type)

    for qdata in tqdm(data):
        graph.prune_graph(qdata, tags_to_remove, stop_words)

    for qdata in tqdm(data):
        sentential_nodes_to_idx, doc_neighbors, match_neighbors = graph.get_final_graph_representation(
            qdata, directed=False)
        qdata['sentential_nodes_to_idx'] = sentential_nodes_to_idx
        qdata['doc_neighbors'] = doc_neighbors
        qdata['match_neighbors'] = match_neighbors
        qdata['question_idx'] = set([
            v for k, v in qdata['sentential_nodes_to_idx'].items()
            if k[0] == 'question'
예제 #10
0
        exit(0)
    if real_k >= n:
        print("K must be less than N\n")
        exit(0)
    k = None
print("N = " + str(n) + "\n")
print("K = " + str(real_k) + "\n")

# generate blob clusters
data, cluster_designation = DataGen.generate_data(n, d, real_k)

# generate concentric circles
# data, cluster_designation = DataGen.generate_circles(n, k, d)

# generate weight matrix from observations
weights = GraphGen.get_weight_matrix(n, data)
# create diagonal matrix based on weight matrix
diagonal = GraphGen.get_diagonal_degree_matrix(n, weights)
# calculate the graph laplacian
laplacian = GraphGen.get_laplacian_matrix(n, diagonal, weights)

# use QR iteration to find eigen values/vectors
# e_vectors, e_values_diag = qr_iter(laplacian, n)
try:
    ret = qr_c.calc_eigen_values_vectors(laplacian.tolist(), n)
except:
    exit(0)
e_vectors = np.array(ret[1])
e_values_diag = np.array(ret[0])

# place eigen values in a diagonal matrix
def run_simulation_one_graph(properties, outfile):
    facts = range(properties['num_facts']+properties['num_noise'])
    agents = []
    all_results = []
    for i in xrange(properties['num_agents']):
        agents.append ( SimpleAgent.SimpleAgent(properties['willingness'],\
                                                properties['competence'],\
                                                properties['num_facts'],\
                                                properties['num_noise'],\
                                                properties['spamminess'],\
                                                properties['selfishness'],\
                                                properties['trust_used'],\
                                                properties['inbox_trust_used'],\
                                                properties['trust_filter_on'],
                                                twitter_model = properties['twitter_model']))
        
    ## Create agent graph
    conn, stats = gg.create_graph_type(agents, properties)

    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))

    nodes_to_try = top_nodes (stats)        
    #print "Nodes", nodes_to_try

    for current_agent in nodes_to_try:
        #print "New node", current_agent
        if current_agent != -1:
            agents[current_agent].capacity = 10 ##set one agent to high capacity
            node_stats = current_agent_stats (current_agent, stats)
            agent_to_track = current_agent
        else:
            node_stats = {}
            agent_to_track = 0

        ## Distribute facts to agents
        for i in facts:
            for j in xrange(properties['agent_per_fact']):
                ## find a random agent, and distribute fact i
                k = random.randint(0,properties['num_agents']-1)
                agents[k].knowledge.add(i)
                
        ## Initialize agents to send everything that they think is valuable 
        ## in their outbox
        for agent in agents:
            agent.init_outbox()

        #action_list = []
        all_stats = ss.SimpleSimulationStats(properties['num_facts'],\
                                             properties['num_noise'],\
                                             stats['num_cc'],
                                             stats['largest_cc'], \
                                             properties['sa_increment'],\
                                             agent_to_track)
    
        ##actual simulation starts here
        for i in xrange(properties['num_steps']):
            x = one_step_simulation(agents)
            #action_list.append(x)
            if i%properties['statistic_taking_frequency'] == 0:
                all_stats.update_stats(agents,i)
    
        summary_results = all_stats.process_sa()
    
        results = {}
        results['setup'] = properties
        results['graph_type'] = properties['graph_type']

        results['total_filtered'] = summary_results['total_filtered']
        results['num_cc'] = summary_results['num_cc']
        results['size_lcc'] = summary_results['size_lcc']
        results['summary_results'] = summary_results
        results['all_sa'] = all_stats.sa
        results['all_comm'] = all_stats.comm
        results['all_sa0'] = all_stats.sa0
        results['all_comm0'] = all_stats.comm0
        results['steps'] = all_stats.steps
        results['node_stats'] = node_stats

        add_to_output(all_results, results, outfile)
        
        for agent in agents:
            agent.clear()
            agent.capacity = 1
    flush_results(all_results, outfile)
예제 #12
0
        collision_delta=delta)
    nodes_all = np.concatenate([nodes_syb, nodes_mal, nodes_hon])

    val_time = 0
    val_pos = (w, h)
    val_rad = 60

    id_to_node = {node.id: node for node in nodes_all}
    gui = GUI(terrain, id_to_node)

    syb_node = nodes_syb[0]
    mal_node = nodes_mal[0]
    syb_pos = syb_node.getPos(0)
    mal_poss = [mal_node.getPos(0) for mal_node in nodes_mal]

    graph_gen = GraphGen(nodes_all, val_time, val_pos, val_rad)
    nodes_val = graph_gen.nodes_val

    #------ TEST 1: AVERAGE DIFFERENCE IN DISTANCES TO SYB VS MAL ------#
    for i in range(len(nodes_hon)):
        hon_pos = nodes_hon[i].getPos(0)
        syb_dist = ((syb_pos[0] - hon_pos[0])**2 +
                    (syb_pos[1] - hon_pos[1])**2)**0.5
        mal_pos = mal_poss[i % NUM_MAL]
        mal_dist = ((mal_pos[0] - hon_pos[0])**2 +
                    (mal_pos[1] - hon_pos[1])**2)**0.5
        GLOBAL_DIST_DIFF += [abs(syb_dist - mal_dist)]
    #-------------------------------------------------------------------#

    #------------ TEST 2: FLIPPING COINS BASED ON DISTANCES ------------#
# 	id_to_edges = {syb_node.id:[], mal_node.id:[]}
def run_simulation_one_graph(properties, outfile):
    facts = range(properties['num_facts'] + properties['num_noise'])
    agents = []
    all_results = []
    for i in xrange(properties['num_agents']):
        agents.append ( SimpleAgent.SimpleAgent(properties['willingness'],\
                                                properties['competence'],\
                                                properties['num_facts'],\
                                                properties['num_noise'],\
                                                properties['spamminess'],\
                                                properties['selfishness'],\
                                                properties['trust_used'],\
                                                properties['inbox_trust_used'],\
                                                properties['trust_filter_on'],
                                                twitter_model = properties['twitter_model']))

    ## Create agent graph
    conn, stats = gg.create_graph_type(agents, properties)

    for agent1 in conn.nodes():
        agent1.connect_to(conn.neighbors(agent1))

    nodes_to_try = top_nodes(stats)
    #print "Nodes", nodes_to_try

    for current_agent in nodes_to_try:
        #print "New node", current_agent
        if current_agent != -1:
            agents[
                current_agent].capacity = 10  ##set one agent to high capacity
            node_stats = current_agent_stats(current_agent, stats)
            agent_to_track = current_agent
        else:
            node_stats = {}
            agent_to_track = 0

        ## Distribute facts to agents
        for i in facts:
            for j in xrange(properties['agent_per_fact']):
                ## find a random agent, and distribute fact i
                k = random.randint(0, properties['num_agents'] - 1)
                agents[k].knowledge.add(i)

        ## Initialize agents to send everything that they think is valuable
        ## in their outbox
        for agent in agents:
            agent.init_outbox()

        #action_list = []
        all_stats = ss.SimpleSimulationStats(properties['num_facts'],\
                                             properties['num_noise'],\
                                             stats['num_cc'],
                                             stats['largest_cc'], \
                                             properties['sa_increment'],\
                                             agent_to_track)

        ##actual simulation starts here
        for i in xrange(properties['num_steps']):
            x = one_step_simulation(agents)
            #action_list.append(x)
            if i % properties['statistic_taking_frequency'] == 0:
                all_stats.update_stats(agents, i)

        summary_results = all_stats.process_sa()

        results = {}
        results['setup'] = properties
        results['graph_type'] = properties['graph_type']

        results['total_filtered'] = summary_results['total_filtered']
        results['num_cc'] = summary_results['num_cc']
        results['size_lcc'] = summary_results['size_lcc']
        results['summary_results'] = summary_results
        results['all_sa'] = all_stats.sa
        results['all_comm'] = all_stats.comm
        results['all_sa0'] = all_stats.sa0
        results['all_comm0'] = all_stats.comm0
        results['steps'] = all_stats.steps
        results['node_stats'] = node_stats

        add_to_output(all_results, results, outfile)

        for agent in agents:
            agent.clear()
            agent.capacity = 1
    flush_results(all_results, outfile)
예제 #14
0
    plt.show()


if __name__ == "__main__":
    p = {}
    gtypes = [('random', 20, 0.15, 3), \
              ('random', 200, 0.03, 3), \
              ('watts_strogatz_graph', 20, 0.1, 5), \
              ('watts_strogatz_graph', 200, 0.01, 8), \
              ('barabasi_albert_graph', 20, 0.1, 10), \
              ('barabasi_albert_graph', 200, 0.01, 20), \
    ]

    gtypes = [('watts_strogatz_graph', 40, 0.1, 3), \
              ('watts_strogatz_graph', 40, 0.1, 8), \
              ('watts_strogatz_graph', 40, 0.1, 15), \
    ]

    for (g, x, y, z) in gtypes:
        p['graph_type'] = g
        p['num_agents'] = x
        p['connection_probability'] = y
        p['num_nodes_to_attach'] = z

        ag = range(p['num_agents'])

        (conn, stats) = gg.create_graph_type(ag, p)
        print p
        print "Connected components:", stats['num_cc']
        histogram(stats, 20 * (1 + int(p['num_agents'] / 60)))