Retrieve the objective function value for multiple solutions
AnsweredHi,
I would like to obtain multiple feasible solutions in my model with gap = 0.1.
I have followed this post and I am able to obtain the solutions with this code:
m.setParam('PoolSearchMode',2)
m.setParam('PoolSolutions',20) #Maximum number of solutions
m.setParam('PoolGap',0.1)
I am able to obtain the values of the variables for each solution but I also would like to know which the objective value for each solution is. Could anybody help me? This is the code for getting the solutions:
solut = m.SolCount
sol = {}
for i in range(solut):
sol.clear()
m.setParam('SolutionNumber', i) #
for var in m.getVars():
if round(var.Xn) != 0 and 'A' in var.Varname:
sol[var.Varname] = round(var.Xn)
print(m.objVal) #This always prints the same objective value, I suppose this is the objective value of the optimal solution.
print(sol)
Thanks in advance.

Official comment
Hi Ana  Please see Retrieving Solutions in the Reference Manual:
Suboptimal solutions can be obtained by first setting the SolutionNumber parameter and then querying the Xn attribute to obtain the solution or the PoolObjVal attribute to obtain the objective value for the corresponding solution.
Gwyneth
Comment actions 
Many thanks! That was the solution.
Is there any way of obtaining notrepeated solutions? For example, in a classical multiple Knapsack problem, if I have 2 bins and 4 items, I'll get these solutions:
Solution 1:
Bin0: item0, item2, item3
Bin1: item 1
Solution 2:
Bin0: item 1
Bin1: item0, item2, item3
, and they are equal. I also get other solutions, which are exactly the same, for example Solution3 = Solution 2. Is they any way of avoiding this if I don't know the actual number of solutions or should I evaluate them later?
Thanks.
Please sign in to leave a comment.
Comments
2 comments