Python+Gurobi求解案例

模型描述

下面是一个工厂的生产计划模型。模型的集合、参数、决策变量与数学模型描述如下:

编程求解

 模型参数通过一个简单的测试数据给出,模型是在python里面从零开始建立,最后输出结果。代码如下:

import gurobipy as gp
from gurobipy import GRB

#production rate of machine k at facility m
pro_rate = [5,5,5,4,4,4,3,3,3,3]

#total capicity of machine k at facility m
capicity = [30,30,30,30,30,30,30,30,30,30] 

#usage cost per unit of machine k
usage_cost = [3,3,3,2,2,2,1,1,1,1]

#number of worker required for machine k
woker_req = [3,3,3,3,3,3,3,3,3,3]

#energy consume cost of factory m
energy_cost = 20

#number of fixed worker at factory m
fixed_worker = 21

#labor cost per unit time of fixed worker
fixed_cost = 0.3

#labor cost per unit time of flexible worker
flexible_cost = 0.5

#output value per unit product
unit_value = 3

#earning per output value when the required value is satisfied
earning_value = 1

#penalty cost per output value when the required value is not satisfied
penalty_cost_value = 3

#the objective output value
req_value = 1900

#the reqiured production quantity
req_quantity = 800

#a big real value number
big_m = 100000

#Range of machines
machines = range(len(capicity))

#build an empty model named factory_decision model
m = gp.Model("factory_decision_model")

#decision variable of machine production time:X[k]
pro_time = m.addVars(machines,lb=0, vtype=GRB.CONTINUOUS, name="pro_time")

#decision variable of fixed worker:Y[k]
fix_worker = m.addVars(machines,lb=0, vtype=GRB.INTEGER, name="fix_worker")

#decision variable of flexible worker:Z[k]
flex_worker = m.addVars(machines,lb=0, vtype=GRB.INTEGER, name="flex_worker")

#decision variable of machine setup: \beta[k]
beta = m.addVars(machines,lb=0, ub=1, vtype=GRB.BINARY, name="beta")

#decison variable of maximum production time:T
max_time = m.addVar(lb=0, ub=30, vtype=GRB.CONTINUOUS, name="max_time")

#decision variable of quantity of output value which beyond the requirement
sup_value = m.addVar(lb=0, ub=float("inf"), vtype=GRB.CONTINUOUS, name="sup_value")

#decision variable of quantity of output value which not satisfy the requirement
blow_value = m.addVar(lb=0, ub=float("inf"), vtype=GRB.CONTINUOUS, name="blow_value")

#set objective function: usage cost of machine + labor cost + enerry cost + output value
m.setObjective(gp.quicksum(usage_cost[k]*pro_time[k] for k in machines) 
                + gp.quicksum((fixed_cost*fix_worker[k] + flexible_cost*flex_worker[k])*pro_time[k] for k in machines)
                + energy_cost*max_time + penalty_cost_value*blow_value, GRB.MINIMIZE)

#Add constraint: production quantity
m.addConstr(gp.quicksum(pro_rate[k]*pro_time[k] for k in machines) == req_quantity, "production")

#Add constraint: output value
m.addConstr(req_quantity*unit_value == req_value + sup_value - blow_value, "output_value")

#Add constrains: capacity
m.addConstrs((pro_time[k] <= capicity[k] for k in machines),"capacity")

#Add constrains: labor requirement
m.addConstrs((fix_worker[k] + flex_worker[k] == woker_req[k]*beta[k] for k in machines),"labor")

#Add constrains: machine setup
m.addConstrs((beta[k]*big_m >= pro_time[k] for k in machines),"setup")

#Add constrains: machine setup_1
m.addConstrs((beta[k] <= pro_time[k]*big_m for k in machines),"setup_1")

#Add constraint: max_time
m.addConstrs((max_time >= pro_time[k] for k in machines), "max_time")

#Add constraint: fixed_worker
m.addConstr(gp.quicksum(fix_worker[k] for k in machines) <= fixed_worker, "fixed_worker")

#save model
m.write('factory_decision_modelPY_2.lp')

