def calculate(self):
        
        sums = []
        
        length = len(self.__expectedUtility)
        for i in range(length):
            euSum = 0
            for j in range(length):
                if j != i:
                    euSum += self.__expectedUtility[i][j]
            
            sums.append(euSum)
        
        maxSum = max(sums)
        minSum = min(sums)
        
        riskVector = []
        for i in range(length):
            Ri = (2 * sums[i] - maxSum - minSum)/(maxSum - minSum)
            ri = (1 - Ri/3)/(1 + Ri/3)
            riskVector.append(ri)

        print ("==== risk vector =====")
        objectListPrint(riskVector)    
            
        return riskVector
Beispiel #2
0
    def makeOffers(self):
        
        length = len(self.__players)
        self.__deltaIJ = [[0 for x in range(length)] for y in range(length)]
        self.__offersIJ = [[0 for x in range(length)] for y in range(length)]
        
        for i, playerI in enumerate(self.__players):
            # calculate where i get offers from
            playerI.offers = []
            
        for i, playerI in enumerate(self.__players):
            # calculate where i get offers from
            for j, playerJ in enumerate(self.__players):
                if i != j and playerI.position != playerJ.position:
                    EIi = self.__expectedCalc.get_expected_utility_ij()[j][i]               #az i ilyennek látja saját győzelmének hasznosságát
                    EIj = self.__expectedCalc.get_expected_utility_ji()[j][i]               #i azt gondolja, hogy ha j győz, akkor ennek j ilyen hasznoságot tulajdonít

                    EJj = self.__expectedCalc.get_expected_utility_ij()[i][j]               #az j ilyennek látja saját győzelmének hasznosságát
                    EJi = self.__expectedCalc.get_expected_utility_ji()[i][j]               #j azt gondolja, hogy ha i győz, akkor ennek i ilyen hasznoságot tulajdonít


                    if EIi > EIj and EIi > 0 and EIj > 0       and EJj > EJi  and EJj > 0 and EJi > 0:                                             # Ha mind a két fél azt gondolja, hogy ő az erősebb, akkor confrontáció lesz:
                    #if EIi > EIj and EIi > 0 and EJj > EJi and EJj > 0 :   

                        if playerI.power() >= playerJ.power():                              # ha I erősebb, akkor az ő pozíciója változatlan marad
                            playerI.offers.append(Offer(playerJ, Offer.CONFRONTATION, playerI.position, EIi))           #playerI.offer = offer, amit player I kap, és amit player J-től kap

                            self.__offersIJ[i][j] = playerI.position
                            
                        elif playerI.power() < playerJ.power():                             # ha I gyengébbnek bizonyul, átveszi J pozícióját
                            playerI.offers.append(Offer(playerJ, Offer.CONFRONTATION, playerJ.position, EIi))

                            self.__offersIJ[i][j] = playerJ.position




                            
                    elif EIi > 0 and EIj < 0 and abs(EIi) > abs(EIj)      and EJj < 0 and EJi > 0 and abs(EJj) < abs(EJi) :
    
                        #xHat = playerJ.power() * ((playerI.position - playerJ.position)/(playerI.power() + playerJ.power()))           #szabi előtti változat

                        #if playerI.position > playerJ.position:
                        xHatI = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))
                        #xHatJ = (playerI.position - playerJ.position) * (abs(EJj) / (abs(EIi) + abs(EJj)))
                        self.__deltaIJ[i][j] = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))               # !! probálkozás az xHat matrix létrehozásársa

                            
                        playerI.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #player I kap kiegyezésre ajánlatot..

                        playerJ.offers.append(Offer(playerI, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #...ami player j-t, a kompromisszumos ajánlattevőt is köti

                        self.__offersIJ[i][j] = playerI.position - xHatI
                        self.__offersIJ[j][i] = playerI.position - xHatI

                    elif EIi > 0 and EIj < 0 and abs(EIi) > abs(EIj)      and EJj < 0 and EJi > 0 and abs(EJj) > abs(EJi) :
    
                        #xHat = playerJ.power() * ((playerI.position - playerJ.position)/(playerI.power() + playerJ.power()))           #szabi előtti változat

                        #if playerI.position > playerJ.position:
                        xHatI = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))
                        #xHatJ = (playerI.position - playerJ.position) * (abs(EJj) / (abs(EIi) + abs(EJj)))
                        self.__deltaIJ[i][j] = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))               # !! probálkozás az xHat matrix létrehozásársa

                            
                        playerI.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #player I kap kiegyezésre ajánlatot..

                        playerJ.offers.append(Offer(playerI, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #...ami player j-t, a kompromisszumos ajánlattevőt is köti

                        self.__offersIJ[i][j] = playerI.position - xHatI
                        self.__offersIJ[j][i] = playerI.position - xHatI    
                        
                    elif EIi > 0 and EIj < 0 and abs(EIi) < abs(EIj):                                                   #player I kapitulál  
                        playerI.offers.append(Offer(playerJ, Offer.CAPITULATION, playerJ.position, EIi))
                        self.__offersIJ[i][j] = playerJ.position                       


                    elif EIi > EIj  and EIi > 0 and EIj > 0     and EJj < EJi and EJj > 0:                                                                     # Ha I azt gondolja, hogy ő az erősebb, J pedig azt, hogy ő a gyengébb:
                        playerI.offers.append(Offer(playerJ, Offer.CONFRONT, playerJ.position, EIi))           # player J offert kap I-től I pozicióját vegye át
                        self.__offersIJ[i][j] = playerJ.position

                    #elif EIi < EIj and EJj > EJi and EIi > 0 and EIj < 0 and EJj > 0 and EJi > 0:                                                                     # Ha I azt gondolja, hogy ő az erősebb, J pedig azt, hogy ő a gyengébb:
                    #    playerI.offers.append(Offer(playerJ, Offer.CONFRONTATION, playerJ.position, EIi))           # player J offert kap I-től I pozicióját vegye át
                    #    self.__offersIJ[i][j] = playerJ.position                           

        for i, playerI in enumerate(self.__players):                        
            print("==== Offers for %s ====" % playerI.name)
            objectListPrint(playerI.offers)

        #print("==== xHat ====")                                                         # !! probálkozás az xHat matrix létrehozásársa
        #tablePrint(self.__deltaIJ)

        print("==== offers ====")                                                         # offer mátrix nyomtatási célból
        print('   ', end = ' ')
        for i, playerI in enumerate(self.__players):
            print('%-8s' % playerI.name, end = ' ')
        print('\n')
        tablePrint2(self.__offersIJ, self.__players)
        
        for player in self.__players:
            if len(player.offers) > 0:

                # Ha a max EU alapján akarjuk kiválasztani az offereket, ezt kell használni
                #max_util = max([offer.eu for offer in player.offers])                              
                #max_offers = [offer for offer in player.offers if offer.eu == max_util]
                #offer = min(max_offers, key=lambda x: abs(player.position - x.offered_position))

                offer = min(player.offers, key=lambda x: abs(player.position - x.offered_position))
                
                #bestOfferFunc = lambda offer : abs(offer.offered_position - player.position)
                #bestOffer = min(player.offers, key = bestOfferFunc)
                
                player.updatePosition(offer.offered_position)
Beispiel #3
0
    def makeOffers(self):
        
        length = len(self.__players)
        self.__deltaIJ = [[0 for x in range(length)] for y in range(length)]
        self.__offersIJ = [[0 for x in range(length)] for y in range(length)]
        
        for i, playerI in enumerate(self.__players):
            # calculate where i get offers from
            playerI.offers = []
            
        for i, playerI in enumerate(self.__players):
            # calculate where i get offers from
            for j, playerJ in enumerate(self.__players):
                if i != j and playerI.position != playerJ.position:
                    EIi = self.__expectedCalc.get_expected_utility_ij()[j][i]               #az i ilyennek látja saját győzelmének hasznosságát
                    EIj = self.__expectedCalc.get_expected_utility_ji()[j][i]               #i azt gondolja, hogy ha j győz, akkor ennek j ilyen hasznoságot tulajdonít

                    EJj = self.__expectedCalc.get_expected_utility_ij()[i][j]               #az j ilyennek látja saját győzelmének hasznosságát
                    EJi = self.__expectedCalc.get_expected_utility_ji()[i][j]               #j azt gondolja, hogy ha i győz, akkor ennek i ilyen hasznoságot tulajdonít


                    # 1.) CONFLICT                                                          # Ha mind a két fél azt gondolja, hogy ő az erősebb, akkor confrontáció lesz:
                    if EIi > EIj and EIi > 0 and EJj > EJi and EJj > 0:   

                        if playerI.power() >= playerJ.power():                              # ha I erősebb, akkor az ő pozíciója változatlan marad
                            playerI.offers.append(Offer(playerJ, Offer.CONFRONTATION, playerI.position, EIi))
                            self.__offersIJ[i][j] = playerI.position
                            
                        elif playerI.power() < playerJ.power():                             # ha I gyengébbnek bizonyul, átveszi J pozícióját
                            playerI.offers.append(Offer(playerJ, Offer.CONFRONTATION, playerJ.position, EIi))
                            self.__offersIJ[i][j] = playerJ.position

                    # 2.) COMPROMISE+        
                    elif EIi > EIj and EIi > 0 and EJj < EJi and EJj > 0:
    
                        xHatI = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))
                           
                        playerI.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #player I kap kiegyezésre ajánlatot...
                        playerJ.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #...ami player j-t, a kompromisszumos ajánlattevőt is köti

                        self.__offersIJ[i][j] = playerI.position - xHatI
                        self.__offersIJ[j][i] = playerI.position - xHatI

                    # 3.) CAPITULATE+   
                    elif EIi > EIj and EIi > 0 and EJj < 0:                                                  #player I kapitulációra kényszeríti J-t 
                        playerJ.offers.append(Offer(playerI, Offer.CAPITULATION, playerI.position, EIi))
                        self.__offersIJ[i][j] = playerJ.position                       

                    # 4.) COMPROMISE-
                    elif EIi < EIj and EIi > 0 and EJj > EJi and EJj > 0:                                                                    # Ha I azt gondolja, hogy ő az erősebb, J pedig azt, hogy ő a gyengébb:

                        xHatI = (playerI.position - playerJ.position) * (abs(EIi) / (abs(EIi) + abs(EJj)))
            
                        playerI.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #player I kap kiegyezésre ajánlatot...
                        playerJ.offers.append(Offer(playerJ, Offer.COMPROMISE, playerI.position - xHatI, EIi))           #...ami player j-t, a kompromisszumos ajánlattevőt is köti

                        self.__offersIJ[i][j] = playerI.position - xHatI
                        self.__offersIJ[j][i] = playerI.position - xHatI

                    # 7.) CAPITULATE-   
                    elif EIi < 0 and EJj > EJi and EJj > 0:                                                  #player J kapitulációra kényszeríti I-t 
                        playerI.offers.append(Offer(playerJ, Offer.CAPITULATION, playerJ.position, EIi))
                        self.__offersIJ[i][j] = playerJ.position 
                         
                        
            print("==== Offers for %s ====" % playerI.name)
            objectListPrint(playerI.offers)



        print("==== offers ====")                                                         # offer mátrix nyomtatási célból
        tablePrint(self.__offersIJ)
        
        for player in self.__players:
            if len(player.offers) > 0:

                # Ha a max EU alapján akarjuk kiválasztani az offereket, ezt kell használni
                #max_util = max([offer.eu for offer in player.offers])                              
                #max_offers = [offer for offer in player.offers if offer.eu == max_util]
                #offer = min(max_offers, key=lambda x: abs(player.position - x.offered_position))

                offer = min(player.offers, key=lambda x: abs(player.position - x.offered_position))
                
                #bestOfferFunc = lambda offer : abs(offer.offered_position - player.position)
                #bestOffer = min(player.offers, key = bestOfferFunc)
                
                player.updatePosition(offer.offered_position)
Beispiel #4
0
    
    return dataToPlot

WEIGHTED_SUM = "Weighted sum"

if __name__ == '__main__':
    
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "countries_test.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "WW1.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "WW1_mod.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "Litigation Case Study_orig.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "Litigation Case Study.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "Litigation Case Study 2.csv")
    players = PlayerCSVReader().readPlayers(APP_RESOURCES + "OIL_PREANA.csv")
    #players = PlayerCSVReader().readPlayers(APP_RESOURCES + "Iranian_Election.csv")
    objectListPrint(players)
    
    print("================ START ================")    
    medianVPC = MedianVoterPositionCalculator(players)
    wpc = WeightedPositionCalculator(players)
    
    risks = [1 for x in range(len(players))]
    
    data = [{"name":x.name, "values":[x.position]} for x in players]
    data.append({"name" : WEIGHTED_SUM, "values": [wpc.calculate()]})
    
    i = 0
    for i in range(20):                              #RANGE!!!
        medianVPC.calculateMedianVoterPosition()
        medianVoter = medianVPC.getMedianVoterPosition()
        print("==> Median Voter:", medianVoter, '\n')