PySCIPOpt
PySCIPOpt copied to clipboard
getting suboptimal values for a binary integer programming problem
I have formulated a budgeted variant of max cut as a MIP based on the answer to this question. I was initially using the Python MIP package, but it quickly was unable to solve my problem as the graph sized increased, so I decided to try PySCIPOpt. However, when comparing the solution to one provided by the Python MIP package it has provided a suboptimal answer (I can verify by working out the cut value). I am assuming that I am formulating something wrong in my MIP model but I am unable to tell what. Below is the code I use for the problem:
def max_cut_heuristic(graph, budget):
N = graph.number_of_nodes()
model = Model("Max Cut")
model.setMaximize()
nodes = [model.addVar(f"{i}", vtype="B") for i in range(N)] # add a binary decision variable for every node
edges = {}
for i, j in graph.edges():
edges[(i, j)] = model.addVar(f"{(i, j)}", vtype="B") # add a binary decision variable for every edge
model.setObjective(quicksum(nodes[i] + nodes[j] - 2 * edges[(i, j)] for i, j in list(graph.edges()))). # define the objective
for i, j in graph.edges():
model.addCons(nodes[i] + nodes[j] - 1 <= edges[(i, j)]) # add the linear constraint
model.addCons(quicksum(nodes[i] for i in range(N)) == budget) # add the budget constraint
model.optimize()
sol = model.getBestSol()
sol = [i for i in range(N) if sol[nodes[i]] == 1]
return sol
is there something obvious that I am doing wrong here?
e: graph is a networkx
graph object.