Пример #1
0
 def parse(self, entry, text=True):
     if text:
         #replace Orange L by Green Circle.
         #due to counterbalancing, otherwise equivalent.
         machine_color = entry[0] if entry[0] != 'O' else 'G'
         machine_shape = entry[1] if entry[1] != 'L' else 'C'
         toy_color = entry[4] if entry[4] != 'O' else 'G'
         toy_shape = entry[5] if entry[1] != 'L' else 'C'
         active = (entry[6] == '1')
         return Datapoint.Datapoint(
             ((toy_color, toy_shape), (machine_color, machine_shape)),
             active)
     else:
         machine_color = self.to_int(
             entry[0]) if entry[0] != 'O' else self.to_int('G')
         machine_shape = self.to_int(
             entry[1]) if entry[1] != 'L' else self.to_int('C')
         toy_color = self.to_int(
             entry[4]) if entry[4] != 'O' else self.to_int('G')
         toy_shape = self.to_int(
             entry[5]) if entry[5] != 'L' else self.to_int('C')
         active = entry[6] == '1'
         return Datapoint.Datapoint(
             ((toy_color, toy_shape), (machine_color, machine_shape)),
             active)
Пример #2
0
import Datapoint
import world

dat_col=[Datapoint.Datapoint((world.available_toys[0],world.machines[0]),True),\
		Datapoint.Datapoint((world.available_toys[2],world.machines[1]),True)]

dat_sha=[Datapoint.Datapoint((world.available_toys[1],world.machines[0]),True),\
		Datapoint.Datapoint((world.available_toys[0],world.machines[1]),True)]

dat_indep=[Datapoint.Datapoint((world.available_toys[0],world.machines[0]),True),\
		Datapoint.Datapoint((world.available_toys[0],world.machines[1]),True)]

Пример #3
0
def make_action(action, condition):
    if condition == 'C':
        return Datapoint.Datapoint(action, action[0][0] == action[1][0])
    elif condition == 'S':
        return Datapoint.Datapoint(action, action[0][1] == action[1][1])
Пример #4
0
def make_forced_action(action, result):
    return Datapoint.Datapoint(action, result)
Пример #5
0
def possible_data(action):
    return [
        Datapoint.Datapoint(action, False),
        Datapoint.Datapoint(action, True)
    ]
Пример #6
0
        #acts1[i].display(), acts2[i].display()

    #print row, col
    kacts[row][col] += 1

print kacts
for i, r in enumerate(kacts):
    print '{3} & {0} & {1} & {2} \ '.format(r[0], r[1], r[2], i)

########
import learners
tl = learners.HypothesesLearner()
# # machines=[(2,0), (1,1), (0,2)]
# # available_toys=[(2,1), (0,0), (1,2)]
import Datapoint
dac = [Datapoint.Datapoint(((0, 0), (0, 2)), True)]
dis = [Datapoint.Datapoint(((0, 0), (2, 0)), False)]
dnon = [Datapoint.Datapoint(((0, 0), (1, 1)), False)]

ents = np.zeros((6, 3))

learners.entropy_gains.model.epsilon = 0.25

