Пример #1
0
 def setStep(self, left: float, right: float, up: float,
             down: float) -> None:
     self.step_left = Coord(left, 0)
     self.step_right = Coord(right, 0)
     self.step_up = Coord(0, up)
     self.step_down = Coord(0, down)
     self.move_vector = self.calculate_move_vector()
Пример #2
0
def main():
    print(os.getcwd())
    path = os.getcwd()
    anim_dir = path + "/anim6"
    if not os.path.exists(anim_dir):
        os.mkdir(anim_dir)
    #NodeBase.NodeBase.setup()
    land, w, h, top_left, bottom_right, g1, g2 = get_grid(
        "optimframe/src/data", -10)
    dx = np.abs(top_left.x - bottom_right.x) / float(w)
    dy = np.abs(top_left.y - bottom_right.y) / float(h)
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.set_xlim(0, w)
    ax.set_ylim(0, h)
    plt.imshow(np.log(land + 20), cmap="hot")
    plt.colorbar()
    bottom_left = Coord(top_left.x, bottom_right.y)
    top_right = Coord(bottom_right.x, top_left.y)

    n = setup_nodes(2, ax, bottom_left, top_right, dx, dy, 200, 128, land, g1,
                    g2)
    txt = plt.text(350, 730, "Iter: 0")
    for d in n():
        txt.set_text("Episode: %d, Iter: %d" % (d[0], d[1]))
        fig.canvas.draw()
        episode_dir = anim_dir + "/{}".format(d[0])
        if not os.path.exists(episode_dir):
            os.mkdir(episode_dir)
        plt.pause(0.001)
Пример #3
0
def main():
    top_left = Coord(-10, -10)
    bottom_right = Coord(10, 10)
    bottom_left = Coord(-10, 10)
    top_right = Coord(10, -10)
    w = 400
    h = 400
    dx = np.abs(top_left.x - bottom_right.x) / float(w)
    dy = np.abs(top_left.y - bottom_right.y) / float(h)
    x, y, z = get_grid(w, h, 50, top_left, bottom_right)

    point = Ellipse((20, 20), 10, 10, 0)
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.add_artist(point)
    #point.set_clip_box(ax.bbox)
    #point.set_alpha(np.random.rand())
    #point.set_facecolor(np.random.rand(3))
    ax.set_xlim(0, 400)
    ax.set_ylim(0, 400)
    plt.imshow(z, cmap="hot")
    n = node(Com(), bottom_left, top_right, dx, dy, z)
    plt.colorbar()
    txt = plt.text(200, 430, "Iter: 0, Temp: 1e9")
    for d in n():
        i, T, p = d
        txt.set_text("Iter: %d, Temp: %.3f" % (i, T))
        point.set_center(p)
        fig.canvas.draw()
        plt.pause(0.01)
Пример #4
0
def get_grid(file, z0=10.):

    top_left = Coord(0, 0)
    bottom_right = Coord(350, 350)
    w = 350  #eng.w
    h = 350  #eng.h

    x = np.linspace(top_left.x, bottom_right.x, w)
    y = np.linspace(top_left.y, bottom_right.y, h)
    xx, yy = np.meshgrid(x, y)
    g1 = 1e8 / ((xx - 100)**2 + (yy - 100)**2)
    g2 = 1e8 / ((xx - 200)**2 + (yy - 200)**2)
    land = g1 + g2

    return land, w, h, top_left, bottom_right, g1, g2
Пример #5
0
    def init(nodes):
        for i in range(len(nodes)):
            i0 = np.random.randint(50, 350)
            j0 = np.random.randint(50, 350)

            coord = Coord(bottom_left.x + dx * i0, top_right.y + dy * j0)
            nodes[i].n.initState(coord)
Пример #6
0
 def state_to_coord(self, state):
     pos = np.array(
         np.unravel_index(np.argmax(state[:, :, 0]), dims=self.state_dim))
     transformed_pos = np.multiply(pos, self.dim_rescale)
     x = transformed_pos[0] * self.dx + self.bottom_left.x
     y = transformed_pos[1] * self.dy + self.top_right.y
     return Coord(x, y)
