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