|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of a triangular fuzzy set. |
| 4 | + * @details |
| 5 | + * This file provides functions to create and manipulate triangular fuzzy sets, |
| 6 | + * including methods to calculate membership values, union, intersection, |
| 7 | + * complement, and a basic plot representation. |
| 8 | + * |
| 9 | + * By @Shreya123714 |
| 10 | + * |
| 11 | + * Based on: https://en.wikipedia.org/wiki/Fuzzy_set |
| 12 | + */ |
| 13 | + |
| 14 | +#include <assert.h> // for assert |
| 15 | +#include <math.h> //for complex math operations |
| 16 | +#include <stdio.h> // for IO operations |
| 17 | +#include <string.h> // for string operations |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief Struct to represent a triangular fuzzy set. |
| 21 | + */ |
| 22 | +struct triangular_fuzzy_set |
| 23 | +{ |
| 24 | + char name[50]; ///< Name of the fuzzy set |
| 25 | + double left_boundary; ///< Left boundary of the fuzzy set |
| 26 | + double peak; ///< Peak (central) value of the fuzzy set |
| 27 | + double right_boundary; ///< Right boundary of the fuzzy set |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief Creates a triangular fuzzy set. |
| 32 | + * @param name The name of the fuzzy set. |
| 33 | + * @param left_boundary The left boundary of the fuzzy set. |
| 34 | + * @param peak The peak of the fuzzy set. |
| 35 | + * @param right_boundary The right boundary of the fuzzy set. |
| 36 | + * @returns A triangular fuzzy set struct. |
| 37 | + */ |
| 38 | +struct triangular_fuzzy_set create_triangular_fuzzy_set(const char *name, |
| 39 | + double left_boundary, |
| 40 | + double peak, |
| 41 | + double right_boundary) |
| 42 | +{ |
| 43 | + struct triangular_fuzzy_set set; |
| 44 | + strncpy(set.name, name, sizeof(set.name) - 1); |
| 45 | + set.name[sizeof(set.name) - 1] = '\0'; // Ensure null termination |
| 46 | + set.left_boundary = left_boundary; |
| 47 | + set.peak = peak; |
| 48 | + set.right_boundary = right_boundary; |
| 49 | + return set; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Calculates the membership value for a given input. |
| 54 | + * @param set The triangular fuzzy set. |
| 55 | + * @param x The value to evaluate the membership for. |
| 56 | + * @returns The membership value. |
| 57 | + */ |
| 58 | +double membership_value(struct triangular_fuzzy_set set, double x) |
| 59 | +{ |
| 60 | + if (x <= set.left_boundary || x >= set.right_boundary) |
| 61 | + { |
| 62 | + return 0.0; // Out of range |
| 63 | + } |
| 64 | + else if (set.left_boundary < x && x <= set.peak) |
| 65 | + { |
| 66 | + return (x - set.left_boundary) / |
| 67 | + (set.peak - set.left_boundary); // Increasing slope |
| 68 | + } |
| 69 | + else if (set.peak < x && x < set.right_boundary) |
| 70 | + { |
| 71 | + return (set.right_boundary - x) / |
| 72 | + (set.right_boundary - set.peak); // Decreasing slope |
| 73 | + } |
| 74 | + return 0.0; // Should not reach here |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Calculates the complement (negation) of this fuzzy set. |
| 79 | + * @param set The triangular fuzzy set. |
| 80 | + * @returns A new triangular fuzzy set representing the complement. |
| 81 | + */ |
| 82 | +struct triangular_fuzzy_set complement(struct triangular_fuzzy_set set) |
| 83 | +{ |
| 84 | + return create_triangular_fuzzy_set(set.name, 1.0 - set.right_boundary, |
| 85 | + 1.0 - set.left_boundary, 1.0 - set.peak); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Calculates the intersection of this fuzzy set with another fuzzy set. |
| 90 | + * @param set The triangular fuzzy set. |
| 91 | + * @param other The other triangular fuzzy set to intersect with. |
| 92 | + * @returns A new triangular fuzzy set representing the intersection. |
| 93 | + */ |
| 94 | +struct triangular_fuzzy_set intersection(struct triangular_fuzzy_set set, |
| 95 | + struct triangular_fuzzy_set other) |
| 96 | +{ |
| 97 | + return create_triangular_fuzzy_set( |
| 98 | + "Intersection", fmax(set.left_boundary, other.left_boundary), |
| 99 | + fmin(set.right_boundary, other.right_boundary), |
| 100 | + (set.peak + other.peak) / 2); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Calculates the union of this fuzzy set with another fuzzy set. |
| 105 | + * @param set The triangular fuzzy set. |
| 106 | + * @param other The other triangular fuzzy set to union with. |
| 107 | + * @returns A new triangular fuzzy set representing the union. |
| 108 | + */ |
| 109 | +struct triangular_fuzzy_set union_sets(struct triangular_fuzzy_set set, |
| 110 | + struct triangular_fuzzy_set other) |
| 111 | +{ |
| 112 | + return create_triangular_fuzzy_set( |
| 113 | + "Union", fmin(set.left_boundary, other.left_boundary), |
| 114 | + fmax(set.right_boundary, other.right_boundary), |
| 115 | + (set.peak + other.peak) / 2); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief Prints the details of the triangular fuzzy set. |
| 120 | + * @param set The triangular fuzzy set. |
| 121 | + */ |
| 122 | +void print_triangular_fuzzy_set(struct triangular_fuzzy_set set) |
| 123 | +{ |
| 124 | + printf("Triangular Fuzzy Set '%s': [%f, %f, %f]\n", set.name, |
| 125 | + set.left_boundary, set.peak, set.right_boundary); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief Basic plotting of the membership function of the fuzzy set. |
| 130 | + * This is a textual representation, as actual graphing is more complex in C. |
| 131 | + * |
| 132 | + * @param set The triangular fuzzy set to plot. |
| 133 | + */ |
| 134 | +void plot_triangular_fuzzy_set(struct triangular_fuzzy_set set) |
| 135 | +{ |
| 136 | + printf("Membership function of %s:\n", set.name); |
| 137 | + for (double x = 0; x <= 1.0; x += 0.1) |
| 138 | + { |
| 139 | + double membership = membership_value(set, x); |
| 140 | + printf("x: %0.1f | Membership: %0.2f | ", x, membership); |
| 141 | + for (int i = 0; i < (int)(membership * 50); i++) |
| 142 | + { // Scale for better visibility |
| 143 | + printf("*"); |
| 144 | + } |
| 145 | + printf("\n"); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * @brief Self-test implementations. |
| 151 | + * @returns void |
| 152 | + */ |
| 153 | +static void tests() |
| 154 | +{ |
| 155 | + struct triangular_fuzzy_set a = |
| 156 | + create_triangular_fuzzy_set("A", 0.0, 0.5, 1.0); |
| 157 | + struct triangular_fuzzy_set b = |
| 158 | + create_triangular_fuzzy_set("B", 0.2, 0.7, 1.0); |
| 159 | + |
| 160 | + // Test membership values |
| 161 | + assert(membership_value(a, 0.1) == 0.2); |
| 162 | + assert(membership_value(a, 0.5) == 1.0); |
| 163 | + assert(membership_value(a, 0.8) == 0.4); |
| 164 | + assert(membership_value(b, 0.6) == 0.8); |
| 165 | + |
| 166 | + // Test union |
| 167 | + struct triangular_fuzzy_set union_ab = union_sets(a, b); |
| 168 | + assert(union_ab.left_boundary == 0.0); |
| 169 | + assert(union_ab.right_boundary == 1.0); |
| 170 | + |
| 171 | + // Test intersection |
| 172 | + struct triangular_fuzzy_set intersection_ab = intersection(a, b); |
| 173 | + assert(intersection_ab.left_boundary == 0.2); |
| 174 | + |
| 175 | + // Test complement |
| 176 | + struct triangular_fuzzy_set complement_a = complement(a); |
| 177 | + assert(complement_a.left_boundary == 0.0); |
| 178 | + |
| 179 | + printf("All tests have successfully passed!\n"); |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * @brief Main function. |
| 184 | + * @returns 0 on exit. |
| 185 | + */ |
| 186 | +int main() |
| 187 | +{ |
| 188 | + tests(); // Run self-test implementations |
| 189 | + |
| 190 | + struct triangular_fuzzy_set a = |
| 191 | + create_triangular_fuzzy_set("A", 0.0, 0.5, 1.0); |
| 192 | + struct triangular_fuzzy_set b = |
| 193 | + create_triangular_fuzzy_set("B", 0.2, 0.7, 1.0); |
| 194 | + |
| 195 | + print_triangular_fuzzy_set(a); |
| 196 | + print_triangular_fuzzy_set(b); |
| 197 | + |
| 198 | + plot_triangular_fuzzy_set(a); |
| 199 | + plot_triangular_fuzzy_set(b); |
| 200 | + |
| 201 | + struct triangular_fuzzy_set union_ab = union_sets(a, b); |
| 202 | + struct triangular_fuzzy_set intersection_ab = intersection(a, b); |
| 203 | + struct triangular_fuzzy_set complement_a = complement(a); |
| 204 | + |
| 205 | + print_triangular_fuzzy_set(union_ab); |
| 206 | + print_triangular_fuzzy_set(intersection_ab); |
| 207 | + print_triangular_fuzzy_set(complement_a); |
| 208 | + |
| 209 | + plot_triangular_fuzzy_set(union_ab); |
| 210 | + plot_triangular_fuzzy_set(intersection_ab); |
| 211 | + plot_triangular_fuzzy_set(complement_a); |
| 212 | + |
| 213 | + return 0; |
| 214 | +} |
0 commit comments