一、内容来源于以下博主的总结
https://www.cnblogs.com/dengfaheng/p/10041488.html
在执行以下步骤之前,需要下载scip,并保证在命令端窗口可以执行scip的功能
二、具体步骤
1、创建cpp空项目文件
2、选择环境,x64 release
3、VC++目录->外部包含目录
在scip安装目录中的include加入
F:\programmsoftware\SCIP\SCIPOptSuite 8.0.3\include
4、VC++目录->库目录
在scip安装目录中的lib加入
F:\programmsoftware\SCIP\SCIPOptSuite 8.0.3\lib
5、链接器->输入->附加库依赖项
加入以下依赖项,有时候可能会报错找不到,可能于scip的版本有关 可以去lib目录看看
libscip.lib或者scip.lib
6、运行如下代码
#include <iostream>
#include <scip/scipdefplugins.h>
#pragma comment(lib,"libscip.lib")
//目标函数: 2x+3y
//con 2x+3y >= 7
int main(void) {
// creates and initializes SCIP data structures
SCIP* _scip;
SCIPcreate(&_scip);
// include default plugins
SCIPincludeDefaultPlugins(_scip);
// creates empty problem and initializes all solving data structures
SCIPcreateProbBasic(_scip, "test");
//sets objective sense of problem
SCIPsetObjsense(_scip, SCIP_OBJSENSE_MINIMIZE);
// changes the value of an existing SCIP_Real parameter
SCIPsetRealParam(_scip, "limits/gap", 0.00001);
SCIPsetRealParam(_scip, "limits/time", 10);
//--添加变量
SCIP_VAR* xVar = nullptr;
SCIPcreateVarBasic(_scip, &xVar, "_x", 0, SCIPinfinity(_scip), 2.0, SCIP_VARTYPE_INTEGER);
SCIPaddVar(_scip, xVar);
SCIP_VAR* yVar = nullptr;
SCIPcreateVarBasic(_scip, &yVar, "_y", 0, SCIPinfinity(_scip), 3.0, SCIP_VARTYPE_INTEGER);
SCIPaddVar(_scip, yVar);
//--添加约束
SCIP_CONS* zCons;
SCIPcreateConsBasicLinear(_scip, &zCons, "z_cons", 0, NULL, NULL, 7, SCIPinfinity(_scip));
SCIPaddCoefLinear(_scip, zCons, xVar, 2);
SCIPaddCoefLinear(_scip, zCons, yVar, 3);
SCIPaddCons(_scip, zCons);
SCIPsolve(_scip);
//--取最优解
SCIP_Real x_value = SCIPgetSolVal(_scip, SCIPgetBestSol(_scip), xVar);
SCIP_Real y_value = SCIPgetSolVal(_scip, SCIPgetBestSol(_scip), yVar);
//--释放资源
SCIPreleaseVar(_scip, &xVar);
SCIPreleaseVar(_scip, &yVar);
SCIPreleaseCons(_scip, &zCons);
SCIPfree(&_scip);
std::cout << "x_value: " << x_value << std::endl;
std::cout << "y_value: " << y_value << std::endl;
system("pause");
return 0;
}
7、运行结果如下
8、后续遇到问题再补充