
我们假设有x1x_1x1个全时服务员在中午12:00-1:00点吃饭有x2x_2x2个全时服务员在中午1:00-2:00吃饭,
我们假设从9:00,10:00,11:00,12:00,1:00开始工作的半时服务员分别有y1,y2,y3,y4,y5y_1,y_2,y_3,y_4,y_5y1,y2,y3,y4,y5个
那么我么有目标与约束条件
Min100x1+100x2+40y1+40y2+40y3+40y4+40y5S.T.{y1+y2+y3+y4+y5<=3x1+x2+y1>=4x1+x2+y1+y2>=3x1+x2+y1+y2+y3>=4x2+y1+y2+y3+y4>=6x1+y2+y3+y4+y5>=5x1+x2+y3+y4+y5>=6x1+x2+y4+y5>=8x1+x2+y5>=8Min \quad 100x_1+100x_2+40y_1+40y_2+40y_3+40y_4+40y_5\\
S.T.\left\{
\begin{aligned}
y_1+y_2+y_3+y_4+y_5<=3\\
x_1+x_2+y_1>=4\\
x_1+x_2+y_1+y_2>=3\\
x_1+x_2+y_1+y_2+y_3>=4\\
x_2+y_1+y_2+y_3+y_4>=6\\
x_1+y_2+y_3+y_4+y_5>=5\\
x_1+x_2+y_3+y_4+y_5>=6\\
x_1+x_2+y_4+y_5>=8\\
x_1+x_2+y_5>=8\\
\end{aligned}
\right.Min100x1+100x2+40y1+40y2+40y3+40y4+40y5S.T.⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧y1+y2+y3+y4+y5<=3x1+x2+y1>=4x1+x2+y1+y2>=3x1+x2+y1+y2+y3>=4x2+y1+y2+y3+y4>=6x1+y2+y3+y4+y5>=5x1+x2+y3+y4+y5>=6x1+x2+y4+y5>=8x1+x2+y5>=8
import cvxpy as cp
from numpy import array
c=array([100.,100,40,40,40,40,40]) #定义目标向量
a=array([[0 , 0, 1, 1, 1, 1, 1],
[-1,-1,-1, 0, 0, 0, 0],
[-1,-1,-1,-1, 0, 0, 0],
[-1,-1,-1,-1,-1, 0, 0],
[0 ,-1,-1,-1,-1,-1, 0],
[-1, 0,-1,-1,-1,-1,-1],
[-1,-1, 0, 0,-1,-1,-1],
[-1,-1, 0, 0, 0,-1,-1],
[-1,-1, 0, 0, 0, 0,-1]
]) #定义约束矩阵
b=array([3,-4,-3,-4,-6,-5,-6,-8,-8]) #定义约束条件的右边向量
x=cp.Variable(7,integer=True) #定义5个决策变量,True为整数规划,False为实数
obj=cp.Minimize(c*x) #构造目标函数
cons=[a*x<=b, x>=0] #构造约束条件
prob=cp.Problem(obj, cons) #构建问题模型
prob.solve(solver='GLPK_MI',verbose =True) #求解问题
print("最优值为:",prob.value)
print("最优解为:\n",x.value)
'''
运行结果:
===============================================================================
CVXPY
v1.1.12
===============================================================================
(CVXPY) Aug 12 04:06:55 PM: Your problem has 7 variables, 2 constraints, and 0 parameters.
(CVXPY) Aug 12 04:06:55 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Aug 12 04:06:55 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Aug 12 04:06:55 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:06:55 PM: Compiling problem (target solver=GLPK_MI).
(CVXPY) Aug 12 04:06:55 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
(CVXPY) Aug 12 04:06:55 PM: Applying reduction Dcp2Cone
(CVXPY) Aug 12 04:06:55 PM: Applying reduction CvxAttr2Constr
(CVXPY) Aug 12 04:06:55 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Aug 12 04:06:55 PM: Applying reduction GLPK_MI
(CVXPY) Aug 12 04:06:55 PM: Finished problem compilation (took 3.101e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:06:55 PM: Invoking solver GLPK_MI to obtain a solution.
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:06:55 PM: Problem status: optimal
(CVXPY) Aug 12 04:06:55 PM: Optimal value: 8.200e+02
(CVXPY) Aug 12 04:06:55 PM: Compilation took 3.101e-02 seconds
(CVXPY) Aug 12 04:06:55 PM: Solver (including time spent in interface) took 2.200e-02 seconds
最优值为: 820.0
最优解为:
[2. 5. 2. 0. 0. 0. 1.]
'''
解不唯一,这个解最终解得x1,x2,y1⋯y5x_1,x_2,y_1\cdots y_5x1,x2,y1⋯y5分别为2. 5. 2. 0. 0. 0. 1.所花费费用为820元。
对于后两问我们只需要更改约束条件重新求解即可
'''
不雇佣半时工
'''
import cvxpy as cp
from numpy import array
import numpy as np
c=array([100.,100,40,40,40,40,40]) #定义目标向量
a=array([[0 , 0, 1, 1, 1, 1, 1],
[-1,-1,-1, 0, 0, 0, 0],
[-1,-1,-1,-1, 0, 0, 0],
[-1,-1,-1,-1,-1, 0, 0],
[0 ,-1,-1,-1,-1,-1, 0],
[-1, 0,-1,-1,-1,-1,-1],
[-1,-1, 0, 0,-1,-1,-1],
[-1,-1, 0, 0, 0,-1,-1],
[-1,-1, 0, 0, 0, 0,-1]
]) #定义约束矩阵
b=array([0,-4,-3,-4,-6,-5,-6,-8,-8]) #定义约束条件的右边向量
x=cp.Variable(7,integer=True) #定义5个决策变量,True为整数规划,False为实数
obj=cp.Minimize(c*x) #构造目标函数
cons=[a*x<=b, x>=0] #构造约束条件
prob=cp.Problem(obj, cons) #构建问题模型
prob.solve(solver='GLPK_MI',verbose =True) #求解问题
print("最优值为:",prob.value)
print("最优解为:\n",x.value)
'''
===============================================================================
CVXPY
v1.1.12
===============================================================================
(CVXPY) Aug 12 04:12:12 PM: Your problem has 7 variables, 2 constraints, and 0 parameters.
(CVXPY) Aug 12 04:12:12 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Aug 12 04:12:12 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Aug 12 04:12:12 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:12:12 PM: Compiling problem (target solver=GLPK_MI).
(CVXPY) Aug 12 04:12:12 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
(CVXPY) Aug 12 04:12:12 PM: Applying reduction Dcp2Cone
(CVXPY) Aug 12 04:12:12 PM: Applying reduction CvxAttr2Constr
(CVXPY) Aug 12 04:12:12 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Aug 12 04:12:12 PM: Applying reduction GLPK_MI
(CVXPY) Aug 12 04:12:12 PM: Finished problem compilation (took 3.001e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:12:12 PM: Invoking solver GLPK_MI to obtain a solution.
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:12:12 PM: Problem status: optimal
(CVXPY) Aug 12 04:12:12 PM: Optimal value: 1.100e+03
(CVXPY) Aug 12 04:12:12 PM: Compilation took 3.001e-02 seconds
(CVXPY) Aug 12 04:12:12 PM: Solver (including time spent in interface) took 0.000e+00 seconds
最优值为: 1100.0
最优解为:
[5. 6. 0. 0. 0. 0. 0.]
'''
'''雇佣半时工'''
import cvxpy as cp
from numpy import array
import numpy as np
c=array([100.,100,40,40,40,40,40]) #定义目标向量
a=array([[0 , 0, 1, 1, 1, 1, 1],
[-1,-1,-1, 0, 0, 0, 0],
[-1,-1,-1,-1, 0, 0, 0],
[-1,-1,-1,-1,-1, 0, 0],
[0 ,-1,-1,-1,-1,-1, 0],
[-1, 0,-1,-1,-1,-1,-1],
[-1,-1, 0, 0,-1,-1,-1],
[-1,-1, 0, 0, 0,-1,-1],
[-1,-1, 0, 0, 0, 0,-1]
]) #定义约束矩阵
b=array([np.inf,-4,-3,-4,-6,-5,-6,-8,-8]) #定义约束条件的右边向量
x=cp.Variable(7,integer=True) #定义5个决策变量,True为整数规划,False为实数
obj=cp.Minimize(c*x) #构造目标函数
cons=[a*x<=b, x>=0] #构造约束条件
prob=cp.Problem(obj, cons) #构建问题模型
prob.solve(solver='GLPK_MI',verbose =True) #求解问题
print("最优值为:",prob.value)
print("最优解为:\n",x.value)
'''
===============================================================================
CVXPY
v1.1.12
===============================================================================
(CVXPY) Aug 12 04:14:02 PM: Your problem has 7 variables, 2 constraints, and 0 parameters.
(CVXPY) Aug 12 04:14:02 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Aug 12 04:14:02 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Aug 12 04:14:02 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:14:02 PM: Compiling problem (target solver=GLPK_MI).
(CVXPY) Aug 12 04:14:02 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
(CVXPY) Aug 12 04:14:02 PM: Applying reduction Dcp2Cone
(CVXPY) Aug 12 04:14:02 PM: Applying reduction CvxAttr2Constr
(CVXPY) Aug 12 04:14:02 PM: Applying reduction ConeMatrixStuffing
(CVXPY) Aug 12 04:14:02 PM: Applying reduction GLPK_MI
(CVXPY) Aug 12 04:14:03 PM: Finished problem compilation (took 3.800e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:14:03 PM: Invoking solver GLPK_MI to obtain a solution.
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Aug 12 04:14:03 PM: Problem status: optimal
(CVXPY) Aug 12 04:14:03 PM: Optimal value: 5.600e+02
(CVXPY) Aug 12 04:14:03 PM: Compilation took 3.800e-02 seconds
(CVXPY) Aug 12 04:14:03 PM: Solver (including time spent in interface) took 1.199e-02 seconds
最优值为: 560.0
最优解为:
[0. 0. 6. 0. 0. 0. 8.]
'''
该博客通过使用整数规划解决餐厅在不同时间段的全职和兼职服务员配置问题,以最小化成本。在不雇佣兼职服务员的情况下,解得最佳方案为雇用5名全职服务员,总成本为1100元;而考虑雇佣兼职服务员时,最佳配置为6名全职服务员和8名兼职服务员,总成本降低至560元。这展示了灵活调整员工类型和数量对于成本控制的重要性。
142

被折叠的 条评论
为什么被折叠?