ents[0, 0] = tl.expected_final_entropy(((2, 1), (2, 0)), dac)
print 'one'
ents[1, 0] = tl.expected_final_entropy(((1, 2), (0, 2)), dac)
ents[2, 0] = tl.expected_final_entropy(((0, 0), (2, 0)), dac)
ents[3, 0] = tl.expected_final_entropy(((2, 1), (0, 2)), dac)
ents[4, 0] = tl.expected_final_entropy(((0, 0), (1, 1)), dac)
ents[5, 0] = tl.expected_final_entropy(((1, 2), (2, 0)), dac)
Пример #7
0
def main(player, n):
    #random.seed(0)

    starttime = time.clock()
    # if player=='adults':
    # 	data=Data.Data(parameters.inputfile_adults)
    # 	data.read(astext=False)
    # elif player=='kids':
    # 	data=Data.Data(parameters.inputfile_kids)
    # 	data.read(astext=False)

    #data=Data.Data(parameters.inputfile_kids)
    data = Data.Data(parameters.inputfile_adults)
    data.read(astext=False)

    n_kids = parameters.n_kids
    truncate = int(n)
    #n_r_theo=parameters.n_r_theo
    n_r_random = parameters.n_r_random

    eg = np.zeros(len(data.get_kids()[:n_kids]))

    if player in ('kids', 'adults'):
        n_r = 1
        for k, kid in enumerate(data.get_kids()[:n_kids]):
            kidseq = data.data[kid][:truncate]
            keg = entropy_gains.ave_theory_expected_entropy_gain(kidseq)[0]
            #print kid, keg
            eg[k] = keg
            #nkegs

    elif player == 'random':
        n_r = n_r_random
        egall = np.zeros((len(data.get_kids()[:n_kids]), n_r))
        for k, kid in enumerate(data.get_kids()[:n_kids]):
            for r in range(n_r_random):
                rl = learners.RandomLearner()
                rlseq = rl.play(min(data.get_kid_nactions(kid), truncate))
                reg = entropy_gains.ave_theory_expected_entropy_gain(rlseq)[0]
                #print kid, reg
                eg[k] += reg
                egall[k, r] = reg
            eg[k] /= n_r
        #[d.display() for d in rlseq]
        #print reg

    elif player == 'theory':
        n_r = n_r_theo
        egall = np.zeros((len(data.get_kids()[:n_kids]), n_r))
        for k, kid in enumerate(data.get_kids()[:n_kids]):
            for r in range(n_r_theo):
                tl = learners.TheoryLearner()
                tlseq = tl.play(min(data.get_kid_nactions(kid), truncate))
                teg = entropy_gains.ave_theory_expected_entropy_gain(tlseq)[0]
                #print kid, teg
                eg[k] += teg
                egall[k, r] = teg
            eg[k] /= n_r

    elif player == 'theoryfull':

        n_long_kids=sum([data.get_kid_nactions(kid)>=truncate \
             for kid in data.get_kids()])
        eig = np.zeros((n_long_kids, 3))
        tlactions = []
        rlactions = []

        k = 0
        for ki, kid in enumerate(data.get_kids()[:n_kids]):
            print 'Run for {0} actions, processing kid {1} out of {2}'.format(
                truncate, ki + 1, n_kids)
            if data.get_kid_nactions(kid) < truncate:
                continue

            #get kid's action sequence
            kidseq = data.data[kid][:truncate]
            #keg, kents=entropy_gains.ave_theory_expected_entropy_gain(kidseq)
            #keg=entropy_gains.theory_expected_entropy_gain(kidseq[-1].action,kidseq[:-1])
            keg = entropy_gains.theory_expected_final_entropy(
                kidseq[-1].action, kidseq[:-1])

            #print 'kid {0} entropies: {1}'.format(k,kents)

            #compute optimal choice entropy gain with kid's action sequence
            tl = learners.TheoryLearner()
            tlaction = tl.choose_action(kidseq[:truncate - 1])
            tlactions.append(tlaction)
            yokedseq = kidseq[:-1] + [
                Datapoint.Datapoint(tlaction, False)
            ]  #this False is generic, shouldn't be taken into account
            #tleg, tlents=entropy_gains.ave_theory_expected_entropy_gain(yokedseq)
            #tleg=entropy_gains.theory_expected_entropy_gain(tlaction, kidseq[:-1])
            tleg = entropy_gains.theory_expected_final_entropy(
                tlaction, kidseq[:-1])

            #print tlents

            reg = 0
            rlactions.append([])
            for r in range(n_r_random):
                rl = learners.RandomLearner()
                #rseq=rl.play(truncate)
                rlaction = rl.choose_action(kidseq[:truncate - 1])
                rlactions[k].append(rlaction)
                yokedseqr = kidseq[:-1] + [
                    Datapoint.Datapoint(rlaction, False)
                ]  #this False is generic, shouldn't be taken into account
                #reg+=entropy_gains.ave_theory_expected_entropy_gain(yokedseqr)[0]
                #reg+=entropy_gains.theory_expected_entropy_gain(rlaction, kidseq[:-1])
                reg += entropy_gains.theory_expected_final_entropy(
                    rlaction, kidseq[:-1])

            reg /= n_r_random

            eig[k, 0] = tleg
            eig[k, 1] = reg
            eig[k, 2] = keg
            #print 'k: {0}, r:{1}, t:{2}'.format(keg, reg, tleg)
            k += 1

    elif player == 'jointfull':

        n_long_kids=sum([data.get_kid_nactions(kid)>=truncate \
             for kid in data.get_kids()])
        eig = np.zeros((n_long_kids, 3))
        tlactions = []
        rlactions = []

        k = 0
        for ki, kid in enumerate(data.get_kids()[:n_kids]):
            if data.get_kid_nactions(kid) < truncate:
                continue

            #get kid's action sequence
            kidseq = data.data[kid][:truncate]
            #keg, kents=entropy_gains.ave_theory_expected_entropy_gain(kidseq)
            #keg=entropy_gains.theory_expected_entropy_gain(kidseq[-1].action,kidseq[:-1])
            keg = entropy_gains.joint_expected_final_entropy(
                kidseq[-1].action, kidseq[:-1])

            #print 'kid {0} entropies: {1}'.format(k,kents)

            #compute optimal choice entropy gain with kid's action sequence
            tl = learners.JointLearner()
            tlaction = tl.choose_action(kidseq[:truncate - 1])
            tlactions.append(tlaction)
            yokedseq = kidseq[:-1] + [
                Datapoint.Datapoint(tlaction, False)
            ]  #this False is generic, shouldn't be taken into account
            #tleg, tlents=entropy_gains.ave_theory_expected_entropy_gain(yokedseq)
            #tleg=entropy_gains.theory_expected_entropy_gain(tlaction, kidseq[:-1])
            tleg = entropy_gains.joint_expected_final_entropy(
                tlaction, kidseq[:-1])

            #print tlents

            reg = 0
            rlactions.append([])
            for r in range(n_r_random):
                rl = learners.RandomLearner()
                #rseq=rl.play(truncate)
                rlaction = rl.choose_action(kidseq[:truncate - 1])
                rlactions[k].append(rlaction)
                yokedseqr = kidseq[:-1] + [
                    Datapoint.Datapoint(rlaction, False)
                ]  #this False is generic, shouldn't be taken into account
                #reg+=entropy_gains.ave_theory_expected_entropy_gain(yokedseqr)[0]
                #reg+=entropy_gains.theory_expected_entropy_gain(rlaction, kidseq[:-1])
                reg += entropy_gains.joint_expected_final_entropy(
                    rlaction, kidseq[:-1])

            reg /= n_r_random

            eig[k, 0] = tleg
            eig[k, 1] = reg
            eig[k, 2] = keg
            #print 'k: {0}, r:{1}, t:{2}'.format(keg, reg, tleg)
            k += 1

    elif player == 'hypfull':

        n_long_kids=sum([data.get_kid_nactions(kid)>=truncate \
             for kid in data.get_kids()])
        eig = np.zeros((n_long_kids, 3))
        tlactions = []
        rlactions = []

        k = 0
        for ki, kid in enumerate(data.get_kids()[:n_kids]):
            if data.get_kid_nactions(kid) < truncate:
                continue

            #get kid's action sequence
            kidseq = data.data[kid][:truncate]
            #keg, kents=entropy_gains.ave_theory_expected_entropy_gain(kidseq)
            #keg=entropy_gains.theory_expected_entropy_gain(kidseq[-1].action,kidseq[:-1])
            keg = entropy_gains.hypotheses_expected_final_entropy(
                kidseq[-1].action, kidseq[:-1])

            #print 'kid {0} entropies: {1}'.format(k,kents)

            #compute optimal choice entropy gain with kid's action sequence
            tl = learners.HypothesesLearner()
            tlaction = tl.choose_action(kidseq[:truncate - 1])
            tlactions.append(tlaction)
            yokedseq = kidseq[:-1] + [
                Datapoint.Datapoint(tlaction, False)
            ]  #this False is generic, shouldn't be taken into account
            #tleg, tlents=entropy_gains.ave_theory_expected_entropy_gain(yokedseq)
            #tleg=entropy_gains.theory_expected_entropy_gain(tlaction, kidseq[:-1])
            tleg = entropy_gains.hypotheses_expected_final_entropy(
                tlaction, kidseq[:-1])

            #print tlents

            reg = 0
            rlactions.append([])
            for r in range(n_r_random):
                rl = learners.RandomLearner()
                #rseq=rl.play(truncate)
                rlaction = rl.choose_action(kidseq[:truncate - 1])
                rlactions[k].append(rlaction)
                yokedseqr = kidseq[:-1] + [
                    Datapoint.Datapoint(rlaction, False)
                ]  #this False is generic, shouldn't be taken into account
                #reg+=entropy_gains.ave_theory_expected_entropy_gain(yokedseqr)[0]
                #reg+=entropy_gains.theory_expected_entropy_gain(rlaction, kidseq[:-1])
                reg += entropy_gains.hypotheses_expected_final_entropy(
                    rlaction, kidseq[:-1])

            reg /= n_r_random

            eig[k, 0] = tleg
            eig[k, 1] = reg
            eig[k, 2] = keg
            #print 'k: {0}, r:{1}, t:{2}'.format(keg, reg, tleg)
            k += 1

    if player in ['random', 'theory', 'kids', 'adults']:
        filename = parameters.output_directory + 'out-' + player + '-' + str(
            truncate) + '_tru-' + str(n_r) + '_real.txt'
        np.savetxt(filename, eg)

    if player in ['random', 'theory']:
        filenameall = parameters.output_directory + 'all-' + player + '-' + str(
            truncate) + '_tru-' + str(n_r) + '_real.txt'
        np.savetxt(filenameall, egall)

    if player in ['theoryfull', 'jointfull', 'hypfull']:
        filename=parameters.output_directory+player+'-'+str(truncate)+'_tru-'\
          +str(n_r_random)+'_rreal.txt'
        np.savetxt(filename, eig)

        with open(parameters.output_directory+player+'-modelactions-'+str(truncate)+'_tru-'+\
         str(n_r_random)+'_rreal.txt','w') as f:
            for kact in tlactions:
                f.write(str(kact) + '\n')

        with open(parameters.output_directory+player+'-randomactions-'+str(truncate)+'_tru-'+\
         str(n_r_random)+'_rreal.txt','w') as f:
            for kact in rlactions:
                f.write(str(kact) + '\n')

    print 'time elapsed for run {0}: {1:.0f} s'.format(
        filename,
        time.clock() - starttime)
