Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.89 KB | None | 0 0
  1. // lab05_multi_criteria_selection.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <iomanip>
  10.  
  11. #define line "_________________________________________________________________________"
  12.  
  13. struct Candidate
  14. {
  15.     std::string name;
  16.     std::vector<int> crit_rates; // Criterions rates
  17.     std::vector<float> norm_crit_rates; // Criterions rates
  18. };
  19.  
  20. void InitializeCands(std::vector<Candidate>& cands)
  21. {
  22.     cands[0].norm_crit_rates.resize(4);
  23.     cands[0].crit_rates.resize(4);
  24.     cands[0].name = "Anatoly";   // Name
  25.     cands[0].crit_rates[0] = 4;  // Education
  26.     cands[0].crit_rates[1] = 2;  // Physical form
  27.     cands[0].crit_rates[2] = 9;  // Appearance
  28.     cands[0].crit_rates[3] = 2;  // Character
  29.  
  30.     cands[1].norm_crit_rates.resize(4);
  31.     cands[1].crit_rates.resize(4);
  32.     cands[1].name = "Alexander";
  33.     cands[1].crit_rates[0] = 8;
  34.     cands[1].crit_rates[1] = 7;
  35.     cands[1].crit_rates[2] = 7;
  36.     cands[1].crit_rates[3] = 7;
  37.  
  38.     cands[2].norm_crit_rates.resize(4);
  39.     cands[2].crit_rates.resize(4);
  40.     cands[2].name = "Vladimir";
  41.     cands[2].crit_rates[0] = 2;
  42.     cands[2].crit_rates[1] = 8;
  43.     cands[2].crit_rates[2] = 9;
  44.     cands[2].crit_rates[3] = 6;
  45.  
  46.     cands[3].norm_crit_rates.resize(4);
  47.     cands[3].crit_rates.resize(4);
  48.     cands[3].name = "Sergey";
  49.     cands[3].crit_rates[0] = 6;
  50.     cands[3].crit_rates[1] = 7;
  51.     cands[3].crit_rates[2] = 7;
  52.     cands[3].crit_rates[3] = 5;
  53.  
  54.    
  55. }
  56.  
  57. void InitializeExpertRewie(float expert_rewies[4][4])
  58. {
  59.     for (int i = 0; i < 4; i++)
  60.         expert_rewies[i][i] = 0.5;
  61.  
  62.     expert_rewies[0][1] = 0;
  63.     expert_rewies[0][2] = 0;
  64.     expert_rewies[0][3] = 0;
  65.  
  66.     expert_rewies[1][0] = 1;
  67.     expert_rewies[1][2] = 0;
  68.     expert_rewies[1][3] = 0;
  69.  
  70.     expert_rewies[2][0] = 1;
  71.     expert_rewies[2][1] = 1;
  72.     expert_rewies[2][3] = 1;
  73.  
  74.     expert_rewies[3][0] = 1;
  75.     expert_rewies[3][1] = 1;
  76.     expert_rewies[3][2] = 0;
  77. }
  78.  
  79. bool CompareCandsByRate(Candidate cand1, Candidate cand2)
  80. {
  81.     int j = 2;  // Appearance
  82.     return cand1.crit_rates[j] < cand2.crit_rates[j];
  83. }
  84.  
  85. bool CompareCandsByDistance(Candidate cand1, Candidate cand2)
  86. {
  87.     int crit1 = 2;  // Appearance
  88.     int crit2 = 3;  // Character
  89.     float distance1 = std::sqrt(std::pow(10 - cand1.crit_rates[crit1], 2) + std::pow(10 - cand1.crit_rates[crit2], 2));
  90.     float distance2 = std::sqrt(std::pow(10 - cand2.crit_rates[crit1], 2) + std::pow(10 - cand2.crit_rates[crit2], 2));
  91.     return distance1 < distance2;
  92. }
  93.  
  94. int find_min_value(std::vector<Candidate> cands, int j)
  95. {
  96.     int min_value=cands[0].crit_rates[j];
  97.  
  98.     for (int i = 1; i < cands.size(); i++)
  99.         if (cands[i].crit_rates[j] < min_value)
  100.             min_value = cands[i].crit_rates[j];
  101.     return min_value;
  102. }
  103.  
  104. int find_max_value(std::vector<Candidate> cands, int j)
  105. {
  106.     int max_value = cands[0].crit_rates[j];
  107.  
  108.     for (int i = 1; i < cands.size(); i++)
  109.         if (cands[i].crit_rates[j] > max_value)
  110.             max_value = cands[i].crit_rates[j];
  111.     return max_value;
  112. }
  113.  
  114. // Tadle 1: Criteria weights
  115. void PrintTable1(int *criteria_weights, float* norm_criteria_weights)
  116. {
  117.     std::cout << " Table 1: Criteria weights" << std::endl;
  118.     std::cout << " __________________________________________________________________ " << std::endl;
  119.     std::cout << "|     Form     |  Education | Phys. Form | Appearance |  Character |" << std::endl;
  120.     std::cout << "|--------------|------------|------------|------------|------------|" << std::endl;
  121.  
  122.     std::cout << "| From 1 to 10 |      ";
  123.     for (int i = 0; i < 4; i++)
  124.         std::cout << criteria_weights[i] << "     |      ";
  125.     std::cout << std::endl;
  126.  
  127.     std::cout << "|  Normalized  |     ";
  128.     for (int i = 0; i < 4; i++)
  129.         std::cout << norm_criteria_weights[i] << "    |     ";
  130.    
  131.     std::cout << std::endl;
  132.  
  133.     std::cout << "|______________|____________|____________|____________|____________|" << std::endl;
  134. }
  135.  
  136. // Tadle 2: Criterions rates for candidates
  137. void PrintTable2(std::vector<Candidate>& cands)
  138. {
  139.     std::cout << " Table 2: Criterions rates for candidates" << std::endl;
  140.     std::cout << " ________________________________________________________________ " << std::endl;
  141.     std::cout << "|    Name    |  Education | Phys. Form | Appearance |  Character |" << std::endl;
  142.     std::cout << "|------------|------------|------------|------------|------------|" << std::endl;
  143.     for (int i = 0; i < 4; i++)
  144.     {
  145.         std::cout << "|  " << cands[i].name;
  146.         for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
  147.             std::cout << " ";
  148.         std::cout << "|      ";
  149.         for (int k = 0; k < 4; k++)
  150.             std::cout << cands[i].crit_rates[k] << "     |      ";
  151.         std::cout << std::endl;
  152.     }
  153.  
  154.     std::cout << "|____________|____________|____________|____________|____________|" << std::endl;
  155. }
  156.  
  157. // Table 3: Minimum acceptable levels for other criteria
  158. void PrintTable3(float* min_allowable_levels, std::vector<Candidate> suitable_cads)
  159. {
  160.     std::cout << " Table 3: Minimum acceptable levels for other criteria " << std::endl;
  161.     std::cout << " ________________________________________________________________ " << std::endl;
  162.     std::cout << "|            |  Education | Phys. Form | Appearance |  Character |" << std::endl;
  163.     std::cout << "|------------|------------|------------|------------|------------|" << std::endl;
  164.  
  165.    
  166.     std::cout << "| Min. level |     ";
  167.     for (int i = 0; i < 4; i++)
  168.         if (i == 2)
  169.             std::cout << "---" << "    |     ";
  170.         else
  171.         {
  172.             std::cout << min_allowable_levels[i] << "    |     ";
  173.         }
  174.     std::cout << std::endl;
  175.     std::cout << "|____________|____________|____________|____________|____________|" << std::endl;
  176.     std::cout << std::endl << "| Note: The main criterion is appearance" << std::endl;
  177.     std::cout << std::endl << "Candidates satisfies the task:" << std::endl;
  178.     for (int i = 0; i < suitable_cads.size(); i++)
  179.     {
  180.         std::cout << " - " << suitable_cads[i].name;
  181.         std::cout << " (Criteria: ";
  182.         for (int j = 0; j < 4; j++)
  183.             std::cout << suitable_cads[i].crit_rates[j] << " ";
  184.         std::cout << ")" << std::endl;
  185.     }
  186.     std::cout << std::endl << "With best appearance:" << std::endl;
  187.     Candidate best_cand = *std::max_element(suitable_cads.begin(), suitable_cads.end(), CompareCandsByRate);
  188.     std::cout << " - " << best_cand.name << "(Appearance: " << best_cand.crit_rates[2] << ")" << std::endl;
  189. }
  190.  
  191. // Table 4: Normalized criterions rates for candidates
  192. void PrintTable4(std::vector<Candidate>& cands, float norm_crit_rates[4][4])
  193. {
  194.     std::cout << std::fixed << std::setprecision(2);
  195.     std::cout << " Table 4: Normalized criterions rates for candidates" << std::endl;
  196.     std::cout << " ____________________________________________________________________________ " << std::endl;
  197.     std::cout << "|    Name    |    Education  |   Phys. Form  |   Appearance  |   Character   |" << std::endl;
  198.     std::cout << "|------------|---------------|---------------|---------------|---------------|" << std::endl;
  199.     for (int i = 0; i < 4; i++)
  200.     {
  201.         std::cout << "|  " << cands[i].name;
  202.         for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
  203.             std::cout << " ";
  204.         std::cout << "|      ";
  205.  
  206.         for (int j = 0; j < 4; j++)
  207.         {
  208.             std::cout << norm_crit_rates[i][j] << "     |      ";
  209.         }
  210.         std::cout << std::endl;
  211.     }
  212.     std::cout << "|____________|_______________|_______________|_______________|_______________|" << std::endl;
  213.    
  214. }
  215.  
  216. // Table 5: The formation and narrowing of the Pareto set
  217. void PrintTable5(std::vector<Candidate>& cands, Candidate best_cand)
  218. {
  219.     std::cout << " Table 5: The formation and narrowing of the Pareto set" << std::endl <<std::endl;
  220.     std::cout << " Utopia point:   (10,10)" << std::endl;
  221.     std::cout << " Distance type:  Euclidian" << std::endl;
  222.     std::cout << " ____________________________________________________________ " << std::endl;
  223.     std::cout << "|    Name    |   Appearance  |   Character   |    Distance   |" << std::endl;
  224.     std::cout << "|------------|---------------|---------------|---------------|" << std::endl;
  225.  
  226.     int criteria_pairs[4][2];
  227.  
  228.     for (int i = 0; i < 4; i++)
  229.         for (int j = 0; j < 2; j++)
  230.             criteria_pairs[i][j] = cands[i].crit_rates[2 + j];
  231.  
  232.     float distances[4];
  233.     for (int i = 0; i < 4; i++)
  234.         distances[i] = std::sqrt(std::pow(10 - criteria_pairs[i][0], 2) + std::pow(10 - criteria_pairs[i][1], 2));
  235.  
  236.     float min_distance = *std::min_element(distances, distances+4);
  237.  
  238.     for (int i = 0; i < 4; i++)
  239.     {
  240.         std::cout << "|  " << cands[i].name;
  241.         for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
  242.             std::cout << " ";
  243.         std::cout << "|        ";
  244.        
  245.         std::cout << criteria_pairs[i][0] << "      |        ";
  246.         std::cout << criteria_pairs[i][1] << "      |     ";
  247.        
  248.         if (distances[i] / 10 < 1)
  249.             std::cout << " ";
  250.         std::cout << distances[i] << "     |" << std::endl;
  251.     }
  252.  
  253.     std::cout << "|____________|_______________|_______________|_______________|" << std::endl;
  254.     std::cout << std::endl << "Candidate with minimal distance: " << std::endl;
  255.     std::cout << " - " << best_cand.name << " (Distance: " << min_distance << ")" << std::endl;
  256.  
  257. }
  258.  
  259. void PrintTable6(std::vector<Candidate> cands)
  260. {
  261.     std::cout << std::fixed << std::setprecision(2);
  262.     std::cout << " Table 6: Normalized criterions rates for candidates" << std::endl;
  263.     std::cout << " ____________________________________________________________________________ " << std::endl;
  264.     std::cout << "|    Name    |    Education  |   Phys. Form  |   Appearance  |   Character   |" << std::endl;
  265.     std::cout << "|------------|---------------|---------------|---------------|---------------|" << std::endl;
  266.     for (int i = 0; i < 4; i++)
  267.     {
  268.         std::cout << "|  " << cands[i].name;
  269.         for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
  270.             std::cout << " ";
  271.         std::cout << "|      ";
  272.  
  273.         for (int j = 0; j < 4; j++)
  274.         {
  275.             std::cout << cands[i].norm_crit_rates[j] << "     |      ";
  276.         }
  277.         std::cout << std::endl;
  278.     }
  279.     std::cout << "|____________|_______________|_______________|_______________|_______________|" << std::endl;
  280. }
  281.  
  282. //Table 7: Expert assessment of criteria (by the method of pairwise comparison)
  283. void PrintTable7(float expert_rewies[4][4])//
  284. {
  285.     std::cout << std::fixed << std::setprecision(1);
  286.     std::cout << " Table 7: Expert assessment of criteria (by the method of pairwise comparison)" << std::endl;
  287.     std::cout << " _______________________________________________________________________________ " << std::endl;
  288.     std::cout << "|              |    Education  |   Phys. Form  |   Appearance  |   Character   |" << std::endl;
  289.     std::cout << "|--------------|---------------|---------------|---------------|---------------|" << std::endl;
  290.  
  291.     std::string Criteria[4] = { "Education", "Phys. Form", "Appearance", "Character" };
  292.     for (int i = 0; i < 4; i++)
  293.     {
  294.         std::cout << "|  " << Criteria[i];
  295.         for (int l = 0; l < 12 - 2 -Criteria[i].size(); l++)
  296.             std::cout << " ";
  297.         std::cout << "  |      ";
  298.  
  299.         for (int j = 0; j < 4; j++)
  300.         {
  301.             std::cout << expert_rewies[i][j] << "      |      ";
  302.         }
  303.         std::cout << std::endl;
  304.     }
  305.     std::cout << "|______________|_______________|_______________|_______________|_______________|" << std::endl;
  306. }
  307.  
  308.  
  309. int main()
  310. {
  311.     std::vector<Candidate> cands;
  312.     cands.resize(4);
  313.     int criteria_weights[4] = { 2, 4, 8, 6 };
  314.     float norm_criteria_weights[4];
  315.  
  316.     for (int i = 0; i < 4; i++)
  317.         norm_criteria_weights[i] = (float)criteria_weights[i] / 20;
  318.  
  319.     // Initialization
  320.     InitializeCands(cands);
  321.  
  322.     std::cout << std::endl;
  323.     std::cout << " CRITERIA WEIGHTS VECTOR" << std::endl;
  324.     std::cout << line << std::endl << std::endl;
  325.     PrintTable1(criteria_weights, norm_criteria_weights);  
  326.     std::cout << std::endl << std::endl << std::endl << std::endl;
  327.  
  328.    
  329.     std::cout << " REPLACING CRITERIA WITH CONSTRAINTS METHOD" << std::endl;
  330.     std::cout << line << std::endl << std::endl;
  331.     PrintTable2(cands);
  332.     std::cout << std::endl << std::endl << std::endl << std::endl;
  333.  
  334.     float norm_crit_rates[4][4];
  335.     float min_allowable_levels[4];
  336.     min_allowable_levels[2] = 0;                   // main criterion
  337.     const float min_allowable_levels_coeffs[4] = {
  338.         0.4, // Education
  339.         0.6, // Physical form
  340.         1,   // Appearance
  341.         0.8  // Character
  342.     };
  343.  
  344.     for (int i = 0; i < 4; i++)
  345.         for (int j = 0; j < 4; j++)
  346.         {
  347.             if (j == 2) // main criterion
  348.             {
  349.                 norm_crit_rates[i][j] = cands[i].crit_rates[j];
  350.                 continue;
  351.             }
  352.             int min_value = find_min_value(cands, j);
  353.             int max_value = find_max_value(cands, j);
  354.             norm_crit_rates[i][j] = (float)(cands[i].crit_rates[j] - min_value)/(max_value-min_value);
  355.  
  356.             min_allowable_levels[j] = min_allowable_levels_coeffs[j] * max_value;
  357.         }
  358.  
  359.     std::vector<Candidate> suitable_cads;
  360.     for (int i = 0; i < 4; i++)
  361.     {
  362.         bool suit_flag = true;
  363.         for (int j = 0; j < 4; j++)
  364.         {
  365.  
  366.             if (!(cands[i].crit_rates[j] > min_allowable_levels[j]))
  367.             {
  368.                 suit_flag = 0;
  369.                 break;
  370.             }
  371.         }
  372.         if (suit_flag)
  373.             suitable_cads.push_back(cands[i]);
  374.     }
  375.  
  376.     std::cout << " REPLACING CRITERIA WITH CONSTRAINTS METHOD" << std::endl;
  377.     std::cout << line << std::endl << std::endl;
  378.     PrintTable3(min_allowable_levels, suitable_cads);
  379.     std::cout << std::endl << std::endl ;
  380.    
  381.     PrintTable4(cands, norm_crit_rates);
  382.     std::cout << std::endl << std::endl << std::endl << std::endl;
  383.  
  384.  
  385.     // Choosed two criteria: appearance (2) and character (3) Weighting and combining criteria
  386.    
  387.  
  388.     std::cout << " THE FORMATION AND NARROWING OF THE PARETO SET" << std::endl;
  389.     std::cout << line << std::endl << std::endl;
  390.     Candidate best_cand = *std::min_element(cands.begin(),cands.end(),CompareCandsByDistance);
  391.     PrintTable5(cands, best_cand);
  392.     std::cout << std::endl << std::endl << std::endl << std::endl;
  393.  
  394.     std::cout << " WEIGHTING AND COMBINING CRITERIA" << std::endl;
  395.     std::cout << line << std::endl << std::endl;
  396.  
  397.     float column_values_sum = 0;
  398.     for (int j = 0; j < 4; j++)          // Normalizing weights
  399.     {
  400.         column_values_sum = 0;
  401.         for (int i = 0; i < 4; i++)
  402.             column_values_sum += cands[i].crit_rates[j];
  403.  
  404.         for (int i = 0; i < 4; i++)
  405.             cands[i].norm_crit_rates[j] = (float)cands[i].crit_rates[j] / column_values_sum;
  406.        
  407.     }
  408.  
  409.     PrintTable6(cands);
  410.     std::cout << std::endl << std::endl;
  411.  
  412.     float expert_rewies[4][4];
  413.     InitializeExpertRewie(expert_rewies);
  414.  
  415.     PrintTable7(expert_rewies);
  416.     std::cout << std::endl << std::endl;
  417.  
  418.     float crit_weights_vector[4] = { 0, 0, 0, 0 };
  419.     for (int i = 0; i < 4; i++)
  420.     {
  421.         for (int j = 0; j < 4; j++)
  422.         {
  423.             if (j == i)
  424.                 continue;
  425.             crit_weights_vector[i] += expert_rewies[i][j];
  426.         }
  427.     }
  428.    
  429.     int weights_sum = 0;
  430.     for (int i = 0; i < 4; i++)
  431.         weights_sum += crit_weights_vector[i];
  432.  
  433.     for (int i = 0; i < 4; i++)
  434.         crit_weights_vector[i] /= weights_sum;
  435.    
  436.     float integral_assessment[4] = { 0,0,0,0 };
  437.  
  438.     for (int i = 0; i < 4; i++)
  439.         for (int j = 0; j < 4; j++)
  440.             integral_assessment[i] += cands[i].norm_crit_rates[j] * crit_weights_vector[j];
  441.  
  442.  
  443.     std::cout << std::setprecision(4);
  444.     std::cout << "Integral assesment: ";
  445.     for (int i = 0; i < 4; i++)
  446.     {
  447.         std::cout << integral_assessment[i] << ", ";
  448.     }
  449.     std::cout << std::endl;
  450.     std::cout << "Vladimir - best: ";
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement