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