示例#1
0
def run():
    env = gym.make('CartPole-v0')
    env._max_episode_steps = None

    state_size = env.observation_space.shape[0]
    action_size = env.action_space.n
    agent = HRL(state_size, action_size)

    horizon = 20

    for e in range(agent.episodes):
        state = torch.tensor(env.reset(), dtype=torch.float)
        goal = torch.tensor([0.0, 0.0, 0.0, 0.0], dtype=torch.float)
        score = 0

        # Rollout
        for t in range(horizon):
            score += 1
            env.render()
            action = agent.act(state, goal,
                               torch.tensor([horizon - t], dtype=torch.float))
            next_state, _, done, _ = env.step(action)
            agent.remember(state, action, next_state, None, None, done)
            agent.augment_goals(state, action, next_state, done)
            state = next_state
            if done:
                print("episode: {}/{}, score: {}, e: {:.2}".format(
                    e, agent.episodes, score, agent.epsilon))
                break

        # Perform optimization
        for _ in range(agent.n):
            agent.replay()
示例#2
0
def main():

    hrl = HRL(graphics='datapixx',
              inputs='responsepixx',
              photometer=None,
              wdth=WIDTH,
              hght=HEIGHT,
              bg=0.27,
              scrn=1,
              lut='lut.csv',
              db=False,
              fs=True)

    # loop over design
    # ================
    for trl in np.arange(start_trl, end_trl):
        run_trial(hrl, trl, start_trl,
                  end_trl)  # function that executes the experiment

    hrl.close()
    print "Session complete"
    rfl.close()
