杉数求解器—尝试求解2024年亚太赛中文赛C题

文摘   2024-07-25 11:49   湖北  
本文由我原创,本人修改了参数尝试。有问题请留言指出:
原理我已经在多商品问题的求解中指出了,下面我直接说一下我的代码和求解结果,模型很简单在这篇文章中2024年亚太赛中文赛C题-一种常见的调度问题求解模型(含相似题对比和提示词工程)已经写的很清楚了。

我们丢代码吧:

## This file is part of the Cardinal Optimizer, all rights reserved.#
import coptpy as cpfrom coptpy import COPT
import mathimport itertools
# Optimization data for multicommodity problemORIG = ['zhenzhou', 'shanghai', 'tianjin']DEST = ['xi‘an', 'kunming', 'shenzhen',]PROD = ['bands',]
supply_list = [19, 20, 12, ]supply = dict(zip(itertools.product(ORIG, PROD), supply_list))
demand_list = [19, 25, 7, ]demand = dict(zip(itertools.product(DEST, PROD), demand_list))
limit_list = [625.0] * len(ORIG) * len(DEST)limit = dict(zip(itertools.product(ORIG, DEST), limit_list))
cost_list = [3500, 13500,10500, 24500, 24000,10500, 17500, 16500,25500,] cost = dict(zip(itertools.product(ORIG, DEST, PROD), cost_list))
# Create COPT environmentenv = cp.Envr()
# Create COPT problemmmulti = env.createModel()
# Add variables to problemvtrans = mmulti.addVars(ORIG, DEST, PROD, nameprefix='trans')
# Add constraints to problemmmulti.addConstrs((vtrans.sum(i, '*', k) == supply[i, k] for i in ORIG for k in PROD), nameprefix='supply')mmulti.addConstrs((vtrans.sum('*', j, k) == demand[j, k] for j in DEST for k in PROD), nameprefix='demand')mmulti.addConstrs((vtrans.sum(i, j, '*') <= limit[i, j] for i in ORIG for j in DEST), nameprefix='multi')
# Set objective functionmmulti.setObjective(vtrans.prod(cost), COPT.MINIMIZE)
# Set optimization parametersmmulti.setParam(COPT.Param.TimeLimit, 100)
# Solve the problemmmulti.solve()
# Display solutionif mmulti.status == COPT.OPTIMAL: print('\nObjective value: {}'.format(mmulti.objval))
print('Variable solution: ') multvars = mmulti.getVars() for mvar in multvars: if math.fabs(mvar.x) >= 1e-6: print(' {0:s} = {1:.6f}'.format(mvar.name, mvar.x))

说明一下,这个案例是可以推广的,比如更复杂一点的。

第一:可以试试看混合调度,就是两个公司调度。

第二:可以试试看加一个天数的约束。

3行3列表示的是对应关系,消耗的情况,其实我个人认为如果没有天数约束,可以直接把天数×在成本里面。可以尝试一下的。

上结果吧:

Cardinal Optimizer v7.1.3. Build date Apr 29 2024Copyright Cardinal Operations 2024. All Rights Reserved
Setting parameter 'TimeLimit' to 100Model fingerprint: ab1d09db
Using Cardinal Optimizer v7.1.3 on WindowsHardware has 4 cores and 8 threads. Using instruction set X86_AVX512_E1 (14)Minimizing an LP problem
The original problem has: 15 rows, 9 columns and 27 non-zero elementsThe presolved problem has: 6 rows, 9 columns and 18 non-zero elements
Starting the simplex solver using up to 8 threads
Method Iteration Objective Primal.NInf Dual.NInf TimeDual 0 0.0000000000e+00 6 0 0.01sDual 4 6.5000612544e+05 0 0 0.01sPostsolvingDual 4 6.5000000000e+05 0 0 0.01s
Solving finishedStatus: Optimal Objective: 6.5000000000e+05 Iterations: 4 Time: 0.01s
Objective value: 650000.0Variable solution: trans(zhenzhou,xi‘an,bands) = 19.000000 trans(shanghai,kunming,bands) = 13.000000 trans(shanghai,shenzhen,bands) = 7.000000 trans(tianjin,kunming,bands) = 12.000000

结果很明显了吧,很典型的混合整数规划,我们终于初步尝试了代码,感谢杉数官方案例。

师苑数模
发布数模协会培训推文,讲解数模算法。赛题讲解及比赛通知。学校竞赛结果及学校竞赛成绩发布等文章。
 最新文章