Coding a strategy for the Ultimatum Game

110 Views Asked by At

I'm coding different strategies for the Ultimatum Game, which is essentially when a sum of cash is to be shared between two people and one person offers the other a percentage and they either accept or reject. In this strategy I'm assuming there is one proposer but multiple responders (so one person is offering people a percentage of money one after the other, kind of like there's a queue of people waiting to receive an offer) and the proposer starts at 50%, if their offer is accepted 3 times in a row then they lower their offer by 5% and continue offering this amount, if their offer is rejected 3 times in a row then they increase their offer percentage by 5% and continue. I've coded this strategy and, in theory, I believe it should work but it won't run, I think I've made it far more complicated than it needs to be and my python is struggling to run the code. If anyone could help simplify my code or show me a ore efficient method it would help a lot. Thanks in advance

from random import choice

very_fair_offer=range(46,101)
fair_offer=range(36,46)
unfair_offer=range(15,36)
very_unfair_offer=range(0,16)


very_fair_odds=[True, True, True, True, True, True, True, True, True, True]
fair_odds=[True, True, True, True, True, True, True, True, False, False]
unfair_odds=[True, True, True, True, True, False, False, False, False, False]
very_unfair_odds = [True, True, True, False, False, False, False, False, False, False, False]

offer=50

number_of_offers=0
accepted_counter=0
rejected_counter=0
while number_of_offers<50:



    if offer in very_fair_offer:
        if choice(very_fair_odds):
            print("Accepted")
            Accepted=True
            number_of_offers=(number_of_offers+1)
            while Accepted==True:
                    accepted_counter=accepted_counter+1
                    rejected_counter=0
                    
            while accepted_counter==3:
                offer=offer-5
                accepted_counter=0
        
        else:
            print("Not accepted")
            Accepted=False
            number_of_offers=number_of_offers+1
            while Accepted==False:
                    rejected_counter=rejected_counter+1
                    accepted_counter=0
                    
            while rejected_counter==3:
                offer=offer+5
                rejected_counter=0


    elif offer in fair_offer:
        if choice(fair_odds):
            print("Accepted")
            Accepted=True
            number_of_offers=number_of_offers+1
            while Accepted==True:
                accepted_counter=accepted_counter+1
                rejected_counter=0
                
            while accepted_counter==3:
                offer=offer-5
                accepted_counter=0
        else:
            print("Not accepted")
            Accepted=False
            number_of_offers=number_of_offers+1
            while Accepted==False:
                    rejected_counter=rejected_counter+1
                    accepted_counter=0
                    
            while rejected_counter==3:
                offer=offer+5
                rejected_counter=0

        
    elif offer in unfair_offer:
        if choice(unfair_odds):
            print("Accepted")
            Accepted=True
            number_of_offers=number_of_offers+1
            while Accepted==True:
                accepted_counter=accepted_counter+1
                rejected_counter=0
                
            while accepted_counter==3:
                offer=offer-5
                accepted_counter=0
        else:
            print("Not accepted")
            Accepted=False
            number_of_offers=number_of_offers+1
            while Accepted==False:
                    rejected_counter=rejected_counter+1
                    accepted_counter=0
                    
            while rejected_counter==3:
                offer=offer+5
                rejected_counter=0


        
    elif offer in very_unfair_offer:
        if choice(very_unfair_odds):
            print("Accepted")
            Accepted=True
            number_of_offers=number_of_offers+1
            while Accepted==True:
                accepted_counter=accepted_counter+1
                rejected_counter=0
                
            while accepted_counter==3:
                offer=offer-5
                accepted_counter=0
        else:
            print("Not accepted")
            Accepted=False
            number_of_offers=number_of_offers+1
            while Accepted==False:
                    rejected_counter=rejected_counter+1
                    accepted_counter=0
                    
            while rejected_counter==3:
                offer=offer+5
                rejected_counter=0


        
    else:
        print("This offer is not valid")
1

There are 1 best solutions below

0
On

Well I optimized it for you. I will come back and edit this alot to update it and make it faster. Come back a lot and check at the bottom for the change log! I also hope that from the change log, you can get an understanding of how to optimise your future projects.

If you get an error from this please reply with the error message and if you think you know how to fix it then please tell me how you think it can be fixed!

from random import choice

very_fair_offer,fair_offer,unfair_offer,very_unfair_offer=range(46,101),range(36,46),(15,36),(0,16)

very_fair_odds=[True]*10
fair_odds=[True, True, True, True, True, True, True, True, False, False]
unfair_odds=[True, True, True, True, True, False, False, False, False, False]
very_unfair_odds = [True, True, True, False, False, False, False, False, False, False, False]

offer,number_of_offers,accepted_counter,rejected_counter=50,0,0,0

while number_of_offers<50:



    if offer in very_fair_offer:
        if choice(very_fair_odds): print("Accepted"); Accepted=True; number_of_offers=(number_of_offers+1)
            while Accepted: accepted_counter=accepted_counter+1; rejected_counter=0
                    
            while accepted_counter==3: offer=offer-5; accepted_counter=0
        
        else:
            print("Not accepted"); Accepted=False; number_of_offers+=1
            while not Accepted: rejected_counter=rejected_counter+1; accepted_counter=0
                    
            while rejected_counter==3: offer=offer+5; rejected_counter=0


    elif offer in fair_offer:
        if choice(fair_odds): print("Accepted"); Accepted=True; number_of_offers=number_of_offers+1;
          
            while Accepted: accepted_counter=accepted_counter+1; rejected_counter=0
                    
            while accepted_counter==3: offer=offer-5; accepted_counter=0
        else:
            print("Not accepted"); Accepted=False; number_of_offers+=1
            while not Accepted: rejected_counter=rejected_counter+1; accepted_counter=0
                    
            while rejected_counter==3: offer=offer+5; rejected_counter=0

        
    elif offer in unfair_offer:
        if choice(unfair_odds):
            print("Accepted"); Accepted=True; number_of_offers=number_of_offers+1
            while Accepted: accepted_counter=accepted_counter+1; rejected_counter=0
                    
            while accepted_counter==3: offer=offer-5; accepted_counter=0
                
        else:
            print("Not accepted"); Accepted=False; number_of_offers+=1
            while not Accepted: rejected_counter=rejected_counter+1; accepted_counter=0
                    
            while rejected_counter==3: offer=offer+5; rejected_counter=0


        
    elif offer in very_unfair_offer:
        if choice(very_unfair_odds):
            print("Accepted"); Accepted=True; number_of_offers=number_of_offers+1
            while Accepted: accepted_counter=accepted_counter+1; rejected_counter=0
                    
            while accepted_counter==3: offer=offer-5; accepted_counter=0
        else:
            print("Not accepted"); Accepted=False; number_of_offers+=1
            while not Accepted: rejected_counter=rejected_counter+1; accepted_counter=0
                    
            while rejected_counter==3: offer=offer+5; rejected_counter=0


        
    else:
        print("This offer is not valid")

V0.1: First version of this optimisation! Made everything a 1 liner replace while x == true with while x, while x == false become while not x, made vars like x,y=1,2 instead of 2 separate statements.