Пример #7
0
def node(com, bottom_left, top_right, dx, dy, z):
    n = DummyGeoOrgNode("", com, 1. / (z[40, 40] + 0.01),
                        Coord(bottom_left.x + dx * 40, top_right.y + dy * 40),
                        [])
    n.setRange(bottom_left, top_right)
    n.setStep(-dx, dx, -dy, dy)
    N = 1000
    n.setJumpSize(2)
    n.temp_dec_speed = 2.

    def move():
        nonlocal N, n, z
        for i in range(1, N):
            n.tick(i, N)
            p = n.getCoord()
            xi = int(np.ceil((p.x - bottom_left.x) / dx))
            yi = int(np.ceil((p.y - top_right.y) / dy))
            if xi >= z.shape[0]:
                xi = z.shape[0] - 1
            if yi >= z.shape[1]:
                yi = z.shape[1] - 1
            n.setHits(1. / (z[xi, yi] + 0.01))
            yield [i, n.getT(), [xi, yi]]

    return move
Пример #8
0
def get_grid(file, z0=10.):
    eng = EnglandPopDistro.EnglandPopDistro()
    print("LOADING...")
    eng.load(file)

    top_left = Coord(0, 0)
    bottom_right = Coord(700, 700)
    w = eng.w
    h = eng.h

    data = np.zeros((w, h), dtype=np.float)
    for i in range(h):
        for j in range(w):
            data[j, i] = eng.get(i, j) + z0

    data = np.flipud(data)

    return data, w, h, top_left, bottom_right
Пример #9
0
def setup_nodes(num_of_nodes, ax, bottom_left, top_right, dx, dy, episodes, N,
                land, g1, g2):
    node_init = node_initializer(bottom_left, top_right, dx, dy, 2)

    nodes = []
    stateAdaptor = StateAdaptor(bottom_left, top_right, np.array([350, 350]),
                                np.array([350, 350]))
    for i in range(num_of_nodes):
        n = DQNGeoOrgNode(str(i), stateAdaptor)
        n.setJump(6)
        node_wrapper = TestNode(n, land, g2, Coord(100, 100), Coord(200, 200))
        nodes.append(node_wrapper)

    node_init(nodes)
    points = getVisPoints(ax, nodes)
    updateNodeStateChannel1(nodes)
    updateLastChannel(nodes, land)

    def move():
        nonlocal nodes, points
        for e in range(episodes):
            node_init(nodes)
            for i in range(N):
                rewards = ""
                losses = ""
                for ni in range(len(nodes)):
                    nodes[ni].run()
                    rewards += "e: %d, i: %d, n: %d: %.2f (%.2f), " % (
                        e, i, ni, nodes[ni].n.reward(), nodes[ni].n._h)
                    losses += "e: {}, i: {}, n: {}: {};".format(
                        e, i, ni, str(nodes[ni].n.getLoss()))
                    nodes[ni].tick()
                    p = nodes[ni].n.getCoord()
                    points[ni].set_center([p.x, p.y])
                updateNodeStateChannel1(nodes)
                if i % 2 == 1:
                    print("Rewards: {}".format(rewards))
                    print("Losses: {}".format(losses))
                if i % 2 == 0:
                    yield [e, i]
            for n in nodes:
                n.n.train(e)

    return move
Пример #10
0
def setup_nodes(num_of_nodes,
                ax,
                com,
                bottom_left,
                top_right,
                dx,
                dy,
                z,
                N=100000):
    nodes = []
    points = []
    for i in range(num_of_nodes):
        i0 = np.random.randint(200, 600)
        j0 = np.random.randint(100, 600)
        coord = Coord(bottom_left.x + dx * i0, top_right.y + dy * j0)
        n = QGeoOrgNode(str(i), com, z[i0, j0], coord, [])
        n.setRange(bottom_left, top_right)
        n.setStep(-dx, dx, dy, -dy)
        n.setJumpSize(4)
        #n.setAlpha(0.1)
        n.setEpsilon(0.1)
        n.setStateSize(700, 700)
        n.setQParams(0.01, 0.9)
        n.temp_dec_speed = 4.
        node_wrapper = TestNode(n)
        nodes.append(node_wrapper)
        point = Ellipse((coord.x, coord.y), 10, 10, 0)
        point.set_facecolor('blue')
        ax.add_artist(point)
        points.append(point)

    def move():
        nonlocal nodes, points
        for i in range(1, N):
            NodeBase.NodeBase.run()
            rewards = ""
            for n in range(len(nodes)):
                rewards += "%d: %.2f (%.2f), " % (n, nodes[n].node.value(),
                                                  nodes[n].node.h)
                nodes[n].node.tick(i, N)
                nodes[n].update_coords()
                p = nodes[n].node.getCoord()
                xi = int(np.ceil((p.x - bottom_left.x) / dx))
                yi = int(np.ceil((p.y - top_right.y) / dy))
                T = i  #nodes[n].node.getT()
                points[n].set_center([xi, yi])
            if i % 2 == 0:
                print("Rewards: {}".format(rewards))
                print_phase = False
            if i % 10 == 0:
                yield [i, T]
            if i % 1000 == 0:
                nodes[n].node.reset()

    return move
