Beispiel #1
0
def run_trial(network, start, tally, correct):
    # what kind of trial is will determine what kind of objects to make for child and experimenter (experimenter only needs two).

    # select reps for child objects.

    # select reps for experimenter objects.

    # try to map child and experimenter objects.

    # make some objects for child, two objects are the same in a dimension.

    obj_analog, network.memory = add_objects(network.memory,
                                             ['obj1', 'obj2', 'obj3'])
    # select a dimension.
    dimension = random.sample(['x_ext', 'y_ext', 'total_ext'], 1)
    dimension.append(random.sample(['same'], 1)[0])
    # get a prop.
    prop = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    # select the objects.
    objs = select_objs(obj_analog)
    # apply prop to objects.
    obj_analog, network.memory = apply_preds(obj_analog, objs, prop,
                                             dimension[0], network.memory)
    # sample a prop from LTM to enter recipient; call it sampled_analog.
    sampled_rep = sample_LTM_rep(network.memory,
                                 dimension,
                                 start,
                                 threshold=0.95)
    sampled_analog = sampled_rep[0].myanalog
    # put obj_analog in the driver and sampled_analog in the recipient.
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(obj_analog), 'analog',
        'driver')
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(sampled_analog), 'analog',
        'recipient')
    # clear and update the contents of driver and recipient.
    network.memory = basicRunDORA.clearDriverSet(network.memory)
    network.memory = basicRunDORA.clearRecipientSet(network.memory)
    network.memory = basicRunDORA.findDriverRecipient(network.memory)
    # do mapping and print results.
    network.do_map()
    basicRunDORA.DORA_GUI.term_map_display(network.memory)  # show mappings.
    # clear mapping result.
    network.memory = basicRunDORA.reset_inferences(network.memory)
    network.memory = basicRunDORA.reset_maker_made_units(network.memory)
    network.memory = basicRunDORA.reset_mappings(network.memory)
    network.memory = basicRunDORA.update_Names_nil(network.memory)
    network.memory = basicRunDORA.indexMemory(network.memory)
    network.memory = basicRunDORA.initialize_memorySet(network.memory)
    #returns.
    return tally, correct
Beispiel #2
0
if simType == 'sym_file':
    symstring = ''
    for line in f:
        symstring += line
    do_run = True
    try:
        exec symstring
    except:
        do_run = False
        print '\nSorry, you have a badly formated sym file. \nI will return to the MainMenu.'

memory = buildNetwork.initializeMemorySet()
# interpret the sym file.
mysym = buildNetwork.interpretSymfile(symProps)
print 'Sym file loaded!'
# build the network object with memory.
memory = basicRunDORA.dataTypes.memorySet()
memory = buildNetwork.buildTheNetwork(mysym[0], memory)
network = basicRunDORA.runDORA(memory, parameters)


def clear_mem():
    network.memory = basicRunDORA.clearDriverSet(network.memory)
    network.memory = basicRunDORA.clearRecipientSet(network.memory)


clear_mem()
network.memory = basicRunDORA.findDriverRecipient(network.memory)
network.do_retrieval()
network.do_map()
Beispiel #3
0
def run_trial(network, start, tally, correct, trial_type):
    # create reps; the kind of trial will determine what kind of objects to make (types are 'whole', 'feature', 'relation').
    obj_analogs, network.memory = add_objects(network.memory, trial_type)
    # child_analog is the last analog of obj_analogs.
    child_analog = obj_analogs[2]
    # if the trial type is whole, put child analog in driver and one of experimenter analogs in recipient, and try to map.
    if trial_type == 'whole':
        # place child objects in driver, and one experimenter object in recipient, and map.
        exp_analog = random.choice(obj_analogs[:2])
        network.memory = basicRunDORA.add_tokens_to_set(
            network.memory, network.memory.analogs.index(child_analog),
            'analog', 'driver')
        network.memory = basicRunDORA.add_tokens_to_set(
            network.memory, network.memory.analogs.index(exp_analog), 'analog',
            'recipient')
        # clear and update the contents of driver and recipient.
        network.memory = basicRunDORA.clearDriverSet(network.memory)
        network.memory = basicRunDORA.clearRecipientSet(network.memory)
        network.memory = basicRunDORA.findDriverRecipient(network.memory)
        # do mapping and print results.
        network.do_map()
        basicRunDORA.DORA_GUI.term_map_display(
            network.memory)  # show mappings.
        # clear mapping result.
        network.memory = basicRunDORA.reset_inferences(network.memory)
        network.memory = basicRunDORA.reset_maker_made_units(network.memory)
        network.memory = basicRunDORA.reset_mappings(network.memory)
        network.memory = basicRunDORA.update_Names_nil(network.memory)
        network.memory = basicRunDORA.indexMemory(network.memory)
        network.memory = basicRunDORA.initialize_memorySet(network.memory)
    else:  # for 'feature' and 'relation' trials.
        # find the dimension experimenter objects match on by comparing experimenter sets.
        dimension = None
        Exp1 = obj_analogs[0]
        Exp2 = obj_analogs[1]
        for link in Exp1.myPOs[0].mySemantics:
            found_flag = False
            if link.mySemantic.ont_status == 'value':
                dim = link.mySemantic.dimension
                val = link.mySemantic.amount
                for link2 in Exp1.myPOs[1].mySemantics:
                    if link2.mySemantic.ont_status == 'value' and link2.mySemantic.dimension == dim and link2.mySemantic.amount == val:
                        found_flag = True
                        dimension = dim
                        break
            if found_flag:
                break
        # sample experimenter objects to use for mapping.
        exp_analog = random.choice(obj_analogs[:2])
        # sample two props from LTM that match dimension.
        propExp = sample_LTM_rep(network.memory,
                                 dimension,
                                 start,
                                 threshold=0.95)
        propChild = sample_LTM_rep(network.memory,
                                   dimension,
                                   start,
                                   threshold=0.95)
        # apply propExp to experimenter objects.
        objsExp = select_objs(exp_analog)
        exp_analog, network.memory = apply_preds(exp_analog, objsExp, propExp,
                                                 dimension, network.memory)
        # apply propChild to child objects.
        # first, find any child objects that match on the dimension and put them in objsChild.
        val0 = [
            x.mySemantic.amount for x in child_analog.myPOs[0].mySemantics
            if x.mySemantic.dimension == dimension
            and x.mySemantic.ont_status == 'value'
        ]
        val1 = [
            x.mySemantic.amount for x in child_analog.myPOs[1].mySemantics
            if x.mySemantic.dimension == dimension
            and x.mySemantic.ont_status == 'value'
        ]
        val2 = [
            x.mySemantic.amount for x in child_analog.myPOs[2].mySemantics
            if x.mySemantic.dimension == dimension
            and x.mySemantic.ont_status == 'value'
        ]
        if val0 == val1:
            objsChild = [child_analog.myPOs[0], child_analog.myPOs[1]]
        elif val1 == val2:
            objsChild = [child_analog.myPOs[1], child_analog.myPOs[2]]
        elif val0 == val2:
            objsChild = [child_analog.myPOs[0], child_analog.myPOs[2]]
        # apply propChild to child objects.
        child_analog, network.memory = apply_preds(child_analog, objsChild,
                                                   propChild, dimension,
                                                   network.memory)
        # child objects in driver, experimenter in recipient, and map.
        network.memory = basicRunDORA.add_tokens_to_set(
            network.memory, network.memory.analogs.index(child_analog),
            'analog', 'driver')
        network.memory = basicRunDORA.add_tokens_to_set(
            network.memory, network.memory.analogs.index(exp_analog), 'analog',
            'recipient')
        # clear and update the contents of driver and recipient.
        network.memory = basicRunDORA.clearDriverSet(network.memory)
        network.memory = basicRunDORA.clearRecipientSet(network.memory)
        network.memory = basicRunDORA.findDriverRecipient(network.memory)
        # do mapping and print results.
        network.do_map()
        basicRunDORA.DORA_GUI.term_map_display(
            network.memory)  # show mappings.
        # clear mapping result.
        network.memory = basicRunDORA.reset_inferences(network.memory)
        network.memory = basicRunDORA.reset_maker_made_units(network.memory)
        network.memory = basicRunDORA.reset_mappings(network.memory)
        network.memory = basicRunDORA.update_Names_nil(network.memory)
        network.memory = basicRunDORA.indexMemory(network.memory)
        network.memory = basicRunDORA.initialize_memorySet(network.memory)
    #returns.
    return tally, correct
