重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“java线性规划问题举例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“java线性规划问题举例分析”吧!
在旌德等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站制作 网站设计制作定制网站制作,公司网站建设,企业网站建设,品牌网站设计,全网整合营销推广,外贸网站建设,旌德网站建设费用合理。
由于我后台是springcloud,所以我调查到两种实现方式。
第一种是运用python的scipy开源库写一个python脚本,然后java后台调用python脚本,求最优解,然后再将结果返回。
第二种是运用java中ibm组件Cplex直接求解,但分为收费和免费版,免费版决策变量限制为1000个,但也够了。找jar包和dll费劲点。
发现他们俩算出来的最优解是相同的,但各个决策变量不太相同。
这里记录一下运用java中ibm组件Cplex求解的方法。
首先看一下官方说明,免费版本的限制。
申请下载免费版本的时候,要先注册一下填一些信息,然后会把下载地址发送到申请时的邮箱中,可以下载win、linux、macos版本。
这个安装完之后是一个ide开发工具CPLEX_Studio,由于我是用idea开发的,所以还要配置如下的步骤:
CPLEX_Studio安装完以后,安装目录下会有一个jar包,需要导入到我们的idea中,我是将jar包导入到我本地maven仓库中,用pom引入的:
CPLEX_Studio中jar包的位置: D:\CPLEX_Studio_Community201\cplex\lib\cplex.jar
mvn install:install-file -Dfile=D:\CPLEX_Studio_Community201\cplex\lib\cplex.jar -DgroupId=cplex -DartifactId=cplex -Dversion=20.1.0.0.R1 -Dpackaging=jar
cplex cplex 20.1.0.0.R1
然后在我们的启动项目时还要加入java.library.path参数,指定CPLEX运行库:
-Djava.library.path="D:\CPLEX_Studio_Community201\cplex\bin\x64_win64"
我在开发中是直接idea配置的:
之后就可以愉快的使用了。
我项目中的不等式方程组是∑求和形式的,这里就手动打码先假设决策变量的数量为 5 * 3 个,经过化简后得到多元一次不等式组如下:
求解最大值 = AX + AY + AZ + BX + BY + BZ + CX + CY + CZ + DX + DY + DZ + EX + EY + EZ AX + AY + AZ <= 25400 BX + BY + BZ <= 18600 CX + CY + CZ <= 39800 DX + DY + DZ <= 53200 EX + EY + EZ <= 5900 AX + AY + AZ <= 10000 BX + BY + BZ <= 10000 CX + CY + CZ <= 10000 DX + DY + DZ <= 10000 EX + EY + EZ <= 10000 AX + BX + CX + DX + EX >= 15000 AY + BY + CY + DY + DY >= 5000 AZ + BZ + CZ + DZ + DZ >= 10000 (50.25-50)*AX + (49.86-50)*BX + (68.80-50)*CX + (49.79-50)*DX + (48.77-50)*EX >= 0 (50.25-60)*AY + (49.86-60)*BY + (68.80-60)*CY + (49.79-60)*DY + (48.77-60)*EY >= 0 (50.25-55)*AZ + (49.86-55)*BZ + (68.80-55)*CZ + (49.79-55)*DZ + (48.77-55)*DZ >= 0 (30.95*(1-2/100)-30)*AX + (31.52*(1-3/100)-30)*BX + (30.58*(1-1/100)-30)*CX + (30.17*(1-1/100)-30)*DX + (27.83*(1-1/100)-30)*EX >= 0 (30.95*(1-2/100)-30)*AY + (31.52*(1-3/100)-30)*BY + (30.58*(1-1/100)-30)*CY + (30.17*(1-1/100)-30)*DY + (27.83*(1-1/100)-30)*EY >= 0 (30.95*(1-2/100)-30)*AZ + (31.52*(1-3/100)-30)*BZ + (30.58*(1-1/100)-30)*CZ + (30.17*(1-1/100)-30)*DZ + (27.83*(1-1/100)-30)*EZ >= 0 (11.32*(1-2/100)-10)*AX + (12.83*(1-3/100)-10)*BX + (16.06*(1-1/100)-10)*CX + (5.68*(1-1/100)-10)*DX + (8.54*(1-1/100)-10)*EX >= 0 (11.32*(1-2/100)-10)*AY + (12.83*(1-3/100)-10)*BY + (16.06*(1-1/100)-10)*CY + (5.68*(1-1/100)-10)*DY + (8.54*(1-1/100)-10)*EY >= 0 (11.32*(1-2/100)-10)*AZ + (12.83*(1-3/100)-10)*BZ + (16.06*(1-1/100)-10)*CZ + (5.68*(1-1/100)-10)*DZ + (8.54*(1-1/100)-10)*EZ >= 0 (6*(1-2/100)-5)*AX + (4*(1-3/100)-5)*BX + (5*(1-1/100)-5)*CX + (2*(1-1/100)-5)*DX + (5*(1-1/100)-5)*EX <= 0 (6*(1-2/100)-5)*AY + (4*(1-3/100)-5)*BY + (5*(1-1/100)-5)*CY + (2*(1-1/100)-5)*DY + (5*(1-1/100)-5)*EY <= 0 (6*(1-2/100)-5)*AZ + (4*(1-3/100)-5)*BZ + (5*(1-1/100)-5)*CZ + (2*(1-1/100)-5)*DZ + (5*(1-1/100)-5)*EZ <= 0 非负约束: AX,AY,AZ,BX,BY,BZ,CX,CY,CZ,DX,DY,DZ,EX,EY,EZ >= 0
上面这些是根据我自己的项目得到的不等式组,可以根据自己的项目来做相应改动。
try { // creat a model IloCplex cplex = new IloCplex(); // 变量的取值范围 double[] lb = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; double[] ub = {10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000}; IloNumVar[] x = cplex.numVarArray(15, lb, ub); // 求解目标 double[] objvals = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; cplex.addMaximize(cplex.scalProd(x, objvals)); // 不等式约束 cplex.addLe(cplex.scalProd(x, new double[]{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 25400); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 18600); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}), 39800); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}), 53200); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}), 5900); cplex.addLe(cplex.scalProd(x, new double[]{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 10000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 10000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}), 10000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}), 10000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}), 10000); cplex.addLe(cplex.scalProd(x, new double[]{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 20000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 20000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}), 20000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}), 20000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}), 20000); cplex.addLe(cplex.scalProd(x, new double[]{-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0}), -15000); cplex.addLe(cplex.scalProd(x, new double[]{0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0}), -5000); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1}), -10000); cplex.addLe(cplex.scalProd(x, new double[]{50 - 50.25, 0, 0, 50 - 49.86, 0, 0, 50 - 68.80, 0, 0, 50 - 49.79, 0, 0, 50 - 48.77, 0, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 60 - 50.25, 0, 0, 60 - 49.86, 0, 0, 60 - 68.80, 0, 0, 60 - 49.79, 0, 0, 60 - 48.77, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 55 - 50.25, 0, 0, 55 - 49.86, 0, 0, 55 - 68.80, 0, 0, 55 - 49.79, 0, 0, 55 - 48.77}), 0); cplex.addLe(cplex.scalProd(x, new double[]{30 - 30.95 * 0.98, 0, 0, 30 - 31.52 * 0.97, 0, 0, 30 - 30.58 * 0.99, 0, 0, 30 - 30.17 * 0.99, 0, 0, 30 - 27.83 * 0.99, 0, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 30 - 30.95 * 0.98, 0, 0, 30 - 31.52 * 0.97, 0, 0, 30 - 30.58 * 0.99, 0, 0, 30 - 30.17 * 0.99, 0, 0, 30 - 27.83 * 0.99, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 30 - 30.95 * 0.98, 0, 0, 30 - 31.52 * 0.97, 0, 0, 30 - 30.58 * 0.99, 0, 0, 30 - 30.17 * 0.99, 0, 0, 30 - 27.83 * 0.99}), 0); cplex.addLe(cplex.scalProd(x, new double[]{10 - 11.32 * 0.98, 0, 0, 10 - 12.83 * 0.97, 0, 0, 10 - 16.06 * 0.99, 0, 0, 10 - 5.68 * 0.99, 0, 0, 10 - 8.54 * 0.99, 0, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 10 - 11.32 * 0.98, 0, 0, 10 - 12.83 * 0.97, 0, 0, 10 - 16.06 * 0.99, 0, 0, 10 - 5.68 * 0.99, 0, 0, 10 - 8.54 * 0.99, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 10 - 11.32 * 0.98, 0, 0, 10 - 12.83 * 0.97, 0, 0, 10 - 16.06 * 0.99, 0, 0, 10 - 5.68 * 0.99, 0, 0, 10 - 8.54 * 0.99}), 0); cplex.addLe(cplex.scalProd(x, new double[]{6 * 0.98 - 5, 0, 0, 4 * 0.97 - 5, 0, 0, 5 * 0.99 - 5, 0, 0, 2 * 0.99 - 5, 0, 0, 5 * 0.99 - 5, 0, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 6 * 0.98 - 5, 0, 0, 4 * 0.97 - 5, 0, 0, 5 * 0.99 - 5, 0, 0, 2 * 0.99 - 5, 0, 0, 5 * 0.99 - 5, 0}), 0); cplex.addLe(cplex.scalProd(x, new double[]{0, 0, 6 * 0.98 - 5, 0, 0, 4 * 0.97 - 5, 0, 0, 5 * 0.99 - 5, 0, 0, 2 * 0.99 - 5, 0, 0, 5 * 0.99 - 5}), 0); if (cplex.solve()) { cplex.output().println("Solution status = " + cplex.getStatus()); cplex.output().println("Solution value = " + cplex.getObjValue()); double[] val = cplex.getValues(x); for (int j = 0; j < val.length; j++) { cplex.output().println("x" + (j+1) + " = " + val[j]); } } cplex.end(); } catch (Exception e) { System.err.println("Concert exception caught: " + e); } // Iteration log . . . // Iteration: 1 Dual objective = 99025.641026 // Solution status = Optimal // Solution value = 44280.112731282905 // x1 = 4022.4510446323093 // x2 = 0.0 // x3 = 5977.548955367691 // x4 = 10000.0 // x5 = 0.0 // x6 = 0.0 // x7 = 211.60418010554542 // x8 = 3814.5789532616586 // x9 = 5973.816866632794 // x10 = 0.0 // x11 = 801.3074331974203 // x12 = 9198.69256680258 // x13 = 2913.635241579134 // x14 = 384.1136135409201 // x15 = 982.3638761628501 // [INFO] ------------------------------------------------------------------------ // [INFO] BUILD SUCCESS // [INFO] ------------------------------------------------------------------------ // [INFO] Total time: 2.757 s // [INFO] Finished at: 2021-04-12T09:58:14+08:00 // [INFO] ------------------------------------------------------------------------
感谢各位的阅读,以上就是“java线性规划问题举例分析”的内容了,经过本文的学习后,相信大家对java线性规划问题举例分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!