예제 #1
0
    def __init__(self, model, color_nodes, adj_list):
        # The maximum number of rounds to run the simulation. Randomly chosen
        # number between 100 and 200.
        self.max_rounds = randint(100, 200)
        # A mapping of the colors and the nodes of that color.
        self.color_nodes = color_nodes
        # The adjacency list of the graph.
        self.adj_list = adj_list

        # Import the correct module for the model.
        if model == "majority_all":
            from models.majority_all import MajorityAll
            self.model = MajorityAll(self.adj_list)
        elif model == "majority_colored":
            from models.majority_colored import MajorityColored
            self.model = MajorityColored(self.adj_list)
        elif model == "most_common_colored":
            from models.most_common_colored import MostCommonColored
            self.model = MostCommonColored(self.adj_list)
        elif model == "random_p":
            from models.random_p import RandomP
            self.model = RandomP(self.adj_list)
        elif model == "weighted_random":
            from models.weighted_random import WeightedRandom
            self.model = WeightedRandom(self.adj_list)
        else:
            print "Not a valid model. Random model selected."
            from models.weighted_random import WeightedRandom
            self.model = WeightedRandom(self.adj_list)
예제 #2
0
  def __init__(self, model, color_nodes, adj_list):
    # The maximum number of rounds to run the simulation. Randomly chosen
    # number between 100 and 200.
    self.max_rounds = randint(100, 200)
    # A mapping of the colors and the nodes of that color.
    self.color_nodes = color_nodes
    # The adjacency list of the graph.
    self.adj_list = adj_list

    # Import the correct module for the model.
    if model == "majority_all":
      from models.majority_all import MajorityAll
      self.model = MajorityAll(self.adj_list)
    elif model == "majority_colored":
      from models.majority_colored import MajorityColored
      self.model = MajorityColored(self.adj_list)
    elif model == "most_common_colored":
      from models.most_common_colored import MostCommonColored
      self.model = MostCommonColored(self.adj_list)
    elif model == "random_p":
      from models.random_p import RandomP
      self.model = RandomP(self.adj_list)
    elif model == "weighted_random":
      from models.weighted_random import WeightedRandom
      self.model = WeightedRandom(self.adj_list)
    else:
      print "Not a valid model. Random model selected."
      from models.weighted_random import WeightedRandom
      self.model = WeightedRandom(self.adj_list)
예제 #3
0
class Simulation:
    """
  Class: Simulation
  -----------------
  Simulates the epidemic using different epidemic models.
  """
    def __init__(self, model, color_nodes, adj_list):
        # The maximum number of rounds to run the simulation. Randomly chosen
        # number between 100 and 200.
        self.max_rounds = randint(100, 200)
        # A mapping of the colors and the nodes of that color.
        self.color_nodes = color_nodes
        # The adjacency list of the graph.
        self.adj_list = adj_list

        # Import the correct module for the model.
        if model == "majority_all":
            from models.majority_all import MajorityAll
            self.model = MajorityAll(self.adj_list)
        elif model == "majority_colored":
            from models.majority_colored import MajorityColored
            self.model = MajorityColored(self.adj_list)
        elif model == "most_common_colored":
            from models.most_common_colored import MostCommonColored
            self.model = MostCommonColored(self.adj_list)
        elif model == "random_p":
            from models.random_p import RandomP
            self.model = RandomP(self.adj_list)
        elif model == "weighted_random":
            from models.weighted_random import WeightedRandom
            self.model = WeightedRandom(self.adj_list)
        else:
            print "Not a valid model. Random model selected."
            from models.weighted_random import WeightedRandom
            self.model = WeightedRandom(self.adj_list)

    def run(self):
        """
    Function: run
    -------------
    Runs the epidemic simulation for each generation of the epidemic until there
    is no more change (i.e. it is stable) or we reach the maximum number of
    generations.

    returns: A tuple (output, results) where the output is a dictionary object
             containing a mapping of each generation to the diff generated and
             results is the final mapping of colors to their nodes.
    """
        # Stores a mapping of nodes to their color.
        node_color = dict([(node, None) for node in self.adj_list])
        # Output to be written to file.
        output = OrderedDict()

        # Choose the initial colors for the nodes.
        diff = init(self.color_nodes, node_color)
        color_mappings = to_color_mapping(diff)

        # Make sure all colors are in the initial diff.
        for color in self.color_nodes.keys():
            if color not in color_mappings:
                color_mappings[color] = []
        output["0"] = color_mappings
        generation = 1

        # Keep calculating the epidemic until it stops changing.
        nodes = self.adj_list.keys()
        while not is_stable(generation, self.max_rounds, output):
            print ".",
            diff = {}
            # Get the next color for the nodes.
            node_color_copy = deepcopy(node_color)
            for node in nodes:
                (changed, color) = self.model.update(node_color, node)
                # Store the node's new color only if it changed.
                if changed:
                    diff[node] = color
                    node_color_copy[node] = color
            node_color = node_color_copy

            # Convert the mapping of a node to its color into colors and
            # corresponding nodes.
            output[str(generation)] = to_color_mapping(diff)
            generation += 1

        print "\nTotal:", generation, "generations."
        return (output, final_color_mapping(self.color_nodes.keys(),
                                            node_color))
