Merge pull request #1 from emil28092005/detached

make x column
This commit is contained in:
emil28092005
2024-09-20 23:18:14 +03:00
committed by GitHub
4 changed files with 235 additions and 135 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
# Prerequisites # Prerequisites
*.d *.d
CMakeLists.txt
.idea
# Compiled Object files # Compiled Object files
*.slo *.slo
*.lo *.lo
+13 -1
View File
@@ -1,6 +1,18 @@
#include <iostream> #include <iostream>
#include "tools/matrix.h"
int main() { int main() {
std::cout << "Hello World!\n"; int n, m;
ColumnVector C(n);
Matrix A(n, m);
ColumnVector b(n);
double eps;
double eps_default;
std::cin >> C;
std::cin >> A;
std::cin >> b;
std::cin >> eps;
return 0; return 0;
} }
+85 -1
View File
@@ -1 +1,85 @@
// implementation of simplex task #include <algorithm>
#include <iostream>
#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) {
Result result;
std::vector<int> basicVars(A.getColumns() - A.getRows());
basicVars[0] = -1;
for (int i = 1; i < basicVars.size(); i++) {
basicVars[i] = static_cast<int>(basicVars.size()) + i;
}
int kc = 0;
double temp = A[0][0];
for (int j = 0; j< A.getColumns(); j++) {
if (A[0][j] < temp) {
temp = A[0][j];
kc = j;
}
}
if (A[0][kc] >= 0) {
result.state = unbounded;
result.solution = new ColumnVector(C.getRows());
for (int i = 0; i < C.getRows(); i++) {
result.solution->operator[](i) = 0;
}
for (int i = 1; i < basicVars.size(); i++) {
if (basicVars[i] <= C.getRows()) {
(*result.solution)[basicVars[i]] = b.getRows() - 1;
}
}
result.objective_fucntion_value = b[0];
}
}
/*
Function_name(C, A, b, eps = eps_default)
Input:
- C: A vector of coefficients of the objective function
- A: A matrix of coefficients of the constraint functions
- b: A vector of right-hand side values
- eps: Approximation accuracy (optional, default = eps_default)
Steps:
1. Print the optimization problem:
- max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn
- subject to the constraints:
- A[0] * x <= b[0]
- A[1] * x <= b[1]
- ...
- A[m] * x <= b[m]
2. Initialize:
- Form the initial tableau by introducing slack variables to convert inequalities into equalities.
3. Iteratively apply the Simplex method:
- Step 1: Identify the entering variable (most negative coefficient in the objective row).
- Step 2: Identify the leaving variable (smallest positive ratio of RHS to pivot column).
- Step 3: Perform pivot operations to update the tableau.
4. Check for optimality or unboundedness:
- If all coefficients in the objective function row are non-negative, the solution is optimal.
- If no leaving variable exists, the problem is unbounded.
5. Return:
- solver_state: {solved, unbounded}
- x*: Optimal vector of decision variables (if solved)
- z: Maximum (or minimum) value of the objective function (if solved)
End Function
*/
+135 -132
View File
@@ -1,210 +1,213 @@
#include "matrix.h" #include "matrix.h"
ColumnVector::ColumnVector(int n) { ColumnVector::ColumnVector(int n) {
rows = n; rows = n;
columns = 1; columns = 1;
columnVector.resize(n); columnVector.resize(n);
} }
ColumnVector::ColumnVector(const ColumnVector& other) { ColumnVector::ColumnVector(const ColumnVector& other) {
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
columnVector = other.columnVector; columnVector = other.columnVector;
} }
int ColumnVector::getRows() const { int ColumnVector::getRows() const {
return rows; return rows;
} }
int ColumnVector::getColumns() const { int ColumnVector::getColumns() const {
return columns; return columns;
} }
double& ColumnVector::operator[](int row) { double& ColumnVector::operator[](int row) {
return columnVector[row]; return columnVector[row];
} }
ColumnVector& ColumnVector::operator=(const ColumnVector& other) { ColumnVector& ColumnVector::operator=(const ColumnVector& other) {
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
columnVector = other.columnVector; columnVector = other.columnVector;
return *this; return *this;
} }
ColumnVector ColumnVector::operator+(ColumnVector& other) { ColumnVector ColumnVector::operator+(ColumnVector& other) {
if (rows != other.rows) { if (rows != other.rows) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
ColumnVector result(rows); ColumnVector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
result[i] = columnVector[i] + other[i]; result[i] = columnVector[i] + other[i];
} }
return result; return result;
} }
ColumnVector ColumnVector::operator-(ColumnVector& other) { ColumnVector ColumnVector::operator-(ColumnVector& other) {
if (rows != other.rows) { if (rows != other.rows) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
ColumnVector result(rows); ColumnVector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
result[i] = columnVector[i] - other[i]; result[i] = columnVector[i] - other[i];
} }
return result; return result;
} }
std::istream& operator>>(std::istream& cin, ColumnVector& vectorObj) { std::istream& operator>>(std::istream& cin, ColumnVector& vectorObj) {
for (int i = 0; i < vectorObj.rows; ++i) { for (int i = 0; i < vectorObj.rows; ++i) {
cin >> vectorObj[i]; cin >> vectorObj[i];
} }
return cin; return cin;
} }
std::ostream& operator<<(std::ostream& cout, ColumnVector& vectorObj) { std::ostream& operator<<(std::ostream& cout, ColumnVector& vectorObj) {
for (int i = 0; i < vectorObj.rows; ++i) { for (int i = 0; i < vectorObj.rows; ++i) {
if (i == vectorObj.rows - 1) { if (i == vectorObj.rows - 1) {
cout << vectorObj[i] << std::endl; cout << vectorObj[i] << std::endl;
} }
else { else {
cout << vectorObj[i] << ' '; cout << vectorObj[i] << ' ';
} }
} }
return cout; return cout;
} }
Matrix::Matrix(int n, int m) { Matrix::Matrix(int n, int m) {
rows = n; rows = n;
columns = m; columns = m;
matrix.resize(n, ColumnVector(m)); matrix.resize(n);
for (auto& row : matrix) {
row = ColumnVector(m);
}
} }
Matrix::Matrix(const Matrix& other) { Matrix::Matrix(const Matrix& other) {
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
matrix = other.matrix; matrix = other.matrix;
} }
int Matrix::getRows() const { int Matrix::getRows() const {
return rows; return rows;
} }
int Matrix::getColumns() const { int Matrix::getColumns() const {
return columns; return columns;
} }
ColumnVector& Matrix::operator[](int row) { ColumnVector& Matrix::operator[](int row) {
return matrix[row]; return matrix[row];
} }
Matrix& Matrix::operator=(const Matrix& other) { Matrix& Matrix::operator=(const Matrix& other) {
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
matrix = other.matrix; matrix = other.matrix;
return *this; return *this;
} }
Matrix Matrix::operator+(Matrix& other) const { Matrix Matrix::operator+(Matrix& other) const {
if (rows != other.rows || columns != other.columns) { if (rows != other.rows || columns != other.columns) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, columns); Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) { for (int j = 0; j < columns; ++j) {
auto x = matrix[i]; auto x = matrix[i];
auto y = other[i]; auto y = other[i];
result[i][j] = x[j] + y[j]; result[i][j] = x[j] + y[j];
} }
} }
return result; return result;
} }
Matrix Matrix::operator-(Matrix& other) const { Matrix Matrix::operator-(Matrix& other) const {
if (rows != other.rows || columns != other.columns) { if (rows != other.rows || columns != other.columns) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, columns); Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) { for (int j = 0; j < columns; ++j) {
auto x = matrix[i]; auto x = matrix[i];
auto y = other[i]; auto y = other[i];
result[i][j] = x[j] - y[j]; result[i][j] = x[j] - y[j];
} }
} }
return result; return result;
} }
Matrix Matrix::operator*(Matrix& other) const { Matrix Matrix::operator*(Matrix& other) const {
if (columns != other.rows) { if (columns != other.rows) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, other.columns); Matrix result(rows, other.columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.columns; ++j) { for (int j = 0; j < other.columns; ++j) {
result[i][j] = 0; result[i][j] = 0;
for (int k = 0; k < columns; ++k) { for (int k = 0; k < columns; ++k) {
auto x = matrix[i]; auto x = matrix[i];
auto y = other[k]; auto y = other[k];
result[i][j] += x[k] * y[j]; result[i][j] += x[k] * y[j];
} }
} }
} }
return result; return result;
} }
ColumnVector Matrix::operator*(ColumnVector other) const { ColumnVector Matrix::operator*(ColumnVector other) const {
if (columns != other.getRows()) { if (columns != other.getRows()) {
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
ColumnVector result(rows); ColumnVector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
result[i] = 0; result[i] = 0;
for (int k = 0; k < columns; ++k) { for (int k = 0; k < columns; ++k) {
auto x = matrix[i]; auto x = matrix[i];
result[i] += x[k] * other[k]; result[i] += x[k] * other[k];
} }
} }
return result; return result;
} }
Matrix Matrix::transpose() const { Matrix Matrix::transpose() const {
Matrix result(columns, rows); Matrix result(columns, rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) { for (int j = 0; j < columns; ++j) {
auto x = matrix[i]; auto x = matrix[i];
result[j][i] = x[j]; result[j][i] = x[j];
} }
} }
return result; return result;
} }
std::istream& operator>>(std::istream& cin, Matrix& matrixObj) { std::istream& operator>>(std::istream& cin, Matrix& matrixObj) {
for (int i = 0; i < matrixObj.rows; ++i) { for (int i = 0; i < matrixObj.rows; ++i) {
for (int j = 0; j < matrixObj.columns; ++j) { for (int j = 0; j < matrixObj.columns; ++j) {
cin >> matrixObj[i][j]; cin >> matrixObj[i][j];
} }
} }
return cin; return cin;
} }
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) { std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) {
for (int i = 0; i < matrixObj.rows; ++i) { for (int i = 0; i < matrixObj.rows; ++i) {
for (int j = 0; j < matrixObj.columns; ++j) { for (int j = 0; j < matrixObj.columns; ++j) {
if (j == matrixObj.columns - 1) { if (j == matrixObj.columns - 1) {
cout << matrixObj[i][j] << std::endl; cout << matrixObj[i][j] << std::endl;
} }
else { else {
cout << matrixObj[i][j] << ' '; cout << matrixObj[i][j] << ' ';
} }
} }
} }
return cout; return cout;
} }