Broncho

This program has been disqualified.


AuthorDeltcho
Submission date2012-01-23 04:39:05.597644
Rating5000
Matches played0
Win rate0

Source code:

import math as np
import random



global val_set, item_occurance, LTM_limit, STM_limit, memory_array, weighted_memory_array, memory_array_index, win_loss_array,win_loss_weight, WLW_STM, MAI_STM
global STM_Halflife, LTM_Halflife, mindset, MA_STM, WMA_STM, WLA_STM, streak, previous_input
global STM_w_bias, STM_w_laystats, STM_w_prediction, LTM_w_prediction, LTM_w_bias, STM_model, learning


if input == "":
    output = random.choice(["R","P","S"])
    winning = []
    STM_w_bias = 0.40
    STM_w_laystats = 0.15
    STM_w_prediction = 0.45
    LTM_w_prediction = 0.65
    LTM_w_bias = 0.35

    memory_array_index = 0
    MAI_STM = 0
    streak = 5
    previous_input = ''

    STM_Halflife = 5
    LTM_Halflife = 25
    LTM_limit = 50
    STM_limit = 7
    STM_model = 3
    learning = 1

    mindset = []
    memory_array = []
    weighted_memory_array = []
    win_loss_array = []
    win_loss_weight = []

    MA_STM = []
    WMA_STM = []
    WLA_STM = []
    WLW_STM = []

    for i in range(STM_limit):
    	MA_STM.append(0)
    	WMA_STM.append(0)
    	WLA_STM.append(0)
    	WLW_STM.append(0)


    for i in range(LTM_limit):
    	memory_array.append(0)
    	weighted_memory_array.append(0)
    	win_loss_array.append(0)
    	win_loss_weight.append(0)

    val_set = ['R','P','S']


    item_occurance = []
    for i in range(len(val_set)):
    	item_occurance.append(0)

def weighted_choice(a):
    random1 = random.uniform(0,1)


    if random1 <= a[0]:
        return 'R'
    elif random1 > a[0] and random1 <= (a[0]+a[1]):
        return 'P'
    elif random1 > (a[0]+a[1]):
        return 'S'

def average(values):
	return sum(values, 0.0) / len(values)

def sum_moo(values):
    return sum(values, 0.0)

def STM (inp,output):
    global val_set, item_occurance, STM_limit, WLW_STM, MAI_STM
    global STM_Halflife, mindset, MA_STM, WMA_STM, WLA_STM, streak, previous_input

    if MAI_STM == (STM_limit):

        LTM_content = LTM(MA_STM[0][0],MA_STM[0][1], WLA_STM[0])

        MA_STM = MA_STM[1:]
        MA_STM.append([inp,output])
        WLA_STM = WLA_STM[1:]
        WLA_STM.append(RPS(inp,output))


        for (i) in range(MAI_STM):
            WMA_STM[i] = salience((MAI_STM-(1+i)),'STM')

        previous_input = MA_STM[MAI_STM-1][1]


    elif MAI_STM < (STM_limit):

        MA_STM[MAI_STM] = [inp,output]
        previous_input = MA_STM[MAI_STM][0]

        WLA_STM[MAI_STM] = RPS(inp,output)
        for (i) in range(MAI_STM):
            WMA_STM[i] = salience((MAI_STM-1-(i)),'STM')

        MAI_STM += 1

    for i in range(len(val_set)):
		if inp == val_set[i]:
			item_occurance[i] += 1


def LTM(inp,output, WLA_value):
    global val_set, item_occurance, LTM_limit, memory_array, weighted_memory_array, memory_array_index, win_loss_array,win_loss_weight, streak


    if inp and output == 0:
        return long_term_memory
    else:


    	if memory_array_index == (LTM_limit):
            memory_array = memory_array[1:]
            memory_array.append([inp,output])
            win_loss_array = win_loss_array[1:]
            win_loss_array.append(WLA_value)


    	elif memory_array_index < (LTM_limit):
            win_loss_array[memory_array_index] = WLA_value
            memory_array[memory_array_index] = [inp,output]


            for (i) in range(memory_array_index):
                weighted_memory_array[i] = salience((memory_array_index-(i+1)),'LTM')


            memory_array_index += 1

    	for i in range(len(val_set)):
    		if inp == val_set[i]:
    			item_occurance[i] += 1

