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

Last change on this file since 1370 was 1370, checked in by forrest, 10 years ago

add ids

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