meta-iocane_v2

This program has been disqualified.


Authorevolvingstuff
Submission date2011-06-20 06:57:57.416590
Rating7067
Matches played2076
Win rate72.78

Source code:

from random import *

def findMatchUs(history_us, depth):
    prefix = history_us[-depth*2:]
    i = history_us[:-2].rfind(prefix)
    if i > -1:
        return history_us[i+depth*2].upper()
    else:
        return None

if input == "":
    shift = {None:None, 'R':'P', 'P':'S', 'S':'R'}
    decay = 0.6
    meta_decay = 0.6
    history_us = ''
    markov_depth = 20
    weight_random = 1
    weights = [[1 for k in range(markov_depth)] for j in range(3)]
    predictions = [[None for k in range(markov_depth)] for j in range(3)]
    meta_prediction = [None for j in range(3)]
    meta_weight = [1 for j in range(3)]
    chosen_prediction = None
    meta_chosen_prediction = None
    output = choice(['R','P','S'])
else:
    #store histories
    history_us += input.lower() + output
    
    #decay models and update weights based on input observation
    weight_random *= decay
    if chosen_prediction != None:
        if input != chosen_prediction:
            weight_random += 1
        for j in range(3):
            for k in range(markov_depth):
                weights[j][k] *= decay
                if input == predictions[j][k]:
                    weights[j][k] += 1

    #decay meta model
    if meta_chosen_prediction != None:
        for i in range(3):
            meta_weight[i] *= meta_decay
            if meta_chosen_prediction == meta_prediction[i]:
                meta_weight[i] += 1
                
    #make new predictions
    prediction = None
    for j in range(3):
        for k in range(markov_depth):
            predictions[j][k] = None
            prediction = findMatchUs(history_us, k+1)
            for j2 in range(j): #iocane-shift
                prediction = shift[shift[prediction]]
            if prediction != None:
                predictions[j][k] = prediction
    
    #choose highest weight
    chosen_prediction = None
    mx = weight_random
    for j in range(3):
        for k in range(markov_depth):
            if predictions[j][k] != None and weights[j][k] > mx:
                mx = weights[j][k]
                chosen_prediction = predictions[j][k]

    #meta-iocane
    for i in range(3):
        meta_prediction[i] = chosen_prediction
        for i2 in range(i):
            meta_prediction[i] = shift[shift[meta_prediction[i]]]

    #choose action
    if chosen_prediction == None:
        output = choice(['R','P','S'])
    else:
        mx = 0
        meta_chosen_prediction = None
        for i in range(3):
            if meta_weight[i] > mx:
                mx = meta_weight[i]
                meta_chosen_prediction = meta_prediction[i]
        output = shift[meta_chosen_prediction]