Exemplo n.º 1
0
def random_solution(station_objects, connection_objects, route_maximum,
                    time_maximum):
    while True:
        visited_connections = []
        total_time = 0
        lining = []

        # make 'route_maximum' routes at max
        for total_routes in range(route_maximum):

            # sets starting station randomly
            current_station = station_objects[randrange(len(station_objects))]

            # starts new route
            route = Route(total_routes, current_station)

            # add route to lining
            lining.append(route)

            # keeps on adding stations until maximum time has been reached
            while True:

                # when all connections are used, return the lining and thus end the algorithm
                if len(connection_objects) == len(visited_connections):
                    return lining

                # picks a random new station out of all connections of the current station
                new_station = random.choice(
                    list(current_station.connections.keys()))

                # finds the time for the new station
                time = int(current_station.connections[new_station])

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # add a new station to the route
                route.add_station(new_station, time)

                # find the connection that was added
                for connection in connection_objects:
                    if (connection.station_a == current_station
                            and connection.station_b == new_station) or (
                                connection.station_a == new_station
                                and connection.station_b == current_station):

                        # if the connection wasn't used before, add it to the visited connections list
                        if connection in visited_connections:
                            break
                        visited_connections.append(connection)

                # set this new station as the current station
                current_station = new_station
Exemplo n.º 2
0
def longest(station_objects, connection_objects, route_maximum, time_maximum):
    while True:

        visited_connections = []
        total_time = 0

        # makes a list of the solution that matches the requirements
        lining = []

        # make 'route_maximum' routes at max
        for total_routes in range(route_maximum):

            # picks a random station to the begin the route from
            current_station = station_objects[randrange(len(station_objects))]

            # starts new route | SJ: WAT DOET DIT?
            route = Route(total_routes, current_station)

            # add route to lining
            lining.append(route)

            # keeps on adding stations until maximum time has been reached
            while True:

                # when all connections have been used, end the algorithm
                if len(connection_objects) == len(visited_connections):
                    solution = Solution(lining, 0.8)
                    return solution

                # make a list of all unused connections of this station
                unused_connections = []
                for connection in current_station.connections:
                    unused_connections.append(connection)
                    for visit in visited_connections:
                        if (visit.station_a == connection
                                and visit.station_b == current_station) or (
                                    visit.station_b == connection
                                    and visit.station_a == current_station):
                            unused_connections.remove(connection)

                # if there are unused connections, find the shortest connection
                if len(unused_connections) > 0:

                    # sets a shortest connection/distance variable
                    longest = None
                    for connection in unused_connections:
                        if longest is None:
                            longest = connection
                        else:
                            if current_station.connections[
                                    connection].time > current_station.connections[
                                        longest].time:
                                longest = connection
                    new_station = longest

                # if there are no unused connections, pick a random connection
                else:
                    new_station = random.choice(
                        list(current_station.connections.keys()))

                # finds the connection
                link = current_station.connections[new_station]

                # finds the time for the new station
                time = link.time

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # add a new station to the route
                route.add_connection(link, time)

                # adds the station to the route
                route.add_station(new_station)

                # find the connection that was added
                for connection in connection_objects:
                    if (connection.station_a == current_station
                            and connection.station_b == new_station) or (
                                connection.station_a == new_station
                                and connection.station_b == current_station):

                        # if the connection wasn't used before, add it to the visited connections list
                        if connection in visited_connections:
                            break
                        visited_connections.append(connection)

                # set this new station as the current station
                current_station = new_station
Exemplo n.º 3
0
def random_solution(station_objects, len_connections, route_maximum,
                    time_maximum, requested_p):

    # while true, reboots the attributes to find a new, valid solution
    while True:

        p = 0
        total_time = 0
        lining = []
        visited_connections = []

        # creates new routes until reached the max. number of routes
        for route_nr in range(route_maximum):

            # sets starting station using random
            current_station = station_objects[randrange(len(station_objects))]

            # makes new route by passing the route number and current station
            route = Route(route_nr, current_station)

            # adds route to lining
            lining.append(route)

            # keeps on adding connections until maximum time has been reached
            while True:

                # p equals or is larger than 0.8, return the lining and thus end the algorithm
                if p >= requested_p:
                    solution = Solution(lining, p)
                    return solution

                # picks a random new station out of all connections of the current station
                new_station = random.choice(
                    list(current_station.connections.keys()))

                # finds the connection
                link = current_station.connections[new_station]

                # finds the time for the new station
                time = link.time

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # adds the new connection to the route
                route.add_connection(link, time)

                # adds the station to the route
                route.add_station(new_station)

                # calculates what connections have been visited by the routes
                if link not in visited_connections:
                    visited_connections.append(link)

                # calculates k
                p = len(visited_connections) / len_connections

                # sets the new station as the current station
                current_station = new_station
