Ejemplo n.º 1
0
        raise KeyboardInterrupt(best_sol)

    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_gap_algorithm(seed_method="cones"):
    algo_name = "FJ81-GAP"
    algo_desc = "Fisher & Jaikumar (1981) generalized assignment problem heuristic"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return gap_init(points,
                        D,
                        d,
                        C,
                        L=L,
                        st=st,
                        K=None,
                        minimize_K=minimize_K,
                        seed_edge_weight_type=wtt,
                        find_optimal_seeds=(not single),
                        seed_method=seed_method)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_gap_algorithm())
Ejemplo n.º 2
0
                        ])

                sol = new_sol
                c_lambda_incs = 0

    except KeyboardInterrupt:  # or SIGINT
        # Pass on the current solution forced feasbile by splitting routes
        #  according to the constraints.
        raise KeyboardInterrupt(_force_feasible(sol, D, d, C, L))

    return without_empty_routes(sol)


# Wrapper for the command line user interface (CLI)
def get_lr3opt_algorithm():
    algo_name = "SG84-LR3OPT"
    algo_desc = "Stewart & Golden (1984) Lagrangian relaxed 3-opt* heuristic"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        if minimize_K:
            raise NotImplementedError(
                "LR3OPT does not support minimizing the number of vehicles")
        return lr3opt_init(D, d, C, L)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_lr3opt_algorithm())
Ejemplo n.º 3
0
        if not p1_better_than_p2 and p2_best_so_far:
                best_sol = phase2_sol
                best_f = phase2_f
                best_K = phase2_K
        
        if interrupted:
            # pass on the current best solution
            raise KeyboardInterrupt(best_sol)
        
        # deterministic version, no retries
        # stochastic version terminates as soon as phase2 succeeds
        if (rr is None) or (phase2_sol is not None):
            break

        
    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_cmt2p_algorithm():
    algo_name = "CMT79-2P"
    algo_desc = "Christofides, Mingozzi & Toth (1979) two phase heuristic"
    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return cmt_2phase_init(D, d, C, L, minimize_K)
    return (algo_name, algo_desc, call_init)
    
if __name__=="__main__":
    from shared_cli import cli
    cli(*get_cmt2p_algorithm())
Ejemplo n.º 4
0
            sol_K = sol.count(0) - 1
        if is_better_sol(best_f, best_K, sol_f, sol_K, minimize_K):
            best_sol = sol
            best_f = sol_f
            best_K = sol_K

        if interrupted:
            # pass on the current best_sol
            raise KeyboardInterrupt(best_sol)

    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_gs_algorithm():
    algo_name = r"Ga67-PS|pi+lamda"
    algo_desc = r"Parallel savings algorithm with Gaskell (1967) $\pi$ and "+\
                r"$\lambda$ criteria"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        savings_method = "both" if not single else "pi"
        return gaskell_savings_init(D, d, C, L, minimize_K, savings_method)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_gs_algorithm())
Ejemplo n.º 5
0
            best_K = auto_route_count
            for k in range(2,auto_route_count+1):
                sol = nearest_neighbor_init(D, d, C, L, emerging_route_count=k)
                sol = without_empty_routes(sol)
                sol_f = objf(sol,D)
                sol_K = sol.count(0)-1
                
                if is_better_sol(best_f, best_K, sol_f, sol_K, minimize_K):
                    best_sol = sol
                    best_f = sol_f
                    best_K = sol_K
                    
            return best_sol
    elif emerging_route_count>1:
        def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
            if minimize_K:
                # todo: remove this when supprot (see TODO notes in algo desc)
                raise NotImplementedError("Nearest neighbor algorithm does "+
                                          " not support minimizing the number"+
                                          " of vehicles")
            return nearest_neighbor_init(D, d, C, L, emerging_route_count)
    else:
        raise ValueError("Not a valid emerging_route_count value "+
                         "(%s) for parallel algorithm"%
                         str(emerging_route_count))
    return (algo_name, algo_desc, call_init)
        
if __name__=="__main__":
    from shared_cli import cli
    cli(*get_pnn_algorithm())
Ejemplo n.º 6
0
            emerging_route_nodes.append(0)
            solution += emerging_route_nodes
            emerging_route_nodes = None

    except KeyboardInterrupt:
        interrupted_solution = solution
        if emerging_route_nodes:
            emerging_route_nodes.append(0)
            interrupted_solution += emerging_route_nodes
        interrupted_solution += routes2sol([n] for n in unrouted)[1:]
        raise KeyboardInterrupt(interrupted_solution)

    return solution


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ss_algorithm(lambda_multiplier='auto'):
    algo_name = "We64-SS"
    algo_desc = "Webb (1964) sequential savings algorithm"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return sequential_savings_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ss_algorithm())
