def run_simulation(network_dir, condition, test_sub_name, disconnection_rate=0.0, drop_rate=0.0, threshold=sys.maxint):
    """
    Network directory should contain network and sample files
    """
    test_name = os.path.basename(network_dir)
    print "%s - %s - %s disconnect(%4.2f) drop(%4.2f) threshold(%d)" % (network_dir, test_name, test_sub_name, disconnection_rate, drop_rate, threshold)
    network_file_path = os.path.join(network_dir, test_name + ".txt")
    assert os.path.exists(network_file_path), "No network file %s exists " % network_file_path
    sample_file_path = os.path.join(network_dir, test_name + ".sample.txt")
    assert os.path.exists(sample_file_path), "No network file %s exists " % sample_file_path

    network = Network(network_file_path)
    host_ids = network.get_host_ids() # [h0, h1, h2]
    hosts = []

    neighbors = network.get_network() # {0:[1], 1:[0,2], 2:[1]}
    for h in host_ids:
        # neighbor should be set in this experiment
        hosts.append(Host(id=h, neighbors=neighbors[h]))

    test_directory, sample = make_ready_for_test(network_dir=network_dir, test_name=test_name, condition=condition, test_sub_name=test_sub_name)

    if test_sub_name.startswith("single"):
        propagation_mode = ContextAggregator.SINGLE_ONLY_MODE
    else:
        propagation_mode = ContextAggregator.AGGREGATION_MODE

    config = {"network":network, "hosts":hosts, "neighbors":neighbors,
              "test_directory":test_directory, "sample":sample,
              "disconnection_rate":disconnection_rate, "drop_rate":drop_rate,
              ContextAggregator.PM:propagation_mode,
              "threshold":threshold}
    s = AggregationAsyncSimulator()
    simulation = s.run(config=config)
    return simulation
def get_average_network_link(network):
    n = Network(network)
    ids = n.get_host_ids()
    result = []
    for id in ids:
        result.append(n.get_neighbors(id))

    sizes = map(len, result)
    return 1.0*sum(sizes)/len(sizes)
def con(location_file, file_path,limit=10):
    result = readLocationFile(location_file)
    #print result
    result = connected(result, connection_limit=limit)
    #print result
    generate_network_file(result, file_path, minus_one=False)
    network = Network()
    network.read(file_path)
    network.dot_gen(file_path + ".dot")
 def generate(self, node):
     """
     >>> g = RandomAddTreeGen()
     >>> n = g.generate(10)
     >>> print n
     """
     self.node = node
     n = Network()
     n.add_node(1)
     for i in range(2, node+1):
         j = 3
         n.add_neighbor(j)
     return n
def generate(trees, result_dir, connection_rate):
    files = glob.glob(trees + os.sep + "tree_*.txt")
    for f in files:
        print f + "%5.1f" % connection_rate
        file_name = os.path.basename(f)
        mesh_name = file_name.replace("tree","mesh")
        mesh_file_name = mesh_name # '_'.join(split_names)
        mesh_file_path = os.path.join(result_dir, mesh_file_name)
        t = Network(f)
        h = TreeGen.tree_to_mesh(t.network, connection_rate)
        #print h
        a = Network(h)
        a.write(mesh_file_path)
        a.dot_gen(mesh_file_path + ".dot")
    def generate(self, node, file_path = None):
        """
        """
        n = Network()
        n.add_node(1)
        for i in range(2, node+1):
            r = range(1, i)
            j = random.choice(r)
            n.add_neighbor(i,j)

        if file_path is not None:
            self.write(n, file_path)

        return n
    def get_hosts(self):
        """
        >>> d = get_simple_test_dir() + os.sep + "test_network1"
        >>> r = ReadReports(d).get_hosts()
        >>> r == ['host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8']
        True
        """
        network_dir = self.network_dir
        network_name = os.path.basename(network_dir)
        network_file_path = os.path.join(network_dir, network_name + ".txt")
        assert os.path.exists(network_file_path), "No %s network file exists" % network_file_path

        n = Network(network_file_path)
        ids = n.get_host_ids()
        return ["host%d" % i for i in sorted(ids)]
def reduce_average_network_link(network, new_network_path, goal):
    network = Network(network).get_network()
    new_network = copy.copy(network)

    for key, neighbors in network.items():
        if len(neighbors) > goal:
            new_neighbors = sorted(neighbors, key=lambda e: random.random())
            new_neighbors = new_neighbors[0:goal]
            diff = set(neighbors) - set(new_neighbors)
            for d in diff:
                if len(new_network[d]) == 1: # if this is the only link, don't do anything
                    new_neighbors.append(d)
                else:
                    print "remove key(%d) from n(%s)" % (key, new_network[d])
                    new_network[d].remove(key)
                    print "removed key(%d) from n(%s)" % (key, new_network[d])
            new_network[key] = new_neighbors

    print new_network
    #new_network = Network.make_symmetric_network(self, new_network)
    n2 = Network(new_network)
    n2.write(new_network_path)
    n2.dot_gen(new_network_path + ".dot")
def get_test_network(condition, network_name):
    d = get_test_files_directory()
    network_file = os.path.join(d, "%s/%s/%s.txt" % (condition, network_name, network_name))
    network = Network()
    network.read(network_file)
    return network
sys.path.insert(0, source_location)

source_location = os.path.dirname(os.path.abspath(__file__)) + "/.."
sys.path.insert(0, source_location)

from aggregation_simulator.utils_configuration import get_test_files_directory, get_configuration
from aggregation_simulator.network import Network
from aggregation_async_simulator import run_simulation
from context_aggregator.utils_same import same

test_name1 = "test_network0"
d = os.path.join(get_test_files_directory(), test_name1)
network_file_path1 = os.path.join(d, test_name1 + ".txt")
simulation_root_dir = get_configuration("config.cfg", "TestDirectory", "simple_test_root_dir")

network = Network()
network.read(network_file_path1)
dot_file_path = os.path.join(d, network_file_path1 + ".dot")

#
# Code duplication from utils.py
#
def get_test_network(condition, network_name):
    d = get_test_files_directory()
    network_file = os.path.join(d, "%s/%s/%s.txt" % (condition, network_name, network_name))
    network = Network()
    network.read(network_file)
    return network

def runit(simulation_root_dir, network_file_path, sample_file_path, condition, test_sub_name, disconnection_rate = 0.0, drop_rate=0.0, threshold=sys.maxint):
    network_dir = make_ready_for_one_file_simulation(simulation_root_dir=simulation_root_dir,
def generate_dot(network_file_path):
    network = Network(network_file_path)
    dot_file_path = network_file_path + ".dot"
    network.dot_gen(dot_file_path)
 def test_dot_gen(self):
     network = Network()
     network.read(network_file_path)
     network.dot_gen(dot_file_path)