def salience(memory_position, memory_type):
    global STM_Halflife, LTM_Halflife, STM_limit, STM_model

    if memory_type == 'STM':
        if STM_model == 1:
            new_salience = 100*(np.e)**((np.log(0.5)/STM_Halflife)*memory_position)
        elif STM_model == 2:

            if memory_position <= (int(STM_limit/2)*1+1):
                new_salience = 100*(np.e)**((np.log(0.5)/STM_Halflife)*((int(STM_limit/2)*1+1)-memory_position))

            else:
                new_salience = 100*(np.e)**((np.log(0.5)/STM_Halflife)*(memory_position-(int(STM_limit/2)*1+1)))
        elif STM_model == 3:

            if memory_position <= (int(STM_limit/2)*1+1):
                new_salience = 100*(np.e)**((np.log(0.5)/STM_Halflife)*(memory_position))

            else:
                new_salience = 100*(np.e)**((np.log(0.5)/STM_Halflife)*(STM_limit-1-memory_position))




    elif memory_type == 'LTM':
        new_salience = 100*(np.e)**((np.log(0.5)/LTM_Halflife)*memory_position)
    return new_salience

def win_loss_salience(MA, MAI, WLA, WLW, f_type):
    global streak
    if f_type == 'pattern':
    	for (i) in range(MAI):
			WLW[i] = 0

        for i in range(MAI):
            if (MAI-i) > streak:
                for x in range(streak):

                    WLW[(MAI-1-i-x)] = average(WLA[(MAI-i-streak):(MAI-i)])
            else:
				for x in range(streak):
					WLW[(MAI-1-i-x)] = average(WLA[:(MAI-i)])

        w_l_pattern = WLW[:]
        return w_l_pattern



    if f_type =='bias':
        for (i) in range(MAI):
            WLW[i] = 0

        for i in range(MAI):
			if (MAI-i-1) > streak:
				for x in range(streak):
					WLW[(MAI-1-i-x)] += average(WLA[(MAI-i-streak):(MAI-i)])
			else:
				for x in range(streak):
					WLW[(MAI-1-i-x)] += average(WLA[:(MAI-i)])

        for (i) in range(MAI):
            WLW[i] = WLW[i]/streak


        w_l_bias = WLW[:]


        return w_l_bias




def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]


def prediction(MA, MAI, WMA, last_input):

    global val_set

    pattern = [[0,0],[0,0]]
    val_set_cnt = []
    val_set_weight = []
    val_set_weight_play = []

    for i in range(len(val_set)):
        val_set_cnt.append(0)
        val_set_weight.append(0)
        val_set_weight_play.append(0)

    for i in range((MAI-1)):

        pattern = [MA[i][0], MA[i+1][0]]

        if pattern[0] == last_input:

            for x in range(len(val_set)):
                if pattern[1] == val_set[x]:
                    val_set_cnt[x] += 1.0
                    val_set_weight[x] += WMA[i+1]

    if sum_moo(val_set_weight) > 0:
        blah_weight = sum_moo(val_set_weight)
        for i in range(len(val_set)):
            val_set_weight[i] = val_set_weight[i] / blah_weight

        for i in range(len(val_set)):
            if i > 0:
                val_set_weight_play[i] = val_set_weight[(i-1)]
            elif i == 0:
                val_set_weight_play[i] = val_set_weight[(len(val_set)-1)]

    else:
        for x in range(len(val_set)):
            val_set_weight_play.append(1.0 / float(len(val_set)))


    return val_set_weight_play




def superstition(MA, MAI):
    global val_set, streak

    val_set_cnt = []
    val_set_cnt_play = []
    for a in range(len(val_set)):
        val_set_cnt.append(0)
        val_set_cnt_play.append(0)
    if MAI > streak:
        for i in range(MAI):

            for x in range(len(val_set)):
                if MA[i][0] == val_set[x]:
                    val_set_cnt[x] += 1.0
        blah_cnt = sum_moo(val_set_cnt)
        for i in range(len(val_set)):
            val_set_cnt[i] = (1 - (val_set_cnt[i] / blah_cnt))
        blah_cnt = sum_moo(val_set_cnt)
        for i in range(len(val_set)):
            val_set_cnt[i] = (1 - (val_set_cnt[i] / blah_cnt)) / blah_cnt


        for i in range(len(val_set)):
            if i > 0:
                val_set_cnt_play[i] = val_set_cnt[(i-1)]
            elif i == 0:
                val_set_cnt_play[i] = val_set_cnt[(len(val_set)-1)]


    else:
        for x in range(len(val_set)):
            val_set_cnt_play.append(1.0 / len(val_set))



    return val_set_cnt_play




def RPS (inp,output):
    if inp == 'R':
        if output == 'R':
            val = 0.5
        elif output == 'P':
			val = 1.0
        elif output == 'S':
			val = 0.0
    elif inp == 'P':
        if output == 'R':
            val = 0.0
        elif output == 'P':
            val = 0.5
        elif output == 'S':
            val = 1.0
    elif inp == 'S':
        if output == 'R':
            val = 1.0
        elif output == 'P':
            val = 0.0
        elif output == 'S':
            val = 0.5
    return val



