Docsity

Simplex Method Implementation in C++: User Data and Initialization, Cheat Sheet of Law

Partial preview of the text

Download Simplex Method Implementation in C++: User Data and Initialization and more Cheat Sheet Law in PDF only on Docsity!

O’ZBEKISTON RESPUBLIKASI AXBOROT KOMUNIKATSIYALARINI

RIVOJLANTIRISH VAZIRLIGI

TOSHKENT AXBOROT TEXNOLOGIYALARI UNVERSITETI

KIBERXAVFSIZLIK FAKULTETI

ALGORITMLASHNI LOYIHALASH FANIDAN RUSLAN ERGASHOV

LABARATORIYA ISHI-

TOSHKENT 2022

USE_DATA.H

#pragma once class user_data {

public: void get_data_from_user(); void user_data_is_valid(); protected: double* function; double* fm; double** system; int* sign; int num_v; int num_l; bool way; }; SIMPLEX.H #pragma once #include #include "user_data.h" class simplex : public user_data { public: void init(); void gen_plane(); bool plane_is_valid(); bool function_is_undefined(); void print_result_to_file(int it_num); private: double func; double** bv; double** sv; double* istr; double* th; double alm; int i_lrow;

void user_data::get_data_from_user() { string num_limits, num_vars, s_var, fr_m, sn, func, w; int i, j; bool validator = false; do { cout << "Sistemadagi cheklovlar sonini kiriting: "; getline(cin, num_limits); if (atoi(num_limits.c_str()) < 2) error(1); else if (atoi(num_limits.c_str()) > 500) error(2); else validator = true; } while (!validator); num_l = atoi(num_limits.c_str()); validator = false; do { cout << "Cheklash sistemasida o'zgaruvchilar sonini kiriting: "; getline(cin, num_vars); if (atoi(num_vars.c_str()) < 2) error(3); else if (atoi(num_vars.c_str()) > 500) error(4); else

validator = true; } while (!validator); num_v = atoi(num_vars.c_str()); validator = false; function = new double[num_v]; system = new double* [num_l]; for (i = 0; i < num_l; i++) system[i] = new double[num_v]; fm = new double[num_l]; sign = new int[num_l]; cout << "\nMaqsad funktsiyasidagi koeffitsientlarni to'ldiring.\n" << endl; for (i = 0; i < num_v; i++) { do { cout << "Maqsad funksiyasining koeffitsientini kiriting x" << i + 1 << ": "; getline(cin, func); if (atof(func.c_str()) == 0) error(0); else { validator = true; function[i] = atof(func.c_str()); } } while (!validator); validator = false; } do { cout << "Maqsad funktsiyasining yo'nalishini kiriting ( min, max ) : "; getline(cin, w);

if (sn == "<=" sn == "=" sn == ">=") { validator = true; if (sn == "<=") sign[i] = 0; if (sn == "=") sign[i] = 1; if (sn == ">=") sign[i] = 2; } else error(0); cout << sign[i] << endl; } while (!validator); validator = false; do { cout << i + 1 << "- Cheklov uchun son kiriting: "; getline(cin, fr_m); if (atof(fr_m.c_str()) == 0) error(0); else validator = true; } while (!validator); fm[i] = atof(fr_m.c_str()); validator = false; cout << endl; } } #include

#include #include #include #include #include "user_data.h" #include "simplex.h" using std::cout; using std::endl; void simplex::init() { int i, j; func = 0; sv = new double* [num_l]; for (i = 0; i < num_l; i++) sv[i] = new double[num_v * 2]; for (i = 0; i < num_l; i++) { for (j = 0; j < num_v; j++) sv[i][j] = system[i][j]; for (; j < num_v * 2; j++) if (i + num_v == j) if (way) sv[i][j] = 1; else sv[i][j] = -1; else sv[i][j] = 0; } istr = new double[num_v * 2]; bv = new double* [num_l];

if (istr[i] < 0) { result = false; break; } if (!way) for (i = 0; i < num_v * 2; i++) if (istr[i] >= 0) { result = false; break; } return result; } bool simplex::function_is_undefined() { int i; for (i = 0; i < num_l; i++) if (th[i] < 0) { return false; } return true; } void simplex::gen_plane() { int i, j, it_num = 0; double A, B; while (!plane_is_valid() && function_is_undefined()) { A = bv[i_lrow][1]; B = istr[i_lcol]; func -= A * B / alm; double* tmp_bv = new double[num_l]; bv[i_lrow][0] = i_lcol;

A = bv[i_lrow][1]; for (i = 0; i < num_l; i++) { B = sv[i][i_lcol]; tmp_bv[i] = bv[i_lrow][1]; if (i != i_lrow) tmp_bv[i] = bv[i][1] - A * B / alm; else tmp_bv[i] /= alm; } for (i = 0; i < num_l; i++) bv[i][1] = tmp_bv[i]; double* tmp_istr = istr; B = istr[i_lcol]; for (i = 0; i < num_v * 2; i++) { A = sv[i_lrow][i]; tmp_istr[i] = istr[i] - A * B / alm; } istr = tmp_istr; double** tmp_sv = new double* [num_l]; for (i = 0; i < num_l; i++) tmp_sv[i] = new double[num_v * 2]; for (i = 0; i < num_l; i++) for (j = 0; j < num_v * 2; j++) { tmp_sv[i][j] = sv[i][j]; A = sv[i_lrow][j]; B = sv[i][i_lcol]; if (i == i_lrow) tmp_sv[i][j] /= alm; else tmp_sv[i][j] = sv[i][j] - A * B / alm; } sv = tmp_sv;

if (function[i] < 0) f_x << "- " << fabs(function[i]) << "x" << i + 1 << " "; if (function[i] > 0) f_x << "+ " << function[i] << "x" << i + 1 << " "; } } std::string minmax; if (way) minmax = "max"; else minmax = "min"; f_x << "=> " << minmax << "\n" << endl; table << f_x.str(); table << "Cheklash sistemasi\n" << endl; std::stringstream math_sys; std::string math_sign; for (i = 0; i < num_l; i++) { for (j = 0; j < num_v; j++) { if (!j) math_sys << system[i][j] << "x" << j + 1 << " "; else if (system[i][j] == 1) if (!j) math_sys << "x" << j + 1 << " "; else math_sys << "+ x" << j + 1 << " "; else if (system[i][j] == -1) if (!j) math_sys << "-x" << j + 1 << " "; else math_sys << "- x" << j + 1 << " ";

else { if (system[i][j] < 0) math_sys << "- " << fabs(system[i][j]) << "x" << j + 1 << " "; else math_sys << "+ " << system[i][j] << "x" << i + 1 << " "; if (!sign[i]) math_sign = "<="; if (sign[i] == 1) math_sign = "="; if (sign[i] == 2) math_sign = ">="; } } math_sys << math_sign << " " << fm[i]; math_sys << endl; } std::string min_or_max; if (way) min_or_max = "maksimum"; else min_or_max = "minimum"; table << math_sys.str() << endl; table << min_or_max << " uchun bu masalani simpleks jadval usuli yordamida yechamiz.\nBirinchi bazani yarating\n" << endl; } for (i = 0; i < num_l; i++) { table << "x" << bv[i][0] + 1 << "\t" << bv[i][1] << "\t"; for (j = 0; j < num_v * 2; j++) table << sv[i][j] << "\t"; if (!plane_is_valid()) table << th[i]; table << "\n" << endl;

ud->get_data_from_user(); ud->init(); ud->gen_plane(); return 0; }