0

我将 cplex 与 C++ 一起使用。最近,我在看下面的例子。然后我对填充功能感到困惑。它是按值传递对吗?但是为什么在执行填充函数后,主函数中创建的模型会更新?它不应该仍然是空模型吗?

// -------------------------------------------------------------- -*- C++ -*-
// File: ilolpex1.cpp
// Version 12.5
// --------------------------------------------------------------------------
// Licensed Materials - Property of IBM
// 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
// Copyright IBM Corporation 2000, 2012. All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
// --------------------------------------------------------------------------
//
// ilolpex1.cpp - Entering and optimizing a problem.  Demonstrates different
// methods for creating a problem.  The user has to choose the method
// on the command line:
//
//    ilolpex1  -r     generates the problem by adding rows
//    ilolpex1  -c     generates the problem by adding columns
//    ilolpex1  -n     generates the problem by adding a list of coefficients

#include <ilcplex/ilocplex.h>
ILOSTLBEGIN

static void
   usage (const char *progname),
   populatebyrow     (IloModel model, IloNumVarArray var, IloRangeArray con),
   populatebycolumn  (IloModel model, IloNumVarArray var, IloRangeArray con),
   populatebynonzero (IloModel model, IloNumVarArray var, IloRangeArray con);

int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env);

      if (( argc != 2 )                         ||
          ( argv[1][0] != '-' )                 ||
          ( strchr ("rcn", argv[1][1]) == NULL )   ) {
         usage (argv[0]);
         throw(-1);
      }

      IloNumVarArray var(env);
      IloRangeArray con(env);

      switch (argv[1][1]) {
         case 'r':
            populatebyrow (model, var, con);
            break;
         case 'c':
            populatebycolumn (model, var, con);
            break;
         case 'n':
            populatebynonzero (model, var, con);
            break;
      }

      IloCplex cplex(model);
      cplex.exportModel("lpex1.lp");

      // Optimize the problem and obtain solution.
      if ( !cplex.solve() ) {
         env.error() << "Failed to optimize LP" << endl;
         throw(-1);
      }

      IloNumArray vals(env);
      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;
      cplex.getDuals(vals, con);
      env.out() << "Duals         = " << vals << endl;
      cplex.getReducedCosts(vals, var);
      env.out() << "Reduced Costs = " << vals << endl;
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();

   return 0;
}  // END main


static void usage (const char *progname)
{
   cerr << "Usage: " << progname << " -X" << endl;
   cerr << "   where X is one of the following options:" << endl;
   cerr << "      r          generate problem by row" << endl;
   cerr << "      c          generate problem by column" << endl;
   cerr << "      n          generate problem by nonzero" << endl;
   cerr << " Exiting..." << endl;
} // END usage


// To populate by row, we first create the variables, and then use them to
// create the range constraints and objective.

static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   x.add(IloNumVar(env, 0.0, 40.0));
   x.add(IloNumVar(env));
   x.add(IloNumVar(env));

   model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2]));

   c.add( - x[0] +     x[1] + x[2] <= 20);
   c.add(   x[0] - 3 * x[1] + x[2] <= 30);

   x[0].setName("x1");
   x[1].setName("x2");
   x[2].setName("x3");

   c[0].setName("c1");
   c[1].setName("c2");
   model.add(c);

}  // END populatebyrow


// To populate by column, we first create the range constraints and the
// objective, and then create the variables and add them to the ranges and
// objective using column expressions.

static void
populatebycolumn (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   IloObjective obj = IloMaximize(env);
   c.add(IloRange(env, -IloInfinity, 20.0, "c1"));
   c.add(IloRange(env, -IloInfinity, 30.0, "c2"));

   x.add(IloNumVar(obj(1.0) + c[0](-1.0) + c[1]( 1.0), 0.0, 40.0));
   x.add(IloNumVar(obj(2.0) + c[0]( 1.0) + c[1](-3.0)));
   x.add(IloNumVar(obj(3.0) + c[0]( 1.0) + c[1]( 1.0)));

   x[0].setName("x1");
   x[1].setName("x2");
   x[2].setName("x3");

   model.add(obj);
   model.add(c);

}  // END populatebycolumn


