Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab05_multi_criteria_selection.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- #define line "_________________________________________________________________________"
- struct Candidate
- {
- std::string name;
- std::vector<int> crit_rates; // Criterions rates
- std::vector<float> norm_crit_rates; // Criterions rates
- };
- void InitializeCands(std::vector<Candidate>& cands)
- {
- cands[0].norm_crit_rates.resize(4);
- cands[0].crit_rates.resize(4);
- cands[0].name = "Anatoly"; // Name
- cands[0].crit_rates[0] = 4; // Education
- cands[0].crit_rates[1] = 2; // Physical form
- cands[0].crit_rates[2] = 9; // Appearance
- cands[0].crit_rates[3] = 2; // Character
- cands[1].norm_crit_rates.resize(4);
- cands[1].crit_rates.resize(4);
- cands[1].name = "Alexander";
- cands[1].crit_rates[0] = 8;
- cands[1].crit_rates[1] = 7;
- cands[1].crit_rates[2] = 7;
- cands[1].crit_rates[3] = 7;
- cands[2].norm_crit_rates.resize(4);
- cands[2].crit_rates.resize(4);
- cands[2].name = "Vladimir";
- cands[2].crit_rates[0] = 2;
- cands[2].crit_rates[1] = 8;
- cands[2].crit_rates[2] = 9;
- cands[2].crit_rates[3] = 6;
- cands[3].norm_crit_rates.resize(4);
- cands[3].crit_rates.resize(4);
- cands[3].name = "Sergey";
- cands[3].crit_rates[0] = 6;
- cands[3].crit_rates[1] = 7;
- cands[3].crit_rates[2] = 7;
- cands[3].crit_rates[3] = 5;
- }
- void InitializeExpertRewie(float expert_rewies[4][4])
- {
- for (int i = 0; i < 4; i++)
- expert_rewies[i][i] = 0.5;
- expert_rewies[0][1] = 0;
- expert_rewies[0][2] = 0;
- expert_rewies[0][3] = 0;
- expert_rewies[1][0] = 1;
- expert_rewies[1][2] = 0;
- expert_rewies[1][3] = 0;
- expert_rewies[2][0] = 1;
- expert_rewies[2][1] = 1;
- expert_rewies[2][3] = 1;
- expert_rewies[3][0] = 1;
- expert_rewies[3][1] = 1;
- expert_rewies[3][2] = 0;
- }
- bool CompareCandsByRate(Candidate cand1, Candidate cand2)
- {
- int j = 2; // Appearance
- return cand1.crit_rates[j] < cand2.crit_rates[j];
- }
- bool CompareCandsByDistance(Candidate cand1, Candidate cand2)
- {
- int crit1 = 2; // Appearance
- int crit2 = 3; // Character
- float distance1 = std::sqrt(std::pow(10 - cand1.crit_rates[crit1], 2) + std::pow(10 - cand1.crit_rates[crit2], 2));
- float distance2 = std::sqrt(std::pow(10 - cand2.crit_rates[crit1], 2) + std::pow(10 - cand2.crit_rates[crit2], 2));
- return distance1 < distance2;
- }
- int find_min_value(std::vector<Candidate> cands, int j)
- {
- int min_value=cands[0].crit_rates[j];
- for (int i = 1; i < cands.size(); i++)
- if (cands[i].crit_rates[j] < min_value)
- min_value = cands[i].crit_rates[j];
- return min_value;
- }
- int find_max_value(std::vector<Candidate> cands, int j)
- {
- int max_value = cands[0].crit_rates[j];
- for (int i = 1; i < cands.size(); i++)
- if (cands[i].crit_rates[j] > max_value)
- max_value = cands[i].crit_rates[j];
- return max_value;
- }
- // Tadle 1: Criteria weights
- void PrintTable1(int *criteria_weights, float* norm_criteria_weights)
- {
- std::cout << " Table 1: Criteria weights" << std::endl;
- std::cout << " __________________________________________________________________ " << std::endl;
- std::cout << "| Form | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|--------------|------------|------------|------------|------------|" << std::endl;
- std::cout << "| From 1 to 10 | ";
- for (int i = 0; i < 4; i++)
- std::cout << criteria_weights[i] << " | ";
- std::cout << std::endl;
- std::cout << "| Normalized | ";
- for (int i = 0; i < 4; i++)
- std::cout << norm_criteria_weights[i] << " | ";
- std::cout << std::endl;
- std::cout << "|______________|____________|____________|____________|____________|" << std::endl;
- }
- // Tadle 2: Criterions rates for candidates
- void PrintTable2(std::vector<Candidate>& cands)
- {
- std::cout << " Table 2: Criterions rates for candidates" << std::endl;
- std::cout << " ________________________________________________________________ " << std::endl;
- std::cout << "| Name | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|------------|------------|------------|------------|------------|" << std::endl;
- for (int i = 0; i < 4; i++)
- {
- std::cout << "| " << cands[i].name;
- for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
- std::cout << " ";
- std::cout << "| ";
- for (int k = 0; k < 4; k++)
- std::cout << cands[i].crit_rates[k] << " | ";
- std::cout << std::endl;
- }
- std::cout << "|____________|____________|____________|____________|____________|" << std::endl;
- }
- // Table 3: Minimum acceptable levels for other criteria
- void PrintTable3(float* min_allowable_levels, std::vector<Candidate> suitable_cads)
- {
- std::cout << " Table 3: Minimum acceptable levels for other criteria " << std::endl;
- std::cout << " ________________________________________________________________ " << std::endl;
- std::cout << "| | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|------------|------------|------------|------------|------------|" << std::endl;
- std::cout << "| Min. level | ";
- for (int i = 0; i < 4; i++)
- if (i == 2)
- std::cout << "---" << " | ";
- else
- {
- std::cout << min_allowable_levels[i] << " | ";
- }
- std::cout << std::endl;
- std::cout << "|____________|____________|____________|____________|____________|" << std::endl;
- std::cout << std::endl << "| Note: The main criterion is appearance" << std::endl;
- std::cout << std::endl << "Candidates satisfies the task:" << std::endl;
- for (int i = 0; i < suitable_cads.size(); i++)
- {
- std::cout << " - " << suitable_cads[i].name;
- std::cout << " (Criteria: ";
- for (int j = 0; j < 4; j++)
- std::cout << suitable_cads[i].crit_rates[j] << " ";
- std::cout << ")" << std::endl;
- }
- std::cout << std::endl << "With best appearance:" << std::endl;
- Candidate best_cand = *std::max_element(suitable_cads.begin(), suitable_cads.end(), CompareCandsByRate);
- std::cout << " - " << best_cand.name << "(Appearance: " << best_cand.crit_rates[2] << ")" << std::endl;
- }
- // Table 4: Normalized criterions rates for candidates
- void PrintTable4(std::vector<Candidate>& cands, float norm_crit_rates[4][4])
- {
- std::cout << std::fixed << std::setprecision(2);
- std::cout << " Table 4: Normalized criterions rates for candidates" << std::endl;
- std::cout << " ____________________________________________________________________________ " << std::endl;
- std::cout << "| Name | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|------------|---------------|---------------|---------------|---------------|" << std::endl;
- for (int i = 0; i < 4; i++)
- {
- std::cout << "| " << cands[i].name;
- for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
- std::cout << " ";
- std::cout << "| ";
- for (int j = 0; j < 4; j++)
- {
- std::cout << norm_crit_rates[i][j] << " | ";
- }
- std::cout << std::endl;
- }
- std::cout << "|____________|_______________|_______________|_______________|_______________|" << std::endl;
- }
- // Table 5: The formation and narrowing of the Pareto set
- void PrintTable5(std::vector<Candidate>& cands, Candidate best_cand)
- {
- std::cout << " Table 5: The formation and narrowing of the Pareto set" << std::endl <<std::endl;
- std::cout << " Utopia point: (10,10)" << std::endl;
- std::cout << " Distance type: Euclidian" << std::endl;
- std::cout << " ____________________________________________________________ " << std::endl;
- std::cout << "| Name | Appearance | Character | Distance |" << std::endl;
- std::cout << "|------------|---------------|---------------|---------------|" << std::endl;
- int criteria_pairs[4][2];
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 2; j++)
- criteria_pairs[i][j] = cands[i].crit_rates[2 + j];
- float distances[4];
- for (int i = 0; i < 4; i++)
- distances[i] = std::sqrt(std::pow(10 - criteria_pairs[i][0], 2) + std::pow(10 - criteria_pairs[i][1], 2));
- float min_distance = *std::min_element(distances, distances+4);
- for (int i = 0; i < 4; i++)
- {
- std::cout << "| " << cands[i].name;
- for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
- std::cout << " ";
- std::cout << "| ";
- std::cout << criteria_pairs[i][0] << " | ";
- std::cout << criteria_pairs[i][1] << " | ";
- if (distances[i] / 10 < 1)
- std::cout << " ";
- std::cout << distances[i] << " |" << std::endl;
- }
- std::cout << "|____________|_______________|_______________|_______________|" << std::endl;
- std::cout << std::endl << "Candidate with minimal distance: " << std::endl;
- std::cout << " - " << best_cand.name << " (Distance: " << min_distance << ")" << std::endl;
- }
- void PrintTable6(std::vector<Candidate> cands)
- {
- std::cout << std::fixed << std::setprecision(2);
- std::cout << " Table 6: Normalized criterions rates for candidates" << std::endl;
- std::cout << " ____________________________________________________________________________ " << std::endl;
- std::cout << "| Name | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|------------|---------------|---------------|---------------|---------------|" << std::endl;
- for (int i = 0; i < 4; i++)
- {
- std::cout << "| " << cands[i].name;
- for (int l = 0; l < 12 - 2 - cands[i].name.size(); l++)
- std::cout << " ";
- std::cout << "| ";
- for (int j = 0; j < 4; j++)
- {
- std::cout << cands[i].norm_crit_rates[j] << " | ";
- }
- std::cout << std::endl;
- }
- std::cout << "|____________|_______________|_______________|_______________|_______________|" << std::endl;
- }
- //Table 7: Expert assessment of criteria (by the method of pairwise comparison)
- void PrintTable7(float expert_rewies[4][4])//
- {
- std::cout << std::fixed << std::setprecision(1);
- std::cout << " Table 7: Expert assessment of criteria (by the method of pairwise comparison)" << std::endl;
- std::cout << " _______________________________________________________________________________ " << std::endl;
- std::cout << "| | Education | Phys. Form | Appearance | Character |" << std::endl;
- std::cout << "|--------------|---------------|---------------|---------------|---------------|" << std::endl;
- std::string Criteria[4] = { "Education", "Phys. Form", "Appearance", "Character" };
- for (int i = 0; i < 4; i++)
- {
- std::cout << "| " << Criteria[i];
- for (int l = 0; l < 12 - 2 -Criteria[i].size(); l++)
- std::cout << " ";
- std::cout << " | ";
- for (int j = 0; j < 4; j++)
- {
- std::cout << expert_rewies[i][j] << " | ";
- }
- std::cout << std::endl;
- }
- std::cout << "|______________|_______________|_______________|_______________|_______________|" << std::endl;
- }
- int main()
- {
- std::vector<Candidate> cands;
- cands.resize(4);
- int criteria_weights[4] = { 2, 4, 8, 6 };
- float norm_criteria_weights[4];
- for (int i = 0; i < 4; i++)
- norm_criteria_weights[i] = (float)criteria_weights[i] / 20;
- // Initialization
- InitializeCands(cands);
- std::cout << std::endl;
- std::cout << " CRITERIA WEIGHTS VECTOR" << std::endl;
- std::cout << line << std::endl << std::endl;
- PrintTable1(criteria_weights, norm_criteria_weights);
- std::cout << std::endl << std::endl << std::endl << std::endl;
- std::cout << " REPLACING CRITERIA WITH CONSTRAINTS METHOD" << std::endl;
- std::cout << line << std::endl << std::endl;
- PrintTable2(cands);
- std::cout << std::endl << std::endl << std::endl << std::endl;
- float norm_crit_rates[4][4];
- float min_allowable_levels[4];
- min_allowable_levels[2] = 0; // main criterion
- const float min_allowable_levels_coeffs[4] = {
- 0.4, // Education
- 0.6, // Physical form
- 1, // Appearance
- 0.8 // Character
- };
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++)
- {
- if (j == 2) // main criterion
- {
- norm_crit_rates[i][j] = cands[i].crit_rates[j];
- continue;
- }
- int min_value = find_min_value(cands, j);
- int max_value = find_max_value(cands, j);
- norm_crit_rates[i][j] = (float)(cands[i].crit_rates[j] - min_value)/(max_value-min_value);
- min_allowable_levels[j] = min_allowable_levels_coeffs[j] * max_value;
- }
- std::vector<Candidate> suitable_cads;
- for (int i = 0; i < 4; i++)
- {
- bool suit_flag = true;
- for (int j = 0; j < 4; j++)
- {
- if (!(cands[i].crit_rates[j] > min_allowable_levels[j]))
- {
- suit_flag = 0;
- break;
- }
- }
- if (suit_flag)
- suitable_cads.push_back(cands[i]);
- }
- std::cout << " REPLACING CRITERIA WITH CONSTRAINTS METHOD" << std::endl;
- std::cout << line << std::endl << std::endl;
- PrintTable3(min_allowable_levels, suitable_cads);
- std::cout << std::endl << std::endl ;
- PrintTable4(cands, norm_crit_rates);
- std::cout << std::endl << std::endl << std::endl << std::endl;
- // Choosed two criteria: appearance (2) and character (3) Weighting and combining criteria
- std::cout << " THE FORMATION AND NARROWING OF THE PARETO SET" << std::endl;
- std::cout << line << std::endl << std::endl;
- Candidate best_cand = *std::min_element(cands.begin(),cands.end(),CompareCandsByDistance);
- PrintTable5(cands, best_cand);
- std::cout << std::endl << std::endl << std::endl << std::endl;
- std::cout << " WEIGHTING AND COMBINING CRITERIA" << std::endl;
- std::cout << line << std::endl << std::endl;
- float column_values_sum = 0;
- for (int j = 0; j < 4; j++) // Normalizing weights
- {
- column_values_sum = 0;
- for (int i = 0; i < 4; i++)
- column_values_sum += cands[i].crit_rates[j];
- for (int i = 0; i < 4; i++)
- cands[i].norm_crit_rates[j] = (float)cands[i].crit_rates[j] / column_values_sum;
- }
- PrintTable6(cands);
- std::cout << std::endl << std::endl;
- float expert_rewies[4][4];
- InitializeExpertRewie(expert_rewies);
- PrintTable7(expert_rewies);
- std::cout << std::endl << std::endl;
- float crit_weights_vector[4] = { 0, 0, 0, 0 };
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (j == i)
- continue;
- crit_weights_vector[i] += expert_rewies[i][j];
- }
- }
- int weights_sum = 0;
- for (int i = 0; i < 4; i++)
- weights_sum += crit_weights_vector[i];
- for (int i = 0; i < 4; i++)
- crit_weights_vector[i] /= weights_sum;
- float integral_assessment[4] = { 0,0,0,0 };
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++)
- integral_assessment[i] += cands[i].norm_crit_rates[j] * crit_weights_vector[j];
- std::cout << std::setprecision(4);
- std::cout << "Integral assesment: ";
- for (int i = 0; i < 4; i++)
- {
- std::cout << integral_assessment[i] << ", ";
- }
- std::cout << std::endl;
- std::cout << "Vladimir - best: ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement