【问题标题】:Linear Programming: Modulo constraint线性规划:模约束
【发布时间】:2018-06-21 13:37:09
【问题描述】:

我正在使用 Coin-Or 的rehearse 来实现线性规划。

我需要一个模数约束。示例: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 为整数?

【问题讨论】:

    标签: c++ linear-programming coin-or-cbc


    【解决方案1】:

    我从未使用过那个库,但我认为你应该关注tests

    核心信息来自自述文件:

    如果您希望某些变量为整数,请使用 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 这看起来相似) .

    仅供参考:这是来自here 的官方 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;
    }
    

    【讨论】:

    • 哦哇哦..我也读了你加粗的那行,我认为它的意思是“使用 CbcSolverInterface!”。无论如何,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    相关资源
    最近更新 更多