线性规划:模约束
Linear Programming: Modulo constraint
我正在使用Coin-Or的排练来实现线性规划。
我需要一个模约束。示例:x
应是3
的倍数。
OsiCbcSolverInterface solver;
CelModel model(solver);
CelNumVar x;
CelIntVar z;
unsigned int mod = 3;
// Maximize
solver.setObjSense(-1.0);
model.setObjective(x);
model.addConstraint(x <= 7.5);
// The modulo constraint:
model.addConstraint(x == z * mod);
x
的结果应为 6。但是,z
设置为2.5
,这应该是不可能的,因为我将其声明为CellIntVar
。
如何强制z
为整数?
我从未使用过那个库,但我认为你应该遵循测试。
核心信息来自自述文件:
如果您希望某些变量是整数,请使用 CelIntVar 而不是 CelNumVar。还必须将求解器绑定到整数线性规划求解器,例如 Coin-cbc。
查看 Rehearse/tests/testRehearse.cpp -> exemple4(((这里呈现:不完整的代码;没有复制粘贴(:
OsiClpSolverInterface *solver = new OsiClpSolverInterface();
CelModel model(*solver);
...
CelIntVar x1("x1");
...
solver->initialSolve(); // this is the relaxation (and maybe presolving)!
...
CbcModel cbcModel(*solver); // MIP-solver
cbcModel.branchAndBound(); // Use MIP-solver
printf("Solution for x1 : %gn", model.getSolutionValue(x1, *cbcModel.solver()));
printf("Solution objvalue = : %gn", cbcModel.solver()->getObjValue());
这种用法(使用Osi获取LP-solver;在Osi提供的LP-solver之上构建MIP求解器并调用brandAndBound(基本上遵循Cbc的内部接口(使用python的cylp看起来很相似(。
作为参考:这是官方的CoinOR CBC(免排练(示例:
// Copyright (C) 2005, International Business Machines
// Corporation and others. All Rights Reserved.
#include "CbcModel.hpp"
// Using CLP as the solver
#include "OsiClpSolverInterface.hpp"
int main (int argc, const char *argv[])
{
OsiClpSolverInterface solver1;
// Read in example model in MPS file format
// and assert that it is a clean model
int numMpsReadErrors = solver1.readMps("../../Mps/Sample/p0033.mps","");
assert(numMpsReadErrors==0);
// Pass the solver with the problem to be solved to CbcModel
CbcModel model(solver1);
// Do complete search
model.branchAndBound();
/* Print the solution. CbcModel clones the solver so we
need to get current copy from the CbcModel */
int numberColumns = model.solver()->getNumCols();
const double * solution = model.bestSolution();
for (int iColumn=0;iColumn<numberColumns;iColumn++) {
double value=solution[iColumn];
if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn))
printf("%d has value %gn",iColumn,value);
}
return 0;
}
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- 约束和显式模板实例化
- IpOpt拒绝解决不受约束的问题
- 使用C++模板时表达约束
- 如何在 SCIP C++ 接口中获取 MILP 约束矩阵中的系数值
- 受约束的成员函数和显式模板实例化
- 具有多种约束(例如重量、体积等)的背包
- Gecode 与 Z3 用于约束随机化
- 如果原型是本地的,则使用流 I/O C++类型约束将失败
- C++打开具有 2 个约束的文件
- 如何拥有受约束的运算符模板?
- 不生成单独约束的变量的 Gurobi 影子价格
- 在 C++20 中对概念约束函数进行排序的规则是什么?
- 如何使用 g2o 优化多约束函数
- 约束类模板函数以接受特定的 POD 类型
- 约束包容是否仅适用于概念?
- 检查特定txt文件中的项目是否符合"名称编号USACO"中的约束
- 对类模板成员的显式专用化的约束
- 如何在没有外部库的情况下使用C++03约束执行基于正则表达式的字符串操作
- 线性规划:模约束