def test_execute(self):
     n = 50
     adjacencyMatrix = getPlanarGraph(n)
     
     network = Network(adjacencyMatrix)
     
     # sprawdzenie metody
     alg = PlanarMIS(network)
     alg.execute()
     
     # sprawdzanie:
     # standardowo jak w MIS
     for node in network.ndList:
         if node.memory['is_in_final_MIS'] == True:
             for neigh in node.neighbors:
                 self.failIf(network.ndList[neigh].memory['is_in_final_MIS'] == True)
                 
         # jesli wezel jest zdominowany, to fail jesli ma w swoim otoczeniu same zdominowane wezly
         elif node.memory['is_in_final_MIS'] == False:
             neigh_states = [network.ndList[neigh].memory['is_in_final_MIS'] for neigh in node.neighbors]
             
             self.failIf(all([state == False for state in neigh_states]))
         # jesli wezel nie jest ani dominatorem ani zdominownay, to fail
         else:
             self.fail("nieprawidlowy stan wezla")
 def testAllInSubgraphs(self):
     # przygotowanie kliki K10
     adjacencyMatrix = [[1 for i in range(10)] for j in range(10)]
     
     for i in range(10):
         adjacencyMatrix[i][i]= 0
     
     network = Network(adjacencyMatrix)
     
     # sprawdzenie metody
     alg = PlanarMIS(network)
     
     for node in network.ndList:
         node.memory['neighbors_cp'] = [index for index in node.neighbors]
         
     result = alg.allInSubgraphs(network.ndList)
     
     self.failUnless(result == False)
     
     for node in network.ndList:
         node.memory['tree_num'] = 0
         
     result = alg.allInSubgraphs(network.ndList)
     
     self.failUnless(result == True)
    def test_executeWithoutJoiningSubgraphs(self):
        n = 10
        adjacencyMatrix = getPlanarGraph(n)
        
        network = Network(adjacencyMatrix)
        
        # sprawdzenie metody
        alg = PlanarMIS(network)
        alg._executeWithoutJoiningSubgraphs()
        
        # sprawdzanie:
        # po kolei dla kazdego poddrzewa
        # poddrzew bedzie mniej niz n/2
        for i in range(n/2):
            for node in network.ndList:
                
                
                # fail jesli wezel nie ma nadal przydzielonego poddrzewa
                self.failIf(not node.memory.has_key('tree_num'))
                
                # wybieranie tylko wezlow z podgrafu nr i
                if node.memory['tree_num'] != i:
                    continue
                
                # fail jesli sasiedzi wezla sa z innego poddrzewa
                neighbors = node.memory['neighbors_cp']
                
                for neigh in neighbors:
                    sub = network.ndList[neigh].memory['tree_num']
                    
                    if sub != i:
                        print 'wezel: ', node.ID, 'sasiad:', neigh
                        self.drawGraph(network.ndList, adjacencyMatrix)
                        self.fail('podgraf mial byc nr ' + str(i)+ ' a jest '+ str(sub))
                    
#                     self.failIf(sub != i)
                    
                # jesli wezel jest dominatorem, to fail jesli jego sasiad tez jest
                if node.memory['state_subgraph'] == 'dominator':
                    for neigh in neighbors:
                        self.failIf(network.ndList[neigh].memory['state_subgraph'] == 'dominator')
                        
                # jesli wezel jest zdominowany, to fail jesli ma w swoim otoczeniu same zdominowane wezly
                elif node.memory['state_subgraph'] == 'dominated':
                    neigh_states = [network.ndList[neigh].memory['state_subgraph'] for neigh in neighbors]
                    
                    self.failIf(all([state == 'dominated' for state in neigh_states]))
                # jesli wezel nie jest ani dominatorem ani zdominownay, to fail
                else:
                    self.fail("nieprawidlowy stan wezla:", node.memory['state_subgraph'])
 def testDivideIntoSubgraphs(self):
     # 1 - tworzenie macierzy
     adjacencyMatrix = [[0, 1, 1, 0, 1, 1, 1, 1],
                        [1, 0, 1, 0, 1, 1, 1, 0],
                        [1, 1, 0, 1, 1, 1, 1, 1],
                        [0, 0, 1, 0, 1, 0, 0, 0],
                        [1, 1, 1, 1, 0, 1, 1, 1],
                        [1, 1, 1, 0, 1, 0, 0, 0],
                        [1, 1, 1, 0, 1, 0, 0, 0],
                        [1, 0, 1, 0, 1, 0, 0, 0]]
     
     network = Network(adjacencyMatrix)
     
     # sprawdzenie metody
     alg = PlanarMIS(network)
     
     
     alg.divideIntoSubgraphs()
     
     
     # wezly z podgrafu i musza miec jako sasiadow tylko inne wezly z podgrafu i - albo zadnych
     for node in network.ndList:
         if node.memory.has_key('tree_num'):
             i = node.memory['tree_num']
             neigh_cp = node.memory['neighbors_cp']
             
             for neigh in neigh_cp:
                 if not network.ndList[neigh].memory.has_key('tree_num'):
                     self.fail("sasiad nie jest w tym zadnym podgrafie")
                 else:
                     self.failIf(network.ndList[neigh].memory['tree_num'] != i)
                     
         else:
             # wszystkie wezly musza byc w jakims podgrafie
                 self.fail("sasiad nie jest w zadnym podgrafie")
                 
     # sprawdzanie ilosci sasiadow
     for node in network.ndList:
             neigh_len = len(node.memory['neighbors_cp'])
             self.failIf(neigh_len > 6)
             
     # wypisywanie pogrfaow
     for i in range(5):
         print 'wezly z podgrafu', i, ':'
         for node in network.ndList:
             if node.memory['tree_num'] == i:
                 print node.ID, 
                 
         print ' '
