1 C++调用Cplex的在VS2010中的配置及示例代码演示
1.1 cplex在vs2010中的配置
- 正常建立一个控制台项目
- 在【项目--属性】下,作如下设置:
- 【C/C++】--【常规】--【附加库目录】,添加如下两个库目录:
你的安装位置\concert\include |
你的安装位置\cplex\include |
- 【C/C++】--【预处理器】--【预处理器定义】,粘贴如下内容:
WIN32 NDEBUG _CONSOLE _LIB IL_STD |
- 【连接器】--【附加库目录】,选中如下两个路径:
你的安装路径\concert\lib\x86_windows_vs2010\stat_mda 你的安装路径\cplex\lib\x86_windows_vs2010\stat_mda |
- 【连接器】--【附加依赖项】,复制以下内容:
concert.lib cplex1261.lib ilocplex.lib |
1.2 测试示例
1.2.1 测试用例
1.2.2 测试代码
//**************************************************************// // the test version of Cplex with C++ // time 2019-09-28 // author A & R //*************************************************************// #include <ilcplex/ilocplex.h> #include<cstdlib> #include<iostream> ILOSTLBEGIN //#define ILOSTLBEGIN using namespace std; int main(int argc, char **argv) { IloEnv env;//construct an Cplex environment env, which belongs to a handle class IloEnv; try{ IloModel model(env);// define the varies "env" IloNumVarArray vars(env);// define the varies vars(env), the x1, x2, x3 can be expressed with vars(0), vars(1), vars(2) // define the bounds of varies vars.add(IloNumVar(env, 0.0, 40.0)); vars.add(IloNumVar(env)); vars.add(IloNumVar(env)); //objective to optimize: model.add(IloMaximize(env, vars[0]+2*vars[1]+3*vars[2]));//opjective: Maximize the x1+2*x2+3*x3; //subject to: model.add(-vars[0]+vars[1]+vars[2]<=20); model.add(vars[0]-3*vars[1]+vars[2]<=30); //expection processing, refer to "C++ try&catch" IloCplex cplex(model); if(!cplex.solve()){ env.error()<<"Failed to optimize LP."<<endl; throw(-1); } IloNumArray vals(env); env.out() << "solution status = "<< cplex.getStatus() << endl; env.out() << "Solution value=" << cplex.getObjective() << endl; cplex.getValues(vals, vars); env.out() << "Values = " << vals << endl; } // cathch functions, refer to "C++ try&catch" catch(IloException&e){ cerr << "Concert exception caught:" << e << endl; } catch(...){ cerr << "Unknown exception caught" << endl; } env.end(); //return 0; system("PAUSE"); return EXIT_SUCCESS; }
1.2.3 运行结果
2 C#调用Cplex在VS2010下的配置
2.1 C#配置Cplex
这个配置比较简单,直接添加两个引用即可
你的Cplex安装目录\cplex\bin\x86_win32\ILOG.CPLEX.dll"
你的Cplex安装目录\cplex\bin\x86_win32\ILOG.Concert.dll"
2.2 算例测试及代码
2.2.1 算例代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ILOG.Concert; using ILOG.CPLEX; namespace csCplex { class Program { static void Main(string[] args) { try { //1 先创建一个Cplex空模型 Cplex cplexModel = new Cplex(); //2 生成决策变量并赋值 INumVar[][] deVar = new INumVar[1][]; double[] lowerBound = { 0.0, 0.0, 0.0}; double[] upBound = { 40.0, double.MaxValue,double.MaxValue }; string[] deVarName = { "x1", "x2", "x3"}; //包含两个变量,下界,上界,变量名称 INumVar[] X = cplexModel.NumVarArray(3, lowerBound, upBound, deVarName); deVar[0] = X; //定义模型目标函数的系数 double[] objCoff = { 1.0, 2.0, 3.0 }; cplexModel.AddMaximize(cplexModel.ScalProd(X, objCoff)); //3 添加约束条件 IRange[][] rng = new IRange[1][]; rng[0] = new IRange[2]; //添加小于等于约束 rng[0][0] = cplexModel.AddLe( cplexModel.Sum(cplexModel.Prod(-1.0, X[0]), cplexModel.Prod(1.0, X[1]),cplexModel.Prod(1.0, X[2])), 20, "约束1"); rng[0][1] = cplexModel.AddLe( cplexModel.Sum(cplexModel.Prod(1.0, X[0]), cplexModel.Prod(-3.0, X[1]), cplexModel.Prod(1.0, X[2])), 30, "约束2"); //4 导出模型 cplexModel.ExportModel("C#调用Cplex演示示例模型.lp"); //5 求解模型 if (cplexModel.Solve()) { int nvars = cplexModel.GetValues(deVar[0]).Length; for (int i = 0; i < nvars; i++) { cplexModel.Output().WriteLine("x" + i + "=" + cplexModel.GetValues(deVar[0])[i]); } } cplexModel.End(); } catch(ILOG.Concert.Exception e) { System.Console.WriteLine("concert exception:" + e); } finally { System.Console.WriteLine("模型求解完毕"); } } } }
2.2.3 运行结果
3 Java语言调用Cplex的配置与示例演示
注意java的位数要与Cplex的位数一致,64位与32位不兼容
3.1 配置
- 添加环境变量
您的Cplex安装路径\cplex\bin\x86_win32\cplex1261.dll
您的Cplex安装路径\cplex\lib\cplex.jar
- 设置构建路径,【你的java项目,右键】--【Bulid Path】--【Condigure Build Path..】--【Existing Project into Workspce】--【Next】
- 【Add External jars】,选择你安装位置下的\cplex\lib\cplex.jar
- 配置库