Compare commits
2
Commits
2749b9dc0b
...
153bcfd017
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
153bcfd017 | ||
|
|
0ed96c9238 |
@@ -0,0 +1,11 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.29)
|
||||||
|
project(SimplexTASK)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
add_executable(SimplexTASK main.cpp
|
||||||
|
simplex.cpp
|
||||||
|
tools/matrix.cpp
|
||||||
|
tools/math.cpp
|
||||||
|
tools/elimination.cpp
|
||||||
|
)
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
GXX := g++ -std=c++20
|
||||||
|
flags := -Wall -fsanitize=address
|
||||||
|
TOOLS := tools
|
||||||
|
BUILD := build
|
||||||
|
|
||||||
|
build:
|
||||||
|
mkdir $(BUILD)
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
|
|
||||||
|
$(GXX) $(BUILD)/*.obj $(flags) -o simplex.out
|
||||||
|
|
||||||
|
$(BUILD)/elimination.obj: $(TOOLS)/elimination.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
|
||||||
|
$(BUILD)/math.obj: $(TOOLS)/math.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
|
||||||
|
$(BUILD)/matrix.obj: $(TOOLS)/matrix.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
|
||||||
|
$(BUILD)/simplex.obj: simplex.cpp
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
|
||||||
|
$(BUILD)/main.obj: main.cpp
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
|
|
||||||
|
$(BUILD)/simplex.out: $(BUILD)/elimination.obj $(BUILD)/math.obj $(BUILD)/matrix.obj $(BUILD)/simplex.obj $(BUILD)/main.obj
|
||||||
|
$(GXX) $(BUILD)/*.obj $(flags) -o simplex.out
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD)
|
||||||
@@ -3,10 +3,27 @@
|
|||||||
#include "tools/math.h"
|
#include "tools/math.h"
|
||||||
#include "simplex.h"
|
#include "simplex.h"
|
||||||
|
|
||||||
|
void manualInput() {
|
||||||
|
int ZLength;
|
||||||
|
std::cout << "Write how many x's the objective function has:" << std::endl;
|
||||||
|
std::cin >> ZLength;
|
||||||
|
Vector Z = {};
|
||||||
|
}
|
||||||
|
void printInitialInputs(Vector C, Matrix A, Vector b) {
|
||||||
|
|
||||||
|
}
|
||||||
int main() {
|
int main() {
|
||||||
// TODO: Initially should be positive
|
// TODO: Initially should be positive
|
||||||
|
std::string doManual = "";
|
||||||
|
std::cout << "Enable manual input? (y/n)" << std::endl;
|
||||||
|
std::cin >> doManual ;
|
||||||
|
if (doManual == "y" or doManual == "Y") {
|
||||||
|
manualInput();
|
||||||
|
} else if (doManual == "n"){
|
||||||
|
|
||||||
Vector C = {-5, -4, 0, 0, 0, 0};
|
}
|
||||||
|
|
||||||
|
Vector C = {5, 4, 0, 0, 0, 0};
|
||||||
|
|
||||||
Matrix A = {
|
Matrix A = {
|
||||||
{6, 4, 1, 0, 0, 0},
|
{6, 4, 1, 0, 0, 0},
|
||||||
@@ -22,7 +39,7 @@ int main() {
|
|||||||
//showMatrix(test);
|
//showMatrix(test);
|
||||||
|
|
||||||
|
|
||||||
Result result = Simplex(C, A, b);
|
Result result = Simplex(C, A, b, 0.1, true);
|
||||||
if(result.state == bounded) {
|
if(result.state == bounded) {
|
||||||
|
|
||||||
if(result.solution == nullptr) {
|
if(result.solution == nullptr) {
|
||||||
@@ -37,3 +54,5 @@ int main() {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+77
-4
@@ -18,7 +18,7 @@ struct Result {
|
|||||||
|
|
||||||
|
|
||||||
Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=true) {
|
Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=true) {
|
||||||
if (maximize == false) {
|
if (maximize == true) {
|
||||||
for (int i = 0; i < C.size(); i++) {
|
for (int i = 0; i < C.size(); i++) {
|
||||||
C[i] = -C[i];
|
C[i] = -C[i];
|
||||||
}
|
}
|
||||||
@@ -33,14 +33,19 @@ Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=tr
|
|||||||
for (size_t i = 1; i < basicVars.size(); i++) {
|
for (size_t i = 1; i < basicVars.size(); i++) {
|
||||||
basicVars[i] = static_cast<int>(basicVars.size()) + i;
|
basicVars[i] = static_cast<int>(basicVars.size()) + i;
|
||||||
}
|
}
|
||||||
|
int iterationCount = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
//3
|
//3
|
||||||
|
iterationCount++;
|
||||||
|
std::cout << "Iteration: ";
|
||||||
|
std::cout << iterationCount << std::endl;
|
||||||
int pivot_column_index = 0;
|
int pivot_column_index = 0;
|
||||||
|
if (maximize) {
|
||||||
|
|
||||||
pivot_column_index = min_index(generalMatrix[0]);
|
pivot_column_index = min_index(generalMatrix[0]);
|
||||||
|
|
||||||
if (generalMatrix[0][pivot_column_index] >= 0) {
|
if (generalMatrix[0][pivot_column_index] >= 0) {
|
||||||
DestroyMatrix destroyedGeneralMatrix = destroyGeneralMatrix(generalMatrix);
|
DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix);
|
||||||
Matrix _A = destroyedGeneralMatrix.A;
|
Matrix _A = destroyedGeneralMatrix.A;
|
||||||
Vector _C = destroyedGeneralMatrix.C;
|
Vector _C = destroyedGeneralMatrix.C;
|
||||||
Vector _b = destroyedGeneralMatrix.b;
|
Vector _b = destroyedGeneralMatrix.b;
|
||||||
@@ -55,9 +60,35 @@ Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=tr
|
|||||||
result.solution->operator[](basicVars[i]) = _b[i];
|
result.solution->operator[](basicVars[i]) = _b[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.objective_function_value = b[0];
|
result.objective_function_value = _C[_C.size()];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maximize == false) {
|
||||||
|
pivot_column_index = max_index(generalMatrix[0]);
|
||||||
|
|
||||||
|
if (generalMatrix[0][pivot_column_index] < 0) {
|
||||||
|
DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix);
|
||||||
|
Matrix _A = destroyedGeneralMatrix.A;
|
||||||
|
Vector _C = destroyedGeneralMatrix.C;
|
||||||
|
Vector _b = destroyedGeneralMatrix.b;
|
||||||
|
|
||||||
|
result.state = bounded;
|
||||||
|
result.solution = new Vector(C.size());
|
||||||
|
for (int i = 0; i < C.size(); i++) {
|
||||||
|
result.solution->operator[](i) = 0;
|
||||||
|
}
|
||||||
|
for (size_t i = 1; i < basicVars.size(); i++) {
|
||||||
|
if (basicVars[i] <= C.size()) {
|
||||||
|
result.solution->operator[](basicVars[i]) = _b[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.objective_function_value = _C[_C.size()];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//4
|
//4
|
||||||
Vector ratio_vector(generalMatrix.getRows());
|
Vector ratio_vector(generalMatrix.getRows());
|
||||||
@@ -78,6 +109,48 @@ Result Simplex(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=tr
|
|||||||
elimination(generalMatrix, pivot_row_index, pivot_column_index);
|
elimination(generalMatrix, pivot_row_index, pivot_column_index);
|
||||||
std::cout << "After:" << std::endl;
|
std::cout << "After:" << std::endl;
|
||||||
showMatrix(generalMatrix);
|
showMatrix(generalMatrix);
|
||||||
|
|
||||||
|
if (maximize) {
|
||||||
|
bool thereIsNegative = false;
|
||||||
|
for (int j = 0; j < generalMatrix.getColumns()-1; ++j) {
|
||||||
|
if (generalMatrix[0][j] < 0) {
|
||||||
|
thereIsNegative = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (thereIsNegative) {
|
||||||
|
for (int j = 0; j < generalMatrix.getColumns()-1; ++j) {
|
||||||
|
if (generalMatrix[0][j] > 0) {
|
||||||
|
if (generalMatrix[0][j] < (eps * (-1))) {
|
||||||
|
showMatrix(generalMatrix);
|
||||||
|
std::cout << generalMatrix[0][j] << std::endl;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (maximize == false) {
|
||||||
|
bool thereIsPositive = false;
|
||||||
|
for (int j = 0; j < generalMatrix.getColumns()-1; ++j) {
|
||||||
|
if (generalMatrix[0][j] > 0) {
|
||||||
|
thereIsPositive = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (thereIsPositive) {
|
||||||
|
for (int j = 0; j < generalMatrix.getColumns()-1; ++j) {
|
||||||
|
if (generalMatrix[0][j] > 0) {
|
||||||
|
if (generalMatrix[0][j] < eps) {
|
||||||
|
showMatrix(generalMatrix);
|
||||||
|
std::cout << generalMatrix[0][j] << std::endl;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
|
||||||
int rows = generalMatrix.getRows();
|
int rows = generalMatrix.getRows();
|
||||||
int cols = generalMatrix.getColumns();
|
int cols = generalMatrix.getColumns();
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ struct DestroyMatrix {
|
|||||||
Vector b;
|
Vector b;
|
||||||
};
|
};
|
||||||
|
|
||||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix);
|
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix);
|
||||||
void elimination(Matrix&, int pivot_row_index, int pivot_column_index);
|
void elimination(Matrix&, int pivot_row_index, int pivot_column_index);
|
||||||
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b);
|
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b);
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -9,7 +9,8 @@ void showMatrix(Matrix matrix) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << maxNumberLength << std::endl;
|
//std::cout << maxNumberLength << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
for (size_t y = 0; y < matrix.getRows(); y++) {
|
for (size_t y = 0; y < matrix.getRows(); y++) {
|
||||||
std::string row = "";
|
std::string row = "";
|
||||||
for (size_t x = 0; x < matrix.getColumns(); x++) {
|
for (size_t x = 0; x < matrix.getColumns(); x++) {
|
||||||
@@ -38,6 +39,7 @@ void showMatrix(Matrix matrix) {
|
|||||||
}
|
}
|
||||||
std::cout << row << std::endl;
|
std::cout << row << std::endl;
|
||||||
}
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector::Vector(int n) {
|
Vector::Vector(int n) {
|
||||||
|
|||||||
Reference in New Issue
Block a user