def pattern_recognition(MA, MAI):
    global val_set, streak, MA_STM
    pattern = []

    val_set_cnt = []
    val_set_weight = []
    val_set_weight_play = []
    pattern_input = []
    pattern = []

    for i in range(len(val_set)):
        val_set_cnt.append(0)
        val_set_weight.append(0)
        val_set_weight_play.append(0)
    for moo in range(STM_limit):
        pattern_input.append(MA_STM[moo][0])

    for i in range((MAI-streak)):
        for x in range(streak):

            pattern.append([MA[i+x][0]])


        if pattern == pattern_input:

            for y in range(len(val_set)):
                if MA[i+x+1][0] == val_set[y]:
                    val_set_cnt[y] += 1.0
        pattern = []

    blah_weight = sum_moo(val_set_weight)
    if sum_moo(val_set_weight) > 0:
        for i in range(len(val_set)):
            val_set_weight[i] = val_set_weight[i] / blah_weight

        for i in range(len(val_set)):
            if i > 0:
                val_set_weight_play[i] = val_set_weight[(i-1)]
            elif i == 0:
                val_set_weight_play[i] = val_set_weight[(len(val_set)-1)]

    else:
        for x in range(len(val_set)):
            val_set_weight_play.append(1.0 / float(len(val_set)))

    return val_set_weight_play



def decision():
    global val_set, item_occurance, LTM_limit, STM_limit, memory_array, weighted_memory_array, memory_array_index, win_loss_array,win_loss_weight, WLW_STM, MAI_STM
    global STM_Halflife, LTM_Halflife, mindset, MA_STM, WMA_STM, WLA_STM, streak, previous_input
    global STM_output, LTM_output
    global STM_w_bias, STM_w_laystats, STM_w_prediction, LTM_w_bias,LTM_w_prediction, STM_w_pat

    if MAI_STM < streak:
        output = weighted_choice([0.333,0.333,0.333])


    else:

        val_set_cnt = []
        val_set_weight = []
        val_set_bias = []
        STM_weights = []
        LTM_weights = []
        for i in range(len(val_set)):
            val_set_cnt.append(0)
            val_set_weight.append(0)
            val_set_bias.append(0)
            STM_weights.append(0)
            LTM_weights.append(0)

        STM_prediction = prediction(MA_STM, MAI_STM, WMA_STM,previous_input)
        #STM_laystats = superstition(MA_STM,MAI_STM)
        STM_bias_weight = win_loss_salience(MA_STM,MAI_STM, WLA_STM, WLW_STM,'bias')

        for i in range(MAI_STM):
            for x in range(len(val_set)):
                if MA_STM[i][1] == val_set[x]:
                    val_set_weight[x] += STM_bias_weight[i]
                    val_set_cnt[x] += 1.0


        if sum_moo(val_set_cnt) > 0:
            blah_cnt = sum_moo(val_set_cnt)
            for i in range(len(val_set)):
                val_set_weight[i] = val_set_weight[i] / blah_cnt
                blah_weight = sum_moo(val_set_weight)
                for i in range(len(val_set)):
                    if blah_weight > 0:
                        val_set_bias[i] = val_set_weight[i] / blah_weight
            else:
                for x in range(len(val_set)):
                    val_set_bias[x] = 1.0 / len(val_set)
        else:
            for x in range(len(val_set)):
                val_set_bias[x] = 1.0 / len(val_set)


        for y in range(len(val_set)):

            #STM_weights[y] = (STM_w_bias*val_set_bias[y]/1.0 + STM_w_laystats*STM_laystats[y]/1.0 + STM_w_prediction*STM_prediction[y]/1.0 )/ (STM_w_bias/1.0 + STM_w_laystats/1.0 + STM_w_prediction/1.0)
            STM_weights[y] = (STM_w_bias*val_set_bias[y]/1.0 + STM_w_prediction*STM_prediction[y]/1.0 )/ (STM_w_bias/1.0 +  STM_w_prediction/1.0)
        #STM_output = [val_set_bias, STM_laystats, STM_prediction]
        STM_output = [val_set_bias, STM_prediction]
        blah_stm = sum_moo(STM_weights)
        for i in range(len(val_set)):
            STM_weights[i] = (STM_weights[i]/blah_stm)

        output = weighted_choice(STM_weights)


        if memory_array_index >= streak:

            val_set_cnt = []
            val_set_weight = []
            val_set_bias = []
            aggregate_weights = []
            for i in range(len(val_set)):
                val_set_cnt.append(0)
                val_set_weight.append(0)
                val_set_bias.append(0)
                aggregate_weights.append(0)



            LTM_prediction = prediction(memory_array,memory_array_index,weighted_memory_array,previous_input)
            LTM_bias_weight = win_loss_salience(memory_array, memory_array_index, win_loss_array, win_loss_weight,'bias')

            for i in range(memory_array_index):
                for x in range(len(val_set)):
                    if memory_array[i][1] == val_set[x]:
                        val_set_weight[x] += LTM_bias_weight[i]
                        val_set_cnt[x] += 1.0
            blah_cnt = sum_moo(val_set_cnt)
            if sum_moo(val_set_cnt) > 0:
                for i in range(len(val_set)):
                    val_set_weight[i] = val_set_weight[i] / blah_cnt
                if sum_moo(val_set_weight) > 0:
                    blah_weight = sum_moo(val_set_weight)
                    for i in range(len(val_set)):
                        val_set_bias[i] = val_set_weight[i] / blah_weight
                else:
                    for x in range(len(val_set)):
                        val_set_bias[x] = 1.0 / len(val_set)
            else:
                for x in range(len(val_set)):
                    val_set_bias[x] = 1.0 / len(val_set)

            for y in range(len(val_set)):

                LTM_weights[y] = (LTM_w_bias*val_set_bias[y] + LTM_w_prediction*LTM_prediction[y]) / (LTM_w_bias + LTM_w_prediction)
            blah_LTM = sum_moo(LTM_weights)
            for i in range(len(val_set)):
                LTM_weights[i] = (LTM_weights[i]/blah_LTM)


            LTM_output = [val_set_bias, LTM_prediction]

            for g in range(len(val_set)):
                aggregate_weights[g] = 0.75*STM_weights[g] + 0.25*LTM_weights[g]
            blah_agg = sum_moo(aggregate_weights)
            for i in range(len(val_set)):
                aggregate_weights[i]= aggregate_weights[i]/blah_agg

            output = weighted_choice(aggregate_weights)

    return output

