discrete_optimization.knapsack package

Subpackages

Submodules

discrete_optimization.knapsack.knapsack_model module

class discrete_optimization.knapsack.knapsack_model.Item(index: int, value: float, weight: float)[source]

Bases: object

index: int
value: float
weight: float
class discrete_optimization.knapsack.knapsack_model.ItemMultidimensional(index: int, value: float, weights: List[float])[source]

Bases: object

index: int
value: float
weights: List[float]
class discrete_optimization.knapsack.knapsack_model.KnapsackModel(list_items: List[Item], max_capacity: float, force_recompute_values: bool = False)[source]

Bases: Problem

evaluate(knapsack_solution: KnapsackSolution) Dict[str, float][source]

Evaluate a given solution object for the given problem.

This method should return a dictionnary of KPI, that can be then used for mono or multiobjective optimization.

Parameters:

variable (Solution) – the Solution object to evaluate.

Returns: Dictionnary of float kpi for the solution.

evaluate_from_encoding(int_vector: List[int], encoding_name: str) Dict[str, float][source]
evaluate_value(knapsack_solution: KnapsackSolution) float[source]
evaluate_weight_violation(knapsack_solution: KnapsackSolution) float[source]
get_attribute_register() EncodingRegister[source]

Returns how the Solution should be encoded.

Returns (EncodingRegister): content of the encoding of the solution

get_dummy_solution() KnapsackSolution[source]
get_objective_register() ObjectiveRegister[source]

Returns the objective definition.

Returns (ObjectiveRegister): object defining the objective criteria.

get_solution_type() Type[Solution][source]

Returns the class implementation of a Solution.

Returns (class): class object of the given Problem.

satisfy(knapsack_solution: KnapsackSolution) bool[source]

Computes if a solution satisfies or not the constraints of the problem.

Parameters:

variable – the Solution object to check satisfability

Returns (bool): boolean true if the constraints are fulfilled, false elsewhere.

class discrete_optimization.knapsack.knapsack_model.KnapsackModel_Mobj(list_items: List[Item], max_capacity: float, force_recompute_values: bool = False)[source]

Bases: KnapsackModel

evaluate(knapsack_solution: KnapsackSolution) Dict[str, float][source]

Evaluate a given solution object for the given problem.

This method should return a dictionnary of KPI, that can be then used for mono or multiobjective optimization.

Parameters:

variable (Solution) – the Solution object to evaluate.

Returns: Dictionnary of float kpi for the solution.

evaluate_mobj(solution: KnapsackSolution) TupleFitness[source]

Default implementation of multiobjective evaluation.

It consists in flattening the evaluate() function and put in an array. User should probably custom this to be more efficient.

Parameters:

variable (Solution) – the Solution object to evaluate.

Returns (TupleFitness): a flattened tuple fitness object representing the multi-objective criteria.

evaluate_mobj_from_dict(dict_values: Dict[str, float]) TupleFitness[source]

Return an multiobjective fitness from a dictionnary of kpi (output of evaluate function).

It consists in flattening the evaluate() function and put in an array. User should probably custom this to be more efficient.

Parameters:

dict_values – output of evaluate() function

Returns (TupleFitness): a flattened tuple fitness object representing the multi-objective criteria.

static from_knapsack(knapsack_model: KnapsackModel) KnapsackModel_Mobj[source]
get_objective_register() ObjectiveRegister[source]

Returns the objective definition.

Returns (ObjectiveRegister): object defining the objective criteria.

class discrete_optimization.knapsack.knapsack_model.KnapsackSolution(problem: KnapsackModel, list_taken: List[int], value: float | None = None, weight: float | None = None)[source]

Bases: Solution

change_problem(new_problem: Problem) None[source]

If relevant to the optimisation problem, change the underlying problem instance for the solution.

This method can be used to evaluate a solution for different instance of problems.

Parameters:

new_problem (Problem) – another problem instance from which the solution can be evaluated

Returns: None

copy() KnapsackSolution[source]

Deep copy of the solution.

The copy() function should return a new object containing the same input as the current object, that respects the following expected behaviour: -y = x.copy() -if do some inplace change of y, the changes are not done in x.

Returns: a new object from which you can manipulate attributes without changing the original object.

lazy_copy() KnapsackSolution[source]

This function should return a new object but possibly with mutable attributes from the original objects.

A typical use of lazy copy is in evolutionary algorithms or genetic algorithm where the use of local move don’t need to do a possibly costly deepcopy.

Returns (Solution): copy (possibly shallow) of the Solution

class discrete_optimization.knapsack.knapsack_model.KnapsackSolutionMultidimensional(problem: MultidimensionalKnapsack | MultiScenarioMultidimensionalKnapsack, list_taken: List[int], value: float | None = None, weights: List[float] | None = None)[source]

Bases: Solution

change_problem(new_problem: Problem) None[source]

If relevant to the optimisation problem, change the underlying problem instance for the solution.

This method can be used to evaluate a solution for different instance of problems.

Parameters:

new_problem (Problem) – another problem instance from which the solution can be evaluated

Returns: None

copy() KnapsackSolutionMultidimensional[source]

Deep copy of the solution.

The copy() function should return a new object containing the same input as the current object, that respects the following expected behaviour: -y = x.copy() -if do some inplace change of y, the changes are not done in x.

Returns: a new object from which you can manipulate attributes without changing the original object.

lazy_copy() KnapsackSolutionMultidimensional[source]

This function should return a new object but possibly with mutable attributes from the original objects.

A typical use of lazy copy is in evolutionary algorithms or genetic algorithm where the use of local move don’t need to do a possibly costly deepcopy.

Returns (Solution): copy (possibly shallow) of the Solution

class discrete_optimization.knapsack.knapsack_model.MultiScenarioMultidimensionalKnapsack(list_problem: Sequence[MultidimensionalKnapsack], method_aggregating: MethodAggregating)[source]

Bases: RobustProblem

get_dummy_solution() KnapsackSolutionMultidimensional[source]
list_problem: Sequence[MultidimensionalKnapsack]
class discrete_optimization.knapsack.knapsack_model.MultidimensionalKnapsack(list_items: List[ItemMultidimensional], max_capacities: List[float], force_recompute_values: bool = False)[source]

Bases: Problem

copy() MultidimensionalKnapsack[source]
evaluate(knapsack_solution: KnapsackSolutionMultidimensional) Dict[str, float][source]

Evaluate a given solution object for the given problem.

This method should return a dictionnary of KPI, that can be then used for mono or multiobjective optimization.

Parameters:

variable (Solution) – the Solution object to evaluate.

Returns: Dictionnary of float kpi for the solution.

evaluate_from_encoding(int_vector: List[int], encoding_name: str) Dict[str, float][source]
evaluate_value(knapsack_solution: KnapsackSolutionMultidimensional) float[source]
evaluate_weight_violation(knapsack_solution: KnapsackSolutionMultidimensional) float[source]
get_attribute_register() EncodingRegister[source]

Returns how the Solution should be encoded.

Returns (EncodingRegister): content of the encoding of the solution

get_dummy_solution() KnapsackSolutionMultidimensional[source]
get_objective_register() ObjectiveRegister[source]

Returns the objective definition.

Returns (ObjectiveRegister): object defining the objective criteria.

get_solution_type() Type[Solution][source]

Returns the class implementation of a Solution.

Returns (class): class object of the given Problem.

satisfy(knapsack_solution: KnapsackSolutionMultidimensional) bool[source]

Computes if a solution satisfies or not the constraints of the problem.

Parameters:

variable – the Solution object to check satisfability

Returns (bool): boolean true if the constraints are fulfilled, false elsewhere.

discrete_optimization.knapsack.knapsack_model.create_noised_scenario(problem: MultidimensionalKnapsack, nb_scenarios: int = 20) List[MultidimensionalKnapsack][source]
discrete_optimization.knapsack.knapsack_model.create_subknapsack_model(knapsack_model: KnapsackModel, solution: KnapsackSolution, indexes_to_remove: Set[int], indexes_to_keep: Set[int] | None = None)[source]
discrete_optimization.knapsack.knapsack_model.from_kp_to_multi(knapsack_model: KnapsackModel) MultidimensionalKnapsack[source]

discrete_optimization.knapsack.knapsack_parser module

discrete_optimization.knapsack.knapsack_parser.get_data_available(data_folder: str | None = None, data_home: str | None = None) List[str][source]

Get datasets available for knapsack.

Params:
data_folder: folder where datasets for knapsack whould be find.

If None, we look in “knapsack” subdirectory of data_home.

data_home: root directory for all datasets. Is None, set by

default to “~/discrete_optimization_data “

discrete_optimization.knapsack.knapsack_parser.parse_file(file_path: str, force_recompute_values: bool = False) KnapsackModel[source]
discrete_optimization.knapsack.knapsack_parser.parse_input_data(input_data: str, force_recompute_values: bool = False) KnapsackModel[source]

Parse a string of the following form : item_count max_capacity item1_value item1_weight … itemN_value itemN_weight

discrete_optimization.knapsack.knapsack_solvers module

discrete_optimization.knapsack.knapsack_solvers.look_for_solver(domain: KnapsackModel) List[Type[SolverKnapsack]][source]
discrete_optimization.knapsack.knapsack_solvers.look_for_solver_class(class_domain: Type[KnapsackModel]) List[Type[SolverKnapsack]][source]
discrete_optimization.knapsack.knapsack_solvers.solve(method: Type[SolverKnapsack], problem: KnapsackModel, **args: Any) ResultStorage[source]

Module contents