Esempio n. 1
0
def set_model(sem_input_file, sem_name, sem_input_macro = True, semantics_name='TCG_semantics_main', 
              grammar_name='TCG_grammar_VB_main', model_params = {}):
    """
    Sets up a TCG production model.
    
    Args:
        - sem_input_file (STR): Semantic input file name
        - sem_name (STR): Semantic input name
        - sem_input_macro (BOOL): True is the input is an ISRF macro
        - semantics_name (STR): Name of the semantic file containing the perceptual, world, and conceptualization knowledge.
        - grammar_name (STR): Name of the grammar file to use.
        - model_prams (dict): Dictionary defining the model parameters (if different than default)
    
    Returns: (model, semantic input genereator)    
    
    """
    SEM_INPUT_PATH = './data/sem_inputs/'
    
    model = TCG_production_system(grammar_name=grammar_name, semantics_name=semantics_name)
    if model_params:
        model.update_params(model_params)
    
    # Set up semantic input generator    
    conceptLTM = model.schemas['Concept_LTM']
    if not(sem_input_macro):
        sem_inputs = TCG_LOADER.load_sem_input(sem_input_file, SEM_INPUT_PATH)
        sem_input = {sem_name:sem_inputs[sem_name]}
        sem_gen = ls.SEM_GENERATOR(sem_input, conceptLTM, speed_param=1)
    if sem_input_macro:
        sem_inputs = TCG_LOADER.load_sem_macro(sem_name, sem_input_file, SEM_INPUT_PATH)
        sem_gen = ls.SEM_GENERATOR(sem_inputs, conceptLTM, speed_param=1)
    
    return (model, sem_gen)
def set_inputs(model, input_name, sem_input_file='diagnostic.json', sem_input_macro=False, speed_param=10):
    """
    Sets up a TCG ISRF inputs generator for TCG production model.
    
    Args:
        - model (): model to which the inputs will be sent
        - input_name (STR): name of the input to be used.
        - sem_input_file (STR): Semantic input file name. For non-macro input, set to 'ALL' to load all inputs from file.
        - sem_input_macro (BOOL): True is the input is an ISRF macro.
        - speed_param (INT): multiplier of the rate defined in the ISRF input (by default the ISFR rate is 1.)
    
    Returns:
        - input SEM_GENERATOR object.
    """
    SEM_INPUT_PATH = './data/sem_inputs/'
    
    
    conceptLTM = model.schemas['Concept_LTM']
    if not(sem_input_macro):
        sem_inputs = TCG_LOADER.load_sem_input(sem_input_file, SEM_INPUT_PATH)
        if input_name == 'ALL':
            sem_gen = ls.SEM_GENERATOR(sem_inputs, conceptLTM, speed_param=speed_param, is_macro=sem_input_macro)
            sem_gen.ground_truths = TCG_LOADER.load_ground_truths(sem_input_file, SEM_INPUT_PATH)
        else:
            sem_input = {input_name:sem_inputs[input_name]}
            sem_gen = ls.SEM_GENERATOR(sem_input, conceptLTM, speed_param=speed_param,is_macro=sem_input_macro)
            ground_truths = TCG_LOADER.load_ground_truths(sem_input_file, SEM_INPUT_PATH)
            sem_gen.ground_truths = ground_truths.get(input_name, None)
    if sem_input_macro:
        sem_inputs = TCG_LOADER.load_sem_macro(input_name, sem_input_file, SEM_INPUT_PATH)
        sem_gen = ls.SEM_GENERATOR(sem_inputs, conceptLTM, speed_param=speed_param, is_macro=sem_input_macro)
        ground_truths = TCG_LOADER.load_ground_truths(sem_input_file, SEM_INPUT_PATH)
        sem_gen.ground_truths = ground_truths.get(input_name, None)
    
    return sem_gen
