Esempio n. 1
0
def distance_field(grid, position, obstacles):
    """
    Compute a distance field on a grid in a given position, 
    with the given obstacles.
    """
    boundary = Queue()                            # bsf uses queue dynamics
    boundary.enqueue((position[0], position[1]))  # put the starting grid in the queue
    visited = [[EMPTY for _ in xrange(WIDTH)] for _ in xrange(HEIGHT)]
    visited[position[0]][position[1]] = FULL      # set the starting cell to visited
    field = Grid(HEIGHT, WIDTH, STRMAP)           # we'll store the distance field as a grid
    field.set_all_to(-1)                          # -1 will symbolize unreachable cells

    # Set obstacles
    for obstacle in obstacles:
        grid.set_obstacle(obstacle[0], obstacle[1])
        field.set_obstacle(obstacle[0], obstacle[1])
    field.set_empty(position[0], position[1])                   # set the root position

    while boundary:                                             # while the queue is not empty
        current = boundary.dequeue()                            # get a grid cell from the queue
        neighbors = grid.four_neighbors(current[0], current[1]) # get the four neighbor cells:
                                                                # up, down, left, and right

        for neighbor in neighbors:                            # for every neighbor cell
            # If the cell hasn't been visited and has no obstacles:
            if not visited[neighbor[0]][neighbor[1]] and grid.is_empty(neighbor[0], neighbor[1]):
                visited[neighbor[0]][neighbor[1]] = FULL      # set it as visited
                boundary.enqueue(neighbor)                    # add it to the queue
                field.set_to(neighbor[0], neighbor[1],        # set the distance from the root
                    field.get_cell(current[0], current[1]) + 1)

    return field
Esempio n. 2
0
def bfs(grid, row, col):
    """
    Perform a breath-first search in the given grid, starting
    in the given cell.
    """
    boundary = Queue()            # bsf uses queue dynamics
    boundary.enqueue((row, col))  # put the starting grid in the queue
    visited = [[EMPTY for _ in xrange(WIDTH)] for _ in xrange(HEIGHT)]
    visited[row][col] = FULL      # set the starting cell to visited

    while boundary:                                             # while the queue is not empty
        current = boundary.dequeue()                            # get a grid cell from the queue
        grid.set_marked(current[0], current[1])                 # (this is for the print)
        neighbors = grid.four_neighbors(current[0], current[1]) # get the four neighbor cells:
        print grid                                              # up, down, left, and right

        for neighbor in neighbors:                        # for every neighbor cell
            if not visited[neighbor[0]][neighbor[1]]:     # if it hasn't been visited
                visited[neighbor[0]][neighbor[1]] = FULL  # set it as visited
                boundary.enqueue(neighbor)                # add it to the queue
                grid.set_full(neighbor[0], neighbor[1])   # (this is for the print)
        print grid
from crawler import Scraper
from multiprocessing import Process

#This basic server will send data to the leaflet frontend
from random import randint
import json
from flaskext import uploads
import pandas as pd
import os
from werkzeug import secure_filename
from glob import glob
from tools import Queue
from central_server import InvestigationLogger

scraper = Scraper()
queue = Queue()


@app.route("/", methods=["GET", "POST"])
def index():
    return render_template("index.html")


#Data Visualization Routes
#Map routes
UPLOAD_FOLDER = os.getcwd() + "/static/uploads"
ALLOWED_EXTENSIONS = set(['csv'])

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

        def linear_split(data, Q_max, max_tour_duration, nr_vehicles):
            """
            # TODO Create docstring

            Args:
                child:

            Returns:

            """
            nonlocal self
            length_penalty = self.solver_inst.w_penalty_duration
            load_penalty = self.solver_inst.w_penalty_load

            def cost_achieved(data, i, x, Q, D, T):
                if Q[x] - Q[i] <= 2 * Q_max:
                    return cost(data, i, x, D, Q, T)
                return sys.maxsize

            def cost(data, i, j, D, Q, T):
                return data[i + 1][3] + D[j] - D[
                    i + 1] + data[j][3] + T[j] - T[i] + load_penalty * max(
                        Q[j] - Q[i] - Q_max, 0) + length_penalty * max(
                            +T[j] + D[j] - D[i + 1] - T[i] - max_tour_duration,
                            0)

            def dominates(data, i, j, k, p, D, Q, T):
                if i <= j:
                    return p[k][i] + data[i + 1][3] - D[i + 1] + T[j] - T[i] + load_penalty * (Q[j] - Q[i]) <= p[k][j] + \
                           data[j + 1][3] - D[
                               j + 1]
                else:
                    return p[k][i] + data[i + 1][3] - D[
                        i + 1] <= p[k][j] + data[j + 1][3] - D[j + 1]

            nr_customers = len(data)

            # compute cumulative distance
            demand = [0] * nr_customers
            cumulated_load = [0] * nr_customers
            duration = [0] * nr_customers
            for index in range(nr_customers):
                distance = 0
                for i in range(0, index):
                    if index > 0:
                        distance += data[i + 1][2]
                demand[index] = distance

            # compute cumulative load
            for index in range(nr_customers):
                load_temp = 0
                duration_temp = 0
                for i in range(1, index + 1):
                    load_temp += data[i][4]
                    duration_temp += data[i][5]
                cumulated_load[index] = load_temp
                duration[index] = duration_temp

            # initializing lists
            p = [[sys.maxsize for _ in range(nr_customers)]
                 for _ in range(nr_vehicles + 1)]
            qu = Queue()
            pred = [[0 for _ in range(nr_customers)]
                    for _ in range(nr_vehicles + 1)]
            p[0][0] = 0

            subsolution = list()
            for k in range(nr_vehicles):
                qu.queue.clear()
                qu.push_back(k)
                for t in range(k + 1, nr_customers):
                    if len(qu.queue) > 0:
                        p[k + 1][t] = p[k][qu.front()] + cost_achieved(
                            data,
                            i=qu.front(),
                            x=t,
                            Q=cumulated_load,
                            D=demand,
                            T=duration)
                        pred[k + 1][t] = qu.front()
                        if t < nr_customers - 1:
                            if not dominates(data=data,
                                             i=qu.back(),
                                             j=t,
                                             k=k,
                                             p=p,
                                             D=demand,
                                             Q=cumulated_load,
                                             T=duration):
                                while len(qu.queue) > 0 and dominates(
                                        data=data,
                                        i=t,
                                        j=qu.back(),
                                        k=k,
                                        Q=cumulated_load,
                                        p=p,
                                        D=demand,
                                        T=duration):
                                    qu.pop_back()
                                qu.push_back(t=t)
                            while len(qu.queue) > 1 and p[k][qu.front()] + cost_achieved(data=data, i=qu.front(),
                                                                                         x=t + 1,
                                                                                         Q=cumulated_load, D=demand,
                                                                                         T=duration) >= \
                                    p[k][qu.front2()] + cost_achieved(data=data, i=qu.front2(), x=t + 1,
                                                                      Q=cumulated_load, D=demand,
                                                                      T=duration):
                                qu.pop_front()

            start = pred[nr_vehicles][nr_customers - 1]
            subsolution.append(
                [data[x][0] for x in range(start + 1, nr_customers)])
            index = start
            for i in range(nr_vehicles - 1, 0, -1):
                start = pred[i][index]
                subsolution.append(
                    [data[x][0] for x in range(start + 1, index + 1)])
                index = start
            return subsolution