Ejemplo n.º 7
0
            best_f = sol_f
            best_K = sol_K

        if interrupted:
            raise KeyboardInterrupt(best_sol)

    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
# (takes an extra argument for enabling parallel version)
def get_mj_algorithm():
    algo_name = "MJ76-INS"
    algo_desc = "Mole & Jameson (1976) sequential cheapest insertion "+\
                "heuristic with a route improvement phase"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        if single:
            return mole_jameson_insertion_init(
                D, d, C, L, minimize_K, strain_criterion="clarke_wright")
        else:
            return mole_jameson_insertion_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_mj_algorithm())
Ejemplo n.º 8
0
                                             minimize_K=minimize_K,
                                             emerging_route_count=1)
            one_eroute_K = sol_ci.count(0) - 1
            return cheapest_insertion_init(D,
                                           d,
                                           C,
                                           L=L,
                                           minimize_K=minimize_K,
                                           emerging_route_count=one_eroute_K)
    elif emerging_route_count > 1:

        def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
            return cheapest_insertion_init(
                D,
                d,
                C,
                L=L,
                minimize_K=minimize_K,
                emerging_route_count=emerging_route_count)
    else:
        raise ValueError("Not a valid parameter value, has to be 'auto' or " +
                         "an integer > 1, (was %s)" %
                         str(emerging_route_count))

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_si_algorithm())
Ejemplo n.º 9
0
    giant_tour_sol, giant_tour_l = tsp_gen_algo(D, list(range(1, N)))

    if __debug__:
        log(
            DEBUG, "TSP tour solution %s (%.2f)" %
            (str(giant_tour_sol), giant_tour_l))

    route_cost_constant = giant_tour_l if minimize_K else 0.0
    return _partition_to_routes_with_cost_matrix(D, d, C, L, giant_tour_sol,
                                                 route_cost_constant,
                                                 partition_tsp_opt_algo,
                                                 route_tsp_opt_algo)


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_rfcs_algorithm():
    algo_name = "Be83-RFCS"
    algo_desc = "Route-first-cluster-second heuristic of Beasley (1983)"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return route_first_cluster_second_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_rfcs_algorithm())
Ejemplo n.º 10
0
    sol = parallel_savings_init(D, d, C, L, minimize_K)

    sol = do_local_search(
        [do_2opt_move],  #, do_2optstar_move],
        sol,
        D,
        d,
        C,
        L,
        LSOPT.BEST_ACCEPT)

    return sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ps2o_algorithm():
    algo_name = "RT79-CAWLIP"
    algo_desc = "Robbins and Turner (1979) CAWLIP parallel "+\
                "savings algorithm with 2-opt* improvement phase"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return cawlip_savings_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ps2o_algorithm())
Ejemplo n.º 11
0
            routes[left_route].extend(routes[right_route])
            routes[right_route] = None

            if __debug__:
                dbg_sol = routes2sol(routes)
                log(
                    DEBUG - 1, "Merged, resulting solution is %s (%.2f)" %
                    (str(dbg_sol), objf(dbg_sol, D)))
    except KeyboardInterrupt:  # or SIGINT
        interrupted_sol = routes2sol(routes)
        raise KeyboardInterrupt(interrupted_sol)

    return routes2sol(routes)


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ps_algorithm():
    algo_name = "CW64-PS"
    algo_desc = "Clarke & Wright (1964) parallel savings algorithm"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return parallel_savings_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ps_algorithm())
Ejemplo n.º 12
0
            routes[i], route_f = solve_tsp(D, routes[i][:-1])

            if __debug__:
                log(DEBUG - 2,
                    "Got TSP solution %s (%.2f)" % (str(routes[i]), route_f))
    except KeyboardInterrupt:  #or SIGINT
        raise KeyboardInterrupt(solution)

    return routes2sol(routes)


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ty_algorithm():
    algo_name = "Ty68-NN"
    algo_desc = "Tyagi (1968) Nearest Neighbor construction heuristic"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        if minimize_K:
            raise NotImplementedError(
                "Nearest neighbor algorithm does not support minimizing the number of vehicles"
            )
        return tyagi_init(D, d, C, L)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ty_algorithm())