// To populate by nonzero, we first create the rows, then create the
// columns, and then change the nonzeros of the matrix 1 at a time.

static void
populatebynonzero (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   IloObjective obj = IloMaximize(env);
   c.add(IloRange(env, -IloInfinity, 20.0));
   c.add(IloRange(env, -IloInfinity, 30.0));

   x.add(IloNumVar(env, 0.0, 40.0));
   x.add(IloNumVar(env));
   x.add(IloNumVar(env));

   obj.setLinearCoef(x[0], 1.0);
   obj.setLinearCoef(x[1], 2.0);
   obj.setLinearCoef(x[2], 3.0);

   c[0].setLinearCoef(x[0], -1.0);
   c[0].setLinearCoef(x[1],  1.0);
   c[0].setLinearCoef(x[2],  1.0);
   c[1].setLinearCoef(x[0],  1.0);
   c[1].setLinearCoef(x[1], -3.0);
   c[1].setLinearCoef(x[2],  1.0);

   c[0].setName("c1");
   c[1].setName("c2");

   x[0].setName("x1");
   x[1].setName("x2");
   x[2].setName("x3");

   model.add(obj);
   model.add(c);
}  // END populatebynonzero
4

3 回答 3

1

是的,这些类是真正的“句柄”类——实际上只是一些语法糖包裹在一个好的旧指针上。原因是这样更容易使代码看起来更干净,并且它允许库更好地控制正在发生的事情,而不是仅仅允许指针无处不在,并且更容易做一些事情,比如确保对象被分配到例如,一个自定义堆,通过使用专门的内存管理来获得更好的性能,该内存管理对它必须管理的对象类型有更多的了解。自 90 年代后期以来,我一直将 CPLEX 用于许多项目,并用它教授了许多课程。如果做得好,它可以使代码更干净。就在您实现自己的自定义扩展时,有时您需要同时编写实现类和句柄类,

于 2013-11-01T00:10:26.120 回答
1

看 ilocplex.h,可能 IloModel 被定义为指针。或手册所述的“手柄”。

我无法使用谷歌轻松找到 ilocplex.h。但这就是我认为它的工作方式:

struct ModelObj {
   int data1, data2, data3;
}

struct Model {
   ModelObj* obj;
}

现在您可以将模型作为值传递,但仍将obj指向引用。

于 2013-10-31T03:44:35.810 回答
-1

请检查:

  1. 你的头文件是否有自定义的复制构造函数?

  2. IloModel是基于指针的类型吗?像exebook提到的那样?

自定义复制构造函数详解

您的代码是否具有自定义的复制构造函数,尤其是当类在堆中创建一些数据字段时。请检查您的代码以了解 class 的定义IloModel

  • 如果没有自定义副本构造,则模型类应按值传递。

  • 如果定义了自定义的复制构造函数,并且只使用了引用,则可以通过引用传递。

这是一段代码,可以向您展示这种类型的构造函数是如何工作的:

#include <string>
#include <iostream>

using namespace std;

class person {
  public:
  std::string _name;
  int* _age_ptr; 

  person (const std::string& name, int age) {
    _name = name;
    _age_ptr = new int(age); 
  }

  person(const person& that) {  // The customized copy constructor
    _name = that._name;
    _age_ptr = that._age_ptr;
    cout << "run copy constructor" << endl;
  }

  void print() {
    cout << "name:" << _name <<", age:" << *_age_ptr << endl;
  }

};

person update_age( person that) {
  cout << "Set age tp 70" << endl;
  *that._age_ptr = 70;
  return that;
}

int main() {
  person a("Jim", 60);
  cout <<"Before update, a" << endl;
  a.print(); // Output: name:Jim, age 60
  cout << endl;
  person b = update_age(a);
  cout <<"After update, a" << endl;
  a.print(); // HERE Output: name:Jim, age 70
  cout << endl;
  cout <<"After update, b" << endl;                                                                       
  b.print();
  return 0;
}

有关复制构造函数的更多信息,这里有一个有用的链接:什么是复制和交换习语?

于 2013-10-31T03:46:14.003 回答