def main(): #define two players, chose gameType as the following: 1-implementation 1 (theoretical) ; 2 - implementation 2(practically form 1); 3 - implementation 3(practically form 2) - recommended player1 = Player(10001, 25, 2, 1700.00, 1, game_Type=3) player2 = Player(10001, 16, 17, 2500.00, 2, game_Type=3) #set links between players. (They have to communicate one with each other) player1.setNeighborPlayer(player2) player2.setNeighborPlayer(player1) #update gains #player1.measureGains() #player2.measureGains() player1.setDirectGain(GainCalculations.getAverageGain(player1.coordinator_id, player1.tx_node.node_id, player1.rx_node.node_id, year=2013, month=8, day=23)) player1.setCrossGain(GainCalculations.getAverageGain(player1.coordinator_id, player2.tx_node.node_id, player1.rx_node.node_id, year=2013, month=8, day=23)) player2.setDirectGain(GainCalculations.getAverageGain(player2.coordinator_id, player2.tx_node.node_id, player2.rx_node.node_id, year=2013, month=8, day=23)) player2.setCrossGain(GainCalculations.getAverageGain(player2.coordinator_id, player1.tx_node.node_id, player2.rx_node.node_id, year=2013, month=8, day=23)) #if you want to see some info about players player1.printPlayerInfo() player2.printPlayerInfo() #check convergence. See if the topology suits the game conditions if not checkConvergence(player1.direct_gain, player2.cross_gain, player1.cross_gain, player2.direct_gain): return livePlot = gameLivePlot(player1, player2) #start live plotting livePlot.startPloting() #start Power Allocation threads player1.startGame() player2.startGame() time.sleep(3) #trigger an event by changing the power of one player player1.unbalance() #wait for the threads to finish while player1.powerAllocation.isAlive() or player2.powerAllocation.isAlive() or livePlot.is_alive(): time.sleep(3) #if you want to plot the results iterations.plotIterations(player1.player_number, player1.tx_node.node_id, player1.rx_node.node_id, player2.player_number, player2.tx_node.node_id, player2.rx_node.node_id, int(player1.cost), int(player2.cost), TruncatedValues=False, saveImage=True) choice = raw_input("Do you want to start PlayerApp threads(sends packets continuously) - yes/no:") if choice.lower() == "yes": player1.playerApp.start() player2.playerApp.start()
def bestResponseAsAfunctionOfCostAndPrx(): #plot best response variation as function of I+N and cost player = Player(10001, 25, 2, 1000.00, player_number=1, game_Type=0) #give the node id which cause interference to player tx2_node_id = 16 #I want to determine the maximum level of interference #average direct gain hii = GainCalculations.getAverageGain(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23) #maximum cross gain hji = GainCalculations.getMinMaxGain(10001, tx2_node_id, player.rx_node.node_id, year=2013, month=8, day=24) #get average noise noise = GainCalculations.getAverageNoise(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23) max_interference_and_noise = 0.001*hji[1] + noise max_interference_and_noise = 10.00*math.log10(max_interference_and_noise/0.001) interference_and_noise = numpy.arange(max_interference_and_noise, max_interference_and_noise-10, -2) cost = numpy.arange(100, 10000, 0.1) #now plot results plot.ioff() plot.clf() plot.grid() plot.title("B%d (c%d, I+N)" %(player.player_number, player.player_number)) plot.xlabel("c%d" %(player.player_number)) plot.ylabel("B%d [dBm]" %(player.player_number)) for i in interference_and_noise: tmp_list = [] tmp_cost = [] for c in cost: tmp_bi = getBi(c, math.pow(10.00, i/10.00)*0.001, 10.00*math.log10(hii)) if tmp_bi!=None: tmp_list.append(tmp_bi) tmp_cost.append(c) plot.plot(tmp_cost, tmp_list, label = "I+N=%.1f dBm" %i) plot.plot([],[],label = "h%d%d = %.3f dB" %(player.player_number, player.player_number, 10.00*math.log10(hii))) plot.axhspan(-55, 0, alpha = 0.1) plot.legend(bbox_to_anchor=(1.05, 1.05)) plot.show() #bestResponseAsAfunctionOfCostAndPrx() #bestResponseAsAFunctionOfDirectGain() #bestResponseAsAFunctionOfRx()
def getInstantNoise(coordinator_id, node_id ,freq=2420e6): #return current noise measured at the specified node #define a Node object rx_node = Node(coordinator_id, node_id) #for how much time we will measure the spectrum sensing_duration = 2 try: #configure node rx_node.setSenseConfiguration(freq, freq, 400e3) #start sensing rx_node.senseStart(time.time()+1, sensing_duration, 5) except Exception: print "Error at sensing start" return #now we have the measured results in a file, we have to read the file and do the average #data returned has the following structure: [[frequency], [average power in Watts for that frequency]]. Normally there is only one frequency noise_power = GainCalculations.getAverageDataMeasurementsFromFile(coordinator_id ,rx_node.node_id)[1][0] print "Noise power %.6fE-12" %(noise_power * 1e12) return noise_power #import noise #powerNoise = getInstanceNoise(9451, 56) #getInstantNoise()
def getAFewParameters(): #From the measured gain, you can get a few results #If you just want to get a list with measured gains: print GainCalculations.getFileResults(10001, 25, 2) #You will find informations about: calculated gain, received RSSI, Noise power, transmitted power, date #if you just want the average gain: print GainCalculations.getAverageGain(10001, 25, 2) print "%.3f dB" %(10.00*math.log10(GainCalculations.getAverageGain(10001, 25, 2))) #if you just want to find info about the noise power GainCalculations.getAverageNoise(10001, 25, 2) GainCalculations.getMinMaxNoise(10001, 25, 2)
def performGainMeasurement(): #You can do a gain measurement at any time you want. You can save the results in a file if you want to check them later #You have to specify the coordinator_id , transmitter's node id, receiver's node id #Optional you can specify at which frequency to measure(2420Mhz default) , to or not to save the results, and how long should the generator transmit a signal (recommended to be at least 5 seconds, default value=10) gain = GainCalculations.calculateInstantGain(10001, 25, 2, measuring_freq=2422e6, saveresults=False, transmitting_duration=5) #returned gain is in linear scale. print gain print "%.3f dB" %(10.00*math.log10(gain))
def measureGains(self): # takes instant channel gains, it does not apply any filter or anything else # measure direct gain self.direct_gain = GainCalculations.calculateInstantGain( self.coordinator_id, self.tx_node.node_id, self.rx_node.node_id, measuring_freq=2420e6, saveresults=False, transmitting_duration=4, ) # measure cross gain self.cross_gain = GainCalculations.calculateInstantGain( self.coordinator_id, self.neighbor_player.tx_node.node_id, self.rx_node.node_id, measuring_freq=2420e6, saveresults=False, transmitting_duration=6, )
def bestResponseAsAFunctionOfRx(): player = Player(10001, 25, 2, 1000.00, 1) #get gain measurements until 23 august player.setDirectGain(GainCalculations.getAverageGain(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23)) #find max Prx_dBm in which Bi > -55 dBm Prx_dBm = -100 while True: tmp_bi = getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, 10.00*math.log10(player.direct_gain)) if tmp_bi is None or tmp_bi<-55: Prx_dBm-=0.001 break Prx_dBm+=0.0001 #now plot results plot.ioff() plot.clf() plot.grid() plot.title("B%d=f(I+N)" %(player.player_number)) plot.xlabel("I+N [dBm]") plot.ylabel("B%d [dBm]" %(player.player_number)) Prx_crt = Prx_dBm Prx_inf = Prx_dBm-20 Prx_step = 0.001 Bi = [] Prx = [] while Prx_crt>=Prx_inf: best_response = getBi(player.cost, math.pow(10.00, Prx_crt/10.00)*0.001, 10.00*math.log10(player.direct_gain)) Bi.append(best_response) Prx.append(Prx_crt) Prx_crt-=Prx_step plot.plot(Prx, Bi, label = "h%d%d=%.1f dBm" %(player.player_number,player.player_number,10.00*math.log10(player.direct_gain)), linewidth = 2) plot.axis([min(Prx)-1, max(Prx)+1, min(Bi)-1, max(Bi)+1]) leg = plot.legend(loc = 'upper right', fontsize = 18) leg.get_frame().set_alpha(0.6) plot.show()
def plotSavedGains(): #If you made several gain measurements, you can plot them #You can apply a filter and take into account only gains before the date mentioned. If no date is mentioned, then all saved measurements will be plotted GainCalculations.plotGains(10001, 25, 2)
def bestResponseAsAFunctionOfDirectGain(): #formula to test: Bi = (1/c) - (Prx/hii), where c is constant, Prx= ct #define a player player = Player(10001, 25, 2, 1000.00, 1) #player = Player(10001, 16, 17, 1000.00, 2) #get min and max channel gains measured until 23 august tmp = GainCalculations.getMinMaxGain(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23) #tmp = [min linear gain, maximum linear gain] min_hii = 10.00*math.log10(tmp[0]) max_hii = 10.00*math.log10(tmp[1]) #define a hii array hii = numpy.arange(min_hii, max_hii, 0.05) #find max Prx_dBm in which Bi > -55 dBm Prx_dBm = -100 while True: tmp_bi = getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, min_hii) if tmp_bi is None or tmp_bi<-55: #return to the previous Prx_dBm Prx_dBm-=0.0001 break Prx_dBm+=0.0001 average_gain = GainCalculations.getAverageGain(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23) standard_deviation = GainCalculations.getStandardDeviation(player.coordinator_id, player.tx_node.node_id, player.rx_node.node_id, year=2013, month=8, day=23) #now plot results plot.ioff() plot.clf() plot.grid() plot.title("B%d=f(h%d%d, I+N)" %(player.player_number, player.player_number, player.player_number)) plot.xlabel("h%d%d [dB]" %(player.player_number, player.player_number)) plot.ylabel("B%d [dBm]" %(player.player_number)) #change rc param plot.rcParams.update({'font.size': 20}) #I just want to add a text to legend plot.plot([0], [0], alpha = 0, label = "c%d=%d" %(player.player_number, player.cost)) #now plot bi Prx_crt = Prx_dBm Prx_step = 3 Prx_inf = Prx_crt - 10 max_bi = -float("inf") min_bi = float("inf") markers = [".", "*", "+", "h", "x", "_"] marker_index = 0 while Prx_crt >= Prx_inf: Bi = [] for i in hii: best_response = getBi(player.cost, math.pow(10.00, Prx_crt/10.00)*0.001, i) Bi.append(best_response) if best_response > max_bi: max_bi = best_response if best_response < min_bi: min_bi = best_response plot.plot(hii, Bi, linewidth = 2.5, label = "Prx %.2f dBm" %(Prx_crt), marker = markers[marker_index], markersize = 5) if marker_index<len(markers): marker_index+=1 else: marker_index = 0 Prx_crt-=Prx_step #set axis limits plot.axis([min(hii)-0.5, max(hii)+0.5, min_bi-2, max_bi+2]) #set ticks plot.xticks(numpy.arange(min(hii),max(hii), 2)) plot.yticks(numpy.arange(min_bi, max_bi, 2)) #plot a vertical line with the average gain plot.vlines(10.00*math.log10(average_gain), plot.axis()[2], plot.axis()[3], "red", label = "Mean gain=%.2f dB" %(10.00*math.log10(average_gain)), linestyle = "--", linewidth = 2) #plot vertical lines with the +- standard deviation max_std_hii = 10.00*math.log10(average_gain+standard_deviation) min_std_hii = 10.00*math.log10(average_gain-standard_deviation) #plot.vlines(min_std_hii, plot.axis()[2], plot.axis()[3], "black", linestyle = "--", linewidth = 1) #plot.vlines(max_std_hii, plot.axis()[2], plot.axis()[3], "black", linestyle = "--", linewidth = 1) #plot some spans for +- standard deviation plot.axvspan(min_std_hii, max_std_hii, facecolor = "gray", alpha = 0.2) #Plot horizontal arrows #plot arrows for +- standard deviation arrow_length = math.fabs(max_std_hii - min_std_hii) plot.arrow(min_std_hii, min_bi + 1, arrow_length, 0, head_width = 0.02*(math.fabs(max(hii)-min(hii))), length_includes_head = True, color = "green",head_length = 0.02*(math.fabs(max(hii)-min(hii))), linewidth = 1.0) plot.arrow(max_std_hii, min_bi + 1, -arrow_length, 0, head_width = 0.02*(math.fabs(max(hii)-min(hii))), length_includes_head = True, color = "green",head_length = 0.02*(math.fabs(max(hii)-min(hii))), linewidth = 1.0) plot.text(min_std_hii+arrow_length/3, min_bi +1.1, "%.3f dB" %arrow_length, fontsize = 18, color = "green") #plot horizontal lines for best response variation #plot.hlines(getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, max_std_hii), min(hii), max(hii), color = "black", linestyle = "--", linewidth = 1) #plot.hlines(getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, min_std_hii), min(hii), max(hii), color = "black", linestyle = "--", linewidth = 1) #plot.axhspan(getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, min_std_hii), getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, max_std_hii), facecolor = "blue", alpha = 0.1) #plot arrows for best response variation #max_std_Bi = getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, max_std_hii) #min_std_Bi = getBi(player.cost, math.pow(10.00, Prx_dBm/10.00)*0.001, min_std_hii) #arrow_length = math.fabs(max_std_Bi - min_std_Bi) #plot.arrow(min(hii) +0.5, max_std_Bi, 0, -arrow_length, head_width = 0.01*(math.fabs(max(hii)-min(hii))), length_includes_head = True, color = "blue", head_length = 0.01*(math.fabs(max(hii)-min(hii))), linewidth = 1.3) #plot.arrow(min(hii) +0.5, min_std_Bi, 0, +arrow_length, head_width = 0.01*(math.fabs(max(hii)-min(hii))), length_includes_head = True, color = "blue", head_length = 0.01*(math.fabs(max(hii)-min(hii))), linewidth = 1.3) #plot.text(min(hii) +0.5, max_std_Bi, "%.3f dBm" %arrow_length, fontsize = 18, weight = 500, color = "blue") leg = plot.legend(loc = "center right", fontsize = 18, bbox_to_anchor=(1, 0.5)) leg.get_frame().set_alpha(0.6) #maximize the window mng = plot.get_current_fig_manager() mng.resize(*mng.window.maxsize()) fig = plot.gcf() fig.set_size_inches( (19, 11) ) #plot.savefig("/home/ciprian/Pictures/best response/%s%d.jpg" %("bi_f(hii)_player", player.player_number), dpi=250) plot.show()
def main(): # You can play a game with two players: one players is represented by a node pair consisting of a transmitter and receiver # You have to specify both players' coordinator_id , transmitter's node id, receiver's node id # Optional you can specify at which frequency to measure(2420Mhz default) , to or not to save the results, and how long should the generator transmit a signal (recommended to be at least 5 seconds, default value=10) listIndex = [] listpTransmitted1 = [] listpTransmitted2 = [] # VESNA power generating list. This must be sorted. Powers are in dBm available_generating_powers = [ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -30, ] # PLAYER 1 Transmitter1 = 51 Receiver1 = 52 # PLAYER 2 Transmitter2 = 54 Receiver2 = 58 # Desired increase of the players' lower SINR # IncreaseOfSINR=2 # IncreaseOfSINR=1.1 IncreaseOfSINR = 1.1 # IncreaseOfSINR=4 # for demonstrating that it is possible po increase the SINR for higher factors # IncreaseOfSINR=2.5 # TYPE OF USE: when real transmission power levels of VESNA are used or not # 1 - measuring gains only once at the beginning and setting transmission power at the end according to "available_generating_powers" # 2 - gains are continuously measured and discrete values for p1 and p2 set during the game. NOTICE: only used, when the gains are measured in real time, and not in advance (or set manually) TypeOfUse = 1 # now not in use # TRANSMISSION POWER for gains calculation pTransmittedGainCalcdBm = -15 # pTransmittedGainCalcdBm=0 pTransmitGainCalculation1dBm = pTransmittedGainCalcdBm pTransmitGainCalculation2dBm = pTransmittedGainCalcdBm # REQUIRED UTILITY TO END ITERATION # TargetUtility=-1.0e-020 TargetUtility = -1.0e-013 # TargetUtility=-1.0e-012 # SAVE RESULTS saveresults = True # saveresults= False # INITIAL TRANSMISSION POWER (reasonable to be set at the same level as for gains calculation) pTransmitteddBm = pTransmittedGainCalcdBm # pTransmitteddBm=-30 pTransmitted1dBm = pTransmitteddBm pTransmitted2dBm = pTransmitteddBm # Maximum number of iterations in the game to prevent that, in the case that the game does not converge, it is not played infinite time # MaxNrOfIterations=100 MaxNrOfIterations = 1000 # MaxNrOfIterations=5 # Counting the number of iterations during the game index = 1 listIndex.append(index) listpTransmitted1.append(pTransmitted1dBm) listpTransmitted2.append(pTransmitted2dBm) # EXPECTED NOISE # Noise1=3.38672324669e-12 # Noise1=2.2512786538e-10 # to demonstrate the increase with IncreaseOfSINR=4 # Noise2=3.35732111544e-12 # Noise2=1.37506518321e-11 # to demonstrate the increase with IncreaseOfSINR=4 # MEASURING NOISE IN THE OFFICE Noise1 = Noise.getInstantNoise(9501, Receiver1) Noise2 = Noise.getInstantNoise(9501, Receiver2) # MEASURING GAINS IN THE JSI OFFICE # h11 h11 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver1, pTransmitGainCalculation1dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h11 = GainCalculations.calculateInstantGainForSINR(9501, 51, 52, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h11 =0.000457079604928 # to demonstrate the increase with IncreaseOfSINR=4 # h11 =0.000269415326193 # h11 =1.17914835642e-06 # for testing the required convergence condition # h21 h21 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver1, pTransmitGainCalculation2dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h21 = GainCalculations.calculateInstantGainForSINR(9501, 54, 52, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h21 =1.17914835642e-06 # to demonstrate the increase with IncreaseOfSINR=4 # h21 =1.4646903564e-05 # h21 =0.000457079604928 # for testing the required convergence condition # h22 h22 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver2, pTransmitGainCalculation2dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h22 = GainCalculations.calculateInstantGainForSINR(9501, 54, 58, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h22 =1.30015521864e-04 # to demonstrate the increase with IncreaseOfSINR=4 # h22 =4.83003296862e-06 # h22 =2.91873033877e-06 # for testing the required convergence condition # h12 h12 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver2, pTransmitGainCalculation1dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h12 = GainCalculations.calculateInstantGainForSINR(9501, 51, 58, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h12 =2.91873033877e-06 # to demonstrate the increase with IncreaseOfSINR=4 # h12 =5.08980966629e-07 # h12 =1.30015521864e-04 # for testing the required convergence condition print Noise1 print "Noise1: %.3f dBm" % (10.00 * math.log10(Noise1 / 0.001)) print Noise2 print "Noise2: %.3f dBm" % (10.00 * math.log10(Noise2 / 0.001)) # returned gain is in linear scale. print h11 print "h11: %.3f dB" % (10.00 * math.log10(h11)) print h21 print "h21: %.3f dB" % (10.00 * math.log10(h21)) print h22 print "h22: %.3f dB" % (10.00 * math.log10(h22)) print h12 print "h12: %.3f dB" % (10.00 * math.log10(h12)) # Checking if received signals are higher than interference if not ((h11 > h21) and (h22 > h12)): print "THE REQUIRED CONVERGENCE CONDITION IS NOT SATISFIED" print "The game is not played as it can not converge." return # Transmission powers of both players pTransmitted1 = math.pow(10, pTransmitted1dBm / 10.00) * 0.001 pTransmitted2 = math.pow(10, pTransmitted2dBm / 10.00) * 0.001 # Signal-to-noise ratio for both players SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) # Interference for both players I1 = pTransmitted2 * h21 I2 = pTransmitted1 * h12 # Defininf the rule of the game: we want to increas the SINR of the player with the lower SINR by factor of parameter TargetUtility if SINR1 > SINR2: SINR2Required = SINR2 * IncreaseOfSINR SINR1Required = SINR1 if SINR2 >= SINR1: SINR1Required = SINR1 * IncreaseOfSINR SINR2Required = SINR2 print SINR1Required print "SINR1Required: %.3f dB" % (10.00 * math.log10(SINR1Required)) print SINR2Required print "SINR2Required: %.3f dB" % (10.00 * math.log10(SINR2Required)) # Required received powers for desired SINRs for both players pReceivedrequired1 = (I1 + Noise1) * SINR1Required pReceivedrequired2 = (I2 + Noise2) * SINR2Required # Required transmission powers for desired SINRs for both players pTransmittedrequired1 = pReceivedrequired1 / h11 pTransmittedrequired2 = pReceivedrequired2 / h22 print pTransmittedrequired1 print "pTransmittedrequired1: %.3f dBm" % (10.00 * math.log10(pTransmittedrequired1 / 0.001)) print pTransmittedrequired2 print "pTransmittedrequired2: %.3f dBm" % (10.00 * math.log10(pTransmittedrequired2 / 0.001)) # Utilities of both players Utility1 = -math.pow((pTransmittedrequired1 - pTransmitted1), 2) Utility2 = -math.pow((pTransmittedrequired2 - pTransmitted2), 2) print Utility1 print "Utility1: %.f" % (Utility1) print Utility2 print "Utility2: %.f" % (Utility2) index = index + 1 # For TypeOfUse is equal 2 if TypeOfUse == 2: if saveresults: results_list = [ pTransmitGainCalculation1dBm, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmitGainCalculation2dBm, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired1: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm1 = nearest_power pTransmittedrequired1 = math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired1: %.3f W" % (pTransmittedrequired1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired2: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm2 = nearest_power pTransmittedrequired2 == math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired2: %.3f W" % (pTransmittedrequired2) listIndex.append(index) listpTransmitted1.append(pTransmittedGainCalcdBm1) listpTransmitted2.append(pTransmittedGainCalcdBm2) pTransmitted1 = pTransmittedrequired1 pTransmitted2 = pTransmittedrequired2 # For TypeOfUse is equal 1 if TypeOfUse == 1: listIndex.append(index) listpTransmitted1.append(10.00 * math.log10(pTransmitted1 / 0.001)) listpTransmitted2.append(10.00 * math.log10(pTransmitted2 / 0.001)) # Iterations in while loop to set the transmission powers according to the desired SINRs # The procedure in the while loop is similar as at the first setting of transmission powers according to the desired SINRS (see above) while Utility1 < TargetUtility or Utility2 < TargetUtility: # For TypeOfUse is equal 2, we continuously measure gains if TypeOfUse == 2: h11 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver1, pTransmittedGainCalcdBm1, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h21 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver1, pTransmittedGainCalcdBm2, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h22 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver2, pTransmittedGainCalcdBm2, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h12 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver2, pTransmittedGainCalcdBm1, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) if not ((h11 > h21) and (h22 > h12)): print "THE REQUIRED CONVERGENCE CONDITION IS NOT SATISFIED" print "The game is stopped as it can not converge." return SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) I1 = pTransmitted2 * h21 I2 = pTransmitted1 * h12 pReceivedrequired1 = (I1 + Noise1) * SINR1Required pReceivedrequired2 = (I2 + Noise2) * SINR2Required pTransmittedrequired1 = pReceivedrequired1 / h11 pTransmittedrequired2 = pReceivedrequired2 / h22 Utility1 = -math.pow((pTransmittedrequired1 - pTransmitted1), 2) Utility2 = -math.pow((pTransmittedrequired2 - pTransmitted2), 2) index = index + 1 if index >= MaxNrOfIterations: Utility1 = 0 Utility2 = 0 if TypeOfUse == 2: if saveresults: results_list = [ pTransmittedGainCalcdBm1, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmittedGainCalcdBm2, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired1: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm1 = nearest_power pTransmittedrequired1 = math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired1: %.3f W" % (pTransmittedrequired1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired2: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm2 = nearest_power pTransmittedrequired2 == math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired2: %.3f W" % (pTransmittedrequired2) listIndex.append(index) listpTransmitted1.append(pTransmittedGainCalcdBm1) listpTransmitted2.append(pTransmittedGainCalcdBm2) pTransmitted1 = pTransmittedrequired1 pTransmitted2 = pTransmittedrequired2 if TypeOfUse == 1: listIndex.append(index) listpTransmitted1.append(10.00 * math.log10(pTransmitted1 / 0.001)) listpTransmitted2.append(10.00 * math.log10(pTransmitted2 / 0.001)) # END OF THE GAME # Displaying some parameters at the end of the game if TypeOfUse == 2: if saveresults: results_list = [ pTransmittedGainCalcdBm1, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmittedGainCalcdBm2, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) print "listIndex:" print listIndex print "listpTransmitted1:" print listpTransmitted1 print "listpTransmitted2:" print listpTransmitted2 print Utility1 print "Utility1: %.f" % (Utility1) print Utility2 print "Utility2: %.f" % (Utility2) print pTransmitted1 print "pTransmitted1: %.3f dBm" % (10.00 * math.log10(pTransmitted1 / 0.001)) print pTransmitted2 print "pTransmitted2: %.3f dBm" % (10.00 * math.log10(pTransmitted2 / 0.001)) SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) print "NrOfIterations: %.f" % (index) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if math.fabs(10.00 * math.log10(pTransmitted1 / 0.001) - available_generating_powers[i]) < min_diferrence: min_diferrence = math.fabs(10.00 * math.log10(pTransmitted1 / 0.001) - available_generating_powers[i]) nearest_power = available_generating_powers[i] pTransmitteddBm1 = nearest_power print pTransmitteddBm1 print "pTransmitted1: %.3f dBm" % (pTransmitteddBm1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if math.fabs(10.00 * math.log10(pTransmitted2 / 0.001) - available_generating_powers[i]) < min_diferrence: min_diferrence = math.fabs(10.00 * math.log10(pTransmitted2 / 0.001) - available_generating_powers[i]) nearest_power = available_generating_powers[i] pTransmitteddBm2 = nearest_power print pTransmitteddBm2 print "pTransmitted2: %.3f dBm" % (pTransmitteddBm2) pTransmitted1 = math.pow(10, pTransmitteddBm1 / 10.00) * 0.001 pTransmitted2 = math.pow(10, pTransmitteddBm2 / 10.00) * 0.001 SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) # Plotting results of the game (attained through iterations) min_y_list = min(available_generating_powers) max_y_list = max(available_generating_powers) plot.figure(1) plot.grid() plot.title("Player 1") plot.plot(listIndex, listpTransmitted1) plot.axis([0, len(listpTransmitted1), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.figure(2) plot.grid() plot.title("Player 2") plot.plot(listIndex, listpTransmitted2) plot.axis([0, len(listpTransmitted2), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.figure(3) plot.grid() plot.title("Player 1 and Player 2") plot.plot(listIndex, listpTransmitted1) plot.plot(listIndex, listpTransmitted2) plot.axis([0, len(listpTransmitted2), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.show()