Beispiel #4
0
def run_NBtrial(network, start, tally, correct):
    # make some objects.
    obj_analog, network.memory = add_objects(network.memory,
                                             ['tree1', 'tree2'])
    # select a dimension.
    dimension = random.sample(['x_ext', 'y_ext', 'total_ext'], 1)
    dimension.append(random.sample(['more', 'less'], 1)[0])
    # get a prop.
    prop = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    correct = 0
    if len(prop) > 0:
        if prop[0].my_type == 'P':
            tally['P'] += 1
            for myRB in prop[0].myRBs:
                my_semantics = [
                    link.mySemantic.name for link in myRB.myPred[0].mySemantics
                ]
                if dimension[0] in my_semantics and dimension[
                        1] in my_semantics:
                    correct = 1
            print('P', correct)
            if correct == 0:
                pdb.set_trace()
        elif prop[0].my_type == 'RB':
            tally['RB'] += 1
            my_semantics = [
                link.mySemantic.name for link in prop[0].myPred[0].mySemantics
            ]
            if dimension[0] in my_semantics and dimension[1] in my_semantics:
                correct = 1
            print('RB', correct)
        else:
            tally['PO'] += 1
            print('guess')
        if correct == 0:
            rand_num = random.random()
            if rand_num > .5:
                correct = 1
    else:
        tally['PO'] += 1
        rand_num = random.random()
        if rand_num > .5:
            correct = 1
        else:
            correct = 0
    # select the objects.
    objs = select_objs(obj_analog)
    # apply prop to objects.
    obj_analog, network.memory = apply_preds(obj_analog, objs, prop,
                                             dimension[0], network.memory)
    # sample a prop from LTM to enter recipient; call it sampled_analog.
    sampled_rep = sample_LTM_rep(network.memory,
                                 dimension,
                                 start,
                                 threshold=0.95)
    sampled_analog = sampled_rep[0].myanalog
    # put obj_analog in the driver and sampled_analog in the recipient.
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(obj_analog), 'analog',
        'driver')
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(sampled_analog), 'analog',
        'recipient')
    # clear and update the contents of driver and recipient.
    network.memory = basicRunDORA.clearDriverSet(network.memory)
    network.memory = basicRunDORA.clearRecipientSet(network.memory)
    network.memory = basicRunDORA.findDriverRecipient(network.memory)
    # do mapping and print results.
    network.do_map()
    basicRunDORA.DORA_GUI.term_map_display(network.memory)  # show mappings.
    # clear mapping result.
    network.memory = basicRunDORA.reset_inferences(network.memory)
    network.memory = basicRunDORA.reset_maker_made_units(network.memory)
    network.memory = basicRunDORA.reset_mappings(network.memory)
    network.memory = basicRunDORA.update_Names_nil(network.memory)
    network.memory = basicRunDORA.indexMemory(network.memory)
    network.memory = basicRunDORA.initialize_memorySet(network.memory)
    #returns.
    return tally, correct
Beispiel #5
0
def run_dora(parameters):
    network, memory, mapping_adj = init_network(parameters)
    precisions, recalls, fscores = [], [], []
    for cycle in range(parameters['run_cyles']):
        print('\n\n************************* Cycle: ' + str(cycle + 1))
        # pdb.set_trace()
        # clear driver and recipient
        network.memory = basicRunDORA.clearDriverSet(network.memory)
        network.memory = basicRunDORA.clearRecipientSet(network.memory)
        # choose random analog from memory
        analog = random.choice(
            network.memory.analogs)  # currently only one analog
        # choose random propositions from memory

        # put the analog in the driver
        analog_num = network.memory.analogs.index(analog)
        network.memory = basicRunDORA.add_tokens_to_set(
            network.memory, analog_num, 'analog', 'driver')
        # retrieve analogs similar to one in the driver from LTM
        network.memory = basicRunDORA.findDriverRecipient(network.memory)
        network.do_retrieval()
        network.memory = basicRunDORA.findDriverRecipient(network.memory)

        # print('In driver : ' + network.memory.driver.Ps[0].name[1])
        # if len(network.memory.recipient.Ps) > 0:
        #    print('In recipient : ' + network.memory.recipient.Ps[0].name[1])
        #    if network.memory.recipient.Ps[0].name[1] == '3':
        #        network.do_map()
        #        print('\n\nMappings:\n')
        #        basicRunDORA.DORA_GUI.term_map_display(network.memory, only_Ps=True, only_max=False)

        if len(network.memory.recipient.Ps) > 0:
            print
            'retrieval successful'
            # map the analogs in driver and recipient
            network.do_map()
        # display ALL mappings
        # display mappings
        print('\n\nMappings:\n')
        basicRunDORA.DORA_GUI.term_map_display(network.memory,
                                               only_Ps=False,
                                               only_max=False)
        # store all mappings :
        for myP in network.memory.driver.Ps:
            for mapping in myP.mappingConnections:
                map_weight = mapping.weight
                map_unit = mapping.recipientToken
                mapping_adj[network.memory.Ps.index(myP),
                            network.memory.Ps.index(map_unit)] += map_weight
        # append rmse to list to keep track of RMSE evolution
        temp_mapping_adj = mapping_adj
        for ind in range(temp_mapping_adj.shape[0]):
            if sum(temp_mapping_adj[ind, :]) > 0:
                temp_mapping_adj[ind, :] = temp_mapping_adj[ind, :] / sum(
                    temp_mapping_adj[ind, :])
        prec, rec, fsc = get_metrics(temp_mapping_adj)
        precisions.append(prec)
        recalls.append(rec)
        fscores.append(fsc)

    # normalise mappings
    for ind in range(mapping_adj.shape[0]):
        if sum(mapping_adj[ind, :]) > 0:
            mapping_adj[ind, :] = mapping_adj[ind, :] / sum(
                mapping_adj[ind, :])
    return precisions, recalls, fscores, mapping_adj
