Skip to content

Commit

Permalink
Fix problem of LargeNeighborhoodSearchRCPSP solver :
Browse files Browse the repository at this point in the history
- problem was happening when trying to call the solver in an instance without addditional constraint and the notebook was failing (silently)
- the bug is now fixed and a unit test is added
  • Loading branch information
g-poveda committed Mar 4, 2024
1 parent a08524a commit 0187872
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,20 @@ def build_constraint_handler(rcpsp_problem: ANY_RCPSP, graph, **kwargs):
graph=graph,
fraction_subproblem=kwargs.get("fraction_subproblem", 0.05),
)
n3 = NeighborConstraintBreaks(
problem=rcpsp_problem,
graph=graph,
fraction_subproblem=kwargs.get("fraction_subproblem", 0.05),
other_constraint_handler=n1,
)
n_mix = NeighborBuilderMix(
list_neighbor=[n1, n2, n3], weight_neighbor=[0.2, 0.5, 0.3]
)
if rcpsp_problem.includes_special_constraint():
n3 = NeighborConstraintBreaks(
problem=rcpsp_problem,
graph=graph,
fraction_subproblem=kwargs.get("fraction_subproblem", 0.05),
other_constraint_handler=n1,
)
n_mix = NeighborBuilderMix(
list_neighbor=[n1, n2, n3], weight_neighbor=[0.2, 0.5, 0.3]
)
else:
n_mix = NeighborBuilderMix(
list_neighbor=[n1, n2], weight_neighbor=[0.5, 0.5]
)
basic_constraint_builder = BasicConstraintBuilder(
neighbor_builder=n_mix,
preemptive=kwargs.get("preemptive", False),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ def __init__(
):
if (
not hasattr(problem, "do_special_constraints")
or not problem.do_special_constraints
or not problem.includes_special_constraint()
):
raise ValueError(
"NeighborConstraintBreaks is meant for problems with special constraints"
Expand Down
37 changes: 37 additions & 0 deletions examples/rcpsp/rcpsp_lns_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2024 AIRBUS and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import logging

from discrete_optimization.generic_rcpsp_tools.large_neighborhood_search_scheduling import (
LargeNeighborhoodSearchScheduling,
)
from discrete_optimization.generic_tools.cp_tools import ParametersCP
from discrete_optimization.rcpsp.rcpsp_model import RCPSPModel
from discrete_optimization.rcpsp.rcpsp_parser import get_data_available, parse_file

logging.basicConfig(level=logging.INFO)


def example_lns_solver():
files_available = get_data_available()
file = [f for f in files_available if "j1201_1.sm" in f][0]
rcpsp_problem: RCPSPModel = parse_file(file)
solver = LargeNeighborhoodSearchScheduling(problem=rcpsp_problem)
parameters_cp = ParametersCP.default()
parameters_cp.time_limit_iter0 = 5
parameters_cp.time_limit = 2
results = solver.solve(
nb_iteration_lns=100,
skip_first_iteration=False,
stop_first_iteration_if_optimal=False,
parameters_cp=parameters_cp,
nb_iteration_no_improvement=200,
max_time_seconds=100,
)
sol, fit = results.get_best_solution_fit()
assert rcpsp_problem.satisfy(sol)


if __name__ == "__main__":
example_lns_solver()
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,20 @@
"outputs": [],
"source": [
"# Different constraint handler methods\n",
"from discrete_optimization.generic_rcpsp_tools.neighbor_builder import (\n",
" ObjectiveSubproblem,\n",
" ParamsConstraintBuilder,\n",
" mix_lot,\n",
")\n",
"\n",
"constraint_handler = mix_lot(\n",
" rcpsp_model=rcpsp_problem,\n",
" nb_cut_parts=[4, 5, 6],\n",
" fraction_subproblems=[0.3],\n",
" params_list=[\n",
" ParamsConstraintBuilder(\n",
" minus_delta_primary=60,\n",
" plus_delta_primary=60,152f1299af4f3f60662a07d1591f3938d973a6c4\n",
" plus_delta_primary=60,\n",
" minus_delta_secondary=20,\n",
" plus_delta_secondary=20,\n",
" constraint_max_time_to_current_solution=True,\n",
Expand Down Expand Up @@ -306,15 +312,23 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline\n",
"fig, ax = plt.subplots(1)\n",
"ax.plot([x[1] for x in results.list_solution_fits], marker=\"o\")\n",
"ax.set_ylabel(\"- makespan\")\n",
"ax.set_xlabel(\"# solution found\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bed3315-16ef-47a4-acce-f84d27f17046",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -333,7 +347,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
"version": "3.9.18"
},
"toc": {
"base_numbering": 1,
Expand Down
35 changes: 35 additions & 0 deletions tests/rcpsp/solver/test_large_neighborhood_search_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2024 AIRBUS and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import pytest

from discrete_optimization.generic_rcpsp_tools.large_neighborhood_search_scheduling import (
LargeNeighborhoodSearchScheduling,
)
from discrete_optimization.generic_tools.cp_tools import ParametersCP
from discrete_optimization.rcpsp.rcpsp_model import RCPSPModel
from discrete_optimization.rcpsp.rcpsp_parser import get_data_available, parse_file


@pytest.mark.parametrize(
"file_name",
["j1201_1.sm", "j1010_9.mm"],
)
def test_lns_solver(file_name):
files_available = get_data_available()
file = [f for f in files_available if file_name in f][0]
rcpsp_problem: RCPSPModel = parse_file(file)
solver = LargeNeighborhoodSearchScheduling(problem=rcpsp_problem)
parameters_cp = ParametersCP.default()
parameters_cp.time_limit_iter0 = 5
parameters_cp.time_limit = 2
results = solver.solve(
nb_iteration_lns=100,
skip_first_iteration=False,
stop_first_iteration_if_optimal=False,
parameters_cp=parameters_cp,
nb_iteration_no_improvement=50,
max_time_seconds=20,
)
sol, fit = results.get_best_solution_fit()
assert rcpsp_problem.satisfy(sol)

0 comments on commit 0187872

Please sign in to comment.