コード例 #1
0
ファイル: gameExample.py プロジェクト: mrlini/logatec-games
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()
コード例 #2
0
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()