diff --git a/CognitiveGraphGen.py b/CognitiveGraphGen.py index c87fa8d..17ddc61 100644 --- a/CognitiveGraphGen.py +++ b/CognitiveGraphGen.py @@ -39,10 +39,11 @@ def get_graph(objects, properties): tries += 1 x = x.to_undirected() cc_conn = nx.connected_components(x) - if len(cc_conn) == 1 or tries > 5: + cc_max = [len(c) for c in sorted(cc_conn, key=len, reverse=True)] + if len(cc_max) == 1 or tries > 5: ##best effort to create a connected graph! break - return x, cc_conn + return x, cc_max def create_graph_type(objects, properties): (x, cc_conn) = get_graph(objects, properties) @@ -51,7 +52,7 @@ def create_graph_type(objects, properties): deg = nx.degree_centrality(x) stats = {'cc':cc, 'bc':bc, 'deg':deg, \ - 'num_cc':len(cc_conn), 'largest_cc':len(cc_conn[0])} + 'num_cc':len(cc_conn), 'largest_cc':cc_conn[0]} conn = nx.Graph() for (i,j) in x.edges(): @@ -113,23 +114,23 @@ def collaborative_graph(objects): object2 = random.choice(objects[5:]) conn.add_edge(object1,object2) conn.add_edge(object2,object1) - + ##Add collaboration between middle row. for object1 in objects[1:5]: for object2 in objects[1:5]: if object1 != object2: conn.add_edge(object1,object2) conn.add_edge(object2,object1) - + ##Link middle layer to root for object in objects[1:5]: conn.node[object]['rank'] = 1 conn.add_edge(object,objects[0]) conn.node[objects[0]]['rank'] = 0 - + return conn -#Create a fulll, five layer bitree. +#Create a fulll, five layer bitree. #This can be adjusted later to be an arbitrary number of layers and arbitrary number of children def hierarchy_graph(objects): conn = nx.DiGraph() @@ -141,7 +142,6 @@ def hierarchy_graph(objects): curr_layer = pow(2,layer)-1 for employee in range(curr_layer+1): #Number of agents in a layer is one more than the ID of the first agent in the layer conn.add_edge(employee+curr_layer, employee/2 + prev_layer) - - - return conn + + return conn diff --git a/Cognitiverunner.py b/Cognitiverunner.py index e4b8777..3b968fb 100644 --- a/Cognitiverunner.py +++ b/Cognitiverunner.py @@ -1,5 +1,5 @@ """ - The runner requires an input configuration file + The runner requires an input configuration file --see template for an example, and an output file for the results. Example: python runner.py config_file output_file """ @@ -19,7 +19,7 @@ def output(output_loc, is_slave, identity, text): sock.sendall('{0:0>8}'.format(str(len(text)))) sock.sendall(text) sock.close() - + else: f = open(output_loc,"a") f.write (text) @@ -68,7 +68,7 @@ def run(config_file, output_loc, is_slave, identity): for d in config ['decisiveness']: for cap in config ['capacity']: for corroboration_threshold in config['corroboration_threshold']: - + print "Case", i, "being executed" print "running for %d/%d facts per group %d groups %d agents"\ %(num_fpro+num_fcon, num_npro+num_ncon, num_groups, num_agents) @@ -90,7 +90,11 @@ def run(config_file, output_loc, is_slave, identity): radius, \ num_steps, \ w, c, e, d, \ + # CM # Missing! corroboration_threshold, \ + # DISC_W_AMBIG # Missing! + # DISP # Missing! + # OUT_CAPACITY # Missing! cap, \ num_trials, \ graph_type,\ diff --git a/Cognitivesimulation.py b/Cognitivesimulation.py index cf983e7..b4aadfd 100644 --- a/Cognitivesimulation.py +++ b/Cognitivesimulation.py @@ -1,10 +1,10 @@ -import random +import random import CognitiveAgent as Agent import CognitiveGraphGen as gg import CognitiveSimulationStats as SimulationStats import networkx as nx -from simutil import * +from simutil import * ########## Initialization code @@ -12,22 +12,23 @@ 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 + ## return the number of connected components and ## the size of the largest connected component - return (len(cc_conn), len(cc_conn[0])) + cc_max = [len(c) for c in sorted(cc_conn, key=len, reverse=True)] + return (len(cc_max), cc_max[0]) def change_agent_property(agents, setup): """ Setup is a dictionary that has new values and a ratio. - It changes a proportion of agents given by the ratio to the + It changes a proportion of agents given by the ratio to the given values for the given properties. """ @@ -57,14 +58,14 @@ def change_agent_property(agents, setup): if 'disposition' in setup.keys(): for i in xrange(cutoff): agents[who[i]].disposition = setup['disposition'] - + def change_agent_property_strict(agents, setup): """ Setup is a dictionary with a parameter as a key, and the value is an array containing two items, the new value for the parameter and a list of agents to update """ - + if 'competence' in setup.keys(): for i in setup['competence'][1]: agents[i].competence = setup['competence'][0] @@ -80,8 +81,8 @@ def change_agent_property_strict(agents, setup): if 'corroboration_threshold' in setup.keys(): for i in setup['corroboration_threshold'][1]: agents[i].corroboration_threshold = setup['corroboration_threshold'][0] - - + + ########## Run simulation @@ -109,7 +110,7 @@ def multi_step_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ SPAMMINESS=0, SELFISHNESS=0, \ TRUST_USED=True, INBOX_TRUST_SORTED=False, \ TRUST_FILTER_ON=True): - + facts = range((NUM_FPRO + NUM_FCON + NUM_NPRO + NUM_NCON)*NUM_GROUPS) random.shuffle(facts) ##print "Created", len(facts), "facts" @@ -127,7 +128,7 @@ def multi_step_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ TRUST_USED, INBOX_TRUST_SORTED, \ TRUST_FILTER_ON) ) - ## Now, change the properties of some agents + ## Now, change the properties of some agents ## based on the agent setup data for setup in AGENT_SETUP: ## each setup is a dictionary change_agent_property_strict(agents, setup) @@ -144,8 +145,8 @@ def multi_step_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ k = random.randint(0,NUM_AGENTS-1) agents[k].receive(i, None ) #agents[k].add_fact(i, i % (NUM_FPRO + NUM_FCON + NUM_NPRO + NUM_NCON) < (NUM_FPRO + NUM_FCON)) - - ## Initialize agents to send everything that they think is valuable + + ## Initialize agents to send everything that they think is valuable ## in their outbox ##for agent in agents: ## agent.init_outbox() @@ -257,7 +258,7 @@ def run_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ results['correct_decisions'] = all_stats.correct_decisions return (results) - + ########## Main body if __name__ == '__main__': @@ -275,7 +276,7 @@ def run_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ GRAPH_TYPE = 'spatial_random' NUM_TRIAL = 1 - ## number of times to repeat the simulation for averaging out values + ## number of times to repeat the simulation for averaging out values # for i in xrange(5): # w = 0.2 + 0.2*i @@ -297,7 +298,7 @@ def run_simulation(NUM_FPRO, NUM_FCON, NUM_NPRO, NUM_NCON, NUM_GROUPS, \ AGENT_SETUP=[{ "ratio" : 0.2,\ "spammer" : 0.8, \ "competence":0.2 }]) - + ##print results print 'w, c, num_cc, size_lcc' print w, c, results['num_cc'], results['size_lcc'] diff --git a/GraphGen.py b/GraphGen.py index c94cde4..fbcd631 100644 --- a/GraphGen.py +++ b/GraphGen.py @@ -36,19 +36,21 @@ def get_graph(objects, properties): x = nx.star_graph(len(objects)-1) tries += 1 cc_conn = nx.connected_components(x) - if len(cc_conn) == 1 or tries > 5: + cc_max = [len(c) for c in sorted(cc_conn, key=len, reverse=True)] + if len(cc_max) == 1 or tries > 5: ##best effort to create a connected graph! break - return x, cc_conn + return x, cc_max def create_graph_type(objects, properties): (x, cc_conn) = get_graph(objects, properties) cc = nx.closeness_centrality(x) bc = nx.betweenness_centrality(x) deg = nx.degree_centrality(x) + print cc_conn stats = {'cc':cc, 'bc':bc, 'deg':deg, \ - 'num_cc':len(cc_conn), 'largest_cc':len(cc_conn[0])} + 'num_cc':len(cc_conn), 'largest_cc':cc_conn[0]} conn = nx.Graph() for (i,j) in x.edges(): @@ -110,20 +112,20 @@ def collaborative_graph(objects): object2 = random.choice(objects[5:]) conn.add_edge(object1,object2) conn.add_edge(object2,object1) - + ##Add collaboration between middle row. for object1 in objects[1:5]: for object2 in objects[1:5]: if object1 != object2: conn.add_edge(object1,object2) conn.add_edge(object2,object1) - + ##Link middle layer to root for object in objects[1:5]: conn.node[object]['rank'] = 1 conn.add_edge(object,objects[0]) conn.node[objects[0]]['rank'] = 0 - + return conn def hierarchy_graph(objects): @@ -142,4 +144,3 @@ def hierarchy_graph(objects): conn.node[objects[0]]['rank'] = 0 return conn - diff --git a/KroneckerTester.py b/KroneckerTester.py index 08897b9..901d148 100644 --- a/KroneckerTester.py +++ b/KroneckerTester.py @@ -1,12 +1,12 @@ -from InitMatrix import InitMatrix -import Generator +from KroneckerInitMatrix import InitMatrix +import KroneckerGenerator import numpy as np import networkx as nx import testgg as test import matplotlib.pyplot as plt def get_graph(nxgraph): - + x = nxgraph cc_conn = nx.connected_components(x) num_cc = nx.number_connected_components(x) @@ -43,7 +43,7 @@ def create_graph_stats(nxgraph): #p = 15 #c = 6 #probArr = np.array([1, c*p, p/c, 0, 0, c*p, 1, p/c, 0, 0, p/c, p/c, 1, p/c, p/c, 0, 0, p/c, 1, c*p, 0, 0, p/c, c*p, 1]) -#init.makeStochasticCustom(probArr) +#init.makeStochasticCustom(probArr) #Networkx Graph Gen as Seed, Alpha Beta after Testing #G = nx.watts_strogatz_graph(5, 2, 0.1) @@ -65,11 +65,11 @@ def create_graph_stats(nxgraph): print nodes print "Kronecker Iterations:" print k -nxgraph = Generator.generateStochasticKron(init, k, False) +nxgraph = KroneckerGenerator.generateStochasticKron(init, k, False) #for line in nx.generate_edgelist(nxgraph, data=False): # print(line) print "Done Creating Network!" -nx.draw(nxgraph) #pos=nx.random_layout(nxgraph) +nx.draw(nxgraph) #pos=nx.random_layout(nxgraph) plt.show() #print "Creating Histogram..." #histogramInput = create_graph_stats(nxgraph) diff --git a/simulation.py b/simulation.py index 1975569..13cfabb 100644 --- a/simulation.py +++ b/simulation.py @@ -1,10 +1,10 @@ -import random -import Agent +import random +import Agent import GraphGen as gg import SimulationStats import networkx as nx -from simutil import * +from simutil import * ########## Initialization code @@ -30,22 +30,23 @@ def create_connectivity(agents, p, type='undirected_random'): 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 + ## return the number of connected components and ## the size of the largest connected component - return (len(cc_conn), len(cc_conn[0])) + cc_max = [len(c) for c in sorted(cc_conn, key=len, reverse=True)] + return (len(cc_max), cc_max[0]) def change_agent_property(agents, setup): """ Setup is a dictionary that has new values and a ratio. - It changes a proportion of agents given by the ratio to the + It changes a proportion of agents given by the ratio to the given values for the given properties. """ @@ -91,7 +92,7 @@ def multi_step_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ SPAMMINESS=0, SELFISHNESS=0, \ TRUST_USED=True, INBOX_TRUST_SORTED=False, \ TRUST_FILTER_ON=True): - + facts = range(NUM_FACTS + NUM_NOISE) ##print "Created", len(facts), "facts" @@ -103,7 +104,7 @@ def multi_step_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ TRUST_USED, INBOX_TRUST_SORTED, \ TRUST_FILTER_ON) ) - ## Now, change the properties of some agents + ## Now, change the properties of some agents ## based on the agent setup data for setup in AGENT_SETUP: ## each setup is a dictionary change_agent_property(agents, setup) @@ -119,8 +120,8 @@ def multi_step_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ ## find a random agent, and distribute fact i k = random.randint(0,NUM_AGENTS-1) agents[k].add_fact(i) - - ## Initialize agents to send everything that they think is valuable + + ## Initialize agents to send everything that they think is valuable ## in their outbox for agent in agents: agent.init_outbox() @@ -203,7 +204,7 @@ def run_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ results['steps'] = all_stats.steps return (results) - + ########## Main body if __name__ == '__main__': @@ -221,7 +222,7 @@ def run_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ GRAPH_TYPE = 'spatial_random' NUM_TRIAL = 1 - ## number of times to repeat the simulation for averaging out values + ## number of times to repeat the simulation for averaging out values # for i in xrange(5): # w = 0.2 + 0.2*i @@ -240,7 +241,7 @@ def run_simulation(NUM_FACTS, NUM_NOISE, NUM_AGENTS, \ AGENT_SETUP=[{ "ratio" : 0.2,\ "spammer" : 0.8, \ "competence":0.2 }]) - + ##print results print 'w, c, num_cc, size_lcc' print w, c, results['num_cc'], results['size_lcc'] diff --git a/tester_cognitive.py b/tester_cognitive.py index 7a8a949..be66b93 100644 --- a/tester_cognitive.py +++ b/tester_cognitive.py @@ -8,7 +8,7 @@ import Cognitivesimulation as sim -import json +import json import sys import time import simplejson as sj @@ -54,7 +54,9 @@ radius, \ num_steps, \ w, c, e, decisiveness, \ + # CM # Missing! corraboration_threshold, \ + # DISC_W_AMBIG, DISP, OUT_CAPACITY, # Missing! cap, \ num_trials, \ graph_type,\ @@ -85,5 +87,3 @@ break print infostr print - - diff --git a/tester_cognitive2.py b/tester_cognitive2.py index 30ac082..7022855 100644 --- a/tester_cognitive2.py +++ b/tester_cognitive2.py @@ -1,5 +1,5 @@ ## good test cases for engagement versus decisiveness -## when there is a lot of noise, actually high engagement reduces the network performance +## when there is a lot of noise, actually high engagement reduces the network performance ## by multiplying the noise in the network instead of network filtering out noise slowly ## in problem cases with high con noise, other cases or when competence is high, engagement ## is good @@ -8,7 +8,7 @@ ## at least I think so import Cognitivesimulation as sim -import json +import json import sys import time import simplejson as sj @@ -43,9 +43,9 @@ # 'engagement' : [0.2,0.5,0.8], # 'decisiveness' : [0.7, 0.8, 0.9], # 'closedmindedness' : [0.0,0.5,1.0], -# 'corroboration_threshold' : [1,2,3], +# 'corroboration_threshold' : [1,2,3], } - + if len(sys.argv) > 1: fname = sys.argv[1] @@ -69,7 +69,9 @@ num_steps, \ w, c, e, decisiveness, cm, \ corroboration_threshold, \ + # DISC_W_AMBIG # Missing! disp, \ + # OUT_CAPACITY # Missing! cap, \ num_trials, \ graph_type,\ @@ -81,8 +83,6 @@ f.write(sj.dumps(results) + "\n") - - infostr = "comp: %.2f, e: %.2f, good: %d/%d, bad: %d/%d, "\ "maxsa: %.2f, decisiveness: %.2f, agf: %d, cf: %d, capacity: %d" \ %(c, e, num_fpro, num_npro, num_fcon, num_ncon, \ @@ -100,4 +100,3 @@ (i > 0 and results['decisions'][i-1] == results['decisions'][i]): break print infostr -