コード例 #1
0
ファイル: breadth_first.py プロジェクト: lexpoon/RailNL-OLA
def breadth_first(map, max_routes, max_time, min_score, depth, ratio):
    """Create solution consisting of set of routes based on breadth first algorithm.

    Keyword arguments:
    depth (int)     length of route after which can be pruned
    ratio (int)     score/length ratio in order to best solution after which can be pruned
    """

    # Get all data for stations in current map
    data = RailNL(map).data

    # Create list where routes can be added
    routes = []

    # Keep track of fraction of used connections
    num_connections = len(all_connections(map))
    connections = connections_station(data)

    # Create random routes until it is not possible anymore due to the constrains
    while len(routes) < max_routes and len(
            connections["used_connections"]) < num_connections:
        breadth_first_route(map,
                            max_time,
                            min_score,
                            data,
                            routes,
                            depth,
                            ratio,
                            definition=None)
        connections = update_connections(map, data, routes)

    return Solution(map, routes)
コード例 #2
0
ファイル: visualize.py プロジェクト: lexpoon/RailNL-OLA
def visualize_all_connections(map, fig):
    """Add a grey line for each possible connection to scattermapbox."""

    data = RailNL(map).data

    # Retrieve coordinates for connections in map
    with open(f'data/Connecties{map}.csv', "r") as f:
        csv_reader = reader(f)
        for connection in csv_reader:
            lon = []
            lat = []
            lat.append(data[connection[0]].coordinates["long"])
            lon.append(data[connection[0]].coordinates["lat"])
            lat.append(data[connection[1]].coordinates["long"])
            lon.append(data[connection[1]].coordinates["lat"])

            # Add grey trace for each connection
            fig.add_trace(go.Scattermapbox(
                mode = "lines",
                lon = lon,
                lat = lat,
                marker = { 'size': 10, 'color': 'rgb(204, 204, 204)' },
                showlegend = False
            ))

    return fig
コード例 #3
0
ファイル: hillclimber.py プロジェクト: lexpoon/RailNL-OLA
def hillclimber(map, max_routes, max_time, min_score, solution, algorithm, depth, ratio, change_routes):
    """"Create hillclimber solution based on greedy output.

    Keyword arguments:
    algorithm (str)     algorithm that will be used to improve solution
    depth (int)         length of route after which can be pruned
    ratio (int)         score/length ratio in order to best solution after which can be pruned
    change_routes (int) amount of routes that can be improved
    """

    # Get all data from stations in map
    data = RailNL(map).data

    # Set best solution which can should be improved
    best_solution = solution

    # Remove routes that needs to be changed
    last_solution = deepcopy(best_solution)
    last_solution = remove_routes(last_solution, change_routes)

    # Add routes to solution to get new solution
    new_routes = add_routes(map, max_time, min_score, data, last_solution,
        algorithm, depth, ratio, change_routes, "improve")

    new_solution = Solution(map, new_routes)

    # Change best solution if needed
    if new_solution.score > last_solution.score:
        best_solution = new_solution

    return best_solution
コード例 #4
0
def short_route_swap(map, max_time, min_score, solution_output):
    """"Create hillclimber solution based on solution output."""

    # Delete short routes from solution
    solution = delete_short_route(map, min_score, solution_output)

    # Return solution if all connections are already used
    if solution == False:
        return Solution(map, solution_output.routes)

    # Update connections that are unused
    data = RailNL(map).data
    solution = Solution(map, solution.routes)
    connections_left = update_connections(
        map, data, solution.routes)['amount_connections']

    # Find stations with unused connection and add these to route
    for traject in solution.routes:
        new_solution = add_unused_connection(solution, map, data, traject,
                                             connections_left)

        if new_solution == False:
            return Solution(map, solution_output.routes)

    return Solution(map, new_solution.routes)
コード例 #5
0
def simulated_annealing(map, max_routes, max_time, min_score, solution,
                        algorithm, depth, ratio, change_routes, i, iterations,
                        formula):
    """"Create hillclimber solution based on greedy output.

    Keyword arguments:
    algorithm (str)         algorithm that will be used to improve solution
    depth (int)             length of route after which can be pruned
    ratio (int)             score/length ratio in order to best solution after which can be pruned
    change_routes (int)     amount of routes that can be improved
    i (int)                 ith iteration
    iterations	 (int)      total iterations of improving
    formula (str)           temperature formula
    """

    # Get all data from stations in map
    data = RailNL(map).data

    # Set best solution which can should be improved
    best_solution = solution

    # Remove routes that needs to be changed
    last_solution = deepcopy(best_solution)
    last_solution = remove_routes(last_solution, change_routes)

    # Add routes to solution to get new solution
    new_routes = add_routes(map, max_time, min_score, data, last_solution,
                            algorithm, depth, ratio, change_routes, "improve")

    new_solution = Solution(map, new_routes)

    # Set temperature depending of formula
    if formula == "linear":
        temperature = iterations / 2 - 0.5 * i
    elif formula == "exponential":
        temperature = iterations / 2 * (0.995**i)

    # Determine if new solution needs to be accepted
    acceptation_probability = 2**Decimal(
        (new_solution.score - last_solution.score) / Decimal(temperature))
    random_probability = random()
    if acceptation_probability > random_probability:
        best_solution = new_solution

    return best_solution
コード例 #6
0
ファイル: randomize.py プロジェクト: lexpoon/RailNL-OLA
def randomize(map, max_routes, max_time):
    """Create solution consisting of random routes."""

    # Get all data of  stations of current map
    data = RailNL(map).data

    # Make empty list for all routes
    routes = []

    # Keep track of fraction of used connections
    num_connections = len(all_connections(map))
    connections = connections_station(data)

    # Make random routes untill it is not possible anymore due to the constrains
    while len(routes) < max_routes and len(
            connections["used_connections"]) < num_connections:
        random_route(map, max_time, data, routes)
        connections = update_connections(map, data, routes)

    # Make solution class and update attributes
    random_solution = Solution(map, routes)

    return random_solution
コード例 #7
0
ファイル: greedy.py プロジェクト: lexpoon/RailNL-OLA
def greedy(map, max_routes, max_time, key):
    """Create solution consisting of routes based on greedy algorithm.

    Keyword arguments:
    key (str)       options: [Connections, Time, Score]. Method Greedy makes choices
    """

    # Get all data from stations in map
    data = RailNL(map).data

    # Create list where routes can be added
    routes = []

    # Keep track of fraction of used connections
    num_connections = len(all_connections(map))
    connections = connections_station(data)

    # Make greedy routes until it is not possible anymore due to the constrains
    while len(routes) < max_routes and len(
            connections["used_connections"]) < num_connections:
        greedy_route(map, max_time, data, routes, key)
        connections = update_connections(map, data, routes)

    return Solution(map, routes)