Exemplo n.º 4
0
def unused(station_objects, len_connections, route_maximum, time_maximum,
           requested_p):
    while True:

        visited_connections = []
        total_time = 0
        p = 0

        # makes a list of the solution that matches the requirements
        lining = []

        # make 'route_maximum' routes at max
        for route_nr in range(route_maximum):

            # picks a random station to the begin the route from
            current_station = station_objects[randrange(len(station_objects))]

            # starts new route
            route = Route(route_nr, current_station)

            # add route to lining
            lining.append(route)

            # keeps on adding stations until maximum time has been reached
            while True:

                # p equals or is larger than 0.8, return the lining and thus end the algorithm
                if p >= requested_p:
                    solution = Solution(lining, p)
                    return solution

                # make a list of all unused connections of this station
                unused_connections = []
                for connection in current_station.connections:
                    if current_station.connections[
                            connection] not in visited_connections:
                        unused_connections.append(connection)

                # if there are unused connections, pick one randomly
                if len(unused_connections) > 0:
                    new_station = random.choice(unused_connections)
                    link = new_station
                # if there are no unused connections, pick a random connection
                else:
                    new_station = random.choice(
                        list(current_station.connections.keys()))

                link = current_station.connections[new_station]

                # finds the time for the new station
                time = link.time

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # add a new connection to the route
                route.add_connection(link, time)

                # add new station to the route
                route.add_station(new_station)

                if link not in visited_connections:
                    visited_connections.append(link)

                # calculates p
                p = len(visited_connections) / len_connections

                # set this new station as the current station
                current_station = new_station
Exemplo n.º 5
0
def railhead(station_objects, connection_objects, route_maximum, time_maximum):
    
    # while true, reboots the attributes to find a new, valid solution
    while True: 
        
        p = 0 
        visited_connections = []
        total_time = 0
        lining = []
        available_railheads = []
        non_railhead_stations = []
        p = 0

        # DIT KAN VAST EENVOUDIGER (SJ)
        # checks whether a station is a railhead and adds to list, else adds it to a non-railhead list 
        for station in station_objects:
            if station.rail_head is True:
                available_railheads.append(station)
            non_railhead_stations.append(station)
                
        # make 'route_maximum' routes at max
        for route_nr in range(route_maximum):
            
            # when railheads are available, pick one of these as a starting station
            if len(available_railheads) > 0:
                current_station = available_railheads[randrange(len(available_railheads))]
                
                # remove this station from the available railheads
                available_railheads.remove(current_station)

            else:
                # else pick a random starting station out of the non-railhead stations
                current_station = non_railhead_stations[randrange(len(non_railhead_stations))]

            # start new route
            route = Route(route_nr, current_station)

            # add route to lining
            lining.append(route)

            # keep on adding stations until maximum time has been reached 
            while True:
                
                # p equals or is larger than 0.8, return the lining and thus end the algorithm
                if p >= 0.8:
                    solution = Solution(lining, p)
                    return solution

                # pick a random new station out of all connections of the current station
                new_station = random.choice(list(current_station.connections.keys()))

                # if new station is a railhead, remove it from list
                if new_station.rail_head and new_station in available_railheads:
                    available_railheads.remove(new_station)

                # finds the connection
                link = current_station.connections[new_station]           
                
                # find the time for the new station 
                time = link.time
                
                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # adds the new connection to the route 
                route.add_connection(link, time)

                # adds the station to the route
                route.add_station(new_station)
                
                # calculates what connections have been visited by the routes              
                if link not in visited_connections:
                   visited_connections.append(link)

                # calculates p
                p = len(visited_connections) / len(connection_objects)
               
                # set this new station as the current station
                current_station = new_station