Esempio n. 3
0
def test_run(seed=None):
    """
    Test run function for the production model.
    """
    if not(seed): # Quick trick so that I can have access to the seed used to run the simulation.
        random.seed(seed)
        seed = random.randint(0,10**9)
        print "seed = %i" %seed
        
    random.seed(seed)
    SEM_INPUT = 'sem_inputs.json' # semantic input files (no macros)
    INPUT_NAME = 'blue_woman_kick_man' # Name of the input to use.
    
    FOLDER = './tmp/TEST_%s_%s/' %(INPUT_NAME, str(seed)) # Folder where the simulation results will be saved.
    
    language_system_P = TCG_production_system(grammar_name='TCG_grammar_VB_main', semantics_name='TCG_semantics_main') # Create model
    
    # Set up semantic input generator    
    conceptLTM = language_system_P.schemas['Concept_LTM']
    sem_inputs = TCG_LOADER.load_sem_input(SEM_INPUT, "./data/sem_inputs/")   
    speed_param = 1
    sem_gen = ls.SEM_GENERATOR(sem_inputs, conceptLTM, speed_param)
 
    generator = sem_gen.sem_generator(INPUT_NAME)
    
    (sem_insts, next_time, prop) = generator.next() #Getting the initial input.
    
    # Test paramters
    language_system_P.params['Control']['task']['start_produce'] = 3100
    language_system_P.params['Control']['task']['time_pressure'] = 200
    language_system_P.params['Grammatical_WM_P']['C2']['confidence_threshold'] = 0.3
    
    set_up_time = -10 # Starts negative to let the system settle before it receives its first input. Also, easier to handle input arriving at t=0.
    max_time = 3000
    save_states = [30, 700, 2000]
    
    flag = False
    for t in range(set_up_time, max_time):
        if next_time != None and t>next_time:
            (sem_insts, next_time, prop) = generator.next()
            print "t:%i, sem: %s (prop: %s)" %(t, ', '.join([inst.name for inst in sem_insts]), prop)
            language_system_P.set_input(sem_insts)
        language_system_P.update()
        output = language_system_P.get_output()
        if not(language_system_P.schemas['Grammatical_WM_P'].comp_links) and t>10 and not(flag):
            print "t:%i, Competition done" % t
            flag = True
            TCG_VIEWER.display_lingWM_state(language_system_P.schemas['Semantic_WM'], language_system_P.schemas['Grammatical_WM_P'], concise=True, folder = FOLDER)
            language_system_P.params['Control']['task']['start_produce'] = t + 10
        if output['Utter']:
            print "t:%i, '%s'" %(t, output['Utter'])
        if t - set_up_time in save_states:
            TCG_VIEWER.display_lingWM_state(language_system_P.schemas['Semantic_WM'], language_system_P.schemas['Grammatical_WM_P'], concise=True, folder = FOLDER)
    
    language_system_P.schemas['Semantic_WM'].show_SemRep()
    language_system_P.schemas['Grammatical_WM_P'].show_dynamics(inst_act=True, WM_act=False, c2_levels=True,  c2_network=False)
    language_system_P.save_sim(FOLDER, 'test_language_output.json')
    
    return language_system_P
Esempio n. 4
0
def run_model(seed=None):
    """
    """
    SEM_INPUT = 'sem_inputs.json'
    INPUT_NAME = 'kick_static_focus_agent'
    
    language_system_P = TCG_production_system(grammar_name='TCG_grammar_VB_main', semantics_name='TCG_semantics_main')
    
    # Set up semantic input generator    
    conceptLTM = language_system_P.schemas['Concept_LTM']
    sem_inputs = TCG_LOADER.load_sem_input(SEM_INPUT, "./data/sem_inputs/")   
    speed_param = 1
    sem_gen = ls.SEM_GENERATOR(sem_inputs, conceptLTM, speed_param)
 
    generator = sem_gen.sem_generator(INPUT_NAME)
    
    (sem_insts, next_time, prop) = generator.next()
    
    # Test paramters
    language_system_P.params['Control']['task']['start_produce'] = 400
    language_system_P.params['Control']['task']['time_pressure'] = 200
    language_system_P.params['Grammatical_WM_P']['C2']['confidence_threshold'] = 0.3
    
    set_up_time = -10 # Starts negative to let the system settle before it receives its first input. Also, easier to handle input arriving at t=0.
    max_time = 900
    out_data = []
    for t in range(set_up_time, max_time):
        if next_time != None and t>next_time:
            (sem_insts, next_time, prop) = generator.next()
            language_system_P.set_input(sem_insts)
        language_system_P.update()
        # Store output
        output = language_system_P.get_output()
        if output['Grammatical_WM_P']:
            out_data.append(output['Grammatical_WM_P'])
        if output['Utter']:
            print "t:%i, '%s'" %(t, output['Utter'])
    
    # Output analysis
    res = prod_analyses(out_data)
    return res