def compute():

    MIN = 50
    MAX = 5000
    step = 500

    repetitions = 100

    adjMatrix = []
    alg = None
    network = None

    tempCommSum = 0
    commTimes = []

    tempAlgSum = 0
    algTimes = []

    for n in range(MIN, MAX, step):
        tempCommSum = 0
        tempAlgSum = 0

        for k in range(repetitions):
            adjMatrix = getPlanarGraph(n)
            network = Network(adjMatrix)

            alg = PlanarMIS(network)

            alg.execute()

            print k
            tempCommSum += network.algCommRoundCounter
            tempAlgSum += alg.roundCounter

        print n, "time: ", datetime.now()

        commTimes.append(tempCommSum / float(repetitions))
        algTimes.append(tempAlgSum / float(repetitions))

    # print times

    fileName = "/home/julka/100_reps/modified_log_star_MIS_planar_graph_comm.txt"
    save(commTimes, fileName)

    fileName = "/home/julka/100_reps/modified_log_star_MIS_planar_graph_alg.txt"
    save(algTimes, fileName)
 def testCreateSubgraph(self):
     # 1 - tworzenie macierzy
     adjacencyMatrix = [[0, 1, 1, 0, 1, 1, 1, 1],
                        [1, 0, 1, 0, 1, 1, 1, 0],
                        [1, 1, 0, 1, 1, 1, 1, 1],
                        [0, 0, 1, 0, 1, 0, 0, 0],
                        [1, 1, 1, 1, 0, 1, 1, 1],
                        [1, 1, 1, 0, 1, 0, 0, 0],
                        [1, 1, 1, 0, 1, 0, 0, 0],
                        [1, 0, 1, 0, 1, 0, 0, 0]]
     
     network = Network(adjacencyMatrix)
     
     # sprawdzenie metody
     alg = PlanarMIS(network)
     
     for node in network.ndList:
         node.memory['neighbors_cp'] = [index for index in node.neighbors]
     
     alg.createSubgraph(network.ndList, 0)
     
     # wypisz wezly z podgrafu
     print 'wezly z podgrafu 0:'
     for node in network.ndList:
         if node.memory.has_key('tree_num'):
             print node.ID,
     print ' '
     
     # wezly z podgrafu 0 musza miec jako sasiadow tylko inne wezly z podgrafu 0 - albo zadnych
     for node in network.ndList:
         if node.memory.has_key('tree_num'):
             neigh_cp = node.memory['neighbors_cp']
             
             for neigh in neigh_cp:
                 if not network.ndList[neigh].memory.has_key('tree_num'):
                     self.fail("sasiad nie jest w tym samym podgrafie")
                     
         else:
             # wezly spoza podgrafu nie moga miec sasiadow w podgrafie
             neigh_cp = node.memory['neighbors_cp']
             
             for neigh in neigh_cp:
                 if network.ndList[neigh].memory.has_key('tree_num'):
                     self.fail("sasiad jest w podgrafie")



from algorithm.MIS_for_planar_graphs import PlanarMIS
from network.network import Network
from graph_generators.n_unit_disk_graph import getUnitDiskGraph

from graph_tool.all import *

adjacencyMatrix = getUnitDiskGraph(10)

# tworzenie sieci
network = Network(adjacencyMatrix)

alg = PlanarMIS()

network.algorithm = alg
alg.network = network

nodes = network.ndList

alg.divideIntoSubgraphs()

# czesc rysujaca
g = Graph(directed=False)

# dodawanie wezlow - indeksowanie bedzie sie zgadzalo
for node in nodes:
    g.add_vertex()