Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions CognitiveGraphGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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()
Expand All @@ -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
10 changes: 7 additions & 3 deletions Cognitiverunner.py
Original file line number Diff line number Diff line change
@@ -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
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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,\
Expand Down
37 changes: 19 additions & 18 deletions Cognitivesimulation.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@

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

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.

"""
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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__':
Expand All @@ -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
Expand All @@ -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']
Expand Down
15 changes: 8 additions & 7 deletions GraphGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand All @@ -142,4 +144,3 @@ def hierarchy_graph(objects):
conn.node[objects[0]]['rank'] = 0

return conn

12 changes: 6 additions & 6 deletions KroneckerTester.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading