SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2. import random
  3. import networkx as nx
  4. import csv
  5. from multiprocessing.dummy import Pool as ThreadPool
  6. from decimal import *
  7.  
  8.  
  9. class Vertex:
  10.     def __init__(self, key, is_active=False):
  11.         self.key = key
  12.         self.neighbors = {}
  13.         self.is_active = is_active
  14.         self.nextState = is_active
  15.  
  16.     def add_neighbor(self, nbr, weight=0):
  17.         self.neighbors[nbr] = weight
  18.  
  19.     def __str__(self):
  20.         return str(self.key) + ' sąsiedzi: ' + str([vert.key for vert in self.neighbors])
  21.  
  22.     def get_connections(self):
  23.         return self.neighbors.keys()
  24.  
  25.     def get_key(self):
  26.         return self.key
  27.  
  28.     def get_weight(self, nbr):
  29.         return self.neighbors[nbr]
  30.  
  31.     def get_active(self):
  32.         return self.is_active
  33.  
  34.     def update_state(self):
  35.         self.is_active = self.nextState
  36.  
  37.     def set_next_state(self, neighbor_counter):
  38.         if not self.is_active and (neighbor_counter == 3 or neighbor_counter == 6):
  39.             self.nextState = True
  40.         elif self.is_active and (neighbor_counter == 2 or neighbor_counter == 3):
  41.             self.nextState = True
  42.         else:
  43.             self.nextState = False
  44.  
  45.  
  46. class Graph:
  47.     def __init__(self):
  48.         self.vertices = {}
  49.         self.active_vertices = 0
  50.  
  51.     def add_vertex(self, vertex):
  52.         self.vertices[vertex.key] = vertex
  53.  
  54.     def get_vertex(self, key):
  55.         try:
  56.             return self.vertices[key]
  57.         except KeyError:
  58.             return None
  59.  
  60.     def __contains__(self, key):
  61.         return key in self.vertices
  62.  
  63.     def add_edge_directed(self, from_key, to_key, weight=0):
  64.         if from_key not in self.vertices:
  65.             self.add_vertex(Vertex(from_key))
  66.         if to_key not in self.vertices:
  67.             self.add_vertex(Vertex(to_key))
  68.         self.vertices[from_key].add_neighbor(self.vertices[to_key], weight)
  69.  
  70.     def add_edge_undirected(self, from_key, to_key, weight=0):
  71.         if from_key not in self.vertices:
  72.             self.add_vertex(Vertex(from_key))
  73.         if to_key not in self.vertices:
  74.             self.add_vertex(Vertex(to_key))
  75.         self.vertices[from_key].add_neighbor(self.vertices[to_key], weight)
  76.         self.vertices[to_key].add_neighbor(self.vertices[from_key], weight)
  77.  
  78.     def get_vertices(self):
  79.         return self.vertices.keys()
  80.  
  81.     def __iter__(self):
  82.         return iter(self.vertices.values())
  83.  
  84.     def get_active_vertices(self):
  85.         return sum(1 for v in self.vertices.values() if v.get_active())
  86.  
  87.     def simulate(self):
  88.         for vert in self.vertices.values():
  89.             number_of_actives = sum(1 for nbr in vert.get_connections() if nbr.get_active())
  90.             vert.set_next_state(number_of_actives)
  91.         for vert in self.vertices.values():
  92.             vert.update_state()
  93.  
  94.     def active_connections(self):
  95.         return [(vert, x) for vert in (vert for vert in self.vertices.values() if vert.is_active) for x in
  96.                 (neighbour for neighbour in vert.get_connections() if neighbour.is_active)]
  97.  
  98.     def add_all_neighbors(self, dim):
  99.         for x in range(dim):
  100.             for y in range(dim):
  101.                 # pomocnicze indeksy
  102.                 mx = x - 1
  103.                 if mx < 0:
  104.                     mx = dim - 1
  105.                 my = y - 1
  106.                 if my < 0:
  107.                     my = dim - 1
  108.                 gx = (x + 1) % dim
  109.                 gy = (y + 1) % dim
  110.  
  111.                 self.add_edge_directed((x, y), (mx, my))
  112.                 self.add_edge_directed((x, y), (mx, y))
  113.                 self.add_edge_directed((x, y), (mx, gy))
  114.                 self.add_edge_directed((x, y), (x, my))
  115.                 self.add_edge_directed((x, y), (x, gy))
  116.                 self.add_edge_directed((x, y), (gx, my))
  117.                 self.add_edge_directed((x, y), (gx, y))
  118.                 self.add_edge_directed((x, y), (gx, gy))
  119.  
  120.     def generate_vertices(self, dim, probability):
  121.         for i in range(dim):
  122.             for j in range(dim):
  123.                 self.add_vertex(Vertex((i, j), random.random() < probability))
  124.  
  125.     def vertices_in_strongly_connected_component(self):
  126.         return len(max(nx.strongly_connected_components(nx.DiGraph(self.active_connections())), key=len))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top