Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Initialize random solutions with SwapRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlan committed Oct 7, 2023
1 parent fa6b8e0 commit e5a8ab8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
32 changes: 20 additions & 12 deletions ddwp/static_solvers/scenario_solver.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, List

import numpy as np

if TYPE_CHECKING:
from pyvrp import ProblemData


from typing import List
import warnings

from ddwp.VrpInstance import VrpInstance
import numpy as np
from pyvrp import (
CostEvaluator,
GeneticAlgorithm,
PenaltyManager,
PenaltyParams,
Population,
PopulationParams,
ProblemData,
RandomNumberGenerator,
Result,
Solution,
Expand All @@ -34,6 +29,7 @@
)
from pyvrp.stop import MaxRuntime

from ddwp.VrpInstance import VrpInstance
from .instance2data import instance2data

warnings.filterwarnings("ignore", category=EmptySolutionWarning)
Expand Down Expand Up @@ -86,9 +82,21 @@ def scenario_solver(
for route_op in route_ops:
ls.add_route_operator(route_op(data))

init = [
Solution.make_random(data, rng) for _ in range(pop_params.min_pop_size)
]
if len(instance.vehicle_types) == 1:
init = [
Solution.make_random(data, rng)
for _ in range(pop_params.min_pop_size)
]
else:
# In case of multiple vehicle types, we can ensure that the initial
# solutions are feasible by running a local search with only SwapRoute.
ls_feas = LocalSearch(data, rng, neighbours)
ls_feas.add_route_operator(SwapRoutes(data))
init = [
ls_feas(Solution.make_random(data, rng), CostEvaluator(100, 100))
for _ in range(pop_params.min_pop_size)
]

algo = GeneticAlgorithm(data, pen_manager, rng, pop, ls, srex, init)

return algo.run(MaxRuntime(time_limit))
Expand Down
16 changes: 9 additions & 7 deletions experiments/limited_fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ def make_parser():
parser.add_argument(
"--num_requests_per_epoch", type=int, nargs="+", default=[75] * 8
)
parser.add_argument("--pct_vehicles", type=float, default=1)
parser.add_argument(
"--secondary_fleet_fixed_cost", type=int, default=14400
)
parser.add_argument("--pct_vehicles", type=float)
parser.add_argument("--secondary_fleet_fixed_cost", type=int, default=1200)

return parser

Expand Down Expand Up @@ -73,9 +71,13 @@ def solve(

_, greedy_sol = solve_dynamic(env_unlimited, greedy)
num_routes_per_epoch = [len(route) for route in greedy_sol.values()]
num_vehicles_per_epoch = [
int(pct_vehicles * num) for num in num_routes_per_epoch
]

if pct_vehicles is None:
num_vehicles_per_epoch = None
else:
num_vehicles_per_epoch = [
int(pct_vehicles * num) for num in num_routes_per_epoch
]

# Run environment again with restricted number of available vehicles.
agent = configure_agent(
Expand Down

0 comments on commit e5a8ab8

Please sign in to comment.