fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. double low;
  5. double high;
  6. } fuzzy_set;
  7.  
  8. typedef struct {
  9. fuzzy_set input_set;
  10. fuzzy_set output_set;
  11. } rule;
  12.  
  13. double membership(double value, fuzzy_set fs) {
  14. if (value <= fs.low) return 0;
  15. if (value >= fs.high) return 1;
  16. return (value - fs.low) / (fs.high - fs.low);
  17. }
  18.  
  19. double apply_rules(rule rules[], double input, int num_rules) {
  20. double numerator = 0;
  21. double denominator = 0;
  22.  
  23. for (int i = 0; i < num_rules; i++) {
  24.  
  25. double membership_degree = membership(input, rules[i].input_set);
  26.  
  27. double output_center = (rules[i].output_set.low + rules[i].output_set.high) / 2;
  28.  
  29. numerator += membership_degree * output_center;
  30.  
  31. denominator += membership_degree;
  32. }
  33.  
  34. return (denominator != 0) ? (numerator / denominator) : 0;
  35. }
  36.  
  37. int main() {
  38. rule rules[] = {
  39. {{0, 5}, {10, 20}},
  40. {{3, 7}, {20, 30}},
  41. {{6, 10}, {30, 40}}
  42. };
  43. int num_rules = sizeof(rules) / sizeof(rules[0]);
  44.  
  45. double test_input[] = {2.0, 4.0, 6.0, 8.0};
  46. int num_inputs = sizeof(test_input) / sizeof(test_input[0]);
  47.  
  48. printf("Вхід | Вихід (дефаззифікація)\n");
  49.  
  50. for (int i = 0; i < num_inputs; i++) {
  51. double output = apply_rules(rules, test_input[i], num_rules);
  52. printf("%.1f | %.2f\n", test_input[i], output);
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Вхід | Вихід (дефаззифікація)
2.0   | 15.00
4.0   | 17.38
6.0   | 19.29
8.0   | 23.00