• [C#]C#调用cplex


    测试环境

    vs2019

    cplex==12.10.0

    winform代码:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using ILOG.Concert;
    11. using ILOG.CPLEX;
    12. using System.Collections;
    13. using static WindowsFormsApp1.AdMIPex1;
    14. namespace WindowsFormsApp1
    15. {
    16. public class AdMIPex1
    17. {
    18. internal class MyBranch : Cplex.BranchCallback
    19. {
    20. internal INumVar[] _vars;
    21. internal MyBranch(INumVar[] vars) { _vars = vars; }
    22. public override void Main()
    23. {
    24. if (!GetBranchType().Equals(Cplex.BranchType.BranchOnVariable))
    25. return;
    26. // Branch on var with largest objective coefficient
    27. // among those with largest infeasibility
    28. double[] x = GetValues(_vars);
    29. double[] obj = GetObjCoefs(_vars);
    30. Cplex.IntegerFeasibilityStatus[] feas = GetFeasibilities(_vars);
    31. double maxinf = 0.0;
    32. double maxobj = 0.0;
    33. int bestj = -1;
    34. int cols = _vars.Length;
    35. for (int j = 0; j < cols; ++j)
    36. {
    37. if (feas[j].Equals(Cplex.IntegerFeasibilityStatus.Infeasible))
    38. {
    39. double xj_inf = x[j] - System.Math.Floor(x[j]);
    40. if (xj_inf > 0.5) xj_inf = 1.0 - xj_inf;
    41. if (xj_inf >= maxinf &&
    42. (xj_inf > maxinf || System.Math.Abs(obj[j]) >= maxobj))
    43. {
    44. bestj = j;
    45. maxinf = xj_inf;
    46. maxobj = System.Math.Abs(obj[j]);
    47. }
    48. }
    49. }
    50. if (bestj >= 0)
    51. {
    52. MakeBranch(_vars[bestj], x[bestj],
    53. Cplex.BranchDirection.Up, GetObjValue());
    54. MakeBranch(_vars[bestj], x[bestj],
    55. Cplex.BranchDirection.Down, GetObjValue());
    56. }
    57. }
    58. }
    59. internal class MySelect : Cplex.NodeCallback
    60. {
    61. public override void Main()
    62. {
    63. int remainingNodes = GetNremainingNodes();
    64. int bestnode = -1;
    65. int maxdepth = -1;
    66. double maxiisum = 0.0;
    67. for (int i = 0; i < remainingNodes; ++i)
    68. {
    69. int depth = GetDepth(i);
    70. double iisum = GetInfeasibilitySum(i);
    71. if ((depth >= maxdepth) &&
    72. (depth > maxdepth || iisum > maxiisum))
    73. {
    74. bestnode = i;
    75. maxdepth = depth;
    76. maxiisum = iisum;
    77. }
    78. }
    79. if (bestnode >= 0) SelectNode(bestnode);
    80. }
    81. }
    82. }
    83. public partial class Form1 : Form
    84. {
    85. public Form1()
    86. {
    87. InitializeComponent();
    88. }
    89. private void button1_Click(object sender, EventArgs e)
    90. {
    91. try
    92. {
    93. Cplex cplex = new Cplex();
    94. cplex.ImportModel(@"D:\lufiles\CPLEX_Studio\cplex\examples\data\mexample.mps");
    95. IEnumerator matrixEnum = cplex.GetLPMatrixEnumerator();
    96. matrixEnum.MoveNext();
    97. ILPMatrix lp = (ILPMatrix)matrixEnum.Current;
    98. cplex.Use(new MyBranch(lp.NumVars));
    99. cplex.Use(new MySelect());
    100. cplex.SetParam(Cplex.Param.MIP.Strategy.Search, Cplex.MIPSearch.Traditional);
    101. if (cplex.Solve())
    102. {
    103. System.Console.WriteLine("Solution status = " + cplex.GetStatus());
    104. System.Console.WriteLine("Solution value = " + cplex.ObjValue);
    105. }
    106. cplex.End();
    107. }
    108. catch (ILOG.Concert.Exception ex)
    109. {
    110. System.Console.WriteLine("Concert exception caught: " + ex);
    111. }
    112. }
    113. }
    114. }

    运行结果:

    1. Selected objective sense: MINIMIZE
    2. Selected objective name: obj
    3. Selected RHS name: rhs
    4. Selected bound name: bnd
    5. Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
    6. CPXPARAM_Preprocessing_Reduce 1
    7. CPXPARAM_Preprocessing_Linear 0
    8. CPXPARAM_MIP_Strategy_Search 1
    9. Found incumbent of value -46.000000 after 0.00 sec. (0.00 ticks)
    10. Tried aggregator 2 times.
    11. Aggregator did 1 substitutions.
    12. Reduced MIP has 2 rows, 3 columns, and 6 nonzeros.
    13. Reduced MIP has 0 binaries, 1 generals, 0 SOSs, and 0 indicators.
    14. Presolve time = 0.00 sec. (0.00 ticks)
    15. Tried aggregator 1 time.
    16. Reduced MIP has 2 rows, 3 columns, and 6 nonzeros.
    17. Reduced MIP has 0 binaries, 1 generals, 0 SOSs, and 0 indicators.
    18. Presolve time = 0.00 sec. (0.00 ticks)
    19. MIP emphasis: balance optimality and feasibility.
    20. MIP search method: traditional branch-and-cut.
    21. Parallel mode: none, using 1 thread.
    22. Root relaxation solution time = 0.02 sec. (0.00 ticks)
    23. Nodes Cuts/
    24. Node Left Objective IInf Best Integer Best Bound ItCnt Gap Variable B NodeID Parent Depth
    25. * 0+ 0 -46.0000 -163.0000 254.35%
    26. * 0+ 0 -122.5000 -163.0000 33.06%
    27. 0 0 -125.2083 1 -122.5000 -125.2083 3 2.21%
    28. 0 0 cutoff -122.5000 3 0.00% 0 0
    29. Elapsed time = 0.02 sec. (0.02 ticks, tree = 0.01 MB, solutions = 2)
    30. Root node processing (before b&c):
    31. Real time = 0.02 sec. (0.02 ticks)
    32. Sequential b&c:
    33. Real time = 0.00 sec. (0.00 ticks)
    34. ------------
    35. Total (root+branch&cut) = 0.02 sec. (0.02 ticks)
    36. Solution status = Optimal
    37. Solution value = -122.5

    注意事项:

    1、调用x64库需要选择x64平台进行编译,不能选择x86平台

    2、要将DLL路径加入环境变量Path,否则需要将DLL复制到运行目录才可以

    拓展:

    1、更多C#案例可以参考官方安装包中的CPLEX_Studio\cplex\examples\src\cs

    2、所有官方测试数据在路径CPLEX_Studio\cplex\examples\data

    3、C#官方安装教程参考CPLEX_Studio\cplex\dotnet.html

  • 相关阅读:
    【16】基础知识:React路由 - React Router 6
    SVN 修改版本库地址url路径
    Python程序员:代码写的好,丝滑的壁纸少不了
    深入探析网络代理与网络安全
    实时跟踪企业订单执行情况,采购系统助力汽车服务企业缩短采购订单周期
    2.28 OrCAD中怎么对元器件管脚属性进行统一更改?
    论文阅读[51]通过深度学习快速识别荧光组分
    Buuctf [MRCTF2020]Ez_bypass 1 WP解析
    Android 系统通过pid和vid固定usb声卡节点
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/133375252