Beispiel #1
0
def maze(width=81, height=51, complexity=.75, density =.75):
    # Only odd shapes
    shape = ((height//2)*2+1, (width//2)*2+1)
    # Adjust complexity and density relative to maze size
    complexity = int(complexity*(5*(shape[0]+shape[1])))
    density    = int(density*(shape[0]//2*shape[1]//2))
    # Build actual maze
    Z = np.zeros(shape, dtype=bool)
    # Fill borders
    Z[0,:] = Z[-1,:] = 1
    Z[:,0] = Z[:,-1] = 1
    # Make isles
    for i in range(density):
        x, y = rnd(0,shape[1]//2)*2, rnd(0,shape[0]//2)*2
        Z[y,x] = 1
        for j in range(complexity):
            neighbours = []
            if x > 1:           neighbours.append( (y,x-2) )
            if x < shape[1]-2:  neighbours.append( (y,x+2) )
            if y > 1:           neighbours.append( (y-2,x) )
            if y < shape[0]-2:  neighbours.append( (y+2,x) )
            if len(neighbours):
                y_,x_ = neighbours[rnd(0,len(neighbours)-1)]
                if Z[y_,x_] == 0:
                    Z[y_,x_] = 1
                    Z[y_+(y-y_)//2, x_+(x-x_)//2] = 1
                    x, y = x_, y_
    return Z
Beispiel #2
0
def make_points_random(number, width, height):
    points = []
    for i in range(number):
        x = rnd(1,width-1)/width
        y = rnd(1,height-1)/height
        points.append(Point(x,y))
    return points
Beispiel #3
0
def maze_recurse(width, height):
    # Init maze as numpy array (all walls)
    Z = numpy.ones((height, width))

    def carve(y, x):
        Z[y, x] = 0
        yield Z
        # get randomized list of neighbours
        neighbours = [(x + 2, y), (x - 2, y), (x, y + 2), (x, y - 2)]
        random.shuffle(neighbours)
        for nx,ny in neighbours:
            if nx < 0 or ny < 0 or nx >= width or ny >= height:
                continue
            if Z[ny, nx] == 1:
                Z[(y + ny) / 2,(x + nx) / 2] = 0
                for m in carve(ny, nx):
                    yield m

    # choose random internal starting point
    x, y = rnd(0, width // 2 - 1) * 2 + 1, rnd(0, height // 2 - 1) * 2 + 1
    for m in carve(y, x):
        yield m

    # carve exits
    Z[1, 0] = Z[-2, -1] = 0
    yield Z
Beispiel #4
0
def maze_non_recurse(width, height):
    # Init maze as numpy array (all walls)
    Z = numpy.ones((height, width))
    stack = []
    # choose random internal starting point
    x, y = rnd(0, width // 2 - 1) * 2 + 1, rnd(0, height // 2 - 1) * 2 + 1
    # get randomized list of neighbours
    n = neighbours(x, y, width, height)
    while True:
        Z[y, x] = 0
        yield Z

        for nx, ny in n:
            if Z[ny, nx] == 1:
                Z[(y + ny) / 2, (x + nx) / 2] = 0
                stack.append((n, (x, y)))
                y, x = ny, nx
                n = neighbours(x, y, width, height)
                break
        else:
            try:
                n, (x, y) = stack.pop()
            except IndexError:
                break

    # carve exits
    Z[1, 0] = Z[-2, -1] = 0
    yield Z
Beispiel #5
0
    def get_sample(self):
        """
        Returns an array of sample points sampled in the specified region using Bridson's Poisson-disk sampling algorithm

        Returns
        =======
        samples: list of tuples of two ints
            A list containing the coordinates sampled on a 2-d region such that no two samples points have distance less than `radius`.
        """
        # initialize with a seed point
        self.__sample__(rnd() * self.width, rnd() * self.height)
        while len(self.queue) > 0:
            idx = int(rnd() * len(self.queue))
            p = self.queue[idx]
            new_inserted = False
            for j in xrange(self.k):
                theta = 2 * np.pi * rnd()
                # radius <= r <= 2 * radius
                r = np.sqrt(3 * rnd() * self.radius**2 + self.radius**2)
                x = p[0] + r * np.cos(theta)
                y = p[1] + r * np.sin(theta)
                if (0 <= x < self.width) and (0 <= y < self.height) and self.__far__(x,y):
                    self.__sample__(x,y)
                    new_inserted = True
                    break
            # remove point from active list
            if not new_inserted:
                self.queue = self.queue[:idx] + self.queue[idx+1:]
                self.samples.append(p)

        return self.samples
Beispiel #6
0
def maze_coords(width, height):
    """Return a generator where each iteration is the x, y coord of
    the next wall (brick?) location.
    """
    # Init maze as numpy array (all walls)
    Z = numpy.ones((height, width))
    stack = []
    # choose random internal starting point
    x, y = rnd(0, width // 2 - 1) * 2 + 1, rnd(0, height // 2 - 1) * 2 + 1
    # get randomized list of neighbours
    n = neighbours(x, y, width, height)
    yield -1, -1
    while True:
        Z[y, x] = 0
        yield (x, y)

        for nx, ny in n:
            if Z[ny, nx] == 1:
                Z[(y + ny) / 2, (x + nx) / 2] = 0
                yield ((x + nx) / 2, (y + ny) / 2)

                stack.append((n, (x, y)))
                y, x = ny, nx
                n = neighbours(x, y, width, height)
                break
        else:
            try:
                n, (x, y) = stack.pop()
                yield (-1, -1)
            except IndexError:
                break

    # carve exits
    Z[1, 0] = Z[-2, -1] = 0
Beispiel #7
0
def get_pos(enem):
    # choose random start position
    pos = array((rnd() * 500., rnd() * 500.))
    # make several iterations
    for i in xrange(25):
        # get direction that minimize loss func
        g = get_grad(pos, enem)
        # do one dimentional optimization
        npos = one_dir_opt(pos, g, enem)
        # check stop condition
        if norm(npos - pos) < 0.5:
            break
        # update position
        pos = npos
    return pos
Beispiel #8
0
 def test_new(self):
     # should raise AttributeError if no name is specified:
     self.assertRaises(AttributeError,Dim,range(3))
     # should raise ValueError if not 1-D:
     self.assertRaises(ValueError,Dim,rnd((2,3)),name='test')
     # should raise ValueError if data is not unique
     self.assertRaises(ValueError,Dim,[1,2,2,3],name='test')
     # should work fine with any number of dimensions as long as it
     # is squeezable or expandable to 1-D:
     tst = Dim(rnd((3,1,1,1,1)),name='test')
     self.assertEquals(tst.name,'test')
     tst = Dim(np.array(5),name='test2')
     self.assertEquals(tst.name,'test2')
     # custom attributes should work, too:
     tst = Dim(range(2),name='test3',custom='attribute')
     self.assertEquals(tst.name,'test3')
     self.assertEquals(tst.custom,'attribute')
     # should raise Attribute Error if name is removed:
     self.assertRaises(AttributeError,tst.__setattr__,'name',None)
Beispiel #9
0
def sample_plot(data, caption, f_name) :
    lc = int(check_output(['wc', '-l', data]).split()[0])
#    print lc
    sample = list(rnd(lc, lc/5, replace=False))
    sample.sort()
#    print sample
    with open(data, 'r') as d_file :
        new_data = []
        n = 0
        i_sample = 0
        n_sample = sample[i_sample]
        while True :
            if n == n_sample :
                line = d_file.readline()
                if not line : break
                print line 
                new_data.append(float(line))
                i_sample += 1
                if i_sample >= len(sample) : break
                n_sample = sample[i_sample]
            n += 1     

    d_file.close

#    data = map(float, data)
#    data = pd.Series(data)
#    data = rnd(data, 1000000000, replace=False)
    
    print '%d samples ommited randomly from dataset.\nConverting to float' % len(new_data)

#    data = [float(i)  for i in new_data if isfloat(i)]
#    print len(data)       
    print 'plotting.....'
    
    fig = plt.figure(1, figsize=(16,16))
    plt.boxplot(new_data, showmeans=True)

    ax = plt.gca()
#    plt.minorticks_on()
    ax.yaxis.grid(True, linestyle='-', which='major', color='grey', alpha=0.5)
    ax.yaxis.grid(True, linestyle='--', which='minor', color='lightgrey', alpha=0.5)

    plt.title(caption)
    fig.savefig(f_name)
Beispiel #10
0
def main(alt=False):
    for i in range(5):
        for j in range(10):
            t = np.linspace(0, tend*(1 + rnd()), nt) + rnd(nt)*t_noise
            A0 = A0_base + A0_noise*rnd()
            if alt:
                def f(y, t):
                    r = 4*y[0]**3*y[1]*atan(1)
                    return [-3*r, -r, r]
                y = odeint(f, [1e-3, 0.1*(i+1), 0], t)[:, 0]
            else:
                y = (2.4*t*atan(1)*(i+1) + A0**-2)**-0.5
                y *= 1 + y_noise*(rnd(nt) - 0.5)**3
                if rnd() < 0.28:
                    print('%d, %d: noise!' % (i+1, j+1))
                    y *= 1 + strong_noise * y_noise*rnd(nt)**13
                else:
                    print('%d, %d: silence...' % (i+1, j+1))
                if not os.path.exists('data/0.%d' % (i+1)):
                    os.mkdir('data/0.%d' % (i+1))
            np.savetxt('data/0.%d/%d.dat' % (i+1, j+1), np.vstack((t, eps_l*y)).T)
Beispiel #11
0
scene = C.scene


def make_sphere(name, location, size, u_segs=32, v_segs=16):
    # Create an empty mesh and the object.
    mesh = D.meshes.new("%s Mesh" % name)
    sphere = D.objects.new(name, mesh)

    # Add the object into the scene.
    scene.objects.link(sphere)
    scene.objects.active = sphere

    # Construct the bmesh cube and assign it to the blender mesh.
    geometry = bmesh.new()
    bmesh.ops.create_uvsphere(geometry, u_segments=u_segs, v_segments=v_segs, diameter=size)
    geometry.to_mesh(mesh)
    sphere.location = location
    geometry.free()


for obj in bpy.data.objects:
    if 'g_' in obj.name:
        bpy.data.objects.remove(obj)

for i in range(10):
    make_sphere("g_sphere_%d" % i, rnd(3) * 10 - 5, 1)

for sphere in [obj for obj in bpy.data.objects if 'g_' in obj.name]:


Beispiel #12
0
 def arbiter(state):
     return bool(rnd(2))
Beispiel #13
0
preamble=json.load(preamblef)
preamblef.close()


#
# Open the data files from a signle run (GENERIC)
#

RUN_NAME=lambda i: NAME+"_"+str(i)
input_file_name=lambda i: os.path.join(NAME, RUN_NAME(i) + ".dat")
supp_file_name=lambda i: os.path.join(NAME, RUN_NAME(i) + ".sup")

try:
    NUM=int(sys.argv[2]) #second argument to this script (if any) is a run number
except:
    NUM=rnd(preamble['Nruns'])

input_file=open(input_file_name(NUM),'rb')
supp_file=open(supp_file_name(NUM),'rb')


#
# Prepare data entries from a single run (GENERIC)
#

DATA={}

#- prepare entries for experiment measurables
if preamble['mids_recorded'] is []:
    pass
else:
 def random_walk(state):
     diff = 2 * rnd(2) - 1
     newpos = (state[id_pos][0] + diff) % X_BOUND
     return newpos
Beispiel #15
0
def start_experiment(stdscr, agent_to_examine):
    NODELAY = 0
    # grid definitions
    X_BOUND = 4  #length

    def in_bounds(pos):
        return (pos >= 0 and pos <= X_BOUND)

    # distance function
    def dist(p, q):
        return abs(p - q)

    # agent discount parameters
    MOTION_PARAMS = tuple([1. - pow(2, -6)])
    #ARBITRATION_PARAMS=tuple([1.-pow(2,-2)])
    #ARBITRATION_PEAK=2

    # initialize a new experiment
    EX = Experiment()
    id_dec = EX.nid('decision')

    # register basic motion agents;
    # - $True$ tag means they will be marked as dependent (on other agents)
    id_rt, id_rtc = EX.register_sensor('rt', True)
    id_lt, id_ltc = EX.register_sensor('lt', True)
    # register motivation for motion agents
    # - this one is NOT dependent on agents except through the position, so
    #   it carries the default False tag.
    id_distM = EX.register('distM')
    id_navM, cid_navM = EX.register_sensor('navM')
    # register supervising agent
    #id_super,id_superc=EX.register_sensor('super',True)

    # agent to be visualized
    id_lookat = EX.nid(agent_to_examine)

    # register arbiter variable whose purpose is to synchronize the responses
    # of agents 'lt' and 'rt' to the action of 'super', it does not depend on
    # agent decisions, hence carries the default False tag.
    id_arbiter = EX.register('ar', True)
    # register the failure mode sensor
    #id_fail,id_failc=EX.register_sensor('fl')

    # add a counter
    id_count = EX.register('count')

    def ex_counter(state):
        return 1 + state[id_count][0]

    EX.construct_measurable(id_count, ex_counter, [0])

    # introduce arbitration
    def arbiter(state):
        return bool(rnd(2))

    EX.construct_measurable(id_arbiter, arbiter, [bool(rnd(2))], 0)

    id_toRT, id_toRTc = EX.register_sensor('toR', True)

    def intention_RT(state):
        return id_rt in state[id_dec][0]

    EX.construct_sensor(id_toRT, intention_RT)

    id_toLT, id_toLTc = EX.register_sensor('toL', True)

    def intention_LT(state):
        return id_lt in state[id_dec][0]

    EX.construct_sensor(id_toLT, intention_LT)

    # failure mode for action $lt^rt$
    id_toF, id_toFc = EX.register_sensor('toF', True)

    def about_to_enter_failure_mode(state):
        return state[id_toLT][0] and state[id_toRT][0]

    EX.construct_sensor(id_toF, about_to_enter_failure_mode)

    # add basic motion agents
    def action_RT(state):
        rt_decided = (id_rt in state[id_dec][0])
        if state[id_toF][0]:
            #return not(rt_decided) if state[id_arbiter][0] else rt_decided
            return state[id_arbiter][0]
        else:
            return rt_decided

    RT = EX.construct_agent(id_rt, id_distM, action_RT, MOTION_PARAMS, True)

    def action_LT(state):
        lt_decided = (id_lt in state[id_dec][0])
        if state[id_toF][0]:
            #return lt_decided if state[id_arbiter][0] else not(lt_decided)
            return not (state[id_arbiter][0])
        else:
            return lt_decided

    LT = EX.construct_agent(id_lt, id_distM, action_LT, MOTION_PARAMS, True)

    # immediately introduce corresponding action sensors
    #EX.assign_sensor(id_rt,True,[id_lt])
    #EX.assign_sensor(id_lt,True,[id_rt])

    #
    ### "mapping" system
    #

    ## introduce agent's position

    # select starting position
    START = rnd(X_BOUND + 1)

    # effect of motion on position
    id_pos = EX.register('pos')

    def motion(state):
        triggers = {id_rt: 1, id_lt: -1}
        diff = 0
        for t in triggers:
            diff += triggers[t] * int(state[t][0])
        newpos = state[id_pos][0] + diff
        if in_bounds(newpos):
            return newpos
        else:
            return state[id_pos][0]

    EX.construct_measurable(id_pos, motion, [START, START])

    ## introduce effect of agent (not/)feeding (finding and consuming targets):

    # generate target position
    TARGET = START
    while dist(TARGET, START) < X_BOUND / 8:
        TARGET = rnd(X_BOUND + 1)

    # set up position sensors
    def xsensor(m):  # along x-axis
        return lambda state: state[id_pos][0] < m + 1

    for ind in xrange(X_BOUND):
        tmp_name = 'x' + str(ind)
        id_tmp, id_tmpc = EX.register_sensor(
            tmp_name)  #registers the sensor pairs
        EX.construct_sensor(id_tmp, xsensor(
            ind))  #constructs the measurables associated with the sensor
        EX.assign_sensor(id_tmp, True,
                         [id_rt, id_lt])  #assigns the sensor to all agents

    # normalized distance to playground (nav function #1)
    # - $id_distM$ has already been registerd
    def distM(state):
        if state[id_pos][0] == TARGET and state[id_pos][1] == TARGET:
            return state[id_distM][0] + 1
        else:
            #sharp (near-logarithmic) spike at the target
            #return 1.-np.log((1.+dist(state[id_pos][0],TARGET))/(X_BOUND+2))
            #linear peak at the target
            return 1 + X_BOUND - dist(state[id_pos][0], TARGET)

    INIT = 1 + X_BOUND - dist(START, TARGET)
    EX.construct_measurable(id_distM, distM, [INIT, INIT])

    def navM(state):
        return state[id_distM][0] - state[id_distM][1] > 0

    EX.construct_sensor(id_navM, navM)
    EX.assign_sensor(id_navM, True,
                     [id_rt, id_lt])  #assigns the sensor to all agents

    #
    ### Initialize agents on GPU
    #

    for agent_name in EX._AGENTS:
        tmp = EX._AGENTS[agent_name].init()

    #
    ### Introduce a conjunction
    #

    #for agent in [RT,LT]:
    #    agent.amper([agent.generate_signal([EX.nid('x0*'),EX.nid('x2')])])
    #

    ### Introduce delayed position sensors for both agents

    for agent in [RT, LT]:
        delay_sigs = [
            agent.generate_signal([EX.nid('x' + str(ind))])
            for ind in xrange(X_BOUND)
        ]
        agent.delay(delay_sigs)

    # another update cycle
    message = EX.update_state()
    #message=EX.update_state()

    ## SET ARTIFICIAL TARGET ONCE AND FOR ALL
    for agent in [RT, LT]:
        for token in ['plus', 'minus']:
            tmp_target = agent.generate_signal([id_navM]).value_all().tolist()
            agent.brain._snapshots[token].setTarget(tmp_target)

    #
    ### Run
    #

    # prepare windows for output
    curses.curs_set(0)
    stdscr.erase()

    # color definitions
    curses.init_color(0, 0, 0, 0)  #black=0
    curses.init_color(1, 1000, 0, 0)  #red=1
    curses.init_color(2, 0, 1000, 0)  #green=2
    curses.init_color(3, 1000, 1000, 0)  #yellow=3
    curses.init_color(4, 1000, 1000, 1000)  #white=4
    curses.init_color(5, 1000, 1000, 500)

    curses.init_pair(1, 0, 1)  #black on red
    curses.init_pair(2, 0, 2)  #green on black
    curses.init_pair(3, 0, 3)  #black on yellow
    curses.init_pair(4, 4, 0)  #white on black
    curses.init_pair(5, 1, 0)  #red on black
    curses.init_pair(6, 0, 5)

    REG_BG = curses.color_pair(4) | curses.A_BOLD
    POS_BG = curses.color_pair(2) | curses.A_BOLD
    NEG_BG = curses.color_pair(1) | curses.A_BOLD
    OBS_BG = curses.color_pair(6) | curses.A_BOLD
    BG = curses.color_pair(5) | curses.A_BOLD
    FG = curses.color_pair(3) | curses.A_BOLD

    WIN = curses.newwin(9, 2 * X_BOUND + 3, 5, 7)
    WINs = curses.newwin(9, 200, 16, 7)
    stdscr.nodelay(NODELAY)

    WIN.bkgdset(ord('.'), REG_BG)

    WIN.overlay(stdscr)
    WINs.overlay(stdscr)

    def print_state(text, id_agent):
        stdscr.clear()
        stdscr.addstr('W-E  A-R-E  R-U-N-N-I-N-G    (press [space] to stop) ')
        stdscr.addstr(2, 3, text)
        stdscr.clrtoeol()
        stdscr.noutrefresh()
        WIN.clear()
        WIN.addstr(0, 0, str(EX.this_state(id_count, 1)))
        WIN.chgat(0, 0, BG)

        ## Unpacking the output from the tested agent (RT/LT)

        #determining the start position of geographic sensors in signals
        geo_start = min(ind for ind, id_tmp in enumerate(LT._SENSORS)
                        if id_tmp == EX.nid('x0'))
        #decompose extra info from agent
        agent = EX._AGENTS[id_agent]
        curr = agent._CURRENT
        targ = agent._TARGET
        pred = agent._PREDICTED

        #choose the signals to visualize (curr,targ or pred)

        for ind, lookat in enumerate([curr, pred, targ]):
            #convert signals to region bounds
            bounds = {'plus': [0, X_BOUND], 'minus': [0, X_BOUND]}
            for token in ['plus', 'minus']:
                for x in xrange(0, X_BOUND):
                    if lookat[token].value(geo_start + 2 * x):
                        bounds[token][1] = x  #pushing down the upper bound
                        break
                    else:
                        continue
                for x in xrange(X_BOUND - 1, -1, -1):
                    if lookat[token].value(geo_start + 2 * x + 1):
                        bounds[token][0] = x + 1  #pushing up the lower bound
                        break
                    else:
                        continue

            #display the results
            tok_BG = {'plus': POS_BG, 'minus': NEG_BG}
            tok_line = {'plus': 3 - ind, 'minus': 6 + ind}
            for token in ['plus', 'minus']:
                min_pos = bounds[token][0]
                max_pos = bounds[token][1]
                for x in xrange(0, X_BOUND):
                    ori = ord('<') if lookat[token].value(
                        geo_start + 2 * x) else (
                            ord('>') if lookat[token].value(geo_start + 2 * x +
                                                            1) else ord('*'))
                    this_BG = tok_BG[token] if (x >= min_pos
                                                and x < max_pos) else BG
                    WIN.addch(tok_line[token], 2 + 2 * x, ori, this_BG)

                    WIN.chgat(tok_line[token], 1 + 2 * min_pos,
                              1 + 2 * (max_pos - min_pos), tok_BG[token])
        # display targets with FG attributes
        WIN.addch(5, 1 + 2 * TARGET, ord('T'), FG)
        # display agent's position with FG attributes
        WIN.addch(4, 1 + 2 * EX.this_state(id_pos, 1), ord('S'), FG)

        ## Unpacking extra information
        WINs.clear()
        WINs.addstr(0, 0, 'Observation:')
        WINs.addstr(4, 0, 'Chosen signed signal:')
        #
        tok_BG = {'plus': POS_BG, 'minus': NEG_BG}
        vpos = {'plus': 6, 'minus': 8}
        hpos = lambda x: 0 if x == 0 else 2 + len('  '.join(namelist[:x]))

        # CURRENT OBSERVATION
        OBS = agent._OBSERVE
        #OBS=Signal([EX.this_state(mid) for mid in agent._SENSORS])

        #SIGNED SIGNAL TO WATCH:
        #SIG=agent._CURRENT

        sig = agent.generate_signal([EX.nid('x0')])
        #sig=agent.generate_signal([EX.nid('{x0*;x2}')])
        #sig=agent.generate_signal([EX.nid('#x2')])
        #sig=agent.generate_signal([EX.nid('#x0*')])
        SIG = agent.brain.up(sig, False)
        #
        namelist = [EX.din(mid) for mid in agent._SENSORS]
        #
        for x, mid in enumerate(agent._SENSORS):
            this_BG = OBS_BG if OBS.value(x) else REG_BG
            WINs.addstr(2, hpos(x), namelist[x], this_BG)
            for token in ['plus', 'minus']:
                this_BG = tok_BG[token] if SIG[token].value(x) else REG_BG
                WINs.addstr(vpos[token], hpos(x), namelist[x], this_BG)

        # refresh the window
        WIN.overlay(stdscr)
        WIN.noutrefresh()
        WINs.noutrefresh()
        curses.doupdate()

    ## Main loop
    while stdscr.getch() != ord(' '):

        # call output subroutine
        print_state('RUNNING:\n' + message, id_lookat)

        # make decisions, update the state
        message = EX.update_state()

    else:
        raise Exception('Aborting at your request...\n\n')
Beispiel #16
0
def start_experiment(run_params):
    # System parameters
    test_name = run_params['test_name']
    host = run_params['host']
    port = run_params['port']

    # initialize a new experiment
    EX = Experiment(test_name, UMARestService(host, port))
    id_dec = 'decision'
    id_count = 'counter'

    # Recording options:
    record_mids = run_params['mids_to_record']
    record_global = run_params['ex_dataQ']
    record_agents = run_params['agent_dataQ']
    #A recorder will be initialized later, at the end of the initialization phase,
    #to enable collection of all available data tags

    # Decision cycles:
    TOTAL_CYCLES = run_params['total_cycles']
    # Parameters and definitions
    MODE = run_params[
        'mode']  #mode by which Sniffy moves around: 'teleport'/'walk'/'lazy'
    X_BOUND = run_params[
        'env_length']  # no. of edges in discrete interval = no. of GPS sensors
    try:
        Discount = float(run_params['discount'])  #discount coefficient, if any
    except KeyError:
        Discount = 0.875
    try:
        Threshold = float(
            run_params['threshold']
        )  #implication threshold, defaulting to the square of the probability of a single position.
    except KeyError:
        Threshold = 1. / pow(X_BOUND + 1, 2)

    # Environment description
    def in_bounds(pos):
        return (pos >= 0 and pos <= X_BOUND)

    def dist(p, q):
        return abs(p - q)  #distance between two points in environment

    # agent parameters according to .yml file

    empirical_observer = {
        'type': 'empirical',
        'AutoTarg': True,
        'threshold': Threshold,
    }

    discounted_observer = {
        'type': 'discounted',
        'discount': Discount,
        'AutoTarg': True,
        'threshold': Threshold,
    }

    qualitative_observer = {
        'type': 'qualitative',
        'AutoTarg': True,
        #'threshold': 0,
    }

    AGENT_PARAMS = {
        '_Q': qualitative_observer,
        '_Eu': empirical_observer,
        '_Ev': empirical_observer,
        '_Du': discounted_observer,
        '_Dv': discounted_observer,
    }
    ORDERED_TYPES = ['_Q', '_Eu', '_Ev', '_Du', '_Dv']

    #Register "observer" agents:
    #  These agents remain inactive throghout the experiment, in order to record
    #  all the UNCONDITIONAL implications among the initial sensors (in their 'minus'
    #  snapshots).
    #  For this purpose, each sensor's FOOTPRINT in the state space (positions in
    #  the interval) is recorded, so that implications may be calculated according
    #  to the inclusions among footprints.
    id_obs = {}
    cid_obs = {}
    for typ in ORDERED_TYPES:
        id_obs[typ], cid_obs[typ] = EX.register_sensor('obs' + typ)

    # register motivation for motion agents
    # - this one is NOT dependent on agents except through the position, so
    #   it carries the default "decdep=False" tag.

    # Value signals for different setups determined as a *function* of distance to target
    id_dist = EX.register('dist')
    id_sig = {}
    for typ in ORDERED_TYPES:
        id_sig[typ] = EX.register('sig' + typ)

    # ...which function? THIS function:
    RESCALING = {
        '_Q': lambda r: r,
        '_Eu': lambda r: 1,
        '_Ev': lambda r: X_BOUND - r,
        '_Du': lambda r: 1,
        '_Dv': lambda r: pow(1. - Discount, r - X_BOUND),
    }

    # OBSERVER agents simply collect implications among the assigned sensors, always inactive
    OBSERVERS = {}
    OBSACCESS = {}
    for typ in ORDERED_TYPES:
        OBSERVERS[typ] = EX.construct_agent(id_obs[typ], id_sig[typ],
                                            lambda state: False,
                                            AGENT_PARAMS[typ])
        OBSACCESS[typ] = UMAClientData(EX._EXPERIMENT_ID, id_obs[typ], 'minus',
                                       EX._service)

    #
    ### "mapping" system
    #

    ## introduce agent's position

    # select starting position
    START = rnd(X_BOUND + 1)

    # effect of motion on position
    id_pos = EX.register('pos')

    def random_walk(state):
        diff = 2 * rnd(2) - 1
        newpos = state[id_pos][0] + diff
        if in_bounds(newpos):
            return newpos
        else:
            return state[id_pos][0]

    def lazy_random_walk(state):
        diff = rnd(3) - 1
        newpos = state[id_pos][0] + diff
        if in_bounds(newpos):
            return newpos
        else:
            return state[id_pos][0]

    def teleport(state):
        return rnd(X_BOUND + 1)

    def back_and_forth(state):
        last_diff = state[id_pos][0] - state[id_pos][1]
        thispos = state[id_pos][0]
        if last_diff != 0:
            newpos = thispos + last_diff
            if in_bounds(newpos):
                return newpos
            else:
                return thispos - last_diff
        else:
            if thispos < X_BOUND:
                return thispos + 1
            else:
                return thispos - 1

    motions = {
        'simple': back_and_forth,
        'walk': random_walk,
        'lazy': lazy_random_walk,
        'teleport': teleport
    }
    EX.construct_measurable(id_pos, motions[MODE], [START, START])

    # generate target position
    TARGET = START
    while dist(TARGET, START) == 0:
        TARGET = rnd(X_BOUND + 1)

    # set up position sensors
    def xsensor(footprint):  # along x-axis
        return lambda state: bool(footprint[state[id_pos][0]])

    def make_footprint():
        return [rnd(2) for ind in xrange(X_BOUND + 1)]

    #generate randomized position sensors and record their semantics
    FOOTPRINTS = []
    all_comp = lambda x: [1 - t for t in x]
    for ind in xrange(X_BOUND):
        tmp_name = 'x' + str(ind)
        tmp_footprint = make_footprint()
        FOOTPRINTS.append(tmp_footprint)
        FOOTPRINTS.append(all_comp(tmp_footprint))
        id_tmp, id_tmpc = EX.register_sensor(tmp_name)
        EX.construct_sensor(id_tmp, xsensor(tmp_footprint))
        OBS.add_sensor(id_tmp)

#Construct footprint-type estimate of target position
    id_targ = {}

    def look_up_target(state, typ):
        return OBSACCESS[typ].getTarget()

    #- construct target estimate measurable for each observer
    INIT = np.zeros(X_BOUND + 1).tolist()
    for typ in ORDERED_TYPES:
        id_targ[typ] = EX.register('targ' + typ)
        EX.construct_measurable(id_targ[typ],
                                partial(look_up_target, typ=typ), [INIT],
                                depth=0)

    # distance to target
    # - $id_distM$ has already been registerd
    def dist_to_target(state):
        return dist(state[id_pos][0], TARGET)

    INIT = dist(START, TARGET)
    EX.construct_measurable(id_dist, dist_to_target, [INIT, INIT])

    #
    ### MOTIVATIONS
    #

    #construct the motivational signal for OBS:
    def rescaling(state, typ):
        return RESCALING[typ](state[id_dist][0])

    for typ in ORDERED_TYPES:
        INIT = RESCALING[typ](dist(START, TARGET))
        EX.construct_measurable(id_sig[typ], partial(rescaling, typ=typ),
                                [INIT, INIT])

    #record the value at each position, for each type:
    VALUES = {
        typ:
        [RESCALING[typ](dist(pos, TARGET)) for pos in xrange(X_BOUND + 1)]
        for typ in ORDERED_TYPES
    }

    # -------------------------------------init--------------------------------------------

    for agent_name in EX._AGENTS:
        EX._AGENTS[agent_name].init()

    QUERY_IDS = {agent_id: {} for agent_id in EX._AGENTS}
    for agent_id in EX._AGENTS:
        for token in ['plus', 'minus']:

            # INTRODUCE DELAYED GPS SENSORS:
            #delay_sigs = [EX._AGENTS[agent_id].generate_signal(['x' + str(ind)], token) for ind in xrange(X_BOUND)]
            #EX._AGENTS[agent_id].delay(delay_sigs, token)

            # MAKE A LIST OF ALL SENSOR LABELS FOR EACH AGENT
            QUERY_IDS[agent_id][token] = EX._AGENTS[
                agent_id].make_sensor_labels(token)

    # START RECORDING
    default_instruction = [cid_obs[typ] for typ in ORDERED_TYPES]
    EX.update_state(default_instruction)
    recorder = experiment_output(EX, run_params)
    recorder.addendum('footprints', FOOTPRINTS)
    recorder.addendum('query_ids', QUERY_IDS)
    recorder.addendum('values', VALUES)
    recorder.addendum('threshold', Threshold)

    # -------------------------------------RUN--------------------------------------------

    ## Main Loop:
    while EX.this_state(id_count) <= TOTAL_CYCLES:
        # update the state
        EX.update_state(default_instruction)
        recorder.record()

    # Wrap up and collect garbage
    recorder.close()
    EX.remove_experiment()
 def lazy_random_walk(state):
     diff = rnd(3) - 1
     newpos = (state[id_pos][0] + diff) % X_BOUND
     return newpos
Beispiel #18
0
 def teleport(state):
     return rnd(X_BOUND + 1)
Beispiel #19
0
 def make_footprint():
     return [rnd(2) for ind in xrange(X_BOUND + 1)]
def main():
    mode = None
    if len(sys.argv) < 2 or sys.argv[1] in ('-h','--help','help'):
        usage()
        sys.exit(0)
    mode = sys.argv[1]
    if not mode in modes:
        print('Unknown working mode: [%s].' % sys.argv[1])
        sys.exit(1)

    opts = parseGlobalArgs()
    if mode in ['hybrid']:
        opts = dict(parseHybridModeArgs(), **opts)

    recorder_id = 0
    neuron_id = []
    sfactor = 1
    offset = 1
    for i in range(len(opts['N'])):
        neuron_id.append(offset+np.arange(opts['N'][i]))
        offset = offset + opts['N'][i]
    all_neurons = [item for sublist in neuron_id for item in sublist]
    # Add RealNeuron here...
    if mode in ['hybrid']:
        real_neuron = max(all_neurons)+1
        all_neurons.append(real_neuron)
    syn = []
    for i,s in zip(range(len(neuron_id)),opts['syn']):
        idx = [np.array([all_neurons[int(rnd(max(all_neurons)))] for i in range(s)]) for n in neuron_id[i]]
        syn.append(idx)
    syn_count = max(all_neurons)+1

    syn_param = [] # [synapse_id,origin,target,weight,type]
    for nn,ss,syn_type,type_weight in zip(neuron_id,syn,opts['synType'],opts['w']):
        for n,s in zip(nn,ss):
            if not opts['autapses']:
                print n, s
                s = s[np.where(s!=n)[0]]
            weights = np.bincount(s)
            targets = np.arange(len(weights))
            targets = targets[np.nonzero(weights)]
            weights = weights[np.nonzero(weights)]
            for t,w in zip(targets,weights):
                syn_param.append([syn_count,n,t,w*type_weight,syn_type])
                syn_count += 2
    if mode in ['hybrid']:
        rn_syn_param = []
        s = np.array([all_neurons[int(rnd(max(all_neurons)))] for i in range(opts['RN_syn_n'])])
        print s
        if not opts['autapses']:
            s = s[np.where(s!=real_neuron)[0]]
        weights = np.bincount(s)
        targets = np.arange(len(weights))
        targets = targets[np.nonzero(weights)]
        weights = weights[np.nonzero(weights)]

        for t,w in zip(targets,weights):
            rn_syn_param.append([syn_count,real_neuron,t,w*opts['RN_syn_w'],opts['RN_syn_type']])
            syn_count +=2
        rn_syn_p = np.array(rn_syn_param)
    syn_p = np.array(syn_param)
    
    root = etree.Element("lcg")
    e_group = etree.SubElement(root,"entities")
    simulation_parameters = etree.SubElement(root,"simulation")
    add_xml_elements(simulation_parameters,{'rate':opts['srate'],
                                            'tend':opts['tend']})
    id_cnt = 0
    if opts['record-all'] or mode in ['hybrid']:
        recorder = etree.SubElement(e_group,"entity")
        add_xml_elements(recorder,{'name':'H5Recorder',
                                   'id':recorder_id})
        add_xml_parameters(recorder,{'compress':True})        
    
    id_cnt+=1
    # Create RealNeuron
    if mode in ['hybrid']:
        nrn = etree.SubElement(e_group,"entity")
        add_xml_elements(nrn,{'name':'RealNeuron',
                                 'id':real_neuron})
        add_xml_parameters(nrn,{'spikeThreshold':opts['threshold'],
                                   'V0':-65,
                                   'deviceFile':'/dev/comedi0',
                                   'inputSubdevice':os.environ['AI_SUBDEVICE'],
                                   'outputSubdevice':os.environ['AO_SUBDEVICE'],
                                   'inputRange':os.environ['RANGE'],
                                   'readChannel':opts['ai'],
                                   'writeChannel':opts['ao'],
                                   'inputConversionFactor':os.environ['AI_CONVERSION_FACTOR_CC'],
                                   'outputConversionFactor':os.environ['AO_CONVERSION_FACTOR_CC'],
                                   'holdLastValue':True,
                                   'reference':os.environ['GROUND_REFERENCE']})
        connections = rn_syn_p[np.where(rn_syn_p[:,1]==real_neuron)[0],0]
        connections =  np.append(connections,0)
        add_xml_connections(nrn,connections)
        for s_id,s_s,s_t,s_w,s_type in rn_syn_param:
            if s_type in [1]:
            # Excitatory
                delay = np.random.uniform(0.1e-3,10e-3)
                E = 0
                tauRise = 0.03e-3
                tauDecay = 5e-3
            else:
                # Inhibitory
                delay = np.random.uniform(0.1e-3,10e-3)
                E = -70
                tauRise = 0.1e-3
                tauDecay = 10e-3
            add_exponential_synapse(e_group,s_id,s_t,delay,s_w,E,tauRise,tauDecay,[s_t])

    # Create all Izhikevich neurons
    for ii,nn in enumerate(neuron_id):
        for n in nn:
            connections = syn_p[np.where(syn_p[:,1]==n)[0],0]
            if opts['record-all']:
               connections =  np.append(connections,0)
            izh_par = {'a': np.random.uniform(opts['a'][ii][0],opts['a'][ii][1]),
                       'b': np.random.uniform(opts['b'][ii][0],opts['b'][ii][1]),
                       'c': np.random.uniform(opts['c'][ii][0],opts['c'][ii][1]),
                       'd': np.random.uniform(opts['d'][ii][0],opts['d'][ii][1]),
                       'Vspk':opts['Vspk'][ii],
                       'Iext': np.random.uniform(opts['Iext'][ii][0],opts['Iext'][ii][1])}
            add_izhikevich_neuron(e_group,n,izh_par,connections)
    # Create all synapses
    for s_id,s_s,s_t,s_w,s_type in syn_param:
        if s_type in [1]:
            # Excitatory
            delay = np.random.uniform(0.1e-3,10e-3)
            E = 0
            tauRise = 0.03e-3
            tauDecay = 5e-3
        else:
            # Inhibitory
            delay = np.random.uniform(0.1e-3,10e-3)
            E = -70
            tauRise = 0.1e-3
            tauDecay = 10e-3
        add_exponential_synapse(e_group,s_id,s_t,delay,s_w,E,tauRise,tauDecay,[s_t])
    xml_tree = etree.ElementTree(root) 
    config_file = 'izhikevich_net.xml'
    xml_tree.write(config_file,pretty_print=True)
    # Run protocol 
    if opts['kernel']:
        sub.call('lcg kernel -I ' + str(opts['ai']) + ' -O ' + str(opts['ao']) + 
                 ' -F ' + str(opts['srate']) + ' -a', shell=True)
    sub.call(lcg.common.prog_name + ' -c ' + config_file +  ' -i ' + str(opts['interval']) +
             ' -n ' + str(opts['nreps']), shell=True)
Beispiel #21
0
def start_experiment(run_params):
    # set randmization seed
    SEED = seed()
    # System parameters
    test_name = run_params['test_name']
    host = run_params['host']
    port = run_params['port']

    # initialize a new experiment
    EX = Experiment(test_name, UMARestService(host, port))
    id_dec = 'decision'
    id_count = 'counter'

    # Recording options:
    record_mids = run_params['mids_to_record']
    record_global = run_params['ex_dataQ']
    record_agents = run_params['agent_dataQ']
    #A recorder will be initialized later, at the end of the initialization phase,
    #to enable collection of all available data tags

    # Decision cycles:
    TOTAL_CYCLES = run_params['total_cycles']
    # Parameters and definitions
    MODE = run_params[
        'mode']  #mode by which Sniffy moves around: 'teleport'/'walk'/'lazy'
    X_BOUND = run_params[
        'env_length']  # no. of edges in discrete interval = no. of GPS sensors
    BEACON_WIDTH = run_params['beacon_width']  # [max] width of beacon sensor
    ENV_LENGTH = X_BOUND
    NSENSORS = X_BOUND

    try:
        Discount = float(run_params['discount'])  #discount coefficient, if any
    except KeyError:
        Discount = 0.75
    try:
        Threshold = float(
            run_params['threshold']
        )  #implication threshold, defaulting to the square of the probability of a single position.
    except KeyError:
        Threshold = 1. / pow(ENV_LENGTH, 2)

    # Environment description
    def in_bounds(pos):
        return (pos >= 0 and pos < X_BOUND)

    def dist(p, q):
        return min(abs(p - q), X_BOUND -
                   abs(p - q))  #distance between two points in environment

    # agent parameters according to .yml file

    empirical_observer = {
        'type': 'empirical',
        'AutoTarg': True,
        'threshold': Threshold,
    }

    discounted_observer = {
        'type': 'discounted',
        'q': Discount,
        'AutoTarg': True,
        'threshold': Threshold,
    }

    qualitative_observer = {
        'type': 'qualitative',
        'AutoTarg': True,
        'threshold': 0,
    }

    AGENT_PARAMS = {
        '_Q': qualitative_observer,
        '_Eu': empirical_observer,
        '_Ev': empirical_observer,
        '_Du': discounted_observer,
        '_Dv': discounted_observer,
    }
    ORDERED_TYPES = ['_Q', '_Eu', '_Ev', '_Du', '_Dv']

    #Register "observer" agents:
    #  These agents remain inactive throghout the experiment, in order to record
    #  all the UNCONDITIONAL implications among the initial sensors (in their 'minus'
    #  snapshots).
    #  For this purpose, each sensor's FOOTPRINT in the state space (positions in
    #  the interval) is recorded, so that implications may be calculated according
    #  to the inclusions among footprints.
    id_obs = {}
    cid_obs = {}
    for typ in ORDERED_TYPES:
        id_obs[typ], cid_obs[typ] = EX.register_sensor('obs' + typ)

    # register motivation for motion agents
    # - this one is NOT dependent on agents except through the position, so
    #   it carries the default "decdep=False" tag.

    # Value signals for different setups determined as a *function* of distance to target
    id_dist = EX.register('dist')
    id_sig = {}
    for typ in ORDERED_TYPES:
        id_sig[typ] = EX.register('sig' + typ)

    # ...which function? THIS function:
    RESCALING = {
        #'_Q': lambda r: r,
        '_Q': lambda r: 1 if r > 0 else 0,
        '_Eu': lambda r: 1,
        '_Ev': lambda r: pow((X_BOUND / 2) - r, 1),
        '_Du': lambda r: 1,
        '_Dv': lambda r: pow(1. - Discount, r - (X_BOUND / 2)),
    }

    # OBSERVER agents simply collect implications among the assigned sensors, always inactive
    OBSERVERS = {}
    OBSACCESS = {}
    for typ in ORDERED_TYPES:
        OBSERVERS[typ] = EX.construct_agent(id_obs[typ], id_sig[typ],
                                            lambda state: False,
                                            AGENT_PARAMS[typ])
        OBSACCESS[typ] = UMAClientData(EX._EXPERIMENT_ID, id_obs[typ], 'minus',
                                       EX._service)

    #
    ### "mapping" system
    #

    ## introduce agent's position

    # select starting position
    START = rnd(X_BOUND)

    # effect of motion on position
    id_pos = EX.register('pos')

    def walk_through(state):
        newpos = (state[id_pos][0] + 1) % X_BOUND
        return newpos

    def random_walk(state):
        diff = 2 * rnd(2) - 1
        newpos = (state[id_pos][0] + diff) % X_BOUND
        return newpos

    def lazy_random_walk(state):
        diff = rnd(3) - 1
        newpos = (state[id_pos][0] + diff) % X_BOUND
        return newpos

    def teleport(state):
        return rnd(X_BOUND)

    motions = {
        'simple': walk_through,
        'walk': random_walk,
        'lazy': lazy_random_walk,
        'teleport': teleport
    }
    EX.construct_measurable(id_pos, motions[MODE], [START, START])

    # generate target position
    TARGET = START
    while dist(TARGET, START) == 0:
        TARGET = rnd(ENV_LENGTH)

    # set up position sensors
    def xsensor(m, width):  # along x-axis
        return lambda state: dist(state[id_pos][0], m) <= width

    # Construct initial sensors and record their semantics
    FOOTPRINTS = []
    all_comp = lambda x: [1 - t for t in x]
    for ind in xrange(X_BOUND):
        # create new beacon sensor centered at $ind$
        tmp_name = 'x' + str(ind)
        id_tmp, id_tmpc = EX.register_sensor(
            tmp_name)  # registers the sensor pairs
        EX.construct_sensor(
            id_tmp, xsensor(ind, BEACON_WIDTH)
        )  # constructs the measurables associated with the sensor
        for typ in ORDERED_TYPES:
            OBSERVERS[typ].add_sensor(id_tmp)
        # record the sensor footprint
        tmp_footprint = [(1 if dist(pos, ind) <= BEACON_WIDTH else 0)
                         for pos in xrange(X_BOUND)]
        FOOTPRINTS.append(tmp_footprint)
        FOOTPRINTS.append(all_comp(tmp_footprint))

    # sensor footprints for this run
    fp = lambda sensor_ind: np.array(FOOTPRINTS[sensor_ind])

    #get internal data for each agent
    id_internal = {}

    def get_internal(state, typ):
        return OBSACCESS[typ].get_all()

    INIT = {}
    for typ in ORDERED_TYPES:
        id_internal[typ] = EX.register('internal' + typ)
        EX.construct_measurable(id_internal[typ],
                                partial(get_internal, typ=typ), [INIT],
                                depth=0)

    #Construct footprint-type estimate of target position
    id_targ = {}

    def look_up_target(state, typ):
        target_fp = np.ones(ENV_LENGTH)
        for ind, val in enumerate(state[id_internal[typ]][0]['target']):
            target_fp = target_fp * fp(ind) if val else target_fp
        return target_fp.tolist()

    #- construct target estimate measurable for each observer
    INIT = np.zeros(ENV_LENGTH).tolist()
    for typ in ORDERED_TYPES:
        id_targ[typ] = EX.register('targ' + typ)
        EX.construct_measurable(id_targ[typ],
                                partial(look_up_target, typ=typ), [INIT],
                                depth=0)

    # distance to target
    # - $id_distM$ has already been registerd
    def dist_to_target(state):
        return dist(state[id_pos][0], TARGET)

    INIT = dist(START, TARGET)
    EX.construct_measurable(id_dist, dist_to_target, [INIT, INIT])

    #
    ### MOTIVATIONS
    #

    #construct the motivational signal for OBS:
    def rescaling(state, typ):
        return RESCALING[typ](state[id_dist][0])

    for typ in ORDERED_TYPES:
        INIT = RESCALING[typ](dist(START, TARGET))
        EX.construct_measurable(id_sig[typ], partial(rescaling, typ=typ),
                                [INIT, INIT])

    #
    ### AUXILIARY COMPUTATIONS
    #

    # value of each position in the environment, needed as np.array, for each run and type
    VALUES = {
        typ: [RESCALING[typ](dist(pos, TARGET)) for pos in xrange(ENV_LENGTH)]
        for typ in ORDERED_TYPES
    }
    vm = lambda typ: np.array(VALUES[typ])
    # extreme (=target) value of signal for each run and type
    v_extreme = lambda typ: vm(typ).min() if typ == '_Q' else vm(typ).max()
    #Construct value-based ground truth target footprint
    GROUND_TARG = {}
    for typ in ORDERED_TYPES:
        GROUND_TARG[typ] = [
            int(abs(val - v_extreme(typ)) < pow(10, -12)) for val in vm(typ)
        ]

    #Ground truth implication matrices
    #
    #- check for inclusions among footprint vectors:
    std_imp_check = lambda x, y: all(fp(x) <= fp(y))

    #- check for ground truth thresholded inclusions (footprint-based)
    def lookup_val(x, y, typ):
        #obtain footprints
        FPx = fp(x)
        FPy = fp(y)
        #compute the expected ground weights
        if typ == '_Q':
            return -1 if not sum(FPx * FPy) else np.extract(
                FPx * FPy, vm(typ)).min()
        else:
            #return (sum(FPx*FPy*vm(typ))+0.)/ENV_LENGTH
            return (sum(FPx * FPy * vm(typ)) + 0.) / sum(vm(typ) + 0.)

    #- check for [ground truth] implications (index based)
    def imp_check(x, y, typ):
        XY = lookup_val(x, y, typ)
        X_Y = lookup_val(compi(x), y, typ)
        X_Y_ = lookup_val(compi(x), compi(y), typ)
        XY_ = lookup_val(x, compi(y), typ)
        if typ == '_Q':  #qualitative implication (zero threshold)
            return int(qless(qmax(XY, X_Y_), XY_))
        else:  #real-valued (statistical) implication
            if y == x:
                #same sensor yields a True entry
                return 1
            elif y == compi(x):
                #complementary sensors yield a False entry
                return 0
            else:
                #otherwise, compare weights:
                #return int(XY_<min(Threshold*sum(vm(typ)),XY,X_Y_,X_Y) or (XY_==0. and X_Y==0.))
                return int(XY_ < min(Threshold, XY, X_Y_, X_Y)
                           or (XY_ == 0. and X_Y == 0.))

    #- construct the ground truth matrices (flattened) for each run and type
    GROUND_WEIGHTS = {typ: [] for typ in ORDERED_TYPES}
    GROUND_RAW_IMPS = {typ: [] for typ in ORDERED_TYPES}
    GROUND_ABS_IMPS = []
    for yind in xrange(2 * NSENSORS):
        for xind in xrange(yind + 1):
            #weights and other expected implications, by type:
            for typ in ORDERED_TYPES:
                lookup = partial(lookup_val, typ=typ)
                check = partial(imp_check, typ=typ)
                # Weight matrix computed from known values of states
                GROUND_WEIGHTS[typ].append(lookup(yind, xind))
                # PCR matrix computed from known values of states; note the transpose!!
                GROUND_RAW_IMPS[typ].append(check(yind, xind))
    #absolute ground-truth implications:
    for yind in xrange(2 * NSENSORS):
        for xind in xrange(yind + 1):
            GROUND_ABS_IMPS.append(std_imp_check(yind, xind))
        if yind % 2 == 0:
            GROUND_ABS_IMPS.append(0)

    #- import weight matrices from core
    id_weights = {typ: EX.register('wgt' + typ) for typ in ORDERED_TYPES}

    def weights(state, typ):
        #print typ+' weights:'
        #print convert_weights(state[id_internal[typ]][0]['weights'])
        #print '\n'
        return flatten(state[id_internal[typ]][0]['weights'])

    for typ in ORDERED_TYPES:
        INIT = (-np.ones(NSENSORS * (2 * NSENSORS + 1)) if typ == '_Q' else
                np.zeros(NSENSORS * (2 * NSENSORS + 1))).tolist()
        EX.construct_measurable(id_weights[typ],
                                partial(weights, typ=typ), [INIT],
                                depth=0)

    #- import raw implications from core
    id_raw_imps = {typ: EX.register('raw' + typ) for typ in ORDERED_TYPES}

    def raw_imps(state, typ):
        #print typ+' implications:'
        #print convert_raw_implications(state[id_internal[typ]][0]['dirs'])
        #print '\n'
        return flatten(state[id_internal[typ]][0]['dirs'])

    INIT = [(1 if x == y else 0) for y in xrange(2 * NSENSORS)
            for x in xrange(y + 1)
            ]  #initialize to (lower triangle of) identity matrix
    for typ in ORDERED_TYPES:
        EX.construct_measurable(id_raw_imps[typ],
                                partial(raw_imps, typ=typ), [INIT],
                                depth=0)

    #- import full implications from core
    id_full_imps = {typ: EX.register('full' + typ) for typ in ORDERED_TYPES}

    def full_imps(state, typ):
        return flatten(state[id_internal[typ]][0]['npdirs'])

    xr = lambda y: y + 2 if y % 2 == 0 else y + 1
    INIT = [(1 if x == y else 0) for y in xrange(2 * NSENSORS)
            for x in xrange(xr(y))
            ]  #initialize to (lower 2*2 triangle of) identity matrix
    for typ in ORDERED_TYPES:
        EX.construct_measurable(id_full_imps[typ],
                                partial(full_imps, typ=typ), [INIT],
                                depth=0)

    #- ell_infinity distance of current weights to ground truth
    id_wdiffs = {typ: EX.register('wdiff' + typ) for typ in ORDERED_TYPES}

    def wdiffs(state, typ):
        #print state[id_weights[typ]][0]
        return max(ldiff(state[id_weights[typ]][0], GROUND_WEIGHTS[typ]))

    for typ in ORDERED_TYPES:
        #print '\n\n'
        #print EX.this_state(id_weights[typ])
        #print len(EX.this_state(id_weights[typ]))
        #print '\n\n'
        #print GROUND_WEIGHTS[typ]
        #print len(GROUND_WEIGHTS[typ])
        #print '\n\n'
        INIT = max(ldiff(EX.this_state(id_weights[typ]), GROUND_WEIGHTS[typ]))
        EX.construct_measurable(id_wdiffs[typ],
                                partial(wdiffs, typ=typ), [INIT],
                                depth=0)

    #- ell_one distance of learned PCR to expected ground truth PCR
    id_rawdiffs = {typ: EX.register('rdiff' + typ) for typ in ORDERED_TYPES}

    def rawdiffs(state, typ):
        return sum(ldiff(state[id_raw_imps[typ]][0], GROUND_RAW_IMPS[typ]))

    for typ in ORDERED_TYPES:
        #print '\n\n'
        #print EX.this_state(id_raw_imps[typ])
        #print GROUND_RAW_IMPS[typ]
        #print '\n\n'
        INIT = sum(ldiff(EX.this_state(id_raw_imps[typ]),
                         GROUND_RAW_IMPS[typ]))
        EX.construct_measurable(id_rawdiffs[typ],
                                partial(rawdiffs, typ=typ), [INIT],
                                depth=0)

    #- ell_one distance of FULL implications to true ABSOLUTE implications
    id_fulldiffs = {typ: EX.register('fdiff' + typ) for typ in ORDERED_TYPES}

    def fulldiffs(state, typ):
        return sum(ldiff(state[id_full_imps[typ]][0], GROUND_ABS_IMPS))

    for typ in ORDERED_TYPES:
        INIT = sum(ldiff(EX.this_state(id_full_imps[typ]), GROUND_ABS_IMPS))
        EX.construct_measurable(id_fulldiffs[typ],
                                partial(fulldiffs, typ=typ), [INIT],
                                depth=0)
    # -------------------------------------init--------------------------------------------

    for agent_name in EX._AGENTS:
        EX._AGENTS[agent_name].init()

    QUERY_IDS = {agent_id: {} for agent_id in EX._AGENTS}
    for agent_id in EX._AGENTS:
        for token in ['plus', 'minus']:

            # INTRODUCE DELAYED GPS SENSORS:
            #delay_sigs = [EX._AGENTS[agent_id].generate_signal(['x' + str(ind)], token) for ind in xrange(NSENSORS)]
            #EX._AGENTS[agent_id].delay(delay_sigs, token)

            # MAKE A LIST OF ALL SENSOR LABELS FOR EACH AGENT
            QUERY_IDS[agent_id][token] = EX._AGENTS[
                agent_id].make_sensor_labels(token)

    # START RECORDING
    default_instruction = [cid_obs[typ] for typ in ORDERED_TYPES]
    EX.update_state(default_instruction)
    recorder = experiment_output(EX, run_params)
    recorder.addendum('query_ids', QUERY_IDS)
    recorder.addendum('threshold', Threshold)
    recorder.addendum('Nsensors', NSENSORS)
    recorder.addendum('env_length', ENV_LENGTH)
    recorder.addendum('ground_targ', GROUND_TARG)
    recorder.record()

    # -------------------------------------RUN--------------------------------------------

    ## Main Loop:
    while EX.this_state(id_count) <= TOTAL_CYCLES:
        # update the state
        EX.update_state(default_instruction)
        recorder.record()

    # Wrap up and collect garbage
    recorder.close()
    EX.remove_experiment()
Beispiel #22
0
              "G5T. R/0.036458333333333925 EB5I R/0.040104166666667496 " \
              "G5/0.203125 R/0.04843749999999858 C5/0.5833333333333334 RH " \
              "BB5Q R/0.16666666666666607 A5T. R/0.036458333333333925 G5I " \
              "R/0.040104166666667496 A5/0.203125 R/0.04843749999999858 " \
              "D5/0.5833333333333334 R/0.5078125 A5/0.1234375 " \
              "R/0.02083333333333215 G5/0.0484375 R/0.0442708333333357 " \
              "F5/0.12083333333333333 R/0.0390625 C5/0.4817708333333333 " \
              "R/0.11093749999999858 A5/0.140625 R/0.014062500000001421 " \
              "G5/0.052083333333333336 R/0.04322916666666643 " \
              "F5/0.11197916666666667 R/0.02708333333333357 " \
              "C5/0.45208333333333334 R/0.16145833333333215"
    PATTERN = PATTERN.replace("/", "-")
    logging.debug("pattern: %s", PATTERN)

    words = PATTERN.split()
    logging.debug("words: %s", words)

    pairs = yield_pairs(words)
    logging.debug("pairs: %s", pairs)

    field = train(pairs, words[-1])
    logging.debug("field: %s", field)

    chain = [rnd(words)]
    for _ in range(80):
        chain.append(rnd(field[chain[-1]]))
    logging.debug("chain: %s", chain)

    RESULT = ' '.join(chain).replace("-", "/")
    logging.info("result: %s", RESULT)