Example #1
0
 def testSpecificPermutation(self, nrPlayers, nodesIdList):
     """
     Check power allocation game convergence rule for specific setup.
     
     Keyword arguments:
     nrPlayers -- number of players
     nodesIdList -- list describing player setup, e.g. [1,2,3,4] 2 player game, player 1 tx node is 1, rx node is 2, 
                    player 2 tx node is 3, rx node is 4.
     """
     nodeObjects = [GameNode(self.coordinatorId, ids) for ids in nodesIdList]
     directGains = getDirectGainsNPl(self.coordinatorId, nodeObjects)
     # check if bad measurement, if so then stop - no reason to continue
     badMeasurements = False
     if directGains is None:
         badMeasurements = True
     else:
         for x in directGains:
             if x is None: badMeasurements = True
     if not badMeasurements:
         # wait for things to cool down
         time.sleep(10)
         if nrPlayers == 2:
             # reuse some code
             crossGains = getCrossGains2Pl(self.coordinatorId, nodeObjects)
         else:
             crossGains = getCrossGainsNPl(self.coordinatorId, nodeObjects)
         if crossGains is None: 
             badMeasurements = True
         else:
             for x in crossGains:
                 if x is None: badMeasurements = True
     
     if badMeasurements:
         print "BAD MEASUREMENT"
     else:
         print "\n"
         print "h_ii: %s" % (', '.join([str(x) for x in directGains]))
         print "h_ii: %s" % (', '.join([str(x) for x in crossGains]))
         if not checkConvergenceRuleForSearch(nrPlayers, directGains, crossGains):
             print "nodes:", nodesIdList, "DO NOT MEET PAPU convergence rule"
         else:
             print "nodes SATISFY convergence condition"
             stringToAppend = '\t'.join([str(x) for x in nodesIdList]) + '\t' + '\t'.join([str(x) for x in directGains])
             stringToAppend += '\t' + '\t'.join([str(x) for x in crossGains])
             print stringToAppend
Example #2
0
 def findNodes(self, nrPlayers, nrCases, startWithRowFromFile=2):
     """ Find candidate nodes, exhaustive search. Do real world measurements and check convergence rule.
     First create a list of node objects and pass elements of this list, identified by their id read 
     from file, to methods that compute instant and cross gain.
     
     
     nrPlayers -- number of players to check for
     nrCases -- stop the search when the number of cases sought is found, for 
                 a 5 player game there a re 3.6e6 possible combinations!!
     startWithRowFromFile -- if something goes wrong experiments can resume from 
                 a specific combination. Read line numbering starts from 1 not from 0!!
     """
     # check if we have enough nodes
     if self.nrNodes < 4:
         raise Exception("The node ID list must contain at least 4 nodes!! "
                         "A player is formed by a transmitter node and a receiver node.")
     
     # check if the number of players can be realized by the available nodes
     if self.nrPayrs < nrPlayers:
         raise Exception("The number of available nodes cannot cover the number "
                         "searched players. (nrAvailableNodes/2) should be > nrPlayers")
     
     totalExperiments = scipy.math.factorial(self.nrNodes) / scipy.math.factorial(self.nrNodes - nrPlayers * 2) - startWithRowFromFile
     question = "total number of possible experiments %d proceed? - yes/no:" % (totalExperiments)
     choice = raw_input(question)
     if choice.lower() == "no":
         print "finishing..."
         return
     else:
         print "ok let's proceed"
         # create a list of vesna nodes and pass these nodes to methods
         vesnaNodes = [GameNode(self.coordinatorId, ids) for ids in self.nodeIDsList]
         path = getNodeCombinations(self.coordinatorId, self.nodeIDsList, nrPlayers)
         print "read permutations from", path
         
         casesFound = 0
         while casesFound < nrCases:
             """Repeat experiments with different node permutations until I find nrCases that satisfy
             PAPU algorithm convergence condition.
             """   
             for i in range(startWithRowFromFile, totalExperiments):
                 # get line that contains node ids, first line is a header
                 line = linecache.getline(path, i)
                 # use ids like this: tx_1, rx_1, tx_2, rx_2...
                 nodesUsed = map(int, line.split())
                 print "nodes used in this experiment ", nodesUsed
                 nodeObjects = list()
                 # get object list of nodes used in this experiment
                 for j in nodesUsed:
                     nodeObjects.append(vesnaNodes[self.nodeIDsList.index(j)])
                 # compute direct gains for nodes used in experiment
                 directGains = getDirectGainsNPl(self.coordinatorId, nodeObjects)
                 if nrPlayers == 2:
                     # reuse some code
                     crossGains = getCrossGains2Pl(self.coordinatorId, nodeObjects)
                 else:
                     crossGains = getCrossGainsNPl(self.coordinatorId, nodeObjects)
                 # if lists contain None then a bad measurement has occur, i.e. noise > gain
                 badMeasurement = False
                 for x in directGains:
                     if x is None:
                         badMeasurement = True
                 for x in crossGains:
                     if x is None:
                         badMeasurement = True
                 if badMeasurement:
                     printBadResultToFile(self.coordinatorId, nrPlayers, self.nodeIDsList, nodesUsed, directGains, crossGains, badMeasurement)
                 else:
                     # check convergence condition
                     if not checkConvergenceRuleForSearch(nrPlayers, directGains, crossGains):
                         print "nodes:", nodesUsed, "do not satisfy PAPU convergence rule"
                         printBadResultToFile(self.coordinatorId, nrPlayers, self.nodeIDsList, nodesUsed, directGains, crossGains, badMeasurement)
                     else:
                         # save results to file
                         casesFound += 1
                         printPAPUtoFile(self.coordinatorId, nrPlayers, self.nodeIDsList, nodesUsed, directGains, crossGains)