source: trunk/Clp/examples/rowColumn.cpp @ 755

Last change on this file since 755 was 459, checked in by forrest, 15 years ago

To show creating by row or column

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1/* Copyright (C) 2004, International Business Machines Corporation
2   and others.  All Rights Reserved.
3
4   This sample program is designed to illustrate programming
5   techniques using CoinLP, has not been thoroughly tested
6   and comes without any warranty whatsoever.
7
8   You may copy, modify and distribute this sample program without
9   any restrictions whatsoever and without any payment to anyone.
10*/
11
12#include "ClpSimplex.hpp"
13#include <cstdio>
14#include <cassert>
15
16int main (int argc, const char *argv[])
17{
18  ClpSimplex  modelByRow, modelByColumn;
19
20  // This very simple example shows how to create a model by row and by column
21  int numberRows=3;
22  int numberColumns=5;
23  // Rim of problem is same in both cases
24  double objective [] = {1000.0,400.0,500.0,10000.0,10000.0};
25  double columnLower[] = {0.0,0.0,0.0,0.0,0.0};
26  double columnUpper[] = {COIN_DBL_MAX,COIN_DBL_MAX,COIN_DBL_MAX,20.0,20.0};
27  double rowLower[] = {20.0,-COIN_DBL_MAX,8.0};
28  double rowUpper[] = {COIN_DBL_MAX,30.0,8.0};
29  // Matrix by row
30  int rowStart[] = {0,5,10,13};
31  int column[] = {0,1,2,3,4,
32                  0,1,2,3,4,
33                  0,1,2};
34  double elementByRow[] = {8.0,5.0,4.0,4.0,-4.0,
35                         8.0,4.0,5.0,5.0,-5.0,
36                         1.0,-1.0,-1.0};
37  // Matrix by column
38  int columnStart[] = {0,3,6,9,11,13};
39  int row[] = {0,1,2,
40               0,1,2,
41               0,1,2,
42               0,1,
43               0,1};
44  double elementByColumn[] = {8.0,8.0,1.0,
45                              5.0,4.0,-1.0,
46                              4.0,5.0,-1.0,
47                              4.0,5.0,
48                              -4.0,-5.0};
49  int numberElements;
50  // Do column version first as it can be done two ways
51  // a) As one step using matrix as stored
52  modelByColumn.loadProblem(numberColumns,numberRows,columnStart,row,elementByColumn,
53                            columnLower,columnUpper,objective,
54                            rowLower,rowUpper);
55  // Solve
56  modelByColumn.dual();
57  // check value of objective
58  assert (fabs(modelByColumn.objectiveValue()-76000.0)<1.0e-7);
59  // b) As two steps - first creating a CoinPackedMatrix
60  // NULL for column lengths indicate they are stored without gaps
61  // Look at CoinPackedMatrix.hpp for other ways to create a matrix
62  numberElements = columnStart[numberColumns];
63  CoinPackedMatrix byColumn(true,numberRows,numberColumns,numberElements,
64                            elementByColumn,row,columnStart,NULL);
65  // now load matrix and rim
66  modelByColumn.loadProblem(byColumn,
67                            columnLower,columnUpper,objective,
68                            rowLower,rowUpper);
69  // Solve
70  modelByColumn.dual();
71  // check value of objective
72  assert (fabs(modelByColumn.objectiveValue()-76000.0)<1.0e-7);
73  // Now do by row
74  // The false says row ordered so numberRows and numberColumns swapped - see CoinPackedMatrix.hpp
75  assert(numberElements == rowStart[numberRows]); // check same number of elements in each copy
76  CoinPackedMatrix byRow(false,numberColumns,numberRows,numberElements,
77                            elementByRow,column,rowStart,NULL);
78  // now load matrix and rim
79  modelByRow.loadProblem(byRow,
80                            columnLower,columnUpper,objective,
81                            rowLower,rowUpper);
82  // Solve
83  modelByRow.dual();
84  // check value of objective
85  assert (fabs(modelByRow.objectiveValue()-76000.0)<1.0e-7);
86  // write solution
87  const double * solution = modelByRow.primalColumnSolution();
88  for (int i=0;i<numberColumns;i++) {
89    if (solution[i])
90      printf("Column %d has value %g\n",
91             i,solution[i]);
92  }
93  modelByRow.writeMps("Tiny.mps");
94  return 0;
95}   
Note: See TracBrowser for help on using the repository browser.