m.setParam('NonConvex', 2)
#Optimize
m.optimize()

#Print solution
print('\nTotal costs:%g'%m.objVal)
print('SOLUTION:')
total_production = 0
for k in machines:
    if beta[k].x > 0.99:
        print('machine %s is setup'% k)
        print('Machine %s production time is %g'% (k, pro_time[k].x))
        total_production += pro_time[k].x*pro_rate[k]
print('The total production amount is %g'%total_production)
print('---------------------------------------')
total_fix_worker = 0
total_flex_worker = 0
for k in machines:
    if beta[k].x > 0.99:
        print('machine %s is setup'% k)
        print('Machine %s need %g fixed worker'%(k,fix_worker[k].x))
        print('Machine %s need %g flexiable worker'%(k,flex_worker[k].x))
        total_fix_worker += fix_worker[k].x
        total_flex_worker += flex_worker[k].x
print('The total fixed worker is %g'%total_fix_worker)
print('The total flexible worker is %g'%total_flex_worker)
print('---------------------------------------')
print('The maximum produciton time is %g'%max_time.x)

参考资料

1.Gurobi官网给的example.

链接:https://pan.baidu.com/s/13YBcRDircm8FW5g_y62otg 
提取码:3v76

2.Gurobi官网的reference manual.

链接:https://pan.baidu.com/s/1Tz2hJ4ETYPptUcCvnKyfqw 
提取码:q05j

总结

其实使用python+gurobi的建模不复杂,如果有问题可以先看下gurobi官网给的example,给出了各种模型的建模和求解示例。如果对于gurobi方法或参数不了解的,则可以进一步查看参考手册。

CVRP(车辆路径问题)是一个经典的组合优化问题,而Gurobi是一个强大的数学规划求解器。在Python中使用Gurobi求解CVRP问题可以通过以下步骤进行: 1. 安装Gurobi:首先,确保已经安装了Gurobi数学优化软件包,并获取了有效的许可证。你可以从Gurobi官方网站上下载并安装Gurobi。 2. 导入Gurobi和其他必要的库:在Python脚本中导入Gurobi和其他需要使用的库,如numpy和pandas。 ```python import gurobipy as gp from gurobipy import GRB import numpy as np import pandas as pd ``` 3. 准备数据:准备CVRP问题需要的数据,包括顾客坐标、需求量、车辆容量等。 4. 创建模型:使用Gurobi创建一个数学规划模型。 ```python model = gp.Model('CVRP') ``` 5. 创建变量:创建决策变量,表示每个顾客是否被分配给某个车辆进行服务。 ```python x = {} # x[i, j]表示顾客i是否被分配给车辆j for i in range(num_customers): for j in range(num_vehicles): x[i, j] = model.addVar(vtype=GRB.BINARY, name=f'x_{i}_{j}') ``` 6. 添加约束条件:添加问题特定的约束条件,如每个顾客只能被一个车辆服务、每个车辆的容量限制等。 ```python # 每个顾客只能被一个车辆服务 for i in range(num_customers): model.addConstr(sum(x[i, j] for j in range(num_vehicles)) == 1) # 每个车辆的容量限制 for j in range(num_vehicles): model.addConstr(sum(demand[i] * x[i, j] for i in range(num_customers)) <= vehicle_capacity) ``` 7. 添加目标函数:添加目标函数,通常是最小化总路径长度或者最小化车辆使用数量。 ```python # 最小化总路径长度 model.setObjective(sum(distance[i, j] * x[i, j] for i in range(num_customers) for j in range(num_vehicles))) ``` 8. 求解模型:调用Gurobi求解方法求解模型。 ```python model.optimize() ``` 9. 解析结果:获取求解结果,并将其解析为可读的形式。 ```python solution = model.getAttr('x', x) routes = [] for j in range(num_vehicles): route = [i for i in range(num_customers) if solution[i, j] > 0.5] routes.append(route) ``` 这只是一个简单的示例,实际应用中可能涉及到更多的约束条件和问题特定的处理。你可以根据自己的需求对代码进行修改和扩展。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荒野猿人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值