-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSimplex.cpp
More file actions
146 lines (121 loc) · 3.91 KB
/
MSimplex.cpp
File metadata and controls
146 lines (121 loc) · 3.91 KB
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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
int main() {
int nRestriccion, nVariable;
string tipoP;
system("cls");
cout << "Ingrese 'MAX' o 'MIN': ";
cin >> tipoP;
cout << "Numero de restricciones: ";
cin >> nRestriccion;
cout << "Numero de variables: ";
cin >> nVariable;
int filas = nRestriccion + 1;
int columnas = nVariable + nRestriccion + 1;
vector<vector<float>> tabla(filas, vector<float>(columnas, 0));
vector<string> nomVariables(nVariable + nRestriccion);
vector<string> base(nRestriccion);
for (int j = 0; j < nVariable; j++) {
nomVariables[j] = "X" + to_string(j + 1);
}
for (int j = 0; j < nRestriccion; j++) {
nomVariables[nVariable + j] = "S" + to_string(j + 1);
}
for (int i = 0; i < nRestriccion; i++) {
cout << "\nRestriccion " << i + 1 << ":\n";
for (int j = 0; j < nVariable; j++) {
cout << "Coeficiente de X" << j + 1 << ": ";
cin >> tabla[i][j];
}
string signo;
cout << "Signo: ";
cin >> signo;
if(signo == "<="){
tabla[i][nVariable + i] = 1;
}else if(signo == ">="){
tabla[i][nVariable + i] = -1;
}
cout << "RHS : ";
cin >> tabla[i][columnas - 1];
base[i] = nomVariables[nVariable + i];
}
cout << "\nFuncion Objetivo:\n";
for (int j = 0; j < nVariable; j++) {
float coef;
cout << "Coeficiente de X" << j + 1 << ": ";
cin >> coef;
tabla[filas - 1][j] = (tipoP == "MIN" || tipoP == "min") ? coef : -coef;
}
int iter = 0;
while (true) {
cout << "\n--- Iteracion " << iter++ << " ---\n";
for (int i = 0; i < filas; i++) {
if (i < nRestriccion)
cout << setw(5) << base[i] << " | ";
else
cout << " Z | ";
for (int j = 0; j < columnas; j++) {
cout << setw(8) << fixed << setprecision(2) << tabla[i][j];
}
cout << "\n";
}
int colPivote = -1;
float minVal = 0;
for (int j = 0; j < columnas - 1; j++) {
if (tabla[filas - 1][j] < minVal) {
minVal = tabla[filas - 1][j];
colPivote = j;
}
}
if (colPivote == -1) break;
int filaPivote = -1;
float minRatio = 1e9;
for (int i = 0; i < filas - 1; i++) {
float val = tabla[i][colPivote];
if (val > 0) {
float ratio = tabla[i][columnas - 1] / val;
if (ratio < minRatio) {
minRatio = ratio;
filaPivote = i;
}
}
}
if (filaPivote == -1) {
cout << "Solución no acotada.\n";
return 0;
}
float pivote = tabla[filaPivote][colPivote];
for (int j = 0; j < columnas; j++) {
tabla[filaPivote][j] /= pivote;
}
for (int i = 0; i < filas; i++) {
if (i != filaPivote) {
float factor = tabla[i][colPivote];
for (int j = 0; j < columnas; j++) {
tabla[i][j] -= factor * tabla[filaPivote][j];
}
}
}
base[filaPivote] = nomVariables[colPivote];
}
cout << "\n--- SOLUCION FINAL ---\n";
vector<float> valores(nVariable, 0.0);
for (int i = 0; i < nRestriccion; i++) {
for (int j = 0; j < nVariable; j++) {
if (base[i] == nomVariables[j]) {
valores[j] = tabla[i][columnas - 1];
}
}
}
for (int j = 0; j < nVariable; j++) {
cout << "X" << j + 1 << " = " << valores[j] << "\n";
}
float Z = tabla[filas - 1][columnas - 1];
if (tipoP == "MIN" || tipoP == "min")
Z = -Z;
cout << "Z = " << Z << "\n";
return 0;
}