How to solve a non binary if else scenario in gurobi
AnsweredI have a problem at hand. The problem is discussed inside the code in the comments. I can upload the excel sheet as well if somebody can help me.
# 
I have a excel file with three 4 columns (lowresedential area, Medium resedential area, High resedental area, Vulnerability) and 250 rows (correspponds to an geographical area). My problem is to optimize the composition of these columns such that total cost (construction +demolition) is minimum. Where demolition occurs when one type (lowresediential can convert to other two and same is valied for other two where they can convert to any of the remaining two) to other type. changing the type impacts the vulnerability value which is calculated using linear regression coefficients. USing a for loop I have defined lists to read these values
dataset= pd.read_csv('urban.csv',sep= ',',header= 0)
for i in range(len(dataset)):
low_r.append(dataset.iloc[i,0])
med_r.append(dataset.iloc[i,1])
high_r.append(dataset.iloc[i,2])
I have defined gurobi variables:
Rl = m.addVars(len(dataset), vtype=GRB.CONTINUOUS,name="Rl")
Rm = m.addVars(len(dataset), vtype=GRB.CONTINUOUS,name="Rm")
Rh = m.addVars(len(dataset), vtype=GRB.CONTINUOUS,name="Rh")
and defined cost variables
Demolowr=m.addVars(len(dataset), vtype=GRB.CONTINUOUS,name="Dlowr") Demomedr=m.addVars(len(dataset), type=GRB.CONTINUOUS,name="Dmedr") Demohighr=m.addVars(len(dataset), type=GRB.CONTINUOUS,name="Dhighr")
Now, in a loop which points to very row of the data follwing operations to optimize
for j in range(len(dataset)):
#Note: My approach is if to minimse cost of demolition and new construction for simplification here I have only taken demolition cost: If the Variable Rl[i] has more value than low_r[j] that means new lowrise buildings has been developed and the total differece must be multiplied by a factor (ex. unit percetage or unit area example for lowrise demolition cost is 1937503.88). Variables in LHS should be assigned the total cost and the objective must minimise this total cost. # Note: The problem I am facing is that I want condition based terms example { IF (low_r[j]Rl[j])>0 which means demolition has occured so multiply by demolition cost and IF (low_r[j]Rl[j])<0 means higher Rl[i] value corrsponds to new lowrise building has been developed thus multiply by a construction cost of lowrise resedential building say for exaple 5244555.88 and store it in new variable say Newlowr[j]} # Note: At the end the total cost should be the sum of construction and demolition
#here I have only written the demolition cost
m.addConstr(Demolowr[j]=((low_r[j]Rl[j])*1937503.88)) m.addConstr(Demomedr[j]=((med_r[j]Rm[j])*4843759.7)) m.addConstr(Demohighr[j]=((high_r[j]Rh[j])*11625023.28))
m.update()
m.addConstr(quicksum((Rl[k]+Rm[k]+Rh[k])for k in range(len(dataset)))/250<=25) m.addConstr(Rl[j]+Rm[j]+Rh[j]<=50)
m.update()
# Optimize model
objective = quicksum((Dlowr[j]+ Dmedr[j]+ Dhighr[j]) for j in range(len(dataset))) m.setObjective(objective, GRB.MINIMIZE)
m.optimize()

Hi,
First of all, please note there is no need to call m.update() for Gurobi v8 in your code.
Also I don't understand, what is your question/problem/issue.
Thanks,
Sonja
0
Please sign in to leave a comment.
Comments
1 comment