线性规划:模约束

Linear Programming: Modulo constraint

本文关键字:约束 线性规划      更新时间:2023-10-16

我正在使用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;
}