Browse files

Upgraded some examples.

  • Loading branch information...
1 parent 6a45523 commit 40f38095b7b3e26c18521fa770020ff80e7aeeb8 @rik0 committed Jan 16, 2013
Showing with 38 additions and 26 deletions.
  1. +10 −10 examples/ba_compare_degree_distribution.py
  2. +27 −15 examples/ba_concurrency_levels.py
  3. +1 −1 pynetsym/graph/nx_impl.py
View
20 examples/ba_compare_degree_distribution.py
@@ -50,7 +50,7 @@ def make_distributions(graph):
F_ccdf = plt.figure()
sim = BA()
- sim.setup_parameters(starting_edges=11, starting_network_size=11, steps=5000)
+ sim.setup_parameters(starting_edges=11, starting_network_size=11, steps=100000)
sim.run()
steps = sim.steps
@@ -67,10 +67,6 @@ def make_distributions(graph):
ccdf_axes.loglog(arange(starting_edges-1, len(sim_ccdf)), sim_ccdf[starting_edges-1:], color='blue')
pdf_axes.loglog(arange(starting_edges, len(sim_pdf)), sim_pdf[starting_edges:], color='blue', marker='o', linestyle='')
- del sim_ccdf
- del sim_pdf
- gc.collect()
-
ba_graph = nx.barabasi_albert_graph(
steps + starting_networks_size,
starting_edges)
@@ -80,11 +76,6 @@ def make_distributions(graph):
ccdf_axes.loglog(arange(starting_edges-1, len(ba_ccdf)), ba_ccdf[starting_edges-1:], color='red', linestyle='--')
pdf_axes.loglog(arange(starting_edges, len(ba_pdf)), ba_pdf[starting_edges:], color='red', marker='+', linestyle='')
- del ba_graph
- del ba_ccdf
- del ba_pdf
- gc.collect()
-
ccdf_axes.set_title('CCDF BA(%d, %d)' % (steps, starting_edges))
ccdf_axes.legend(('Agent-Based', 'NetworkX'), loc='best')
ccdf_axes.set_xlabel('Degree $x$')
@@ -109,4 +100,13 @@ def make_distributions(graph):
F_pdf.savefig(path.join(os.curdir, directory_name, 'ba-pdf.%s' % format),
dpi=DPI, format=format)
+ import pandas as pd
+ ccdf_df = pd.DataFrame({'Agent-based simulation': pd.Series(sim_ccdf, index=arange(len(sim_ccdf))),
+ 'Traditional simulation': pd.Series(ba_ccdf, index=arange(len(ba_ccdf))),})
+ ccdf_df.to_csv(path.join(os.curdir, directory_name, 'ba-ccdf.csv'))
+
+ pdf_df = pd.DataFrame({'Agent-based simulation': pd.Series(sim_pdf, index=arange(len(sim_pdf))),
+ 'Traditional simulation': pd.Series(ba_pdf, index=arange(len(ba_pdf))),})
+ pdf_df.to_csv(path.join(os.curdir, directory_name, 'ba-pdf.csv'))
+
View
42 examples/ba_concurrency_levels.py
@@ -1,5 +1,6 @@
from matplotlib import pyplot as plt
from os import path
+from traits.trait_types import Int
from pynetsym import node_manager, BasicConfigurator
from pynetsym.generation_models import nx_barabasi_albert as barabasi_albert
import networkx as nx
@@ -19,17 +20,14 @@ def plot_simulation(sim, ccdf_axes, pdf_axes, color, marker, linestyle):
steps = sim.steps
starting_edges = sim.starting_edges
starting_networks_size = sim.starting_network_size
- graph = sim.graph.handle
- del sim
- gc.collect()
- sim_ccdf, sim_pdf = make_distributions(graph)
- del graph
- ccdf_axes.loglog(arange(starting_edges - 1, len(sim_ccdf)), sim_ccdf[starting_edges - 1:], color=color, linestyle=linestyle)
- pdf_axes.loglog(arange(starting_edges, len(sim_pdf)), sim_pdf[starting_edges:], color=color, marker=marker, linestyle='')
- del sim_ccdf
- del sim_pdf
- gc.collect()
- return steps, starting_edges
+ with sim.graph.handle as graph:
+ del sim
+ gc.collect()
+ sim_ccdf, sim_pdf = make_distributions(graph)
+ del graph
+ ccdf_axes.loglog(arange(starting_edges - 1, len(sim_ccdf)), sim_ccdf[starting_edges - 1:], color=color, linestyle=linestyle)
+ pdf_axes.loglog(arange(starting_edges, len(sim_pdf)), sim_pdf[starting_edges:], color=color, marker=marker, linestyle='')
+ return steps, starting_edges, sim_ccdf, sim_pdf
def make_distributions(graph):
bins = np.bincount(graph.degree().values())
@@ -43,16 +41,20 @@ def make_distributions(graph):
class Node2(Node):
+ starting_edges = Int
+
def activate(self):
forbidden = set()
forbidden.add(self.id)
+ rs = self.graph.random_selector
while self.starting_edges:
- random_node = self.graph.preferential_attachment_node()
+ random_node = rs.preferential_attachment()
if random_node not in forbidden:
self.link_to(random_node)
forbidden.add(random_node)
self.starting_edges -= 1
- self.cooperate()
+ self.cooperate()
+
class Activator2(Activator):
@@ -99,9 +101,9 @@ class configurator_type(BasicConfigurator):
sim = BA()
- steps, starting_edges = plot_simulation(sim, ccdf_axes, pdf_axes, 'blue', 'o', '-')
+ steps, starting_edges, sim_ccdf, sim_pdf = plot_simulation(sim, ccdf_axes, pdf_axes, 'blue', 'o', '-')
sim2 = BA2()
- plot_simulation(sim2, ccdf_axes, pdf_axes, 'red', '+', '--')
+ steps2, starting_edges2, sim_ccdf2, sim_pdf2 = plot_simulation(sim2, ccdf_axes, pdf_axes, 'red', '+', '--')
ccdf_axes.set_title('CCDF BA(%d, %d)' % (steps, starting_edges))
ccdf_axes.legend(('Not-Interruptible Handler', 'Interruptible Handler'), loc='best')
@@ -128,3 +130,13 @@ class configurator_type(BasicConfigurator):
dpi=DPI, format=format)
+ import pandas as pd
+ ccdf_df = pd.DataFrame({'Frequency in Non-Interruptible Handler Approach': pd.Series(sim_ccdf, index=arange(len(sim_ccdf))),
+ 'Frequency in Interruptible Handler Approach': pd.Series(sim_ccdf2, index=arange(len(sim_ccdf2))),})
+ ccdf_df.index.name = 'Node degree'
+ ccdf_df.to_csv(path.join(os.curdir, directory_name, 'nni-ccdf.csv'))
+
+ pdf_df = pd.DataFrame({'Frequency in Non-Interruptible Handler Approach': pd.Series(sim_pdf, index=arange(len(sim_pdf))),
+ 'Frequency in Interruptible Handler Approach': pd.Series(sim_pdf2, index=arange(len(sim_pdf2))),})
+ pdf_df.to_csv(path.join(os.curdir, directory_name, 'nni-pdf.csv'))
+ pdf_df.index.name = 'Node degree'
View
2 pynetsym/graph/nx_impl.py
@@ -29,7 +29,7 @@ def parameters(self):
return dict(graph=nx.Graph())
def __init__(self, graph, random_selector=None):
- self.nx_graph = graph
+ self.nx_graph = nx.Graph() if graph is None else graph
self.random_selector = (NxRandomSelector(graph_container=self)
if random_selector is None else random_selector)

0 comments on commit 40f3809

Please sign in to comment.