I am trying to use python's multiprocessing library to solve many LPs in parallel (each LP has a different righthand side). However, every time I run my code, I get slightly different results for the optimal solution of the LPs (Below: "output" is not the same every time; neither is "sum(output)". I am new to python multiprocessing so I could be missing something.
from multiprocessing import Pool
import gurobipy
m = None
def f(x):
m.optimize()
m.getConstrs()[0].setAttr("rhs", 2.0 + x)
return m.objVal
if __name__ == '__main__':
#global gurobiModel
# Create a new model
m = gurobipy.Model("model")
# Create variables
x = m.addVar(vtype=gurobipy.GRB.BINARY, name="x")
y = m.addVar(vtype=gurobipy.GRB.BINARY, name="y")
z = m.addVar(vtype=gurobipy.GRB.BINARY, name="z")
# Integrate new variables
m.update()
# Set objective
m.setObjective(x + y + 2 * z, gurobipy.GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
m.setParam('OutputFlag', False )
pool = Pool(processes=4) # start 4 worker processes
output = pool.map(f, range(10))
print output
print sum(output)