Skip to content

Commit

Permalink
Fix issues with random.sample() for python>=3.11
Browse files Browse the repository at this point in the history
Starting from python 3.11, random.sample() must apply to a sequence with
no automatic conversion from sets or dicts.

See https://docs.python.org/3/library/random.html#random.sample.
  • Loading branch information
nhuet authored and g-poveda committed Jan 23, 2024
1 parent 06ee518 commit 0f9551d
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ def find_subtasks(
subtasks.update(task_intersect)
len_subtask = len(subtasks)
if len(subtasks) >= self.nb_jobs_subproblem:
subtasks = set(random.sample(subtasks, self.nb_jobs_subproblem))
subtasks = set(random.sample(list(subtasks), self.nb_jobs_subproblem))
return subtasks, self.set_tasks.difference(subtasks)


Expand Down Expand Up @@ -1184,7 +1184,7 @@ def find_subtasks(
)
len_subtasks = len(subtasks)
if len_subtasks > self.nb_jobs_subproblem:
subtasks = random.sample(subtasks, self.nb_jobs_subproblem)
subtasks = set(random.sample(list(subtasks), self.nb_jobs_subproblem))
return subtasks, self.set_tasks.difference(subtasks)
else:
return self.other.find_subtasks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def problem_constraints(
subtasks = set(problems_output.keys())
if len(subtasks) >= 0.2 * current_solution.problem.n_jobs:
subtasks = set(
random.sample(subtasks, int(0.2 * current_solution.problem.n_jobs))
random.sample(list(subtasks), int(0.2 * current_solution.problem.n_jobs))
)
else:
subtasks = subtasks.union(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def solve(self, **kwargs: Any) -> ResultStorage:
sol, fit = results_storage.get_best_solution_fit()
indexes_to_remove = set(
random.sample(
all_indexes,
list(all_indexes),
int(proportion_to_remove * self.knapsack_model.nb_items),
)
)
Expand Down
4 changes: 2 additions & 2 deletions discrete_optimization/pickup_vrp/solver/lp_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ def adding_constraint(self, rebuilt_dict: Dict[int, List[Node]]) -> None:
edges_to_constraint[v].update(
set(
random.sample(
set(
list(
self.linear_solver.variable_decisions[
"variables_edges"
][v]
Expand All @@ -1606,7 +1606,7 @@ def adding_constraint(self, rebuilt_dict: Dict[int, List[Node]]) -> None:
edges_to_constraint[v].update(
set(
random.sample(
set(
list(
self.linear_solver.variable_decisions[
"variables_edges"
][v]
Expand Down
4 changes: 2 additions & 2 deletions discrete_optimization/pickup_vrp/solver/lp_solver_pymip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def adding_constraint(self, rebuilt_dict: Dict[int, List[Node]]) -> None:
edges_to_constraint[v].update(
set(
random.sample(
set(
list(
self.linear_solver.variable_decisions[
"variables_edges"
][v]
Expand All @@ -1049,7 +1049,7 @@ def adding_constraint(self, rebuilt_dict: Dict[int, List[Node]]) -> None:
edges_to_constraint[v].update(
set(
random.sample(
set(
list(
self.linear_solver.variable_decisions[
"variables_edges"
][v]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down Expand Up @@ -803,7 +803,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down
6 changes: 3 additions & 3 deletions discrete_optimization/rcpsp/solver/rcpsp_cp_lns_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down Expand Up @@ -168,7 +168,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down Expand Up @@ -283,7 +283,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down
8 changes: 4 additions & 4 deletions discrete_optimization/rcpsp/solver/rcpsp_lp_lns_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def adding_constraint_from_results_store(
# Fix start time for a subset of task.
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_fix_start_time * nb_jobs),
)
)
Expand Down Expand Up @@ -246,7 +246,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down Expand Up @@ -333,7 +333,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down Expand Up @@ -418,7 +418,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_to_fix * nb_jobs),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.n_jobs
jobs_to_fix = set(
random.sample(
current_solution.schedule.keys(), int(self.fraction_to_fix * nb_jobs)
list(current_solution.schedule), int(self.fraction_to_fix * nb_jobs)
)
)
for lj in last_jobs:
Expand All @@ -90,7 +90,7 @@ def adding_constraint_from_results_store(
self.employees_position = self.problem.employees_list
task_to_fix = set(
random.sample(
current_solution.schedule.keys(),
list(current_solution.schedule),
int(self.fraction_task_to_fix_employee * nb_jobs),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def adding_constraint_from_results_store(
# Fix start time for a subset of task.
jobs_to_fix = set(
random.sample(
current_solution.rcpsp_schedule.keys(),
list(current_solution.rcpsp_schedule),
int(self.fraction_fix_start_time * nb_jobs),
)
)
Expand Down Expand Up @@ -179,7 +179,7 @@ def adding_constraint_from_results_store(
nb_jobs = self.problem.nb_tasks
jobs_to_fix = set(
random.sample(
current_solution.schedule.keys(), int(self.fraction_to_fix * nb_jobs)
list(current_solution.schedule), int(self.fraction_to_fix * nb_jobs)
)
)
for lj in last_jobs:
Expand Down

0 comments on commit 0f9551d

Please sign in to comment.