source: trunk/Clp/examples/addBits.cpp @ 1559

Last change on this file since 1559 was 1559, checked in by stefan, 10 years ago

merge split branch into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1/* $Id: addBits.cpp 1559 2010-06-05 19:42:36Z stefan $ */
2// Copyright (C) 2005, International Business Machines
3// Corporation and others.  All Rights Reserved.
4
5
6/*
7  This is a simple example to create a model using CoinModel.
8  For even simpler methods see addRows.cpp and addColumns.cpp
9
10  This reads in one model and then creates another one:
11  a) Row bounds
12  b) Column bounds and objective
13  c) Adds elements one by one by row.
14
15  Solve
16
17  It then repeats the exercise:
18  a) Column bounds
19  b) Objective - half the columns as is and half with multiplier of "1.0+multiplier"
20  c) It then adds rows one by one but for half the rows sets their values
21          with multiplier of "1.0+1.5*multiplier" where column affected
22
23  It then loops with multiplier going from 0.0 to 2.0 in increments of 0.1
24
25  (you can have as many different strings as you want)
26*/
27#include "ClpConfig.h"
28#include "ClpSimplex.hpp"
29#include "CoinHelperFunctions.hpp"
30#include "CoinTime.hpp"
31#include "CoinModel.hpp"
32#include <iomanip>
33#include <cassert>
34
35int main(int argc, const char *argv[])
36{
37     // Empty model
38     ClpSimplex  model;
39     std::string mpsFileName;
40     if (argc >= 2) mpsFileName = argv[1];
41     else {
42#if defined(COIN_HAS_NETLIB) && defined(NETLIBDIR)
43          mpsFileName = NETLIBDIR "/25fv47.mps";
44#else
45          fprintf(stderr, "Do not know where to find netlib MPS files.\n");
46          exit(1);
47#endif
48     }
49     int status = model.readMps(mpsFileName.c_str(), true);
50
51     if (status) {
52          fprintf(stderr, "Bad readMps %s\n", mpsFileName.c_str());
53          fprintf(stdout, "Bad readMps %s\n", mpsFileName.c_str());
54          exit(1);
55     }
56     // Point to data
57     int numberRows = model.numberRows();
58     const double * rowLower = model.rowLower();
59     const double * rowUpper = model.rowUpper();
60     int numberColumns = model.numberColumns();
61     const double * columnLower = model.columnLower();
62     const double * columnUpper = model.columnUpper();
63     const double * columnObjective = model.objective();
64     CoinPackedMatrix * matrix = model.matrix();
65     // get row copy
66     CoinPackedMatrix rowCopy = *matrix;
67     rowCopy.reverseOrdering();
68     const int * column = rowCopy.getIndices();
69     const int * rowLength = rowCopy.getVectorLengths();
70     const CoinBigIndex * rowStart = rowCopy.getVectorStarts();
71     const double * element = rowCopy.getElements();
72     //const int * row = matrix->getIndices();
73     //const int * columnLength = matrix->getVectorLengths();
74     //const CoinBigIndex * columnStart = matrix->getVectorStarts();
75     //const double * elementByColumn = matrix->getElements();
76
77     // solve
78     model.dual();
79     // Now build new model
80     CoinModel build;
81     double time1 = CoinCpuTime();
82     // Row bounds
83     int iRow;
84     for (iRow = 0; iRow < numberRows; iRow++) {
85          build.setRowBounds(iRow, rowLower[iRow], rowUpper[iRow]);
86          // optional name
87          build.setRowName(iRow, model.rowName(iRow).c_str());
88     }
89     // Column bounds and objective
90     int iColumn;
91     for (iColumn = 0; iColumn < numberColumns; iColumn++) {
92          build.setColumnLower(iColumn, columnLower[iColumn]);
93          build.setColumnUpper(iColumn, columnUpper[iColumn]);
94          build.setObjective(iColumn, columnObjective[iColumn]);
95          // optional name
96          build.setColumnName(iColumn, model.columnName(iColumn).c_str());
97     }
98     // Adds elements one by one by row (backwards by row)
99     for (iRow = numberRows - 1; iRow >= 0; iRow--) {
100          int start = rowStart[iRow];
101          for (int j = start; j < start + rowLength[iRow]; j++)
102               build(iRow, column[j], element[j]);
103     }
104     double time2 = CoinCpuTime();
105     // Now create clpsimplex
106     ClpSimplex model2;
107     model2.loadProblem(build);
108     double time3 = CoinCpuTime();
109     printf("Time for build using CoinModel is %g (%g for loadproblem)\n", time3 - time1,
110            time3 - time2);
111     model2.dual();
112     // Now do with strings attached
113     // Save build to show how to go over rows
114     CoinModel saveBuild = build;
115     build = CoinModel();
116     time1 = CoinCpuTime();
117     // Column bounds
118     for (iColumn = 0; iColumn < numberColumns; iColumn++) {
119          build.setColumnLower(iColumn, columnLower[iColumn]);
120          build.setColumnUpper(iColumn, columnUpper[iColumn]);
121     }
122     // Objective - half the columns as is and half with multiplier of "1.0+multiplier"
123     // Pick up from saveBuild (for no reason at all)
124     for (iColumn = 0; iColumn < numberColumns; iColumn++) {
125          double value = saveBuild.objective(iColumn);
126          if (iColumn * 2 < numberColumns) {
127               build.setObjective(iColumn, columnObjective[iColumn]);
128          } else {
129               // create as string
130               char temp[100];
131               sprintf(temp, "%g + abs(%g*multiplier)", value, value);
132               build.setObjective(iColumn, temp);
133          }
134     }
135     // It then adds rows one by one but for half the rows sets their values
136     //      with multiplier of "1.0+1.5*multiplier"
137     for (iRow = 0; iRow < numberRows; iRow++) {
138          if (iRow * 2 < numberRows) {
139               // add row in simple way
140               int start = rowStart[iRow];
141               build.addRow(rowLength[iRow], column + start, element + start,
142                            rowLower[iRow], rowUpper[iRow]);
143          } else {
144               // As we have to add one by one let's get from saveBuild
145               CoinModelLink triple = saveBuild.firstInRow(iRow);
146               while (triple.column() >= 0) {
147                    int iColumn = triple.column();
148                    if (iColumn * 2 < numberColumns) {
149                         // just value as normal
150                         build(iRow, triple.column(), triple.value());
151                    } else {
152                         // create as string
153                         char temp[100];
154                         sprintf(temp, "%g + (1.5*%g*multiplier)", triple.value(), triple.value());
155                         build(iRow, iColumn, temp);
156                    }
157                    triple = saveBuild.next(triple);
158               }
159               // but remember to do rhs
160               build.setRowLower(iRow, rowLower[iRow]);
161               build.setRowUpper(iRow, rowUpper[iRow]);
162          }
163     }
164     time2 = CoinCpuTime();
165     // Now create ClpSimplex
166     // If small switch on error printing
167     if (numberColumns < 50)
168          build.setLogLevel(1);
169     int numberErrors = model2.loadProblem(build);
170     // should fail as we never set multiplier
171     assert(numberErrors);
172     time3 = CoinCpuTime() - time2;
173     // subtract out unsuccessful times
174     time1 += time3;
175     time2 += time3;
176     build.associateElement("multiplier", 0.0);
177     numberErrors = model2.loadProblem(build);
178     assert(!numberErrors);
179     time3 = CoinCpuTime();
180     printf("Time for build using CoinModel is %g (%g for successful loadproblem)\n", time3 - time1,
181            time3 - time2);
182     build.writeMps("zero.mps");
183     // It then loops with multiplier going from 0.0 to 2.0 in increments of 0.1
184     for (double multiplier = 0.0; multiplier < 2.0; multiplier += 0.1) {
185          build.associateElement("multiplier", multiplier);
186          numberErrors = model2.loadProblem(build, true);
187          assert(!numberErrors);
188          model2.dual();
189     }
190
191     return 0;
192}
Note: See TracBrowser for help on using the repository browser.