def adjust_weights(inp,out):
    global memory_array_index, LTM_output, STM_output, streak, learning, STM_limit, LTM_limit
    global STM_w_bias, STM_w_laystats, STM_w_prediction, LTM_w_bias,LTM_w_prediction
    win = RPS(inp,out)

    if win >= 0.50 and memory_array_index > streak and learning == 1:

        for i in range(len(val_set)):
            if val_set[i] == out:


                if (LTM_output[0][i] > LTM_output[1][i]) and (LTM_w_prediction >= (0.05*win)):
                    LTM_w_bias += 0.05*win
                    LTM_w_prediction -= 0.05*win
                if (LTM_output[0][i] < LTM_output[1][i]) and (LTM_w_bias >= (0.05*win)):
                    LTM_w_bias -= 0.05*win
                    LTM_w_prediction += 0.05*win


                if ((STM_output[0][i] > STM_output[1][i]) and (STM_w_prediction >= 0.05*win)):
                    STM_w_bias += 0.10*win

                    STM_w_prediction -= 0.05*win

                if ((STM_output[1][i] > STM_output[0][i]) and (STM_w_bias >= 0.05*win)):
                    STM_w_bias -= 0.05*win

                    STM_w_prediction -= 0.05*win


##                if ((STM_output[0][i] > STM_output[1][i]) and (STM_w_laystats >= 0.05*win)) and ((STM_output[0][i] > STM_output[2][i]) and (STM_w_prediction >= 0.05*win)):
##                    STM_w_bias += 0.10*win
##                    STM_w_laystats -= 0.05*win
##                    STM_w_prediction -= 0.05*win
##
##                if ((STM_output[1][i] > STM_output[0][i]) and (STM_w_bias >= 0.05*win) ) and ((STM_output[1][i] > STM_output[2][i]) and (STM_w_prediction >= 0.05*win)):
##                    STM_w_bias -= 0.05*win
##                    STM_w_laystats += 0.10*win
##                    STM_w_prediction -= 0.05*win
##
##                if ((STM_output[2][i] > STM_output[0][i]) and (STM_w_bias >= 0.05*win)) and ((STM_output[2][i] > STM_output[1][i]) and (STM_w_laystats >= 0.05*win)):
##                    STM_w_bias -= 0.05*win
##                    STM_w_laystats -= 0.05*win
##                    STM_w_prediction += 0.10*win


        return [LTM_w_bias,LTM_w_prediction, STM_w_bias, STM_w_laystats, STM_w_prediction]
    else:
        pass

if (input == 'R' or input == 'P' or input == 'S'):
    winning.append(RPS(input,output))
    a = random.randint(1,2)
    if average(WLA_STM) > 0.50:
        STM(input,output)
        adjust_weights(input,output)
        output = decision()

    else:
        STM(input,output)
        adjust_weights(input,output)
        decision()
        output = random.choice(["R","P","S"])

else:
    output = decision()