Precedens constraint wrong
回答済みThe model worked fine until the constraint with precedence and I wondering what is wrong. Due to that constraint, my solution count is 0 and model is infeasible. Hope for some good answers, thank you.
Here is my model:
import gurobipy
import numpy as np
import pandas as pd
import itertools
from itertools import combinations
from pulp import *
##Sets##
Activity = ['Tow-Out', 'Anchor-Transp', 'Anchor-Inst', 'Hook-up','Cable-lay','FinalCom'] # indexed by i
Vesseltypes = ['OSV', 'AHTS', 'CLV', 'SOV']
Vesselnumber = np.arange(0, 7, 1)
Time_periods = np.arange(0,3,1)
Turbines = np.arange(0, 1, 1)
print(Time_periods)
Activities = np.arange(0, len(Activity)*(len(Turbines)), 1)
#print(len(Activities))
##Dictionaries##
#Dictionary of duration for each operation in hours
dur = np.array([72, 48, 24, 72, 24, 168, 48])
#Scaling the array to yield for all turbines
duration = np.concatenate((dur, np.tile(dur[0:6], len(Turbines)-1)))
print(duration)
# Costs dictionary, cost of all vessel types in 1000 $
Cost_mobilization = {0 : 55,
1 : 115,
2 : 150,
3 : 74}
#Requirement matrix; Bollard Pull, Lifting capacity in tonnes, anchor handling equipment and cable laying equipment
Operation_req = np.array([[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
Matrix_Req = np.concatenate((Operation_req, np.tile(Operation_req[0:6,:], [len(Turbines)-1,1])), axis = 0)
#print(Matrix_Req)
Matrix_Dep = np.array([[0, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 0]])
print(len(Matrix_Dep))
#Scaling the matrix to yield for all turbines
Matrix1_Dep = np.zeros([len(Matrix_Dep)+(len(Matrix_Dep))*(len(Turbines)-1),
len(Matrix_Dep)+(len(Matrix_Dep))*(len(Turbines)-1)])
Matrix1_Dep[0:len(Matrix_Dep),0:len(Matrix_Dep)] = Matrix_Dep
for i in range(1, len(Turbines), 1):
Matrix1_Dep[0:1, len(Matrix_Dep):len(Matrix_Dep)+i*(len(Matrix_Dep))] = np.tile(Matrix_Dep[0:1,0:7],[1,i])
Matrix1_Dep[len(Matrix_Dep)+(i-1)*(len(Matrix_Dep)):len(Matrix_Dep)+i*(len(Matrix_Dep)),len(Matrix_Dep)+(i-1)*(len(Matrix_Dep)):len(Matrix_Dep)+i*(len(Matrix_Dep))] = Matrix_Dep[0:7,0:7]
print(Matrix1_Dep)
## Decision variables
# 1 if vessel v starts performing activity i at time period t, 0 otherwise
vessel_selected = LpVariable.dicts("VesselSelected",
[(v, i, t, k)
for v in range(len(Vesseltypes))
for i in range(len(Activity))
for t in range(len(Time_periods))
for k in range(len(Vesselnumber))], 0, 1, LpBinary
)
prob = LpProblem("FSM", LpMinimize)
## Objective function
prob +=lpSum(Cost_mobilization[v]*vessel_selected[(v,i,t,k)]
for v in range(len(Vesseltypes))
for i in range(len(Activity))
for t in range(len(Time_periods))
for k in range(len(Vesselnumber)))
## Constraints
# Requirement constraints
for v in range(len(Vesseltypes)):
for i in range(len(Activity)):
for t in range(len(Time_periods)):
for k in range(len(Vesselnumber)):
prob += vessel_selected[(v,i,t,k)] <= Matrix_Req[i][v]
# Operation constraints
for i in range(len(Activity)):
prob += lpSum(vessel_selected[(v,i,t,k)]
for v in range(len(Vesseltypes))
for t in range(len(Time_periods))
for k in range(len(Vesselnumber))) == 1
for v in range(len(Vesseltypes)):
for k in range(len(Vesselnumber)):
prob += lpSum(vessel_selected[(v,i,t,k)]
for i in range(len(Activity))
for t in range(len(Time_periods))) <= 1
# Precedens constraint
for i in range(1, len(Activity), 1): # Activity i needs to be performed before starting activity j
for j in range(1, len(Activity), 1):
prob += lpSum(vessel_selected[(v,i,t,k)] - lpSum(vessel_selected[(v,j,t,k)]
for v in range(len(Vesseltypes))
for t in range(len(Time_periods))
for k in range(len(Vesselnumber)))) >= 0
#SOLUTION
prob.writeMPS("LPModel.mps")
prob.solve(GUROBI())
print("Status:",LpStatus[prob.status])
-
正式なコメント
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?. -
The constraint mathematic formulated is shown in picture below:
0 -
Hi Thomas,
I only looked at the precedence constraint in your code and -- without knowing the details of the problem you are modelling -- here's what I noticed: In your code, i and j range over the same set and could be swapped without changing the resulting model, correct? From your comment, the constraint should enforce that activity i happen before j, but because of what I just said it would also enforce that j happen before i. If both cannot be true at the same time, the model is infeasible. As I've said in your previous question, you could write the model to an LP file and check whether the resulting constraints look as you want them.
In general, to debug infeasibility you could also try computing an IIS.
Silke
0
投稿コメントは受け付けていません。
コメント
3件のコメント