Ejemplo n.º 13
0
    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ptl_algorithm():
    algo_name = "FR76-1PTL"
    algo_desc = "Foster & Ryan (1976) Petal set covering algorithm"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        if single:
            return petal_init(points,
                              D,
                              d,
                              C,
                              L,
                              minimize_K=minimize_K,
                              required_iterations=1,
                              relaxe_SCP_solutions=False)

        else:
            return petal_init(points, D, d, C, L, minimize_K=minimize_K)
            #minimize_K=True)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ptl_algorithm())
Ejemplo n.º 14
0
                    log(DEBUG, "Previous sweep produced solution %s (%.2f)\n\n" %
                                 (str(sol),sol_f))
                    
                if is_better_sol(best_f, best_K, sol_f, sol_K, minimize_K):
                    best_sol = sol
                    best_f = sol_f
                    best_K = sol_K
    except KeyboardInterrupt: # or SIGINT
        raise KeyboardInterrupt(best_sol)
        
    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_swp_algorithm():
    algo_name = "Sweep"
    algo_desc = "Sweep algorithm without route improvement heuristics"
    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        seed_search = SMALLEST_ANGLE if single else BEST_ALTERNATIVE
        direction = "cw" if single else "both"
        return sweep_init(points, D, d, C, L, minimize_K,
                          direction=direction, seed_node=seed_search)
    return (algo_name, algo_desc, call_init)
    
if __name__=="__main__":
    from shared_cli import cli
    cli(*get_swp_algorithm())    
    
    
Ejemplo n.º 15
0
        if __debug__:
            log(DEBUG, "Improved solution %s (%.2f)" % (sol, sol_f))

        if is_better_sol(best_f, best_K, sol_f, sol_K, minimize_K):
            best_sol = sol
            best_f = sol_f
            best_K = sol_K

        if interrupted:
            raise KeyboardInterrupt(best_sol)

    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_wh_algorithm():
    algo_name = "WH72-SwLS"
    algo_desc = "Wren and Holliday (1972) Sweep heuristic"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        seed_node = 1 if single else BEST_OF_FOUR
        return wren_holliday_init(points, D, d, C, L, minimize_K, seed_node)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_wh_algorithm())
Ejemplo n.º 16
0
        seed_node,
        routing_algo=solve_tsp,
        prepare_callback_datastructures=_pack_datastructures_callback,
        intra_route_improvement=_improvement_callback)


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_gm_algorithm():
    algo_name = "GM74-SwRI"
    algo_desc = "Gillett & Miller (1974) Sweep algorithm with emering "+\
                "route improvement"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        if single:
            direction = "ccw"
            seed_node = 1  #first node
        else:
            direction = "both"
            seed_node = BEST_ALTERNATIVE

        return gillet_miller_init(points, D, d, C, L, minimize_K, direction,
                                  seed_node)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_gm_algorithm())
Ejemplo n.º 17
0
    except KeyboardInterrupt:  #or SIGINT
        interrupted_sol = _geedy_merge(D, d, C, L, W, savings, route_sets,
                                       tsp_cache, demand_cache)
        raise KeyboardInterrupt(interrupted_sol)

    # the optimized TSP tours for the routes are cached, reuse
    final_routes = [
        _get_tsp_sol(rs, tsp_cache, D, solve_tsp)[0] for rs in route_sets
    ]
    sol = [0] + [n for route in final_routes for n in route[1:]]

    return sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_mm_algorithm():
    algo_name = "DV89-MM"
    algo_desc = "Desrochers and Verhoog (1989) maximum matching based "+\
                "savings algorithm"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return mbsa_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_mm_algorithm())
Ejemplo n.º 18
0
            if __debug__:
                log(
                    DEBUG, "Best so far solution %s (%.2f) found with L'=%d" %
                    (sol, sol_f, Lcounter))
            best_sol = sol
            best_f = sol_f
            best_K = sol_K
            #best_sL = iterL

        if interrupted:
            raise KeyboardInterrupt(best_sol)

    return best_sol


# ---------------------------------------------------------------------
# Wrapper for the command line user interface (CLI)
def get_ims_algorithm():
    algo_name = "HP76-PS|IMS"
    algo_desc = "Holmes & Parker (1976) parallel savings supression algorithm"

    def call_init(points, D, d, C, L, st, wtt, single, minimize_K):
        return suppression_savings_init(D, d, C, L, minimize_K)

    return (algo_name, algo_desc, call_init)


if __name__ == "__main__":
    from shared_cli import cli
    cli(*get_ims_algorithm())