Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- //for this task matrix D is 4x4, c 4x1, A 2x4, b 2x1, delta 4x4
- //knowing that the size of simplex is defined as 6x17 (last column for matrices b and c)
- static float D[4][4];
- static float c[4];
- static float A[2][4];
- static float b[2];
- static float delta[4][4];
- static float simplex[6][17];
- //function creating matrix D basing on entered coefficients staying near x1^2, x2^2 and x1x2
- void D_initializer()
- {
- std::cout << "Filling the D matrix." << std::endl;
- for (int i = 0; i < 4;i++)//initially filling with 0's
- {
- for (int j = 0;j < 4;j++)
- {
- D[i][j] = 0.0;
- }
- }
- std::cout << "Enter coefficient of x1 square: ";
- std::cin >> D[0][0];
- std::cout << "Enter coefficient of x2 square: ";
- std::cin >> D[1][1];
- std::cout << "Enter coefficient of product x1*x2: ";
- std::cin >> D[0][1];
- D[0][1] = D[0][1] / 2;//the value of x1*x2 multiplication happen twice, so it has to be divided by two
- D[1][0] = D[0][1];
- //in other positions are 0's
- }
- //function showing on the content of the matrix D on the console window
- void D_printer()
- {
- std::cout << "Matrix D" << std::endl;
- for (int i = 0;i < 4;i++)
- {
- for (int j = 0;j < 4;j++)
- {
- std::cout << D[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- //function D checks if matrix is symmetric and nonnegatively defined; if it is returns 1, otherwise returns 0;
- bool D_checker()
- {
- for (int i = 0;i < 4;i++)//is it symmetrical?
- {
- for (int j = i;j < 4; j++)
- {
- if(D[i][j]!=D[j][i])
- {
- std::cout << "Matrix D is not simmetrical" << std::endl;
- return 0;
- }
- }
- }
- if ((D[0][0] * D[1][1] - D[0][1] * D[1][0]) < 0)//to check if it is nonnegatively defined in this case, it is necessary to check if 2x2
- {
- std::cout << "Matrix D is negatively defined" << std::endl;
- return 0;
- }
- return 1;
- }
- void C_initializer()
- {
- std::cout << "Enter coefficient of x1: ";
- std::cin >> c[0];
- std::cout << "Enter coefficient of x2: ";
- std::cin >> c[1];
- c[3] = 0;
- c[4] = 0;
- }
- //function showing on the content of the matrix c on the console window
- void C_printer()
- {
- std::cout << "Matrix c" << std::endl;
- for (int i = 0; i < 4;i++)
- {
- std::cout << c[i] << std::endl;
- }
- }
- void Ab_initializer()
- {
- bool inequality;
- for (int i = 0;i < 2;i++)
- {
- std::cout << "Does the inequality sign in "<<i+1<<". constraint is =<? (1-yes, 0-no): ";
- std::cin >> inequality;
- if (inequality)
- {
- A[i][i+2] = 1;
- }
- else
- {
- A[i][i+2] = -1;
- }
- A[i][3 - i] = 0;
- std::cout << "Enter coefficient of x1 in " << i+1 << ". constraint: ";
- std::cin >> A[i][0];
- std::cout << "Enter coefficient of x2 in " << i+1 << ". constraint: ";
- std::cin >> A[i][1];
- std::cout << "Enter value of b in " << i+1 << ". constraint: ";
- std::cin >> b[i];
- }
- }
- //function showing on the content of the matrix A on the console window
- void A_printer()
- {
- std::cout << "Matrix A" << std::endl;
- for (int i = 0;i < 2;i++)
- {
- for (int j = 0;j < 4;j++)
- {
- std::cout << A[i][j] << "\t";
- }
- std::cout<<std::endl;
- }
- }
- //function showing on the content of the matrix b on the console window
- void b_printer()
- {
- std::cout << "Matrix b" << std::endl;
- std::cout << b[0] << std::endl;
- std::cout << b[1] << std::endl;
- }
- void delta_setter()
- {
- for (int i = 0;i < 4;i++)
- {
- for (int j = i;j < 4;j++)
- {
- if (i == j)
- {
- if (c[i] < 0)
- {
- delta[i][j] = -1;
- }
- else
- {
- delta[i][j] = 1;
- }
- }
- else
- {
- delta[i][j] = delta[j][i] = 0;
- }
- }
- }
- }
- //function showing on the content of the delta matrix on the console window
- void delta_printer()
- {
- std::cout << "Delta" << std::endl;
- for (int i = 0;i < 4;i++)
- {
- for (int j = 0;j < 4;j++)
- {
- std::cout << delta[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- void simplex_setter()
- {
- for (int i = 0;i < 2;i++)
- {
- for (int j = 0;j < 4;j++)
- {
- simplex[i][j] = A[i][j];
- }
- }
- for (int i = 2;i < 6;i++)
- {
- for (int j = 0;j < 4; j++)
- {
- simplex[i][j] = 2 * D[i - 2][j];
- }
- }
- for (int i = 2;i < 6;i++)
- {
- for (int j = 4; j < 6;j++)
- {
- simplex[i][j] = -A[j - 4][i - 2];
- simplex[i][j+2] = A[j - 4][i - 2];
- }
- }
- for (int i = 2;i < 6;i++)
- {
- for (int j = 8;j < 12;j++)
- {
- if (j == (i+6))
- {
- simplex[i][j] = -1;
- }
- else
- {
- simplex[i][j] = 0;
- }
- }
- }
- for (int i = 2;i < 6;i++)
- {
- for (int j = 12;j < 16;j++)
- {
- simplex[i][j] = delta[i - 2][j - 12];
- }
- }
- for (int i = 0;i < 2;i++)
- {
- simplex[i][16] = b[i];
- }
- for (int i = 2;i < 6;i++)
- {
- if (c[i - 2] != 0)
- {
- simplex[i][16] = -c[i - 2];
- }
- else
- {
- simplex[i][16] = c[i - 2];
- }
- }
- for (int j = 4;j < 16;j++)
- {
- simplex[0][j] = 0;
- simplex[1][j] = 0;
- }
- }
- //function showing on the content of the simplex tableaux on the console window
- void simplex_printer()
- {
- std::cout << "Simplex" << std::endl;
- for (int i = 0;i < 6;i++)
- {
- for (int j = 0;j < 17;j++)
- {
- std::cout << simplex[i][j] << " ";
- }
- std::cout<<std::endl;
- }
- }
- //function writing on the content of the simplex tableaux into "input.txt" file
- void output_creator()
- {
- std::ofstream output;
- output.open("input.txt");
- for (int i = 0;i < 6;i++)
- {
- for (int j = 0;j < 17;j++)
- {
- output << simplex[i][j] << " ";
- }
- output << std::endl;
- }
- output.close();
- }
- int main()
- {
- std::cout << "PROBLEM OF MINIMIZATION" << std::endl;
- D_initializer();
- if (D_checker())
- {
- D_printer();
- C_initializer();
- C_printer();
- Ab_initializer();
- A_printer();
- b_printer();
- delta_setter();
- delta_printer();
- simplex_setter();
- simplex_printer();
- output_creator();
- std::cout << "Simplex succesfully created. Result is written in \"input.txt\" file." << std::endl;
- }
- else
- {
- std::cout << "Unnable to create simplex. Check wether given quadratic problem is solvable by aim of constructing matrix D or all data was entered correctly." << std::endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement