discrete_optimization.facility.solvers package
Submodules
discrete_optimization.facility.solvers.cp_mzn module
- class discrete_optimization.facility.solvers.cp_mzn.CpFacilityModel(*values)[source]
Bases:
Enum- DEFAULT_INT = 0
- DEFAULT_INT_LNS = 1
- class discrete_optimization.facility.solvers.cp_mzn.CpFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, silent_solve_error: bool = False, **kwargs: Any)[source]
Bases:
MinizincCpSolver,FacilitySolverCP solver linked with minizinc implementation of coloring problem.
- problem
facility problem instance to solve
- Type:
- params_objective_function
params of the objective function
- Type:
- cp_solver_name
backend solver to use with minizinc
- Type:
- silent_solve_error
if True, raise a warning instead of an error if the underlying instance.solve() crashes
- Type:
bool
- \*\*args
unused
- hyperparameters: list[Hyperparameter] = [EnumHyperparameter(name='cp_model', default=<CpFacilityModel.DEFAULT_INT: 0>, depends_on=None, name_in_kwargs='cp_model'), EnumHyperparameter(name='cp_solver_name', default=<CpSolverName.CHUFFED: 0>, depends_on=None, name_in_kwargs='cp_solver_name')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- init_model(**kwargs: Any) None[source]
Initialise the minizinc instance to solve for a given instance.
- Keyword Arguments:
cp_model (CpFacilityModel) – CP model version
object_output (bool) – specify if the solution are returned in a FacilitySolCp object or native minizinc output.
Returns: None
- model: Model | None
- retrieve_solution(_output_item: str | None = None, **kwargs: Any) FacilitySolution[source]
Return a d-o solution from the variables computed by minizinc.
- Parameters:
_output_item – string representing the minizinc solver output passed by minizinc to the solution constructor
**kwargs – keyword arguments passed by minzinc to the solution contructor containing the objective value (key “objective”), and the computed variables as defined in minizinc model.
Returns:
discrete_optimization.facility.solvers.cpsat module
- class discrete_optimization.facility.solvers.cpsat.CpSatFacilitySolver(problem: Problem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
AllocationCpSolver[Customer,Facility],TasksCpSatSolver[Customer],WarmstartMixin- get_task_unary_resource_is_present_variable(task: Customer, unary_resource: Facility) LinearExpr | IntVar | int | int8 | uint8 | int32 | uint32 | int64 | uint64[source]
Return a 0-1 variable/expression telling if the unary_resource is used for the task.
This variable should be 0 whenever an optional task is absent.
NB: sometimes the given resource is never to be used by a task and the variable has not been created. The convention is to return 0 in that case.
- problem: FacilityProblem
- retrieve_solution(cpsolvercb: CpSolverSolutionCallback) FacilitySolution[source]
Construct a do solution from the cpsat solver internal solution.
It will be called each time the cpsat solver find a new solution. At that point, value of internal variables are accessible via cpsolvercb.Value(VARIABLE_NAME).
- Parameters:
cpsolvercb – the ortools callback called when the cpsat solver finds a new solution.
- Returns:
the intermediate solution, at do format.
- set_warm_start(solution: FacilitySolution) None[source]
Make the solver warm start from the given solution.
- variables: dict
discrete_optimization.facility.solvers.dp module
- class discrete_optimization.facility.solvers.dp.DpFacilityModeling(*values)[source]
Bases:
Enum- CUSTOMER = 0
- FACILITY = 1
- class discrete_optimization.facility.solvers.dp.DpFacilitySolver(problem: FacilityProblem, **kwargs: Any)[source]
Bases:
DpSolver,FacilitySolver,WarmstartMixin- hyperparameters: list[Hyperparameter] = [CategoricalHyperparameter(name='solver', default=<class 'builtins.CABS'>, depends_on=None, name_in_kwargs='solver'), EnumHyperparameter(name='modeling', default=<DpFacilityModeling.CUSTOMER: 0>, depends_on=None, name_in_kwargs='modeling')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- init_model(modeling: DpFacilityModeling = DpFacilityModeling.CUSTOMER, **kwargs: Any) None[source]
Initialize internal model used to solve.
Can initialize a ortools, milp, gurobi, … model.
- modeling: DpFacilityModeling
- set_warm_start(solution: FacilitySolution) None[source]
Make the solver warm start from the given solution.
discrete_optimization.facility.solvers.facility_solver module
- class discrete_optimization.facility.solvers.facility_solver.FacilitySolver(problem: Problem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
SolverDO- problem: FacilityProblem
discrete_optimization.facility.solvers.gphh module
Genetic programming based solver for facility location problem.
- class discrete_optimization.facility.solvers.gphh.FeatureEnum(*values)[source]
Bases:
Enum- CAPACITIES = 'capacities'
- DEMAND_MINUS_CAPACITY = 'demand_minus_capacity'
- DISTANCE = 'distance'
- class discrete_optimization.facility.solvers.gphh.GphhFacilitySolver(problem: FacilityProblem, training_domains: list[FacilityProblem] | None = None, weight: int = 1, params_gphh: ParametersGphh | None = None, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs)[source]
Bases:
FacilitySolver- build_solution(domain: FacilityProblem, individual: Any | None = None, func_heuristic: Callable[[...], ArrayLike] | None = None) ResultStorage[source]
- evaluate_heuristic(individual: Any, domains: list[FacilityProblem]) list[float][source]
- init_model(**kwargs: Any) None[source]
Initialize internal model used to solve.
Can initialize a ortools, milp, gurobi, … model.
- pset: PrimitiveSet
- solve(**kwargs: Any) ResultStorage[source]
Generic solving function.
- Parameters:
callbacks – list of callbacks used to hook into the various stage of the solve
**kwargs – any argument specific to the solver
Solvers deriving from SolverDo should use callbacks methods .on_step_end(), … during solve(). But some solvers are not yet updated and are just ignoring it.
Returns (ResultStorage): a result object containing potentially a pool of solutions to a discrete-optimization problem
- class discrete_optimization.facility.solvers.gphh.ParametersGphh(set_feature: set[FeatureEnum], set_primitives: PrimitiveSetTyped, tournament_ratio: float, pop_size: int, n_gen: int, min_tree_depth: int, max_tree_depth: int, crossover_rate: float, mutation_rate: float, deap_verbose: bool)[source]
Bases:
objectCustom class to parametrize the GphhGenericRcpspSolver solver.
- set_feature
the set of feature to consider
- set_primitives
set of operator/primitive to consider.
- static default() ParametersGphh[source]
- discrete_optimization.facility.solvers.gphh.capacity(problem: FacilityProblem, customer_index: int, **kwargs: Any) list[float][source]
Capacity feature. :param problem: problem instance :type problem: FacilityProblem :param customer_index: [unused] customer index to compute distances to facilities :type customer_index: int
- discrete_optimization.facility.solvers.gphh.closest_facility(problem: FacilityProblem, customer_index: int, **kwargs: Any) int[source]
Closest facility feature for a given customer index.
- Parameters:
problem (FacilityProblem) – problem instance
customer_index (int) – [unused] customer index to compute distances to facilities
Returns (int): closest facility index
- discrete_optimization.facility.solvers.gphh.demand_minus_capacity(problem: FacilityProblem, customer_index: int, **kwargs: Any) list[float][source]
Compute demand-capacity feature for a given customer_index
- Parameters:
problem (FacilityProblem) – problem instance
customer_index (int) – customer index to compute distances to facilities
- discrete_optimization.facility.solvers.gphh.distance(problem: FacilityProblem, customer_index: int, **kwargs: Any) list[float][source]
Compute distance to facilitied for a given customer index
- Parameters:
problem (FacilityProblem) – problem instance
customer_index (int) – customer index to compute distances to facilities
discrete_optimization.facility.solvers.greedy module
- class discrete_optimization.facility.solvers.greedy.DistanceBasedGreedyFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs)[source]
Bases:
FacilitySolver- solve(**kwargs: Any) ResultStorage[source]
Generic solving function.
- Parameters:
callbacks – list of callbacks used to hook into the various stage of the solve
**kwargs – any argument specific to the solver
Solvers deriving from SolverDo should use callbacks methods .on_step_end(), … during solve(). But some solvers are not yet updated and are just ignoring it.
Returns (ResultStorage): a result object containing potentially a pool of solutions to a discrete-optimization problem
- class discrete_optimization.facility.solvers.greedy.GreedyFacilitySolver(problem: Problem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
FacilitySolverbuild a trivial solution pack the facilities one by one until all the customers are served
- solve(**kwargs: Any) ResultStorage[source]
Generic solving function.
- Parameters:
callbacks – list of callbacks used to hook into the various stage of the solve
**kwargs – any argument specific to the solver
Solvers deriving from SolverDo should use callbacks methods .on_step_end(), … during solve(). But some solvers are not yet updated and are just ignoring it.
Returns (ResultStorage): a result object containing potentially a pool of solutions to a discrete-optimization problem
discrete_optimization.facility.solvers.lns_lp module
- class discrete_optimization.facility.solvers.lns_lp.GurobiFacilityConstraintHandler(problem: FacilityProblem, fraction_to_fix: float = 0.9)[source]
Bases:
_BaseFacilityConstraintHandler,GurobiConstraintHandlerConstraint builder used in LNS+LP for coloring problem with gurobi solver.
This constraint handler is pretty basic, it fixes a fraction_to_fix proportion of allocation of customer to facility.
- problem
input coloring problem
- Type:
- fraction_to_fix
float between 0 and 1, representing the proportion of nodes to constrain.
- Type:
float
- adding_constraint_from_results_store(solver: GurobiFacilitySolver, result_storage: ResultStorage, result_storage_last_iteration: ResultStorage, **kwargs: Any) Iterable[Any][source]
Add constraints to the internal model of a solver based on previous solutions
- Parameters:
solver – solver whose internal model is updated
result_storage – all results so far
result_storage_last_iteration – results from last LNS iteration only
**kwargs
- Returns:
list of added constraints
- class discrete_optimization.facility.solvers.lns_lp.InitialFacilityMethod(*values)[source]
Bases:
Enum- DUMMY = 0
- GREEDY = 1
- class discrete_optimization.facility.solvers.lns_lp.InitialFacilitySolution(problem: FacilityProblem, initial_method: InitialFacilityMethod, params_objective_function: ParamsObjectiveFunction)[source]
Bases:
InitialSolutionInitial solution provider for lns algorithm.
- problem
input coloring problem
- Type:
- initial_method
the method to use to provide the initial solution.
- Type:
- get_starting_solution() ResultStorage[source]
- hyperparameters: list[Hyperparameter] = [EnumHyperparameter(name='initial_method', default=None, depends_on=None, name_in_kwargs='initial_method')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- class discrete_optimization.facility.solvers.lns_lp.MathOptConstraintHandlerFacility(problem: FacilityProblem, fraction_to_fix: float = 0.9)[source]
Bases:
_BaseFacilityConstraintHandler,OrtoolsMathOptConstraintHandlerConstraint builder used in LNS+LP for coloring problem with mathopt solver.
This constraint handler is pretty basic, it fixes a fraction_to_fix proportion of allocation of customer to facility.
- problem
input coloring problem
- Type:
- fraction_to_fix
float between 0 and 1, representing the proportion of nodes to constrain.
- Type:
float
discrete_optimization.facility.solvers.lp module
Linear programming models and solve functions for facility location problem.
- class discrete_optimization.facility.solvers.lp.CbcFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
FacilitySolverMilp formulation using cbc solver.
- hyperparameters: list[Hyperparameter] = [CategoricalHyperparameter(name='use_matrix_indicator_heuristic', default=True, depends_on=None, name_in_kwargs='use_matrix_indicator_heuristic'), IntegerHyperparameter(name='n_shortest', default=10, depends_on=None, name_in_kwargs='n_shortest', low=0, high=100, step=1, log=False), IntegerHyperparameter(name='n_cheapest', default=10, depends_on=None, name_in_kwargs='n_cheapest', low=0, high=100, step=1, log=False)]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- init_model(**kwargs: Any) None[source]
Initialize internal model used to solve.
Can initialize a ortools, milp, gurobi, … model.
- model: Solver | None
- retrieve() ResultStorage[source]
- solve(parameters_milp: ParametersMilp | None = None, time_limit: int | None = 30, **kwargs: Any) ResultStorage[source]
Generic solving function.
- Parameters:
callbacks – list of callbacks used to hook into the various stage of the solve
**kwargs – any argument specific to the solver
Solvers deriving from SolverDo should use callbacks methods .on_step_end(), … during solve(). But some solvers are not yet updated and are just ignoring it.
Returns (ResultStorage): a result object containing potentially a pool of solutions to a discrete-optimization problem
- class discrete_optimization.facility.solvers.lp.GurobiFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
GurobiMilpSolver,_BaseLpFacilitySolverMilp solver using gurobi library
- coloring_problem
facility problem instance to solve
- Type:
- params_objective_function
objective function parameters (however this is just used for the ResultStorage creation, not in the optimisation)
- Type:
- class discrete_optimization.facility.solvers.lp.MathOptFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
OrtoolsMathOptMilpSolver,_BaseLpFacilitySolverMilp solver using gurobi library
- coloring_problem
facility problem instance to solve
- Type:
- params_objective_function
objective function parameters (however this is just used for the ResultStorage creation, not in the optimisation)
- Type:
- convert_to_variable_values(solution: FacilitySolution) dict[Variable, float][source]
Convert a solution to a mapping between model variables and their values.
Will be used by set_warm_start() to provide a suitable SolutionHint.variable_values. See https://or-tools.github.io/docs/pdoc/ortools/math_opt/python/model_parameters.html#SolutionHint for more information.
- hyperparameters: list[Hyperparameter] = [CategoricalHyperparameter(name='use_matrix_indicator_heuristic', default=True, depends_on=None, name_in_kwargs='use_matrix_indicator_heuristic'), IntegerHyperparameter(name='n_shortest', default=10, depends_on=('use_matrix_indicator_heuristic', [True]), name_in_kwargs='n_shortest', low=0, high=100, step=1, log=False), IntegerHyperparameter(name='n_cheapest', default=10, depends_on=('use_matrix_indicator_heuristic', [True]), name_in_kwargs='n_cheapest', low=0, high=100, step=1, log=False)]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- discrete_optimization.facility.solvers.lp.compute_length_matrix(facility_problem: FacilityProblem) tuple[NDArray[float64], NDArray[int64], NDArray[float64]][source]
Precompute all the cost of allocation in a matrix form.
A matrix “closest” is also computed, sorting for each customers the facility by distance.
- Parameters:
facility_problem (FacilityProblem) – facility problem instance to compute cost matrix
Returns: setup cost vector, sorted matrix distance, matrix distance
- discrete_optimization.facility.solvers.lp.prune_search_space(facility_problem: FacilityProblem, n_cheapest: int = 10, n_shortest: int = 10) tuple[ndarray, ndarray][source]
Utility function that can prune the search space.
Output of this function will be used to : - consider only the n_cheapest facility that has the cheapest setup_cost - consider only the n_shortest (closest actually) facilities for each customers
- Parameters:
facility_problem (FacilityProblem) – facility problem instance
n_cheapest (int) – select the cheapest setup cost facilities
n_shortest (int) – for each customer, select the closest facilities
Returns: tuple of matrix, first element is a matrix (facility_count, customer_count) with 2 as value when we should consider the allocation possible. Second element in the (facility,customer) matrix distance.
discrete_optimization.facility.solvers.optal module
- class discrete_optimization.facility.solvers.optal.ModelingOptal(*values)[source]
Bases:
Enum- ALLOCATION = 'Allocation'
- SCHEDULING = 'Scheduling'
- class discrete_optimization.facility.solvers.optal.OptalFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs)[source]
Bases:
AllocationCpSolver[Customer,Facility],SchedulingOptalSolver[Customer]- get_task_interval(task: Customer) cp.IntervalVar[source]
Return interval variable for the task.
This variable corresponds to the task schedule, whatever the mode. This is the main optal variable (start, end and duration are derived from it)
If the task is optional, this interval should be also taken into account the presence/absence of the task.
- get_task_unary_resource_is_present_variable(task: Customer, unary_resource: Facility) cp.BoolExpr[source]
Return a 0-1 variable/expression telling if the unary_resource is used for the task.
This variable should be 0 whenever an optional task is absent.
NB: sometimes the given resource is never to be used by a task and the variable has not been created. The convention is to return 0 in that case.
- hyperparameters = [EnumHyperparameter(name='modeling', default=<ModelingOptal.ALLOCATION: 'Allocation'>, depends_on=None, name_in_kwargs='modeling')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- problem: FacilityProblem
discrete_optimization.facility.solvers.quantum module
- class discrete_optimization.facility.solvers.quantum.FacilityQiskit(problem: FacilityProblem)[source]
Bases:
object
- class discrete_optimization.facility.solvers.quantum.QaoaFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs)[source]
Bases:
FacilitySolver,QiskitQaoaSolver
- class discrete_optimization.facility.solvers.quantum.VqeFacilitySolver(problem: FacilityProblem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs)[source]
Bases:
FacilitySolver,QiskitVqeSolver
discrete_optimization.facility.solvers.toulbar module
- class discrete_optimization.facility.solvers.toulbar.FacilityConstraintHandlerDestroyFacilityToulbar(problem: FacilityProblem)[source]
Bases:
ConstraintHandlerSelect some factories, where all current allocated customers to those factories are totally free to be reallocated in the reduced problem.
- adding_constraint_from_results_store(solver: ToulbarSolverLns, result_storage: ResultStorage, result_storage_last_iteration: ResultStorage, **kwargs: Any) Iterable[Any][source]
Add constraints to the internal model of a solver based on previous solutions
- Parameters:
solver – solver whose internal model is updated
result_storage – all results so far
result_storage_last_iteration – results from last LNS iteration only
**kwargs
- Returns:
list of added constraints
- class discrete_optimization.facility.solvers.toulbar.FacilityConstraintHandlerToulbar(problem: FacilityProblem, fraction_of_customers: float | None = 0.4)[source]
Bases:
ConstraintHandlerThe allocated facility is frozen for a subpart of customers (fraction given in the constructor)
- adding_constraint_from_results_store(solver: ToulbarSolverLns, result_storage: ResultStorage, result_storage_last_iteration: ResultStorage, **kwargs: Any) Iterable[Any][source]
Add constraints to the internal model of a solver based on previous solutions
- Parameters:
solver – solver whose internal model is updated
result_storage – all results so far
result_storage_last_iteration – results from last LNS iteration only
**kwargs
- Returns:
list of added constraints
- class discrete_optimization.facility.solvers.toulbar.ModelingToulbarFacility(*values)[source]
Bases:
Enum- BINARY = 1
- INTEGER = 0
- class discrete_optimization.facility.solvers.toulbar.ToulbarFacilitySolver(problem: Problem, params_objective_function: ParamsObjectiveFunction | None = None, **kwargs: Any)[source]
Bases:
ToulbarSolver,FacilitySolver,WarmstartMixin- client_var_to_index: dict | None
- hyperparameters = [CategoricalHyperparameter(name='vns', default=None, depends_on=None, name_in_kwargs='vns'), EnumHyperparameter(name='modeling', default=<ModelingToulbarFacility.INTEGER: 0>, depends_on=None, name_in_kwargs='modeling')]
Hyperparameters available for this solver.
- These hyperparameters are to be feed to **kwargs found in
__init__()
init_model() (when available)
solve()
- init_model(**kwargs)[source]
Initialize internal model used to solve.
Can initialize a ortools, milp, gurobi, … model.
- init_model_boolean_variable(**kwargs)[source]
Init a model with boolean variable array, indicating if a factory f is assigned to customer c
- init_model_integer_variable(**kwargs)[source]
Integer encoding, where main decision variable is an array of integer, giving for each customer what is the facility index.
- key_to_index: dict | None
- modeling: ModelingToulbarFacility
- retrieve_solution(solution) FacilitySolution[source]
- retrieve_solution_binary(solution) FacilitySolution[source]
- retrieve_solution_integer(solution) FacilitySolution[source]
- set_warm_start(solution: FacilitySolution) None[source]
Make the solver warm start from the given solution.
- set_warm_start_binary(solution: FacilitySolution) None[source]
- set_warm_start_integer(solution: FacilitySolution) None[source]
- used_var_to_index: dict | None