didppy.fractional_knapsack

didppy.fractional_knapsack(set, capacity, values, weights)

Returns an expression representing the optimal objective value for the fractional knapsack problem.

Parameters:
Returns:

Expression representing the optimal solution for the fractional knapsack problem.

Return type:

FloatExpr

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