|
Gurobi+Python平台、Python+Gurobi入门
代码参考:- from gurobipy import *
- def HFS_Model_MS(nJ, M, P):
- print('MS')
- # model
- m = Model()
- X = {}
- Y = {}
- T = {}
- C = {}
- CS = {}
- S = {}
- bigM = {}
- ms = {}
- # paramters
- Mi = [[], [], [], []]
- nI = len(M)
- nL = len(P[1])
- bigM = 100000
- it = 0
- for i in range(nI):
- for j in range(M[i]):
- Mi[i].append(it)
- it = it + 1
- # variables
- for j in range(nJ):
- for k in range(nJ):
- X[(j, k)] = m.addVar(vtype=GRB.BINARY, name="X%d,%d" % (j, k))
- for i in range(nI):
- for k in range(nJ):
- for l in range(nL):
- Y[(i, k, l)] = m.addVar(vtype=GRB.BINARY, name="Y%d,%d,%d" % (i, k, l))
- for j in range(nJ):
- for i in range(nI):
- T[(j, i)] = m.addVar(vtype=GRB.CONTINUOUS, name="T%d,%d" % (j, i))
- C[(j, i)] = m.addVar(vtype=GRB.CONTINUOUS, name="C%d,%d" % (j, i))
- for i in range(nI):
- for k in range(nJ):
- CS[(i, k)] = m.addVar(vtype=GRB.CONTINUOUS, name="CS%d,%d" % (i, k))
- S[(i, k)] = m.addVar(vtype=GRB.CONTINUOUS, name="S%d,%d" % (i, k))
- ms = m.addVar(vtype=GRB.CONTINUOUS, name='ms')
- m.update()
- # constraints
- for j in range(nJ):
- m.addConstr(quicksum(X[(j, k)] for k in range(nJ)) == 1)
- for k in range(nJ):
- m.addConstr(quicksum(X[(j, k)] for j in range(nJ)) == 1)
- for i in range(nI):
- for k in range(nJ):
- m.addConstr(quicksum(Y[(i, k, l)] for l in Mi[i]) == 1)
- for i in range(nI - 1):
- for j in range(nJ):
- for k in range(nJ):
- for l in range(nL):
- m.addConstr(T[(j, i + 1)] >= T[(j, i)] + P[j][l] - bigM * (2 - X[(j, k)] - Y[(i, k, l)]))
- for i in range(nI):
- for k in range(1, nJ):
- for t in range(nJ):
- for l in range(nL):
- if (t < k):
- m.addConstr(S[(i, k)] >= S[(i, t)] + quicksum(
- P[j][l] * X[(j, t)] - bigM * (2 - Y[(i, k, l)] - Y[(i, t, l)]) for j in range(nJ)))
- for i in range(nI):
- for k in range(nJ):
- for j in range(nJ):
- m.addConstr(S[(i, k)] >= T[(j, i)] - bigM * (1 - X[(j, k)]))
- m.addConstr(S[(i, k)] <= T[(j, i)] + bigM * (1 - X[(j, k)]))
- for i in range(nI):
- for j in range(nJ):
- for k in range(nJ):
- for l in range(nL):
- m.addConstr(C[(j, i)] >= T[(j, i)] + P[j][l] - bigM * (2 - X[(j, k)] - Y[(i, k, l)]))
- for i in range(nI):
- for k in range(nJ):
- for l in range(nL):
- m.addConstr(CS[(i, k)] >= S[(i, k)] + quicksum(X[(j, k)] * P[j][l] for j in range(nJ)) - bigM * (
- 1 - Y[(i, k, l)]))
- for i in range(nI):
- for j in range(nJ):
- m.addConstr(T[(j, i)] >= 0)
- m.addConstr(C[(j, i)] >= 0)
- for i in range(nI):
- for k in range(nJ):
- m.addConstr(S[(i, k)] >= 0)
- m.addConstr(CS[(i, k)] >= 0)
- for j in range(nJ):
- m.addConstr(ms >= C[(j, nI - 1)])
- # set objective
- m.setObjective(ms, GRB.MINIMIZE)
- m.setParam("TimeLimit", 3600)
- m.setParam("OutputFlag", False)
- # optimize
- m.optimize()
- # for (j, k) in X:
- # if X[(j, k)].x >= 0.9:
- # print("Wert von X" + str((j, k)) + ": " + str(X[(j, k)].x))
- # for (i, k, l) in Y:
- # if Y[(i, k, l)].x >= 0.9:
- # print("Wert von Y" + str((i, k, l)) + ": " + str(Y[(i, k, l)].x))
- # for (j,i) in T:
- # if T[(j,i)].x >= 1:
- # print("Wert von T" + str((j,i))+ ": " + str(T[(j,i)].x ))
- # for (i,k) in S:
- # if S[(i,k)].x >= 1:
- # print("Wert von S" + str((i,k))+ ": " + str(S[(i,k)].x ))
- # for (j, i) in C:
- # if C[(j,i)].x >= 1:
- # print("Wert von C" + str((j, i)) + ": " + str(C[(j, i)].x))
- # for (i,k) in CS:
- # if CS[(i,k)].x >= 1:
- # print("Wert von CS" + str((i,k))+ ": " + str(CS[(i,k)].x ))
- m.write("model_ms.lp")
- return m.objVal, m.Runtime, m.status
- def main():
- nJ = 0
- M = [0, 0, 0, 0]
- P = []
- results_MS = open("Results_MS.txt", "w")
- results_MS.write("Nr." + " " + "Obj_Val" + " " + "Run_Time" + " " + "Status \n")
- for inst in range(13,192):
- with open("I4_" + str(inst + 1) + ".txt", "r") as textFile:
- lines = [line.split() for line in textFile]
- textFile.close()
- nJ = int(lines[3][0])
- M[0] = int(lines[5][0])
- M[1] = int(lines[6][0])
- M[2] = int(lines[7][0])
- M[3] = int(lines[8][0])
- nM: int = M[0] + M[1] + M[2] + M[3]
- P = [[0 for i in range(nM)] for j in range(nJ)]
- z = 0
- for i in range(nM):
- for j in range(nJ):
- P[j][i] = float(lines[z + 10][0])
- z = z + 1
- print(inst)
- obj_Val_MS, run_Time_MS, status_MS = HFS_Model_MS(nJ, M, P)
- results_MS.write(
- str(inst + 1) + " " + str(obj_Val_MS) + " " + str(run_Time_MS) + " " + str(status_MS) + "\n")
- results_MS.close()
- main()
复制代码
|
|