-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
364 lines (302 loc) · 12.4 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <float.h>
#include <cstdlib>
#include <list>
#include "main.h"
using namespace std;
void trivial(vector<vector<double>> coefficients, vector<double> upper_bound, vector<double>&x) {
//find the smallest upperbound adn the largest lower bound. if lower>upper then infeasible. otherwise, pick x in between upper and lower.
// Initialize min_upper_bound to positive infinity
double min_upper_bound = numeric_limits<double>::infinity();
// Initialize max_lower_bound to negative infinity
double max_lower_bound = -numeric_limits<double>::infinity();
bool hasLowerBound = false;
bool hasUpperBound = false;
// Find the smallest upper bound and the largest lower bound starting from index 1
for (size_t i = 0; i < coefficients[0].size(); i++) {
if (coefficients[0][i] < 0) {
double lower_bound = upper_bound[i] / (coefficients[0][i]);
hasLowerBound = true;
if (lower_bound > max_lower_bound) {
max_lower_bound = lower_bound;
}
} else if (coefficients[0][i] == 0) {
// Handle the case where the coefficient is zero (no dependence on x)
if (upper_bound[i] < 0) {
cout << "The system is infeasible." << endl;
exit(1);
}
}
else {
double upper_bound_value = upper_bound[i];
if (!isfinite(upper_bound_value)) {
upper_bound_value = max_lower_bound;
} else {
hasUpperBound = true;
}
if (upper_bound_value < min_upper_bound) {
min_upper_bound = upper_bound_value;
}
}
}
if (hasLowerBound && hasUpperBound && max_lower_bound > min_upper_bound) {
cout << "The system is infeasible." << endl;
exit(1);
}
else {
if (!hasLowerBound) {
max_lower_bound = min_upper_bound;
} else if (!hasUpperBound) {
min_upper_bound = max_lower_bound;
}
cout << "The minimum upper bound is: " << min_upper_bound << " and the maximum lower bound is: " << max_lower_bound << endl;
double x_value = (max_lower_bound + min_upper_bound) / 2;
x.emplace_back(x_value);
}
}
void notTrivial(vector<double>& x, list<vector<vector<double>>>& system, list<vector<double>>& allBounds) {
/*
Multiply the respective columns by the x value for each then subtract the upper_bound by x.
The order of x is the same order as coefficients. i.e., the first element in x is x1, which will multiply with the first column in coefficients (matrix multiplication).
Then, call trivial() with the last column of coefficient. trivial will push x_n to the end of x.
*/
int size = system.size();
for (int i = 0; i < size; i++) {
vector<vector<double>>& currentCoeff = system.front();
vector<double>& currentBounds = allBounds.front();
cout << "New system is:" << endl;
vector<double> oldBounds=currentBounds;
for (size_t i = 0; i < currentCoeff.size(); i++) {
for (size_t j = 0; j < currentCoeff[i].size(); j++) {
if(j!=currentCoeff[i].size()-1){
cout << currentCoeff[i][j] <<"("<< x[j]<<")"<<"+" << ' ';
}
else{
cout << currentCoeff[i][j] <<"x"<<j+1 << ' ';
}
}
cout << "<= " << oldBounds[i] << endl;
}
cout<<endl;
// Multiply the columns of the coefficient matrix by the values in x then subtract the constant A(n)x(n) from b(n)
for (size_t j = 0; j < currentCoeff.size(); j++) {
for (size_t k = 0; k < currentCoeff[j].size() - 1; k++) {
currentCoeff[j][k] *= x[k];
currentBounds[j] -= currentCoeff[j][k];
}
}
cout << "New updated system is:" << endl;
for (size_t i = 0; i < currentCoeff.size(); i++) {
for (size_t j = 0; j < currentCoeff[i].size(); j++) {
if(j!=currentCoeff[i].size()-1){
}
else{
cout << currentCoeff[i][j] <<"x"<<j+1 << ' ';
}
}
cout << "<= " << currentBounds[i] << endl;
}
cout<<endl;
// Call the trivial function with the last column of coefficients
vector<vector<double>> lastColumn(1, vector<double>(currentCoeff.size()));
for (size_t j = 0; j < currentCoeff.size(); j++) {
lastColumn[0][j] = currentCoeff[j].back();
}
trivial(lastColumn, currentBounds, x);
// Remove the processed system and bounds from the lists
system.pop_front();
allBounds.pop_front();
}
}
void newSystem(vector<vector<double>>& coefficients, vector<double>& upper_bound, list<vector<vector<double>>>& system, list<vector<double>>& allBounds) {
vector<vector<double>> newCoefficients;
vector<double> newUpperBound;
vector<vector<double>> positiveCoeffs; // Store positive coefficients (or 0)
vector<double> positiveBounds; // Store corresponding bounds
vector<vector<double>> negativeCoeffs; // Store negative coefficients (or 0)
vector<double> negativeBounds; // Store corresponding bounds
for (int i = 0; i < coefficients.size(); i++) {
if (coefficients[i][coefficients[0].size() - 1] == 0) { // J0
vector<double> newRow(coefficients[i].begin(), coefficients[i].end() - 1); // Remove the last column
newCoefficients.emplace_back(newRow);
newUpperBound.emplace_back(upper_bound[i]);
} else if (coefficients[i][coefficients[0].size() - 1] < 0) { // J-
negativeCoeffs.emplace_back(coefficients[i]);
negativeBounds.emplace_back(upper_bound[i]);
} else { // J+
positiveCoeffs.emplace_back(coefficients[i]);
positiveBounds.emplace_back(upper_bound[i]);
}
}
// Check if either J+ or J- is empty
if (negativeCoeffs.empty()) {
// Add positive coefficients to cancel the last column
vector<double> newRow(coefficients[0].size() - 1, 0.0);
for (const auto& row : positiveCoeffs) {
for (int k = 0; k < row.size() - 1; k++) {
newRow[k] += row[k];
}
}
newCoefficients.emplace_back(newRow);
// Sum the upper bounds from J+
double upperSum = 0.0;
for (size_t i = 0; i < positiveCoeffs.size(); i++) {
upperSum += positiveBounds[i];
}
newUpperBound.emplace_back(upperSum);
}
else if (positiveCoeffs.empty()) {
// Subtract negative coefficients to cancel the last column
vector<double> newRow(coefficients[0].size() - 1, 0.0);
for (const auto& row : negativeCoeffs) {
for (int k = 0; k < row.size() - 1; k++) {
newRow[k] -= row[k];
}
}
newCoefficients.emplace_back(newRow);
// Sum the upper bounds from J-
double upperSum = 0.0;
for (size_t i = 0; i < negativeCoeffs.size(); i++) {
upperSum -= negativeBounds[i]; // Subtract the upper bounds
}
newUpperBound.emplace_back(upperSum);
}
else {
// Both J+ and J- are non-empty, create new systems by adding each combination
for (size_t i = 0; i < positiveCoeffs.size(); i++) {
for (size_t j = 0; j < negativeCoeffs.size(); j++) {
vector<double> newRow(coefficients[0].size() - 1, 0.0);
for (int k = 0; k < newRow.size(); k++) {
newRow[k] = positiveCoeffs[i][k] + negativeCoeffs[j][k];
}
newCoefficients.emplace_back(newRow);
// Calculate the upper bound as the sum of individual upper bounds
double upperSum = positiveBounds[i] + negativeBounds[j];
newUpperBound.emplace_back(upperSum);
}
}
}
// Print the coefficients and corresponding upper bounds before simplification
cout << "New system is:" << endl;
for (size_t i = 0; i < newCoefficients.size(); i++) {
for (size_t j = 0; j < newCoefficients[i].size(); j++) {
if(j!=newCoefficients[i].size()-1){
cout << newCoefficients[i][j] <<"x"<<j+1 <<" +" << ' ';
}
else{
cout << newCoefficients[i][j] <<"x"<<j+1 << ' ';
}
}
cout << "<= " << newUpperBound[i] << endl;
}
cout<<endl;
// Simplify the new coefficients and bounds
simplifyCoefficients(newCoefficients, newUpperBound, system, allBounds);
}
void simplifyCoefficients(vector<vector<double>>& newCoefficients, vector<double>& newUpperBound, list<vector<vector<double>>>& system, list<vector<double>>& allBounds) {
for (size_t i = 0; i < newCoefficients.size(); ++i) {
if (newCoefficients[i].empty()) {
continue;
}
double lastCoefficient = newCoefficients[i].back();
double divisor = abs(lastCoefficient);
if (divisor != 0) {
for (size_t j = 0; j < newCoefficients[i].size(); ++j) {
newCoefficients[i][j] /= divisor;
}
newUpperBound[i] /= divisor;
}
}
// Print the simplified coefficients
cout<<"This is simplified to: "<<endl;
for (size_t i = 0; i < newCoefficients.size(); i++) {
for (size_t j = 0; j < newCoefficients[i].size(); j++) {
if(j!=newCoefficients[i].size()-1){
cout << newCoefficients[i][j] <<"x"<<j+1 <<" +" << ' ';
}
else{
cout << newCoefficients[i][j] <<"x"<<j+1 << ' ';
}
}
cout << "<= " << newUpperBound[i] << endl;
}
cout<<endl;
// Push the simplified coefficients to the system
system.push_front(newCoefficients);
allBounds.push_front(newUpperBound);
if (newCoefficients[0].size() == 1) {
return; // Exit the function when the trivial case is reached
}
else{
newSystem(system.front(),allBounds.front(),system,allBounds);
}
}
int main(int argc, char *argv[]) {
// if (argc != 2) {
// cerr << "Usage: " << argv[0] << " <input_file>" << endl;
// return 1;
// }
ifstream inputFile;
inputFile.open("test2.txt");
if (!inputFile) {
cerr << "Error opening the file." << endl;
return 1;
}
int numRows, numCols;
inputFile >> numRows >> numCols;
vector<vector<double>> coefficients(numRows, vector<double>(numCols));
list<vector<vector<double>>> system;
vector<double> upper_bound(numRows);
list<vector<double>> allBounds;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (!(inputFile >> coefficients[i][j])) {
cerr << "Error reading double values from the file." << endl;
return 1;
}
}
}
for(int i=0; i < numRows; i++){
if (!(inputFile >> upper_bound[i])) {
cerr << "Error reading double values from the file." << endl;
return 1;
}
}
inputFile.close();
// system.push_back(coefficients);
// allBounds.push_back(upper_bound);
for (size_t i = 0; i < coefficients.size(); i++) {
for (size_t j = 0; j < coefficients[i].size(); j++) {
if(j!=coefficients[i].size()-1){
cout << coefficients[i][j] <<"x"<<j+1 <<" +" << ' ';
}
else{
cout << coefficients[i][j] <<"x"<<j+1 << ' ';
}
}
cout << "<= " << upper_bound[i] << endl;
}
cout<<endl;
vector<double> x;
simplifyCoefficients(coefficients,upper_bound,system,allBounds);
vector<vector<double>> lastColumn(1, vector<double>(system.front().size()));
for (size_t j = 0; j < system.front().size(); j++) {
lastColumn[0][j] = system.front()[j].back();
}
trivial(lastColumn,allBounds.front(),x);
system.pop_front();
allBounds.pop_front();
if(!system.empty()){
notTrivial(x, system, allBounds);
}
for(int i=0; i<x.size(); i++){
cout<< "x"<<i+1<<" = " << x[i] << " ";
}
return 0;
}