Beispiel #6
0
def run_RGtrial(network, start, tally, correct):
    # make some objects for child.
    obj_analog1, network.memory = add_objects(network.memory,
                                              ['obj1', 'obj2', 'obj3'])
    # make some objects for experimenter.
    obj_analog2, network.memory = add_objects(network.memory,
                                              ['obj1', 'obj2', 'obj3'])
    # select a dimension.
    dimension = random.sample(['x_ext', 'y_ext', 'total_ext'], 1)
    dimension.append(random.sample(['more', 'less'], 1)[0])
    # get two props.
    prop1 = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    prop2 = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    prop3 = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    prop4 = sample_LTM_rep(network.memory, dimension, start, threshold=0.95)
    # apply prop to objects.
    obj_analog1, network.memory = apply_preds(
        obj_analog1, [obj_analog1.myPOs[0], obj_analog1.myPOs[1]], prop1,
        dimension[0], network.memory)
    obj_analog1, network.memory = apply_preds(
        obj_analog1, [obj_analog1.myPOs[1], obj_analog1.myPOs[2]], prop2,
        dimension[0], network.memory)
    obj_analog2, network.memory = apply_preds(
        obj_analog2, [obj_analog2.myPOs[0], obj_analog2.myPOs[1]], prop3,
        dimension[0], network.memory)
    obj_analog2, network.memory = apply_preds(
        obj_analog2, [obj_analog2.myPOs[1], obj_analog2.myPOs[2]], prop4,
        dimension[0], network.memory)
    # put obj_analog1 in the driver and obj_analog2 in the recipient.
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(obj_analog1), 'analog',
        'driver')
    network.memory = basicRunDORA.add_tokens_to_set(
        network.memory, network.memory.analogs.index(obj_analog2), 'analog',
        'recipient')
    # clear and update the contents of driver and recipient.
    network.memory = basicRunDORA.clearDriverSet(network.memory)
    network.memory = basicRunDORA.clearRecipientSet(network.memory)
    network.memory = basicRunDORA.findDriverRecipient(network.memory)
    # do mapping and print results.
    network.do_map()
    basicRunDORA.DORA_GUI.term_map_display(network.memory)  # show mappings.
    # order the recipient and driver objects.
    driver_objs = []
    for myPO in network.memory.driver.POs:
        if myPO.predOrObj == 0:
            driver_objs.append(myPO)
    recipient_objs = []
    for myPO in network.memory.recipient.POs:
        if myPO.predOrObj == 0:
            recipient_objs.append(myPO)
    # select an item from recipient as the sticker.
    sticker_index = random.sample([0, 1, 2], 1)
    sticker_index = sticker_index[0]
    # find recipient sticker max map unit.
    max_map = 0.0
    max_map_unit = None
    for mapping in recipient_objs[sticker_index].mappingConnections:
        if mapping.weight > max_map:
            max_map = mapping.weight
            max_map_unit = mapping.driverToken
    # check if maps to appropriate driver object and update correct variable.
    if max_map_unit is driver_objs[sticker_index]:
        correct = 1
    elif max_map_unit == None:
        rand_num = random.random()
        if rand_num < .34:
            correct = 1
        else:
            correct = 0
    else:
        correct = 0
    # clear mapping result.
    network.memory = basicRunDORA.reset_inferences(network.memory)
    network.memory = basicRunDORA.reset_maker_made_units(network.memory)
    network.memory = basicRunDORA.reset_mappings(network.memory)
    network.memory = basicRunDORA.update_Names_nil(network.memory)
    network.memory = basicRunDORA.indexMemory(network.memory)
    network.memory = basicRunDORA.initialize_memorySet(network.memory)
    #returns.
    return correct