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. double output_value;
  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 defuzzify(rule rules[], double input, int num_rules) {
  20. double weighted_sum = 0;
  21. double weight_sum = 0;
  22.  
  23. for (int i = 0; i < num_rules; i++) {
  24.  
  25. double membership_degree = membership(input, rules[i].input_set);
  26.  
  27. weighted_sum += membership_degree * rules[i].output_value;
  28.  
  29. weight_sum += membership_degree;
  30. }
  31.  
  32. return (weight_sum != 0) ? (weighted_sum / weight_sum) : 0;
  33. }
  34.  
  35. int main() {
  36.  
  37. rule rules[] = {
  38. {{0, 5}, 10},
  39. {{3, 7}, 20},
  40. {{6, 10}, 30}
  41. };
  42. int num_rules = sizeof(rules) / sizeof(rules[0]);
  43.  
  44. double test_input[] = {2.0, 4.0, 6.0, 8.0};
  45. int num_inputs = sizeof(test_input) / sizeof(test_input[0]);
  46.  
  47. printf("Вхід | Вихід (дефаззифікація)\n");
  48.  
  49. for (int i = 0; i < num_inputs; i++) {
  50. double output = defuzzify(rules, test_input[i], num_rules);
  51. printf("%.1f | %.2f\n", test_input[i], output);
  52. }
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Вхід | Вихід (дефаззифікація)
2.0   | 10.00
4.0   | 12.38
6.0   | 14.29
8.0   | 18.00