Пример #11
0
def setup_nodes(num_of_nodes,
                ax,
                com,
                bottom_left,
                top_right,
                dx,
                dy,
                z,
                N=100000):
    nodes = []
    points = []
    f = open("toby_loader/data/csv/centres-ordered-by-population.csv", "r")
    cities = [line.split(',') for line in f.readlines()]
    for i in range(num_of_nodes):
        i0 = int(cities[i][2])
        j0 = int(cities[i][1])
        coord = Coord(bottom_left.x + dx * i0, top_right.y + dy * j0)
        n = DummyGeoOrgNode(str(i), com, z[i0, j0], coord, [])
        n.setRange(bottom_left, top_right)
        n.setStep(-dx, dx, dy, -dy)
        n.setJumpSize(4)
        n.setAlpha(0.1)
        #n.setEpsilon(0.1)
        #n.setStateSize(700,700)
        #n.setQParams(0.01, 0.9)
        n.temp_dec_speed = 4.
        node_wrapper = TestNode(n)
        nodes.append(node_wrapper)
        point = Ellipse((coord.x, coord.y), 10, 10, 0)
        point.set_facecolor('blue')
        ax.add_artist(point)
        points.append(point)
    regions = np.ones((700, 700), dtype=np.int)

    def move():
        nonlocal nodes, points, regions
        for i in range(1, N):
            NodeBase.NodeBase.run()
            rewards = ""
            for n in range(len(nodes)):
                nodes[n].node.tick(i, N)
                nodes[n].update_coords()
                p = nodes[n].node.getCoord()
                xi = int(np.ceil((p.x - bottom_left.x) / dx))
                yi = int(np.ceil((p.y - top_right.y) / dy))
                T = nodes[n].node.getT()
                points[n].set_center([xi, yi])
                rewards += "%d: %.2f; " % (n, nodes[n].node.value())
            #NodeBase.NodeBase.set_regions(regions)
            #print(rewards)
            #print(regions)
            yield [i, T]

    return move
Пример #12
0
def main():
    NodeBase.NodeBase.setup()
    land, w, h, top_left, bottom_right = get_grid("optimframe/src/data", -10)
    dx = np.abs(top_left.x - bottom_right.x) / float(w)
    dy = np.abs(top_left.y - bottom_right.y) / float(h)
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.set_xlim(0, w)
    ax.set_ylim(0, h)
    plt.imshow(np.log(land + 20), cmap="hot")
    plt.colorbar()

    bottom_left = Coord(top_left.x, bottom_right.y)
    top_right = Coord(bottom_right.x, top_left.y)

    n = setup_nodes(20, ax, Com(), bottom_left, top_right, dx, dy, land)
    txt = plt.text(350, 730, "Iter: 0, Temp: 1e9")
    for d in n():
        i, T = d
        txt.set_text("Iter: %d, Temp: %.3f" % (i, T))
        fig.canvas.draw()
        plt.pause(0.01)
Пример #13
0
 def init(nodes):
     rnd_i = 0
     citi_i = 0
     for i in range(len(nodes)):
         if rnd_i < num_of_rand_nodes:
             rnd_i += 1
             i0 = np.random.randint(50, 650)
             j0 = np.random.randint(50, 650)
         else:
             i0 = int(cities[citi_i][2])
             j0 = int(cities[citi_i][1])
             citi_i += 1
         coord = Coord(bottom_left.x + dx * i0, top_right.y + dy * j0)
         nodes[i].n.initState(coord)
Пример #14
0
 def setStateSize(self, w, h, nw=28, nh=28):
     self.width = w
     self.height = h
     self.QTable = np.zeros((nw * nh, len(self.move_vector)))
     self.state_to_coord = {}
     dx = w / nw
     dy = h / nh
     for i in range(nw):
         for j in range(nh):
             self.state_to_coord[i + nw * j] = Coord(i * dx, j * dy)
     self.state = (np.random.randint(0,
                                     nw - 1), np.random.randint(0, nh - 1))
     self.state_to_idx = lambda ij: ij[0] + nw * ij[1]
     self.nw = nw
     self.nh = nh