[Clp] C++ Ok, C - dont know Re. CLP from a C prog.
Alexey Lvov
lvov at us.ibm.com
Fri May 15 10:02:51 EDT 2009
>>I am a beginner regarding CLP, and I would like to ask you whether CLP
can be called from a C program. If so, is there some documentation that
explains how to do that?
Hi,
Do you mean exactly C, not C++? For C I do not know the answer (the chances
are it may not be possible because COIN it written in C++). For C++ please
see below. CLP documentation starts with a simple example which shows how
to solve problem from a file. I would also put a reference to file
Clp-1.10.0/Clp/examples/addRows.cpp (or similar) to the front page of CLP
doc. It shows how to create and solve CLP problem in memory. Please find
two examples below. #1 follows CLP doc. #2 follows what John Forrest sent
me over lately. The second one worked much better for me. ( Also to be able
to "make" your program, first follow the instructions in Clp-1.10.0/INSTALL
).
----------------------------------------------------
Dr. Alexey Lvov
IBM T.J. Watson Research Institute
1101 Kitchawan Rd. (Rt. 134) Office 34-159
Yorktown Heights NY 10598
Tel: (914)-945-3732.
1) (a piece from Clp-1.10.0/Clp/examples/addRows.cpp )
// make DRIVER=NNN makes file NNN.cpp //
#include "ClpSimplex.hpp"
#include "ClpPrimalColumnSteepest.hpp"
#include "ClpDualRowSteepest.hpp"
int main ()
{
// Minimize x0 + 2 * x1 + 4 * x2 + 5 * x3,
//
// 0 <= x0 <= 15,
// 0 <= x1 ,
// x2 is not bound by any constraints,
// -1 <= x3 ,
//
// x0 - x2 <= 3,
//
// x0 + x1 + x2 + x3 = 23.
// --- BEGIN add the objective function and bounds on individual var.
--- //
const int num_variables = 4;
ClpSimplex model;
model.resize(0,num_variables); // The first argument must be 0.
model.setObjectiveCoefficient(0, 1.0);
// Lower bounds for variables are 0.0 by default.
model.setColumnUpper( 0, 15.0);
model.setObjectiveCoefficient(1, 2.0);
// Lower bounds for variables are 0.0 by default
model.setColumnUpper( 1, COIN_DBL_MAX);
model.setObjectiveCoefficient(2, 4.0);
model.setColumnLower( 2, -COIN_DBL_MAX);
model.setColumnUpper( 2, COIN_DBL_MAX);
model.setObjectiveCoefficient(3, 5.0);
model.setColumnLower( 3, -1.0);
model.setColumnUpper( 3, COIN_DBL_MAX);
// --- END add the objective function and bounds on individual var.
--- //
// -- BEGIN add equality and ineq. constr. on lin. combinations of var.
-- //
int * rowIndex;
double * rowValue;
int num_nonzero_coef;
num_nonzero_coef = 2;
rowIndex = new int [num_nonzero_coef];
rowValue = new double[num_nonzero_coef];
rowIndex[0] = 0; rowValue[0] = 1.0;
rowIndex[1] = 2; rowValue[1] = -1.0;
// The 4-th argument is the lower bound,
// The 5-th argument is the upper bound.
model.addRow(num_nonzero_coef, rowIndex, rowValue, -COIN_DBL_MAX, 3.0);
delete[] rowValue;
delete[] rowIndex;
num_nonzero_coef = 4;
rowIndex = new int [num_nonzero_coef];
rowValue = new double[num_nonzero_coef];
rowIndex[0] = 0; rowValue[0] = 1.0;
rowIndex[1] = 1; rowValue[1] = 1.0;
rowIndex[2] = 2; rowValue[2] = 1.0;
rowIndex[3] = 3; rowValue[3] = 1.0;
// The standard way of adding an equality constraint is to set both
// the lower and upper bound to the same value.
model.addRow(num_nonzero_coef, rowIndex, rowValue, 23.0, 23.0);
delete[] rowValue;
delete[] rowIndex;
// -- END add equality and ineq. constr. on lin. combinations of var.
-- //
// Increase level of detail
model.setLogLevel(4);
// The model has been set up. Now solve!
model.primal();
char const * const status_message[5] = {
"optimal",
"primal infeasible",
"primal unbounded, dual infeasible",
"stopped on iterations etc",
"stopped due to errors"};
std::cout << "Model status is \""
<< status_message[model.status()] << "\" after "
<< model.numberIterations() << " iterations - objective is "
<< model.objectiveValue() << std::endl;
std::cout << "The model has " << model.numberRows()
<< " rows and " << model.numberColumns()
<< " variables." << std::endl;
if (num_variables != model.numberColumns()) {
std::cout << "ERROR : num_variables != model.numberColumns() .\n";
return -1;
}
double * columnPrimal = model.primalColumnSolution();
int iColumn;
printf("Solution:\n");
for (iColumn = 0; iColumn < num_variables; iColumn++)
printf("x%d = %8.5f\n", iColumn, columnPrimal[iColumn]);
return 0;
}
2)
#include "ClpSimplex.hpp"
#include "ClpSolve.hpp"
#include "CoinModel.hpp"
.......
..........
ClpSimplex * model = new ClpSimplex;
model->resize(0, lp_num_variables); // The first argument must be 0.
for (i = 0; i < lp_num_variables; i++) {
model->setObjectiveCoefficient(i, lp_objective_function[i]);
model->setColumnLower(i, (i < lp_num_s_variables) ? 0.0 :
-COIN_DBL_MAX);
model->setColumnUpper(i, COIN_DBL_MAX);
}
{
// use {} so model2 will vanish
CoinModel model2;
for (i = 0; i < lp_num_constr_on_s; i++) { // equality constraints
model2.addRow(lp_constraints[i].num_coefficients,
lp_constraints[i].indexes,
lp_constraints[i].coefficients,
lp_constraints[i].right_part,
lp_constraints[i].right_part);
}
for ( ; i < lp_num_constraints; i++) { // inequality constraints
model2.addRow(lp_constraints[i].num_coefficients,
lp_constraints[i].indexes,
lp_constraints[i].coefficients,
lp_constraints[i].right_part,
COIN_DBL_MAX);
}
model->addRows(model2,false);
}
model->setLogLevel(1);
ClpSolve solveOptions;
model->setPerturbation(50);
model->initialSolve(solveOptions);
...........
..........
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/clp/attachments/20090515/9ac17acf/attachment.html>
More information about the Clp
mailing list