Exemplo n.º 6
0
def shortest(station_objects, len_connections, route_maximum, time_maximum,
             requested_p):
    while True:

        p = 0
        visited_connections = []
        total_time = 0

        # makes a list of the solution that matches the requirements
        lining = []

        # make 'route_maximum' routes at max
        for total_routes in range(route_maximum):

            # picks a random station to the begin the route from
            current_station = station_objects[randrange(len(station_objects))]

            # starts new route
            route = Route(total_routes, current_station)

            # add route to lining
            lining.append(route)

            # keeps on adding stations until maximum time has been reached
            while True:

                # when all connections have been used, end the algorithm
                if p >= requested_p:
                    solution = Solution(lining, 1)
                    return solution

                # make a list of all unused connections of this station
                unused_connections = []
                for connection in current_station.connections:
                    if current_station.connections[
                            connection] not in visited_connections:
                        unused_connections.append(connection)

                # if there are unused connections, find the shortest connection
                if len(unused_connections) > 0:

                    # sets a shortest connection/distance variable
                    shortest = None
                    for connection in unused_connections:
                        # connection = current_station.connections[connection]
                        if shortest is None:
                            shortest = connection

                        else:
                            if current_station.connections[
                                    connection].time < current_station.connections[
                                        shortest].time:
                                shortest = connection
                    new_station = shortest

                # if there are no unused connections, pick a random connection
                else:
                    new_station = random.choice(
                        list(current_station.connections.keys()))

                # find the corresponding connection
                link = current_station.connections[new_station]

                # finds the time for the new station
                time = link.time

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    total_time += route.total_time
                    break

                # add the new connection to the route
                route.add_connection(link, time)

                # add the station to the route
                route.add_station(new_station)

                # update visited connections:
                if link not in visited_connections:
                    visited_connections.append(link)

                # calculate p
                p = len(visited_connections) / len_connections

                # set this new station as the current station
                current_station = new_station
Exemplo n.º 7
0
def greedy_lookahead(station_objects, len_connections, route_maximum,
                     time_maximum, requested_p):

    while True:

        visited_connections = []
        p_per_connection = 1 / len_connections
        lining = []
        p = 0

        # make 'route_maximum' routes at max
        for total_routes in range(route_maximum):

            status = False
            while status == False:

                # kies een beginstation dat nog ongebruikte connecties heeft
                current_station = station_objects[randrange(
                    len(station_objects))]

                for connection in current_station.connections:
                    if current_station.connections[
                            connection] not in visited_connections:
                        status = True

            # starts new route
            route = Route(total_routes, current_station)

            # add route to lining
            lining.append(route)

            # keeps on adding stations until maximum time has been reached
            while True:

                # p equals or is larger than x, return the lining and thus end the algorithm
                if p >= requested_p:
                    solution = Solution(lining, p)
                    return solution

                options = {}

                # loop over all possibilities for this station
                for connection in current_station.connections:

                    # for each of these options, find all their children
                    for child_connection in connection.connections:

                        option = [current_station]
                        option.append(connection)
                        option.append(child_connection)

                        # find the connection that was added
                        future_connections = []

                        link1 = current_station.connections[connection]
                        link2 = connection.connections[child_connection]
                        future_connections.append(link1)
                        future_connections.append(link2)

                        options[tuple(option)] = tuple(future_connections)

                possible_k = {}

                # bekijk alle opties
                for option in options:

                    min = 0
                    p_upgrade = 0
                    k = 0

                    # per optie, bekijk iedere connectie
                    for possible_connect in options[option]:

                        # haal de tijd uit de connectie en bereken de totale tijd
                        min += possible_connect.time

                        # kijk of de connecties al bereden zijn, zo niet verhoog p
                        if possible_connect not in visited_connections:
                            p_upgrade += p_per_connection

                        # bereken de k van deze optie
                        k = 10000 * p_upgrade - min
                        possible_k[option] = k

                # zoek de optie met de hoogste k
                best = None
                for possibility in possible_k:
                    if best is None:
                        best = possibility
                    else:
                        if possible_k[possibility] > possible_k[best]:
                            best = possibility

                # when the best option doesn't improve the score, stop adding connections to this route
                if possible_k[best] <= 0:
                    break

                # when it does improve the score, define the new station
                new_station = best[1]

                # finds the connection
                link = current_station.connections[new_station]

                # finds the time for the new station
                time = int(current_station.connections[new_station].time)

                # stops adding stations until the total time would exceed the maximum time
                if time + route.total_time > time_maximum:
                    break

                # adds the new connection to the route
                route.add_connection(link, time)

                # adds the station to the route
                route.add_station(new_station)

                # calculates what connections have been visited by the routes
                if link not in visited_connections:
                    visited_connections.append(link)

                # calculates p
                p = len(visited_connections) / len_connections

                current_station = new_station