Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. enum scales
  5. {
  6. LOW_DEFENSE = -3,
  7. MEDIUM_DEFENSE = -2,
  8. HIGH_DEFENSE = -1,
  9. LOW_ATTACK = 1,
  10. MEDIUM_ATTACK = 2,
  11. HIGH_ATTACK = 3
  12. };
  13.  
  14. typedef struct{
  15. int enemies_our_side;
  16. int allies_our_side;
  17. int our_finalizations;
  18. int their_finalizations;
  19. double our_ball_possession;
  20. double their_ball_possession;
  21.  
  22. double distance; // for knn
  23. int agressive_scale;
  24. } Point;
  25.  
  26. bool cmp(Point a, Point b){
  27. return (a.distance < b.distance);
  28. }
  29.  
  30. int classify(Point dataset[], int n, int k, Point p){
  31. double sigma = 2.0;
  32. // calculate distance from p to all dataset
  33. for(int i = 0; i < n; i++){
  34. int dif = 0;
  35. dif += pow(abs(p.enemies_our_side - dataset[i].enemies_our_side), sigma);
  36. dif += pow(abs(p.allies_our_side - dataset[i].allies_our_side), sigma);
  37. dif += pow(abs(p.our_finalizations - dataset[i].our_finalizations), sigma);
  38. dif += pow(abs(p.their_finalizations - dataset[i].their_finalizations), sigma);
  39. dif += pow(abs(p.our_ball_possession - dataset[i].our_ball_possession), sigma);
  40. dif += pow(abs(p.their_ball_possession - dataset[i].their_ball_possession), sigma);
  41. dataset[i].distance = pow(dif, 1.0/sigma); // mintowski distance
  42. }
  43.  
  44. sort(dataset, dataset + n, cmp); // sort based in distance
  45.  
  46. int freq[7] = {0}; // we have 6 groups (in enum)
  47. int best = 0; // looking for the best scale
  48.  
  49. for(int i = 0; i < k; i++){ // look for the first k elements after sort
  50. freq[dataset[i].agressive_scale + 3]++; // +3 because offset (low defense starts with -3)
  51. if(freq[dataset[i].agressive_scale + 3] > best){
  52. best = dataset[i].agressive_scale; // if have a best scale, set it on best variable
  53. }
  54. }
  55.  
  56. return best; // return best
  57. }
  58.  
  59. void printAttributes(Point a, bool isDataset){
  60. cout << "Allies our side: " << a.allies_our_side << endl;
  61. cout << "Enemies our side: " << a.enemies_our_side << endl;
  62. cout << "Our ball possession: " << a.our_ball_possession << endl;
  63. cout << "Their ball possession: " << a.their_ball_possession << endl;
  64. cout << "Our finalizations: " << a.our_finalizations << endl;
  65. cout << "Their finalizations: " << a.their_finalizations << endl;
  66. if(isDataset){
  67. cout << "Distance: " << a.distance << endl;
  68. cout << "Scale: " << a.agressive_scale << endl;
  69. }
  70. cout << endl;
  71. }
  72.  
  73. int main(){
  74. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // rand gen
  75.  
  76. int n = 6; // Number of data points
  77. int k = 3; // parameter k (k firsts will be analyzed)
  78. Point arr[n];
  79.  
  80. // generating randomic points
  81. for(int i = 0; i < n; i++){
  82. arr[i].allies_our_side = rng() % 6;
  83. arr[i].enemies_our_side = rng() % 6;
  84. arr[i].our_ball_possession = rng() % 100;
  85. arr[i].their_ball_possession = abs(100 - arr[i].our_ball_possession);
  86. arr[i].our_finalizations = rng() % 10;
  87. arr[i].their_finalizations = rng() % 10;
  88. }
  89.  
  90. // setting the scales to test
  91. arr[0].agressive_scale = LOW_DEFENSE;
  92. arr[1].agressive_scale = MEDIUM_DEFENSE;
  93. arr[2].agressive_scale = HIGH_DEFENSE;
  94. arr[3].agressive_scale = LOW_ATTACK;
  95. arr[4].agressive_scale = MEDIUM_ATTACK;
  96. arr[5].agressive_scale = HIGH_ATTACK;
  97.  
  98. // generating a random configuration to test
  99. Point randomPoint;
  100. randomPoint.allies_our_side = rng() % 6;
  101. randomPoint.enemies_our_side = rng() % 6;
  102. randomPoint.our_ball_possession = rng() % 100;
  103. randomPoint.their_ball_possession = abs(100 -randomPoint.our_ball_possession);
  104. randomPoint.our_finalizations = rng() % 10;
  105. randomPoint.their_finalizations = rng() % 10;
  106.  
  107. int classification = classify(arr, n, k, randomPoint);
  108.  
  109. // printing for debug
  110. for(int i = 0; i < n; i++){
  111. cout << "Dataset id " << i << endl;
  112. printAttributes(arr[i], true);
  113. }
  114. cout << "Random point" << endl;
  115. printAttributes(randomPoint, false);
  116.  
  117. cout << "Classified for: " << classification << endl;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement