示例#1
0
def generate():
    try:
        tracks = []
        last = []
        wait = 2  # seconds
        d = Database()
        while test:
            yield d.merge(client.get('/tracks/67106027'))
            yield d.merge(client.get('/tracks/66620496'))

        while True:
            log.info("Grabbing fresh tracklist from SoundCloud...")
            with Timer() as t:
                while not tracks:
                    try:
                        tracks =  client.get('/tracks', order='hotness', limit=200, offset=0)
                        tracks += client.get('/tracks', order='hotness', limit=200, offset=200)
                    except HTTPError as h:
                        log.warning("Got %s from SoundCloud. Retrying in %2.2f seconds...",
                                    h, wait)
                        time.sleep(wait)

            log.info("Got %d tracks in %2.2fms.", len(tracks), t.ms)

            if last and not any([t.id == last[-1].id for t in tracks]):
                tracks.append(last[-1])
            tracks = cull(tracks)

            try:
                tracks = [d.merge(t) for t in tracks]
            except:
                log.warning("Could not merge tracks with DB due to:\n%s", traceback.format_exc())

            log.info("Solving TSP on %d tracks...", len(tracks))
            with Timer() as t:
                tracks = [tracks[i] for i in tsp.solve(tracks, distance, len(tracks) * config.tsp_mult)]
            log.info("Solved TSP in %2.2fms.", t.ms)

            for track in tracks:
                for criterion in criteria:
                    criterion.postcompute(track)

            if last:
                i = getIndexOfId(tracks, last[-1].id) + 1
                tracks = tracks[i:] + tracks[:i]

            for track in tracks:
                yield track

            last = tracks
            tracks = []
    except:
        print traceback.format_exc()
        log.critical("%s", traceback.format_exc())
示例#2
0
def update_trip_path(trip_mpois, paths, graph):
    """
        solve TSP problem for finding the shortest path through all 
        the nodes in this trip 
    """
    n_nodes = len(trip_mpois)
    # adjacency matrix
    new_paths = np.zeros(shape=(n_nodes, n_nodes))

    # iterate through all the nodes and create a list of nodes with sequential id
    for i, node1 in enumerate(trip_mpois):
        for j, node2 in enumerate(trip_mpois):
            new_paths[i, j] = paths[node1, node2]

    # new_paths = new_paths/np.max(new_paths[new_paths < _INF])
    # new_paths[np.isinf(new_paths)] = _INF

    # create a dummy edge between end and start node with weight 0
    new_paths[1,0] = -_INF
    # new_paths[0,1] = _INF

    shortest_path = None
    if n_nodes > 5:
        shortest_path, dist = tsp.solve(n_nodes, new_paths)
        # shortest_path = range(n_nodes)
    else:
        shortest_path = range(n_nodes)

    trip_path = np.array(trip_mpois)[shortest_path]

    if ___DEBUG:
        fname = 'dump/' + str(n_nodes) + '.dist'
        np.savetxt(fname, new_paths, fmt='%.6f')
    
        mpoi_pos = np.zeros(shape=(n_nodes,2))
    
        for i, node in enumerate(trip_mpois):
            pos_3d = graph.vs[node]['position']
            assert node == graph.vs[node].index
            mpoi_pos[i,:] = pos_3d[:2]

        fname = 'dump/' + str(n_nodes) + '.pos'
        np.savetxt(fname, mpoi_pos)
    
    # print trip_mpois, trip_path

    return trip_path
示例#3
0
    for i in xrange(1, len(tracks)):
        print "%2.2f" % distance(tracks[i], tracks[i - 1]),
        for criterion in criteria:
            print "\t%2.1f/%2.1f" % criterion(tracks[i], tracks[i - 1]),
        print "\t%2.1f" % ((tracks[i].tempo if hasattr(tracks[i], 'tempo') else tracks[i].bpm) or 0),
        print "\t", tracks[i].title, "by", tracks[i].user['username']

if __name__ == "__main__":
    print "Testing the BRAIN..."
    d = Database()
    o = None
    while not o:
        try:
            o  = client.get('/tracks', order='hotness', limit=200)
            o += client.get('/tracks', order='hotness', limit=200, offset=200)
        except HTTPError as e:
            print "Error from SC. (%s) Trying again..." % e
            pass
    tracks = cull([d.merge(t) for t in o])

    print "Solving TSP on %d tracks..." % len(tracks)

    with Timer() as t:
        tracks = [tracks[i] for i in tsp.solve(tracks, distance, len(tracks) * config.tsp_mult)]
    print "Solved TSP in %2.2fms." % t.ms
    #   TODO:   Use standard deviation to find the limit of deviation
    #           for tempo difference in tracks. I.e.: Any tracks that don't
    #           fit next to their neighbours should be removed, then TSP re-run.

    print_table(tracks)
示例#4
0
def generate():
    try:
        tracks = []
        last = []
        wait = 2  # seconds
        d = Database()
        while test:
            yield d.merge(client.get('/tracks/73783917'))

        while True:
            log.info("Grabbing fresh tracklist from SoundCloud...")
            with Timer() as t:
                while not tracks:
                    try:
                        tracks =  client.get('/tracks', order='hotness', limit=200, offset=0)
                        tracks += client.get('/tracks', order='hotness', limit=200, offset=200)
                    except Exception as e:
                        log.warning("Got %s from SoundCloud. Retrying in %2.2f seconds...",
                                    e, wait)
                        time.sleep(wait)

            log.info("Got %d tracks in %2.2fms.", len(tracks), t.ms)
            emit('tracks_fetch', {"count": len(tracks), "ms": t.ms})

            if last and not any([t.id == last[-1].id for t in tracks]):
                tracks.append(last[-1])
            tracks = cull(tracks)

            tracks += list(get_force_mix_tracks(d))

            try:
                tracks = [d.merge(t) for t in tracks]
            except:
                log.warning("Could not merge tracks with DB due to:\n%s", traceback.format_exc())

            log.info("Solving TSP on %d tracks...", len(tracks))
            with Timer() as t:
                tracks = [tracks[i] for i in tsp.solve(tracks, distance, len(tracks) * config.tsp_mult)]
            log.info("Solved TSP in %2.2fms.", t.ms)
            emit('tsp_solve', {"count": len(tracks), "ms": t.ms})

            for track in tracks:
                for criterion in criteria:
                    criterion.postcompute(track)

            if last:
                i = getIndexOfId(tracks, last[-1].id) + 1
                tracks = tracks[i:] + tracks[:i]

            for track in tracks:
                for priority in get_immediate_tracks(d):
                    emit('decide_priority')
                    yield priority
                emit('decide_normal')
                yield track

            last = tracks
            tracks = []
    except:
        print traceback.format_exc()
        log.critical("%s", traceback.format_exc())
示例#5
0
    for i in xrange(1, len(tracks)):
        print "%2.2f" % distance(tracks[i], tracks[i - 1]),
        for criterion in criteria:
            print "\t%2.1f/%2.1f" % criterion(tracks[i], tracks[i - 1]),
        print "\t%2.1f" % ((tracks[i].tempo if hasattr(tracks[i], 'tempo') else tracks[i].bpm) or 0),
        print "\t", tracks[i].title, "by", tracks[i].user['username']

if __name__ == "__main__":
    print "Testing the BRAIN..."
    d = Database()
    o = None
    while not o:
        try:
            o  = client.get('/tracks', order='hotness', limit=200)
            o += client.get('/tracks', order='hotness', limit=200, offset=200)
        except HTTPError as e:
            print "Error from SC. (%s) Trying again..." % e
            pass
    tracks = cull([d.merge(t) for t in o])

    print "Solving TSP on %d tracks..." % len(tracks)

    with Timer() as t:
        tracks = [tracks[i] for i in tsp.solve(tracks, distance, len(tracks) * config.tsp_mult)]
    print "Solved TSP in %2.2fms." % t.ms
    #   TODO:   Use standard deviation to find the limit of deviation
    #           for tempo difference in tracks. I.e.: Any tracks that don't
    #           fit next to their neighbours should be removed, then TSP re-run.

    print_table(tracks)
示例#6
0
def generate():
    try:
        tracks = []
        last = []
        wait = 2  # seconds
        d = Database()
        while test:
            yield d.merge(client.get('/tracks/67106027'))
            yield d.merge(client.get('/tracks/66620496'))

        while True:
            log.info("Grabbing fresh tracklist from SoundCloud...")
            with Timer() as t:
                while not tracks:
                    try:
                        tracks = client.get('/tracks',
                                            order='hotness',
                                            limit=200,
                                            offset=0)
                        tracks += client.get('/tracks',
                                             order='hotness',
                                             limit=200,
                                             offset=200)
                    except HTTPError as h:
                        log.warning(
                            "Got %s from SoundCloud. Retrying in %2.2f seconds...",
                            h, wait)
                        time.sleep(wait)

            log.info("Got %d tracks in %2.2fms.", len(tracks), t.ms)

            if last and not any([t.id == last[-1].id for t in tracks]):
                tracks.append(last[-1])
            tracks = cull(tracks)

            try:
                tracks = [d.merge(t) for t in tracks]
            except:
                log.warning("Could not merge tracks with DB due to:\n%s",
                            traceback.format_exc())

            log.info("Solving TSP on %d tracks...", len(tracks))
            with Timer() as t:
                tracks = [
                    tracks[i] for i in tsp.solve(tracks, distance,
                                                 len(tracks) * config.tsp_mult)
                ]
            log.info("Solved TSP in %2.2fms.", t.ms)

            for track in tracks:
                for criterion in criteria:
                    criterion.postcompute(track)

            if last:
                i = getIndexOfId(tracks, last[-1].id) + 1
                tracks = tracks[i:] + tracks[:i]

            for track in tracks:
                yield track

            last = tracks
            tracks = []
    except:
        print traceback.format_exc()
        log.critical("%s", traceback.format_exc())