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

Last change on this file since 1552 was 1552, checked in by mjs, 11 years ago

Format examples with 'astyle -A4 -p'.

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