예제 #4
0
class Simulation:
  """
  Class: Simulation
  -----------------
  Simulates the epidemic using different epidemic models.
  """

  def __init__(self, model, color_nodes, adj_list):
    # The maximum number of rounds to run the simulation. Randomly chosen
    # number between 100 and 200.
    self.max_rounds = randint(100, 200)
    # A mapping of the colors and the nodes of that color.
    self.color_nodes = color_nodes
    # The adjacency list of the graph.
    self.adj_list = adj_list

    # Import the correct module for the model.
    if model == "majority_all":
      from models.majority_all import MajorityAll
      self.model = MajorityAll(self.adj_list)
    elif model == "majority_colored":
      from models.majority_colored import MajorityColored
      self.model = MajorityColored(self.adj_list)
    elif model == "most_common_colored":
      from models.most_common_colored import MostCommonColored
      self.model = MostCommonColored(self.adj_list)
    elif model == "random_p":
      from models.random_p import RandomP
      self.model = RandomP(self.adj_list)
    elif model == "weighted_random":
      from models.weighted_random import WeightedRandom
      self.model = WeightedRandom(self.adj_list)
    else:
      print "Not a valid model. Random model selected."
      from models.weighted_random import WeightedRandom
      self.model = WeightedRandom(self.adj_list)


  def run(self):
    """
    Function: run
    -------------
    Runs the epidemic simulation for each generation of the epidemic until there
    is no more change (i.e. it is stable) or we reach the maximum number of
    generations.

    returns: A tuple (output, results) where the output is a dictionary object
             containing a mapping of each generation to the diff generated and
             results is the final mapping of colors to their nodes.
    """
    # Stores a mapping of nodes to their color.
    node_color = dict([(node, None) for node in self.adj_list])
    # Output to be written to file.
    output = OrderedDict()

    # Choose the initial colors for the nodes.
    diff = init(self.color_nodes, node_color)
    color_mappings = to_color_mapping(diff)

    # Make sure all colors are in the initial diff.
    for color in self.color_nodes.keys():
      if color not in color_mappings:
        color_mappings[color] = []
    output["0"] = color_mappings
    generation = 1

    # Keep calculating the epidemic until it stops changing.
    nodes = self.adj_list.keys()
    while not is_stable(generation, self.max_rounds, output):
      print ".",
      diff = {}
      # Get the next color for the nodes.
      node_color_copy = deepcopy(node_color)
      for node in nodes:
        (changed, color) = self.model.update(node_color, node)
        # Store the node's new color only if it changed.
        if changed:
          diff[node] = color
          node_color_copy[node] = color
      node_color = node_color_copy

      # Convert the mapping of a node to its color into colors and
      # corresponding nodes.
      output[str(generation)] = to_color_mapping(diff)
      generation += 1

    print "\nTotal:", generation, "generations."
    return (output, final_color_mapping(self.color_nodes.keys(), node_color))