如何定义一个决策变量作为其他两个决策变量的函数?

时间:2019-01-07 16:35:39

标签: python optimization gurobi

在满足特定需求的同时,我正在尝试优化穿越运河的船舶的总成本。示例性目标函数如下。 由于船只可以彼此相邻并彼此交叉,因此我尝试将总数定义为彼此后面的数字与彼此相邻的数字的乘积。

程序无法正确解决。如何正确定义从c1到c5的乘积,然后满足需求的约束?

谢谢您的帮助! 问候 克里斯蒂娜

# Import Gurobi Solver into PyCharm to later optimize model
from gurobipy import *

#required input data:

width = 250
draft = 13
demand = 7100000
length = 80000

#definition of the model
m = Model('Model 1')

#total number of ships
c1 = m.addVar(vtype=GRB.INTEGER, name="c1")
c2 = m.addVar(vtype=GRB.INTEGER, name="c2")
c3 = m.addVar(vtype=GRB.INTEGER, name="c3")
c4 = m.addVar(vtype=GRB.INTEGER, name="c4")
c5 = m.addVar(vtype=GRB.INTEGER, name="c5")
#ships next to each other
b1 = m.addVar(vtype=GRB.INTEGER, name="b1")
b2 = m.addVar(vtype=GRB.INTEGER, name="b2")
b3 = m.addVar(vtype=GRB.INTEGER, name="b3")
b4 = m.addVar(vtype=GRB.INTEGER, name="b4")
b5 = m.addVar(vtype=GRB.INTEGER, name="b5")
#ships behind each other
a1 = m.addVar(vtype=GRB.INTEGER, name="a1")
a2 = m.addVar(vtype=GRB.INTEGER, name="a2")
a3 = m.addVar(vtype=GRB.INTEGER, name="a3")
a4 = m.addVar(vtype=GRB.INTEGER, name="a4")
a5 = m.addVar(vtype=GRB.INTEGER, name="a5")

#definition of objective function
m.setObjective(c1+2*c2+3*c3+4*c4+5*c5, GRB.MINIMIZE)

#draft restriction:
#vessel types that do not fit in the maximal draft are automatically excluded from further modelling:
if 20>draft:
    c5=0

if 15.2>draft:
    c4=0

if 14.5>draft:
    c3=0

if 12.5>draft:
    c2=0

if 10>draft:
    c1=0

#Total number of ships is simplified as product of ships next to each other and ships behind each other
def c1=a1*b1
def c2=a2*b2
def c3=a3*b3
def c4=a4*b4
def c5=a5*b5

#Constraint 1: meet forecasted demand 
con1=m.addConstr(2500*c1+4500*c2+8000*c3+13000*c4+50000*c5>=demand)

#Constraint 2: width of the waterway
con2=m.addConstr(20*a1+32*a2+43*a3+49*a4+74*a5<=width)

#Constraint 3: length restriction for length of canal
con3=m.addConstr(215*b1+290*b2+340*b3+366*b4+503*b5<=length) 

m.optimize()

0 个答案:

没有答案