source: trunk/Clp/examples/addBits.cpp

Last change on this file was 1935, checked in by stefan, 6 years ago

fix compiler warnings; fix path to sample instance in minimum

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