Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

The implementation of the simplex method using c++ for handling user data and initialization. The user_data class is defined, which includes methods for getting data from the user and validating its correctness. The simplex class is also defined, which inherits from user_data and includes methods for initializing the simplex tableau and checking its validity. The code includes error handling and input validation.

Typology: Cheat Sheet

2023/2024

Uploaded on 02/12/2024

saad-falcon
saad-falcon 🇺🇿

1 document

1 / 16

Toggle sidebar

Related documents


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-3 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 <sstream> #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; 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 (w == "max" w == "MAX" w == "min" w == "MIN") { validator = true; if (w == "max" w == "MAX") way = true; else way = false; } else error(0); } while (!validator); cout << "\nChegaralash sistemasini to`ldiring. \n" << endl; for (i = 0; i < num_l; i++) { cout << i + 1 << "- chegarani kiriting.\n" << endl; for (j = 0; j < num_v; j++) { do { cout << "Koeffitsientni kiriting X" << j + 1 << ": "; getline(cin, s_var); if (atof(s_var.c_str()) == 0) error(0); else { validator = true; } } while (!validator); system[i][j] = atof(s_var.c_str()); validator = false; } do { cout << i + 1 << " - cheklov belgisini kiriting. ( <=, =, >= ) : "; getline(cin, sn); 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 <iostream> 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; i_lcol = 0; for (i = 0; i < num_l; i++) th[i] = bv[i][1] / sv[i][i_lcol]; i_lrow = 0; for (i = 0; i < num_l - 1; i++) if (th[i] > th[i + 1]) i_lrow = i + 1; alm = sv[i_lrow][i_lcol]; it_num++; print_result_to_file(it_num); } if (!function_is_undefined()) cout << "\nMaqsad funktsiyasi cheklanmagan, bu misolning yechimlari yo'q\n" << endl; else { cout << "\nf(x) = " << func << "\n" << endl; for (i = 0; i < num_l; i++) { cout << "x" << bv[i][0] + 1 << " = " << bv[i][1] << endl; } cout << "\nBarcha hisob-kitoblar table.txt fayliga yoziladi\n" << endl; } } void simplex::print_result_to_file(int it_num) { int i, j; if (!it_num) { table << "Maqsad funktsiyasi berilgan:\n" << endl; std::stringstream f_x; f_x << "f(x) = "; for (i = 0; i < num_v; i++) { if (!i) f_x << function[i] << "x" << i + 1 << " "; else { } table << "f(x)\t" << func << "\t"; for (i = 0; i < num_v * 2; i++) table << istr[i] << "\t"; table << "\n"; if (plane_is_valid()) { if (plane_is_valid() && function_is_undefined()) table << "\nUshbu reja optimal va takomillashtirishni talab qilmaydi. Yechim topildi." << endl; std::ofstream outfile("table.txt"); outfile << table.str(); } else { std::string ln_or_gn; if (way) ln_or_gn = "ijobiy bo'lmagan"; else ln_or_gn = "ijobiy"; std::stringstream num_of_plane; if (!it_num) num_of_plane << "Birinchi tayanch"; else num_of_plane << it_num + 1 << "- ham rejalashtiring"; table << "\n" << num_of_plane.str() << " optimal emas\nindeks qatorida mavjud " << ln_or_gn << " elementlar.\nEroni yaxshilash kerak.\n" << endl; } } #include "simplex.h" int main() { setlocale(LC_ALL, "Tenglama"); simplex* ud = new simplex; ud->get_data_from_user(); ud->init(); ud->gen_plane(); return 0; }
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved