>>> import pyomo.environ as pyo
>>> import pyomo.contrib.alternative_solutions as aos
>>> values = [10,9,2,1,1]
>>> weights = [10,9,2,1,1]
>>> K = len(values)
>>> capacity = 12
>>> m = pyo.ConcreteModel()
>>> m.x = pyo.Var(range(K), within=pyo.Binary)
>>> m.o = pyo.Objective(expr=sum(values[i] * m.x[i] for i in range(K)), sense=pyo.maximize)
>>> m.c = pyo.Constraint(expr=sum(weights[i] * m.x[i] for i in range(K)) <= capacity)
>>> solns = aos.enumerate_binary_solutions(m, num_solutions=10, solver="glpk", abs_opt_gap = 0.5)
>>> assert(len(solns) == 4)
>>> for soln in sorted(solns, key=lambda s: str(s.get_variable_name_values())):
... print(soln)
{
"fixed_variables": [],
"objective": "o",
"objective_value": 12.0,
"solution": {
"x[0]": 0,
"x[1]": 1,
"x[2]": 1,
"x[3]": 0,
"x[4]": 1
}
}
{
"fixed_variables": [],
"objective": "o",
"objective_value": 12.0,
"solution": {
"x[0]": 0,
"x[1]": 1,
"x[2]": 1,
"x[3]": 1,
"x[4]": 0
}
}
{
"fixed_variables": [],
"objective": "o",
"objective_value": 12.0,
"solution": {
"x[0]": 1,
"x[1]": 0,
"x[2]": 0,
"x[3]": 1,
"x[4]": 1
}
}
{
"fixed_variables": [],
"objective": "o",
"objective_value": 12.0,
"solution": {
"x[0]": 1,
"x[1]": 0,
"x[2]": 1,
"x[3]": 0,
"x[4]": 0
}
}