def calculate(self):
        length = len(self.__players)
        probabilityOfSuccess = [[0 for x in range(length)] for y in range(length)]

        
        for i, playerI in enumerate(self.__players):
            for j, playerJ in enumerate(self.__players):
                numerator = 0
                denominator = 0

                for k, playerK in enumerate(self.__players):
                    if k != i and k != j:
                        arg = self.calculateAllianceProbabilityArg(playerI, playerJ, playerK)

                        if (arg > 0):
                            numerator += arg
                        denominator += abs(arg)
                    
                if denominator != 0:
                    probabilityOfSuccess[i][j] = (numerator / denominator)
                else:
                    # is it right?
                    probabilityOfSuccess[i][j] = 0
                    
        print ("====Prob of Success =====")
        tablePrint(probabilityOfSuccess)

        return probabilityOfSuccess
Esempio n. 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


                    # 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)
Esempio n. 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


                    #if EIi > EIj and EJj > EJi and EIi > 0 and EIj > 0 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 EJj > EJi and EIi > 0 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 abs(EJj) < abs(EJi) 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(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
                        
                    elif EIi > 0 and EIj < 0 and abs(EIi) < abs(EIj):                                                   #player I kapitulációra kényszeríti J-t 
                        playerI.offers.append(Offer(playerJ, Offer.CAPITULATION, playerJ.position, EIi))
                        self.__offersIJ[i][j] = playerJ.position                       


                    elif EIi > EIj and EJj < EJi and EIi > 0 and EIj > 0:# 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                           
                        
            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
        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))
                compromiseOffers = [offer for offer in player.offers if offer.offer_type == Offer.COMPROMISE]
                confrontationOffers = [offer for offer in player.offers if offer.offer_type == Offer.CONFRONT]
                capOffers = [offer for offer in player.offers if offer.offer_type == Offer.CAPITULATION]


                if len(compromiseOffers) > 0:
                    minComp = min(compromiseOffers, key=lambda x: abs(player.position - x.offered_position))
                    player.updatePosition(minComp.offered_position)
                elif len(confrontationOffers) > 0: 
                    minConf = min(confrontationOffers, key=lambda x: abs(player.position - x.offered_position))
                    player.updatePosition(minConf.offered_position)
                elif len(capOffers) > 0:
                    minCap = min(capOffers, key=lambda x: abs(player.position - x.offered_position))
                    player.updatePosition(minCap.offered_position)
    def calculateExpectedUtility(self):
        probSuccCalc = ProbabilityOfSuccessCalculator(self.__players)
        probabilityOfSuccess = probSuccCalc.calculate()
        probSQCalc = ProbabilityOfStatusQuoCalculator(self.__players, probabilityOfSuccess)
        probabilityOfStatusQuo = probSQCalc.calculate()

        length = len(self.__players)

        self.__expectedUtilityIJ = [[0 for x in range(length)] for y in range(length)]
        self.__expectedUtilityJI = [[0 for x in range(length)] for y in range(length)]

        for i, playerI in enumerate(self.__players):
            for j, playerJ in enumerate(self.__players):
                probSucc = probabilityOfSuccess[i][j]
                # probSQ = probabilityOfStatusQuo[i][j]
                probSQ = 1

                usi = USI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[i]
                ).calculate()
                ufi = UFI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[i]
                ).calculate()
                ubi = UBI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[i]
                ).calculate()
                uwi = UWI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[i]
                ).calculate()
                usq = USQ(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[i]
                ).calculate()

                T = 1

                prevDistance = abs(playerI.previousPosition - playerJ.previousPosition)
                currentDistance = abs(playerI.position - playerJ.position)
                if prevDistance >= currentDistance:
                    T = 1
                else:
                    T = 0

                self.__expectedUtilityIJ[i][j] = (
                    playerJ.salience * (probSucc * usi + (1 - probSucc) * ufi)
                    + (1 - playerJ.salience) * usi
                    - probSQ * usq
                    - (1 - probSQ) * (T * ubi + (1 - T) * uwi)
                )

                probSucc = probabilityOfSuccess[j][i]
                # probSQ = probabilityOfStatusQuo[j][i]
                probSQ = 1

                usi = USI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[j]
                ).calculate()
                ufi = UFI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[j]
                ).calculate()
                ubi = UBI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[j]
                ).calculate()
                uwi = UWI(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[j]
                ).calculate()
                usq = USQ(
                    self.__medianVoter, playerI, playerJ, self.__maxDifferenceBetweenPositions, self.__risks[j]
                ).calculate()

                self.__expectedUtilityJI[i][j] = (
                    playerJ.salience * (probSucc * usi + (1 - probSucc) * ufi)
                    + (1 - playerJ.salience) * usi
                    - probSQ * usq
                    - (1 - probSQ) * (T * ubi + (1 - T) * uwi)
                )

        print("====E(Uij) =====")
        tablePrint(self.__expectedUtilityIJ)

        print("====E(Uji) =====")
        tablePrint(self.__expectedUtilityJI)