didppy.fractional_knapsack
- didppy.fractional_knapsack(set, capacity, values, weights)
Returns an expression representing the optimal objective value for the fractional knapsack problem.
- Parameters:
set (SetExpr, SetVar, SetResourceVar, or SetConst) – Set of items to choose from.
capacity (IntExpr, IntVar, IntResourceVar, int, FloatExpr, FloatVar, FloatResourceVar, or float) – Capacity of the knapsack.
values (list of IntExpr, IntVar, IntResourceVar, int, FloatExpr, FloatVar, FloatResourceVar, or float, IntTable1D, or FloatTable1D) – Values of the items.
weights (list of IntExpr, IntVar, IntResourceVar, int, FloatExpr, FloatVar, FloatResourceVar, or float, IntTable1D, or FloatTable1D) – Weights of the items.
- Returns:
Expression representing the optimal solution for the fractional knapsack problem.
- Return type:
- Raises:
DIDPPyException – If the number of values and weights does not match.
PanicException – If the set or capacity expression is not valid.
Notes
An empty set (no items to choose from) is not an error: the expression evaluates to 0.0.
Examples
>>> import didppy as dp >>> model = dp.Model() >>> obj = model.add_object_type(number=4) >>> var = model.add_set_var(object_type=obj, target=[0, 1, 2]) >>> state = model.target_state >>> capacity = 5 >>> values = [2, 3, 5, 10] >>> weights = [1, 2, 4, 1] >>> expr = dp.fractional_knapsack(var, capacity, values, weights) >>> expr.eval(state, model) 7.5