1

我正在使用 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成为一个整数?

4

1 回答 1

2

我从未使用过那个库,但我认为你应该遵循测试

核心信息来自自述文件:

如果您希望某些变量是整数,请使用 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 : %g\n", model.getSolutionValue(x1, *cbcModel.solver()));
printf("Solution objvalue = : %g\n", cbcModel.solver()->getObjValue());

这种用法(使用 Osi 获得 LP-solver;在 Osi-provided-LP-solver 之上构建 MIP-solver 并调用 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 %g\n",iColumn,value);
   }
  return 0;
}
于 2018-06-21T14:05:17.953 回答