My name is Boyu. I am a college student and newbie in python and Gurobi. Currently, one step of my model is solving 48 independent LPs. These LPs are independent and each has the same number of variables and constraints. The only difference between these LPs is the values of the coefficient and they are all known before running the model.
I start by building a small example which is similar to the real case in my model, the code is shown below:
First, I defined a function which is used for building and solving a single LP, where a,b,c,d are dictionaries for the coefficients which are known, the m is the dictionary for the LPs, and onject_val is a dictionary used for recording the object value for each LP:
from gurobipy import *
from gurobipy import GRB
# Create a new model
m[i] = Model()
# Create variables
x[i] = m[i].addVar(vtype=GRB.BINARY)
y[i] = m[i].addVar(vtype=GRB.BINARY)
z[i] = m[i].addVar(vtype=GRB.BINARY)
# Set objective
m[i].setObjective(x[i] + y[i] + 2 * z[i] , GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m[i].addConstr(x[i] + a[i] * y[i] + b[i] * z[i] <= c[i])
# Add constraint: x + y >= 1
m[i].addConstr(x[i] + y[i] >= d[i])
# Optimize model
Them, I input the coefficient values:
Then I tried two approaches for running them parallel, the first approach utilized the joblib package and multiprocessing package:
from joblib import Parallel, delayed
num_cores = multiprocessing.cpu_count()
Parallel(n_jobs=num_cores)(delayed(test)(i) for i in [1,2,3,4,5])
However, the error occurs:
AttributeError: b"Unable to retrieve attribute 'objVal'"
The second approach I tried is using the multiprocessing package only:
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
However, this approach will never finish running.
Could anybody give me some help for that? I am a newbie for gurobi and python and it will be really really appreciated if someone can give me some help.
Please sign in to leave a comment.