示例#3
0
    bfl = open('results/%s/mlds/%s_blocks_done.txt' %(vp_id, vp_id), 'a')
    if blocksdone == None:
        block_headers = ['number', 'block']
        bfl.write('\t'.join(block_headers)+'\n')
        bfl.flush()
        
    # Pass this to HRL if we want to use gamma correction.
    lut = 'lut.csv'   
    
    if inlab:
        ## create HRL object
        hrl = HRL(graphics='datapixx',
                  inputs='responsepixx',
                  photometer=None,
                  wdth=WIDTH,
                  hght=HEIGHT,
                  bg=0.27,
                  scrn=1,
                  lut=lut,
                  db = False,
                  fs=True)

    else: 
        hrl = HRL(graphics='gpu',
                  inputs='keyboard',
                  photometer=None,
                  wdth=WIDTH,
                  hght=HEIGHT,
                  bg=0.27,
                  scrn=1,
                  lut=lut,
                  db = True,
def main():

    debug = 0
    #print debug
    if debug:

        ### HRL Parameters for DESKTOP ###
        graphics = 'gpu'
        inputs = 'keyboard'

        # Screen GPU
        wdth = 800
        hght = 600
        fs = False  # fullscreen mode off
        scrn = 0  # screen number

    else:

        ### HRL Parameters for DATAPIXX ###
        graphics = 'datapixx'
        inputs = 'responsepixx'  # or 'responsepixx'

        wdth = 1024
        hght = 768
        #wdth = 1600
        #hght = 1200
        #wdth = 2048
        #hght = 1536
        fs = True  # fullscreen mode
        scrn = 1  # screen number

    #############################################
    hrl = HRL(graphics=graphics,
              inputs=inputs,
              lut=lut,
              photometer=None,
              wdth=wdth,
              hght=hght,
              bg=background,
              fs=fs,
              scrn=scrn)

    whlf = wdth / 2.0
    hhlf = hght / 2.0
    weht = wdth / 8.0
    heht = hght / 8.0

    # texture creation in buffer, input is numpy array [0-1]
    white = hrl.graphics.newTexture(np.array([[1]]))
    black = hrl.graphics.newTexture(np.array([[0]]))

    s = 150
    white.draw((whlf - s, hhlf - s), (s, s))
    black.draw((whlf, hhlf - s), (s, s))
    white.draw((whlf, hhlf), (s, s))
    black.draw((whlf - s, hhlf), (s, s))

    hrl.graphics.flip(clr=False)

    #######################################
    while True:
        #hrl.graphics.flip(clr=True)   # clr= True to clear buffer

        (btn, t1) = hrl.inputs.readButton()
        #print btn

        if btn == 'Space':
            break

    # closing window
    hrl.close()
def main():
    # determine design and result file name
    try:
        (design_fn, result_fn, completed_trials, total_trials,
         check_fn) = prepare_files()
    except EndExperiment:
        return 0
    result_headers = [
        'Trial', 'adaptor_type', 'coaxial_lum', 'test_lum', 'match_lum',
        'flank_lum', 'test_loc', 'grating_ori', 'response_time',
        'match_initial', 'adaptor_shift'
    ]
    global hrl
    hrl = HRL(graphics='datapixx',
              inputs='responsepixx',
              photometer=None,
              wdth=1024,
              hght=768,
              bg=0.5,
              dfl=design_fn,
              rfl=result_fn,
              rhds=result_headers,
              scrn=1,
              lut='lut0to88.csv',
              db=False,
              fs=True)

    # monkey patch to use non-blocking readButton function
    # should be removed once hrl.inputs.readButton is fixed
    hrl.inputs.readButton = lambda to: waitButton(hrl.datapixx, to)

    # set the bar width of the grating. this value determines all positions
    global bar_width
    bar_width = 38
    y_border = 768 // 2 - int(6 * bar_width)
    x_border = (1024 - 6 * bar_width) // 2
    # the coordinates of the four possible test check positions
    # test coordinates are (y,x) position within the grating array
    global test_coords
    test_coords = ((bar_width * 3, bar_width * 2), (bar_width * 3,
                                                    bar_width * 3),
                   (bar_width * 4, bar_width * 2), (bar_width * 4,
                                                    bar_width * 3))
    # match coordinates are (x,y) screen positions
    global match_coords
    match_coords = ((x_border + bar_width * 2, 384 + int(bar_width * 2)),
                    (x_border + bar_width * 3, 384 + int(bar_width * 2)),
                    (x_border + bar_width * 2, 384 + int(bar_width * 1)),
                    (x_border + bar_width * 3, 384 + int(bar_width * 1)))
    global match_bg_loc
    match_bg_loc = (x_border, 384)
    global stim_loc
    stim_loc = (x_border, y_border)

    # prepare fixation mark textures
    global fix_inner
    fix_inner = hrl.graphics.newTexture(np.ones((1, 1)) * .5, 'circle')
    global fix_outer
    fix_outer = hrl.graphics.newTexture(np.zeros((1, 1)), 'circle')

    # prepare confirmation texture
    global confirmation
    confirmation = hrl.graphics.newTexture(draw_text('Weiter?', bg=.5))

    # show instruction screen
    if completed_trials == 0:
        for i in range(6):
            instructions = plt.imread('instructions%d.png' % (i + 1))[..., 0]
            instructions = hrl.graphics.newTexture(instructions)
            instructions.draw((0, 0))
            hrl.graphics.flip(clr=True)
            btn = None
            while btn != 'Space':
                btn, _ = hrl.inputs.readButton(to=3600)

        # show test trials
        test_dsgn = [{
            'adaptor_type': 'none',
            'grating_ori': 'horizontal',
            'grating_vals': '0.45,0.55',
            'test_loc': '0',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'vertical',
            'grating_ori': 'horizontal',
            'grating_vals': '0.55,0.45',
            'test_loc': '1',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'horizontal',
            'grating_ori': 'horizontal',
            'grating_vals': '0.45,0.55',
            'test_loc': '2',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'vertical',
            'grating_ori': 'vertical',
            'grating_vals': '0.55,0.45',
            'test_loc': '2',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'none',
            'grating_ori': 'vertical',
            'grating_vals': '0.45,0.45',
            'test_loc': '1',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'vertical_shifted',
            'grating_ori': 'vertical',
            'grating_vals': '0.55,0.45',
            'test_loc': '2',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'none',
            'grating_ori': 'vertical',
            'grating_vals': '0.55,0.55',
            'test_loc': '1',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'horizontal_shifted',
            'grating_ori': 'vertical',
            'grating_vals': '0.45,0.55',
            'test_loc': '1',
            'test_lum': '0.5'
        }, {
            'adaptor_type': 'horizontal',
            'grating_ori': 'vertical',
            'grating_vals': '0.45,0.55',
            'test_loc': '3',
            'test_lum': '0.5'
        }]
        for dsgn in test_dsgn:
            run_trial(dsgn)

    # show experiment start confirmation
    hrl.graphics.flip(clr=True)
    lines = [
        u'Die Probedurchgänge sind fertig.',
        u'Wenn du bereit bist, drücke die mittlere Taste.', u' ',
        u'Wenn du noch Fragen hast, oder mehr Probedurchgänge',
        u'machen willst, wende dich an den Versuchsleiter.'
    ]
    for line_nr, line in enumerate(lines):
        textline = hrl.graphics.newTexture(draw_text(line, fontsize=36))
        textline.draw(((1024 - textline.wdth) / 2,
                       (768 / 2 - (3 - line_nr) * (textline.hght + 10))))
    hrl.graphics.flip(clr=True)
    btn = None
    while btn != 'Space':
        btn, _ = hrl.inputs.readButton(to=3600)

    ### Core Loop ###

    # hrl.designs is an iterator over all the lines in the specified design
    # matrix, which was loaded at the creation of the hrl object. Looping over
    # it in a for statement provides a nice way to run each line in a design
    # matrix. The fields of each design line (dsgn) are drawn from the design
    # matrix in the design file (design.csv).

    start_time = time.time()
    try:
        for trial, dsgn in enumerate(hrl.designs):

            # skip trials that we already had data for
            if trial < completed_trials:
                continue

            # check if we should take a break (every 15 minutes)
            if time.time() - start_time > (60 * 15):
                show_break(trial, total_trials)
                start_time = time.time()

            match_lum, t, bg_values = run_trial(dsgn)

            # convert adaptor type name to convention more useful for analysis
            adaptor_ori = dsgn['adaptor_type'].replace('_shifted', '')
            if adaptor_ori == 'none':
                adaptor_type = 'none'
            elif dsgn['grating_ori'] == adaptor_ori:
                adaptor_type = 'parallel'
            else:
                adaptor_type = 'orthogonal'
            # determine flank and coaxial luminance based on test patch position
            grating_vals = [float(v) for v in dsgn['grating_vals'].split(',')]
            loc = int(dsgn['test_loc'])
            go = dsgn['grating_ori']
            if (go == 'horizontal' and loc in [2, 3]) or \
               (go == 'vertical' and loc in [0, 2]):
                coaxial_lum, flank_lum = grating_vals
            else:
                flank_lum, coaxial_lum = grating_vals

            # Once a value has been chosen by the subject, we save all relevant
            # variables to the result file by loading it all into the hrl.results
            # dictionary, and then finally running hrl.writeResultLine().
            hrl.results['Trial'] = trial
            hrl.results['test_lum'] = dsgn['test_lum']
            hrl.results['adaptor_type'] = adaptor_type
            hrl.results['test_loc'] = dsgn['test_loc']
            hrl.results['grating_ori'] = dsgn['grating_ori']
            hrl.results['coaxial_lum'] = coaxial_lum
            hrl.results['flank_lum'] = flank_lum
            hrl.results['response_time'] = time.time() - t
            hrl.results['match_lum'] = float(match_lum)
            hrl.writeResultLine()

            # write match bg values to file
            with open(check_fn, 'a') as f:
                f.write('%s\n' % ','.join('%0.4f' % x for x in bg_values))

            # We print the trial number simply to keep track during an experiment
            print hrl.results['Trial']

    # catch EndExperiment exception raised by pressing escp for clean exit
    except EndExperiment:
        print "Experiment aborted"
    # And the experiment is over!
    hrl.close()
    print "Session complete"
def main(files):

    debug = 0
    #print debug
    if debug:

        ### HRL Parameters for DESKTOP ###
        graphics = 'gpu'
        inputs = 'keyboard'

        # Screen GPU
        wdth = 800
        hght = 600
        fs = False  # fullscreen mode off
        scrn = 0  # screen number

    else:

        ### HRL Parameters for DATAPIXX ###
        graphics = 'datapixx'
        inputs = 'responsepixx'  # or 'responsepixx'

        wdth = 1024
        hght = 768
        fs = True  # fullscreen mode
        scrn = 1  # screen number

    #############################################
    hrl = HRL(graphics=graphics,
              inputs=inputs,
              lut=lut,
              photometer=None,
              wdth=wdth,
              hght=hght,
              bg=background,
              fs=fs,
              scrn=scrn)

    whlf = wdth / 2.0
    hhlf = hght / 2.0
    #qh = hght/4.0
    #qw = wdth/4.0
    #ws = wdth/6.0

    # reading PNGs and converting to grayscale
    textures = []
    for fname in files:

        print "loading... %s " % fname
        ## the image is loaded using PIL module and converted to a
        ## numpy array ranging from 0 to 1.
        image = Image.open(fname).convert("L")
        im = np.asarray(image) / 255.

        # texture creation in buffer, input is numpy array [0-1]
        tex = hrl.graphics.newTexture(im)
        textures.append(tex)

    # generating text for filenames to be displayed
    texts = []
    for fname in files:
        im = draw_text(fname, bg=0.27, text_color=0, fontsize=20)
        tex = hrl.graphics.newTexture(im)
        texts.append(tex)

    #######
    i = 0
    #######################################
    while True:
        tex = textures[i]
        tex.draw((whlf - tex.wdth / 2, hhlf - tex.hght / 2))

        tex = texts[i]
        tex.draw((100, 100))

        hrl.graphics.flip(clr=True)  # clr= True to clear buffer
        print "currently showing: %s" % files[i]

        (btn, t1) = hrl.inputs.readButton()
        #print btn

        if btn == 'Space':
            break
        elif btn == 'Right':
            i += 1
        elif btn == 'Left':
            i -= 1

        if i < 0:
            i = len(textures) - 1
        if i == len(textures):
            i = 0

    # closing window
    hrl.close()