SCIP求解器使用VS软c++语言调用详细方法

一、内容来源于以下博主的总结

https://www.cnblogs.com/dengfaheng/p/10041488.html

在C++中使用SCIP求解器-CSDN博客

在执行以下步骤之前,需要下载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、后续遇到问题再补充 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值