Can't get the results
進行中Hi, we are trying to work with this code, but not able to get the output. Not sure if the problem is with the objective function. Could you please help us out? We are able to print the values from the excel sheet but not able to get the results. As you can see we are getting all zeros.
Thank you
import gurobipy as gp
from openpyxl import load_workbook
if __name__ == '__main__':
# Input excel files with arcs data(sheet 1) and commodities data (sheet2)
# Generate lista to store 'objects'
#Arcs = [(1,2,1,20),(1,3,1,10),(2,3,2,10),(2,4,4,20),(3,4,8,40),(3,5,5,10),(4,5,3,30)]
#Nodes = [1,2,3,4,5]
#Commodities = [(1,4,15),(1,5,5),(2,5,10),(3,5,5)]
Arcs = []
Vertex = []
Commodities = []
# Open input file(excel) that is in the current folder
#loc1=("/Users/fer/PycharmProjects/project/Input_lecct.xlsx")
loc1 = (r"C:\Users\rithi\Downloads\Input_lecct.xlsx")
wb = load_workbook(filename=loc1, read_only=True)
ws = wb['Arcs']
for row in ws.rows:
for cell in row:
print(cell.value)
# Close the workbook after reading
#wb = openpyxl.load_workbook(loc1, read_only=True)
#sheet=wb.active
#wb.close()
List_arcs = tuple(wb["Arcs"].iter_rows())
List_commo = tuple(wb["Commodities"].iter_rows())
print(List_commo)
#play_data = wb['Arcs']
#print(play_data)
#wb.close()
# Objective function
model = gp.Model("MCF")
x = {}
for m in range(len(Arcs)):
for k in range(len(Commodities)):
x[k, Arcs[m].To, Arcs[m].From] = model.addVar(obj = Arcs[m].Cost, vtype = "C",
name = "x(%d, %d, %d)" % (k, Arcs[m].To, Arcs[m].From))
model.update()
# constraints
# capacity
Capacity = {}
for m in range(len(Arcs)):
Capacity[Arcs[m].From, Arcs[m].To] = model.addConstrs(gp.quicksum(x[k.Arcs[m].To.Arcs[m].From]
for k in range(len(Commodities))),
'<=', Arcs[m].Capac, name = 'Capacity(%d)' % (m))
# conservation constraint
Continuity = {}
for k in range(len(Commodities)):
for j in range(len(Nodes)):
if j == Commodities[k].From:
Continuity[k, j] = model.addConstr(gp.quicksum(x[k, j, p] for p in Nodes[j].OutLinks
- gp.quicksum(x[k, p, j] for p in Nodes[j].InLinks)),
'=', Commodities[k].Quant, name = 'Continuity(%d, %d)' % (k, j))
elif j == Commodities[k].To:
Continuity[k, j] = model.addConstr(gp.quicksum(x[k, j, p] for p in Nodes[j].OutLinks)
- gp.quicksum(x[k, p, j] for p in Nodes[j].InLinks),
'=', -Commodities[k].Quant, name = 'Continuity(%d, %d)' % (k, j))
else:
Continuity[k, j] = model.addConstr(gp.quicksum(x[k, j, p] for p in Nodes[j].OutLinks)
- gp.quicksum( x[k, p, j] for p in Nodes[j].InLinks),
'=', 0, name = 'Continuity(%d, %d)' % (k, j))
model.update()
model.optimize()
status = model.status
if status != gp.GRB.Status.OPTIMAL:
if status == gp.GRB.Status.UNBOUNDED:
print("The model cannot be solved because it is unbounded")
elif status == gp.GRB.Status.INFEASIBLE:
print('The model is infeasible; compute IIS')
model.computeIIS()
print('\n The following constraints cannot be satisfied:')
for c in model.getConstrs():
if c.IISConstrs():
print('%s' % c.constrName)
elif status != gp.GRB.Status.INF_OR_UNBD:
print('Optimization was stopped with status %d' % status)
exit(0)
print(MCF)
-
正式なコメント
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 why not try our AI Gurobot?. -
When I look at your log output screen, it appears that there were 0 constraints and 0 variables added to the model:
Optimize a model with 0 rows, 0 columns, and 0 nonzeros
It looks like you may not be specifying \(\texttt{Arcs}\), \(\texttt{Vertex}\), and \(\texttt{Commodities}\) after initializing them. Once you set these, your variable and constraint loops should populate your model.
0 -
Thank you for answering, could you like give an example to help me understand more as I am new to this.
0 -
I think I see a good example from your code. In lines 12-14, you create \(\texttt{Arcs}\), \(\texttt{Nodes}\), and \(\texttt{Commodities}\):
Arcs = []
Nodes = [] # I assume Nodes/Vertex should be the same
Commodities = []As we discussed, the challenge is that these variables are never defined before being used to define your \(\texttt{for}\) loops later in the code. Therefore, you could use the code you commented out to give these variables values instead of or after initializing them:
Arcs = [(1,2,1,20),(1,3,1,10),(2,3,2,10),(2,4,4,20),(3,4,8,40),(3,5,5,10),(4,5,3,30)]
Nodes = [1,2,3,4,5]
Commodities = [(1,4,15),(1,5,5),(2,5,10),(3,5,5)]Alternatively, it looks like your data may be stored in "C:\Users\rithi\Downloads\Input_lecct.xlsx". So, it may be best to pull the data out of this. While I do not know quite what your data looks like, my best guess is that the data you want for \(\texttt{Arcs}\) is stored in \(\texttt{List_arcs}\) in your code. And the data you want to \(\texttt{Commodities}\) is stored in \(\texttt{List_commo}\) in your code.
0 -
Thank you, also our input data is from the spreadsheet not from the code itself, could you guide us on how to iterate the data?
0 -
You can read from an excel spreadsheet in Python using the pandas.read_excel() function. There is a good tutorial on how to use this function here: Read Excel with Python Pandas - Python Tutorial.
Here is an example of reading a column named 'vertex' from a worksheet 'Vertex' in an excel file called 'MyExcelFile.xslx':
import pandas as pd
df = pd.read_excel('MyExcelFile.xlsx',sheet_name="Vertex") # read in data from your excel spreadsheet
data_from_column_vertex = df['vertex'].tolist() # get a list from the column vertex
print(data_from_column_vertex) # Review the column that you pulled inI will leave it to you to read the rest of your data from your spreadsheets then define \(\texttt{Arcs}\), \(\texttt{Nodes}\), and \(\texttt{Commodoties}\). Once you have populated these variables, the loops you wrote (e.g., \(\texttt{for j in range(len(Nodes)):}\) ) will have something to loop over.
0 -
See Code not working – Gurobi Support Portal for the remainder of the conversation.
0
投稿コメントは受け付けていません。
コメント
7件のコメント