Code not working and do not know how to proceed. Any one know what I am doing wrong?
回答済みHi there, I have created an LP optimization model for optimizing the optimal buffer allocation between machines. I believe the model itself to be good enough and need to translate it to code for use with Gurobi. I have come quite far but there are just a few essential things I am missing. Could someone maybe take a look at my code and help?

import pandas as pd
import gurobipy as gp
from gurobipy import GRB
df_dict = {'Machine 1': {1: 3, 2: 2, 3: 2, 4: 3, 5: 0, 6: 1, 7: 5, 8: 5, 9: 5, 10: 5},
'Machine 2': {1: 1, 2: 6, 3: 5, 4: 1, 5: 3, 6: 3, 7: 5, 8: 3, 9: 2, 10: 5},
'Machine 3': {1: 1, 2: 5, 3: 0, 4: 4, 5: 2, 6: 6, 7: 2, 8: 2, 9: 2, 10: 5},
'Machine 4': {1: 3, 2: 2, 3: 1, 4: 4, 5: 2, 6: 4, 7: 0, 8: 0, 9: 2, 10: 2},
'Machine 5': {1: 1, 2: 4, 3: 4, 4: 4, 5: 7, 6: 7, 7: 7, 8: 0, 9: 3, 10: 8}}
df= pd.DataFrame(df_dict)
#------------------------------------------------------------------------------------------
# Data for the parameters
# Machines
machine5 = ['Machine 5']
machines = ['Machine 1', 'Machine 2', 'Machine 3', 'Machine 4']
# Buffers
buffer5 = 'b5'
buffers = ['b1', 'b2', 'b3', 'b4']
# Running time
time = df.index.to_list()
# Speed
# Machines 1 - 4
speeds = df[df.columns[:-1]].stack().to_dict()
# Machine 5
speed_M5 = df[df.columns[-1:]].stack().to_dict()
# Max total buffer capacity
k_max = 40
#------------------------------------------------------------------------------------------
# Model
model = gp.Model()
# Parameters
th5 = model.addVars(time, name = 'Throughput 5') # The throughputs of the last machine
thi = model.addVars(machines, time, name = 'Throughputs') # The throughputs of the machines
b5 = model.addVars(time, name ='Buffer 5') # The amount of units in the last buffer
bj = model.addVars(buffers, time, ub = k_max, name = "Buffers") # Actual amount of units in buffer i
kj = model.addVars(buffers, time, ub = k_max, name = "Max cap buffers") # Max capacity in the buffers
# Constraints
# Throughput Machine 5 1
th5_1 = model.addConstrs((th5[t] <= speed_M5[t, m] for t in time for m in machine5))
# Throughput Machine 5 2
# th5_2 = model.addConstrs((th5[t] for t in time) <= 3) # <-- Not sure how to do this
# Amount buffer 5
amount_b5 = (th5[t] for t in time) # The amount that enters the final buffer
# Buffer max
buff_k = model.addConstrs((bj[b, t] <= kj[b, t] for t in time for b in buffers))
# Amount Buffers 1
amount_bj_1 = model.addConstrs((bj[b, t] <= bj[b, time[time[t]-1]]
- thi[machines[machines[m]-1, t]] + speeds[t, m]
for t in time for m in machines for b in buffers))
# Amount Buffers 2
amount_bj_1 = add.Constrs((bj[b, t] for t in time for b in buffers)
<= (bj[b, time[time[t]-1]] - thi[machines[m]-1, t]
+ bj[[buffers[buffers[b]-1], time[time[t]-1]]]))
# Throughput machines
th_all = add.Constrs((thi[m, t] == bj[b, t] - bj[b, time[time[t]-1]] - thj[machines[machines[m]-1, t]]
for t in time for m in machines for b in buffers))
# Max Total Buffer Capacity
kmax_all = addConstrs() # <-- not sure how to do this
# Objective function
obj = gp.quicksum(b5[t] for t in time)
model.setObjective(obj, GRB.MAXIMIZE)
# Start optimization
model.optimize()
if not model.status == gp.GRB.OPTIMAL:
print("something went wrong")
print("optimal value",model.objval)
model.printAttr("X")
I feel like I am very close but it just does not want to work. I have added a small df as for use as small reproducible sample. Any help would be greatly appreciated!
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
I will discuss one type of constraints, because the others can be fixed in the same way.
You are trying to model the constraints
\[b_j(t) \leq b_j(t-1) - th_{i+1}(t) + v_i(t)\]
Your times \(t\) are in \(\{1,\dots,10\}\). First thing that comes up is that you have to start at \(t\geq 2\), because there is no \(b_j(0)\).
Your machines are given as \(m \in \{\text{machine }1, \dots, \text{machine }5\}\). Thus, you have to go to at most machine \(4\), because there is no \(th_{\text{machine }6}\)(t).
You can model the above constraints asmachines = ['Machine 1', 'Machine 2', 'Machine 3', 'Machine 4', 'Machine 5']
time = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
buffers = ['b1', 'b2', 'b3', 'b4', 'b5']
speeds = df[df.columns[:-1]].stack().to_dict()
k_max = 40
model = gp.Model()
thi = model.addVars(machines, time, name = 'Throughputs') # The throughputs of the machines
bj = model.addVars(buffers, time, ub = k_max, name = "Buffers") # Actual amount of units in buffer i
amount_bj_1 = model.addConstrs((bj[b, t] <= bj[b, t-1]
- thi[machines[m+1],t] + speeds[t, machines[m]]
for t in time if t>=2 for m in range(len(machines)-1) for b in buffers))
model.write("myLP.lp") # write LP file to analyze whether the model looks correct so farA few remarks:
- It is best to add constraints one by one and check whether the constraints you generated are correct.
This makes debugging easier - Avoid using indices or names with whitespaces. In your particular case, consider using 'Machine_X' instead of 'Machine X'
- It might be easier to use integer indices \(1,2,3,...\) for machines instead of strings Machine_1, Machine_2, ...
Best regards,
Jaromił0 - It is best to add constraints one by one and check whether the constraints you generated are correct.
投稿コメントは受け付けていません。
コメント
2件のコメント