Пример #8
0
def main(player, n):

    #random.seed(0)
    starttime = time.clock()
    data = Data.Data()
    data.read(astext=False)
    n_kids = parameters.n_kids
    truncate = int(n)
    #n_r_theo=parameters.n_r_theo
    n_r_random = parameters.n_r_random
    epsilons = [0.001, 0.005, 0.01, 0.05, 0.1, 0.25]
    epsilons = [0.25]  #001]
    #epsilons=[0.002, 0.003, 0.004, 0.006, 0.007, 0.008, 0.009]
    #epsilons=[0.006]

    for epsilon in epsilons:
        entropy_gains.model.epsilon = epsilon
        #eg=np.zeros(len(data.get_kids()[:n_kids]))

        if player in ['theory', 'joint', 'hypotheses']:

            n_long_kids=sum([data.get_kid_nactions(kid)>=truncate \
                 for kid in data.get_kids()])
            eig = np.zeros((n_long_kids, 3))
            olactions = []
            rlactions = []

            k = 0
            for ki, kid in enumerate(data.get_kids()[:n_kids]):
                #for ki,kid in enumerate(data.get_kids()[5:6]):
                if data.get_kid_nactions(kid) < truncate:
                    continue

                #get kid's action sequence
                kidseq = data.data[kid][:truncate]
                keg = entropy_gains.expected_final_entropy(
                    player, kidseq[-1].action, kidseq[:-1])

                #compute optimal choice entropy gain with kid's action sequence
                if player == 'theory':
                    ol = learners.TheoryLearner()
                elif player == 'joint':
                    ol = learners.JointLearner()
                elif player == 'hypotheses':
                    ol = learners.HypothesesLearner()

                olaction = ol.choose_action(kidseq[:truncate - 1])
                olactions.append(olaction)
                yokedseq = kidseq[:-1] + [
                    Datapoint.Datapoint(olaction, False)
                ]  #this False is generic, shouldn't be taken into account
                oleg = entropy_gains.expected_final_entropy(
                    player, olaction, kidseq[:-1])

                reg = 0
                rlactions.append([])
                for r in range(n_r_random):
                    rl = learners.RandomLearner()
                    rlaction = rl.choose_action(kidseq[:truncate - 1])
                    rlactions[k].append(rlaction)
                    yokedseqr = kidseq[:-1] + [
                        Datapoint.Datapoint(rlaction, False)
                    ]  #this False is generic, shouldn't be taken into account
                    reg += entropy_gains.expected_final_entropy(
                        player, rlaction, kidseq[:-1])

                reg /= n_r_random

                eig[k, 0] = oleg
                eig[k, 1] = reg
                eig[k, 2] = keg
                k += 1

        # save this epsilons
        filename=parameters.output_directory+player+'-'+str(truncate)+'_tru-'\
           +str(n_r_random)+'_rreal-'+str(epsilon)+'.txt'
        np.savetxt(filename, eig)

        with open(parameters.output_directory+player+'-modelactions-'+str(truncate)+'_tru-'+\
         str(n_r_random)+'_rreal-'+str(epsilon)+'.txt','w') as f:
            for kact in olactions:
                f.write(str(kact) + '\n')

        with open(parameters.output_directory+player+'-randomactions-'+str(truncate)+'_tru-'+\
         str(n_r_random)+'_rreal-'+str(epsilon)+'.txt','w') as f:
            for kact in rlactions:
                f.write(str(kact) + '\n')
Пример #9
0
import low_model as model
import world
import Datapoint

m0, m1, m2 = world.machines
t0, t1, t2 = world.available_toys
model.epsilon = 1e-3
# print m0
# for h in model.singleh_space:
# 	print 'hyp: {0}, p: {1}'.format(h, model.p_hypothesis(h,m))

d0 = [Datapoint.Datapoint((t0, m1), True)]
n1 = 0
n2 = 0
p1s = []
p2s = []
#print t1, m0
print world.machines
print world.available_toys
for h1 in model.singleh_space:
    p1 = 0
    p2 = 0

    for h2 in model.singleh_space:
        for h3 in model.singleh_space:
            p1 += model.p_hypotheses_data((h1, h2, h3), [])
            p2 += model.p_hypotheses_data((h1, h2, h3), d0)
    #print 'hyp: {0}, p: {1}, ppost: {2}'.format(h1, p1,p2)

    n1 += p1
    n2 += p2