Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6be5c4e20 | ||
|
|
cd6de49b34 | ||
|
|
df12b44d35 | ||
|
|
3cba9318ef | ||
|
|
c301992d25 | ||
|
|
c7edcf8232 | ||
|
|
c73cdc184b | ||
|
|
62334882dc |
@@ -1,18 +1,19 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "tools/matrix.h"
|
#include "tools/matrix.h"
|
||||||
|
#include "tools/math.h"
|
||||||
|
#include "simplex.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int n, m;
|
ColumnVector C(6);
|
||||||
ColumnVector C(n);
|
Matrix A(6, 4);
|
||||||
Matrix A(n, m);
|
int Carr[C.getColumns()] = {-5, -4, 0, 0, 0, 0};
|
||||||
ColumnVector b(n);
|
for (int i = 0; i < C.getRows(); ++i) {
|
||||||
double eps;
|
C[i] = Carr[i];
|
||||||
double eps_default;
|
}
|
||||||
|
A = {{6, 4, 1, 0, 0, 0}, {1, 2, 0, 1, 0, 0}, {-1, 1, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 1}};
|
||||||
std::cin >> C;
|
ColumnVector b(A.getRows());
|
||||||
std::cin >> A;
|
b = {24, 6, 1, 2};
|
||||||
std::cin >> b;
|
std::cout << A << std::endl;
|
||||||
std::cin >> eps;
|
std::cout << C << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+50
-2
@@ -1,6 +1,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "tools/matrix.h"
|
#include "tools/matrix.h"
|
||||||
|
#include "tools/math.h"
|
||||||
|
|
||||||
enum solver_state {
|
enum solver_state {
|
||||||
unbounded,
|
unbounded,
|
||||||
@@ -10,10 +11,55 @@ enum solver_state {
|
|||||||
struct Result {
|
struct Result {
|
||||||
solver_state state;
|
solver_state state;
|
||||||
ColumnVector *solution;
|
ColumnVector *solution;
|
||||||
double objective_fucntion_value;
|
double objective_function_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize) {
|
struct FracturedMatrix {
|
||||||
|
Matrix A;
|
||||||
|
ColumnVector C;
|
||||||
|
ColumnVector b;
|
||||||
|
int pivot_column_index;
|
||||||
|
int pivot_row_index;
|
||||||
|
FracturedMatrix();
|
||||||
|
FracturedMatrix(const FracturedMatrix& other) : A(other.A), C(other.C), b(other.b), pivot_column_index(other.pivot_column_index), pivot_row_index(other.pivot_row_index) {}
|
||||||
|
FracturedMatrix(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) : A(A), C(C), b(b), pivot_column_index(pivot_column_index), pivot_row_index(pivot_row_index) {}
|
||||||
|
FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
|
||||||
|
A = other.A;
|
||||||
|
C = other.C;
|
||||||
|
b = other.b;
|
||||||
|
pivot_column_index = other.pivot_column_index;
|
||||||
|
pivot_row_index = other.pivot_row_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) {
|
||||||
|
|
||||||
|
std::cout << "C: " << C << std::endl;
|
||||||
|
std::cout << "A: " << A << std::endl;
|
||||||
|
std::cout << "b: " << b << std::endl;
|
||||||
|
|
||||||
|
int n = C.getRows();
|
||||||
|
int m = A.getColumns();
|
||||||
|
|
||||||
|
while (b[0] < eps) {
|
||||||
|
//3
|
||||||
|
int pivot_column_index = 0;
|
||||||
|
pivot_column_index = Math::max_index(C);
|
||||||
|
|
||||||
|
//4
|
||||||
|
ColumnVector ratio_vector(m);
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
||||||
|
}
|
||||||
|
int pivot_row_index = Math::min_index(ratio_vector);
|
||||||
|
|
||||||
|
//5
|
||||||
|
struct FracturedMatrix fractured_matrix();
|
||||||
|
fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
Result result;
|
Result result;
|
||||||
std::vector<int> basicVars(A.getColumns() - A.getRows());
|
std::vector<int> basicVars(A.getColumns() - A.getRows());
|
||||||
basicVars[0] = -1;
|
basicVars[0] = -1;
|
||||||
@@ -43,7 +89,9 @@ Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.objective_fucntion_value = b[0];
|
result.objective_fucntion_value = b[0];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef SIMPLEX_H
|
||||||
|
#define SIMPLEX_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "tools/matrix.h"
|
||||||
|
|
||||||
|
enum solver_state {
|
||||||
|
unbounded,
|
||||||
|
bounded
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Result {
|
||||||
|
solver_state state;
|
||||||
|
ColumnVector *solution;
|
||||||
|
double objective_fucntion_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize = true);
|
||||||
|
|
||||||
|
#endif // SIMPLEX_H
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#include "elimination.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
struct FracturedMatrix {
|
||||||
|
Matrix A;
|
||||||
|
ColumnVector C;
|
||||||
|
ColumnVector b;
|
||||||
|
int pivot_column_index;
|
||||||
|
int pivot_row_index;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DestroyMatrix {
|
||||||
|
Matrix A;
|
||||||
|
ColumnVector C;
|
||||||
|
ColumnVector b;
|
||||||
|
};
|
||||||
|
|
||||||
|
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
||||||
|
int rows = generalMatrix.getRows();
|
||||||
|
int cols = generalMatrix.getColumns();
|
||||||
|
|
||||||
|
Matrix A(rows - 1, cols - 1);
|
||||||
|
ColumnVector C(cols - 1);
|
||||||
|
ColumnVector b(rows - 1);
|
||||||
|
|
||||||
|
for (int j = 0; j < cols - 1; ++j) {
|
||||||
|
C[j] = generalMatrix[0][j];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < rows; ++i) {
|
||||||
|
b[i - 1] = generalMatrix[i - 1][cols - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < rows; ++i) {
|
||||||
|
for (int j = 0; j < cols - 1; ++j) {
|
||||||
|
A[i - 1][j] = generalMatrix[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {A, C, b};
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
||||||
|
Matrix generalMatrix(A.getRows() + 1, A.getColumns() + 1);
|
||||||
|
for (int i = 0; i < A.getColumns(); i++) {
|
||||||
|
generalMatrix[0][i] = C[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < A.getRows(); j++) {
|
||||||
|
generalMatrix[j][A.getColumns()] = b[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < A.getRows(); i++) {
|
||||||
|
for (int j = 0; j < A.getColumns(); j++) {
|
||||||
|
generalMatrix[i + 1][j] = A[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return generalMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) {
|
||||||
|
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||||
|
|
||||||
|
int rows = generalMatrix.getRows();
|
||||||
|
int cols = generalMatrix.getColumns();
|
||||||
|
|
||||||
|
double pivotElement = generalMatrix[pivot_row_index][pivot_column_index];
|
||||||
|
|
||||||
|
for (int j = 0; j < cols; ++j) {
|
||||||
|
generalMatrix[pivot_row_index][j] /= pivotElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rows; ++i) {
|
||||||
|
if (i == pivot_row_index)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
double pivotColumnCoefficient = generalMatrix[i][pivot_column_index];
|
||||||
|
|
||||||
|
for (int j = 0; j < cols; ++j) {
|
||||||
|
generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DestroyMatrix destroyedMatrix = destroyGeneralMatrix(generalMatrix);
|
||||||
|
|
||||||
|
pivot_column_index = Math::max_index(destroyedMatrix.C);
|
||||||
|
|
||||||
|
ColumnVector ratio_vector(A.getRows());
|
||||||
|
|
||||||
|
for (int i = 0; i < A.getRows(); i++) {
|
||||||
|
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
||||||
|
}
|
||||||
|
|
||||||
|
int pivot_row_index = Math::min_index(ratio_vector);
|
||||||
|
|
||||||
|
return {A, C, b, pivot_column_index, pivot_row_index};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef ELIMINATION_H
|
||||||
|
#define ELIMINATION_H
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Elimination {
|
||||||
|
public:
|
||||||
|
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ELIMINATION_H
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
#include "math.h"
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
double min(ColumnVector columnVector) {
|
||||||
|
double temp = columnVector[0];
|
||||||
|
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||||
|
if (columnVector[j] < temp) {
|
||||||
|
temp = columnVector[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
double max(ColumnVector columnVector) {
|
||||||
|
double temp = columnVector[0];
|
||||||
|
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||||
|
if (columnVector[j] > temp) {
|
||||||
|
temp = columnVector[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int min_index(ColumnVector columnVector) {
|
||||||
|
int temp_index = 0;
|
||||||
|
double temp = columnVector[0];
|
||||||
|
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||||
|
if (columnVector[j] < temp) {
|
||||||
|
temp_index = j;
|
||||||
|
temp = columnVector[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int max_index(ColumnVector columnVector) {
|
||||||
|
int temp_index = 0;
|
||||||
|
double temp = columnVector[0];
|
||||||
|
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||||
|
if (columnVector[j] > temp) {
|
||||||
|
temp_index = j;
|
||||||
|
temp = columnVector[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
double min(std::vector<double> array) {
|
||||||
|
double temp = array[0];
|
||||||
|
for (size_t i = 1; i < array.size(); i++) {
|
||||||
|
if (array[i] < temp) {
|
||||||
|
temp = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
double max(std::vector<double> array) {
|
||||||
|
double temp = array[0];
|
||||||
|
for (size_t i = 1; i < array.size(); i++) {
|
||||||
|
if (array[i] > temp) {
|
||||||
|
temp = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int min_index(std::vector<double> array) {
|
||||||
|
int temp_index = 0;
|
||||||
|
double temp = array[0];
|
||||||
|
for (size_t i = 1; i < array.size(); i++) {
|
||||||
|
if (array[i] < temp) {
|
||||||
|
temp_index = i;
|
||||||
|
temp = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int max_index(std::vector<double> array) {
|
||||||
|
int temp_index = 0;
|
||||||
|
double temp = array[0];
|
||||||
|
for (size_t i = 1; i < array.size(); i++) {
|
||||||
|
if (array[i] > temp) {
|
||||||
|
temp_index = i;
|
||||||
|
temp = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp_index;
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef MATH_H
|
||||||
|
#define MATH_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
double min(ColumnVector columnVector);
|
||||||
|
double max(ColumnVector columnVector);
|
||||||
|
int min_index(ColumnVector columnVector);
|
||||||
|
int max_index(ColumnVector columnVector);
|
||||||
|
double min(std::vector<double> array);
|
||||||
|
double max(std::vector<double> array);
|
||||||
|
int min_index(std::vector<double> array);
|
||||||
|
int max_index(std::vector<double> array);
|
||||||
|
|
||||||
|
#endif // MATH_H
|
||||||
+1
-1
@@ -77,7 +77,7 @@ std::ostream& operator<<(std::ostream& cout, ColumnVector& vectorObj) {
|
|||||||
Matrix::Matrix(int n, int m) {
|
Matrix::Matrix(int n, int m) {
|
||||||
rows = n;
|
rows = n;
|
||||||
columns = m;
|
columns = m;
|
||||||
matrix.resize(n);
|
matrix.resize(n, ColumnVector(m));
|
||||||
for (auto& row : matrix) {
|
for (auto& row : matrix) {
|
||||||
row = ColumnVector(m);
|
row = ColumnVector(m);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user