def fuzzy_system(generation_val, convergence_val): # just to ensure that value excceding 1 will be taken care by the code. if (convergence_val > 1): convergence_val = 0.99 x_generation = np.arange(0, 1500, 50) x_convergence = np.arange(0, 1, 0.1) x_recombinationRate = np.arange(0, 0.5, 0.1) # Generate fuzzy membership functions generation_lo = fuzz.trapmf(x_generation, [0, 0, 200, 300]) generation_md = fuzz.trimf(x_generation, [290, 550, 700]) generation_hi = fuzz.trapmf(x_generation, [690, 700, 1500, 1500]) convergence_lo = fuzz.trapmf(x_convergence, [0, 0, 0.2, 0.3]) convergence_md = fuzz.trapmf(x_convergence, [0.25, 0.4, 0.6, 0.75]) convergence_hi = fuzz.trapmf(x_convergence, [0.7, 0.8, 1, 1]) recom_lo = fuzz.trapmf(x_recombinationRate, [0, 0, 0.3, 0.4]) recom_md = fuzz.trapmf(x_recombinationRate, [0.35, 0.5, 0.5, 0.7]) recom_hi = fuzz.trapmf(x_recombinationRate, [0.7, 0.9, 1, 1]) generation_level_lo = fuzz.interp_membership(x_generation, generation_lo, generation_val) generation_level_md = fuzz.interp_membership(x_generation, generation_md, generation_val) generation_level_hi = fuzz.interp_membership(x_generation, generation_hi, generation_val) convergence_level_lo = fuzz.interp_membership(x_convergence, convergence_lo, convergence_val) convergence_level_md = fuzz.interp_membership(x_convergence, convergence_md, convergence_val) convergence_level_hi = fuzz.interp_membership(x_convergence, convergence_hi, convergence_val) # if generation level is high, recombination is low, do nothing to explore rate_activation_lo = np.fmin(generation_level_hi, recom_lo) # if generation is medium and convergence is low, recombination is medium: Try to boost the exploration active_rule1 = np.fmin(generation_level_md, fuzz.fuzzy_not(convergence_level_lo)) rate_activation_md = np.fmin(active_rule1, recom_md) # if generation is low or medium and convergence is high, give high recombination active_rule2 = np.fmax(fuzz.fuzzy_not(generation_level_hi), convergence_level_hi) rate_activation_hi = np.fmin(active_rule2, recom_hi) aggregated = np.fmax(rate_activation_lo, np.fmax(rate_activation_md, rate_activation_hi)) try: recom_rate = fuzz.defuzz(x_recombinationRate, aggregated, 'centroid') except: # if the member function is empty, just set the value to default low only recom_rate = 0.25 return recom_rate
# Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). abc1 = [0, 25, 50] abc2 = [25, 50, 75] young = fuzz.membership.trimf(X, abc1) middle_aged = fuzz.membership.trimf(X, abc2) # Compute the different operations using inbuilt functions. one = np.ones(75) zero = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) union = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) complement_a = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] alg_sum = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) alg_product = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot().
delta = 0.001 start = 0 stop = 10 + delta step = 0.5 x = np.arange(start, stop + delta, step) # Triangular membership function x1 = np.arange(0, 5 + delta, step) trimf = fuzz.trimf(x1, [0, 2.5, 5]) # Trapezoidal membership function x2 = np.arange(4, 10 + delta, step) trapmf = fuzz.trapmf(x2, [4, 6, 8, 10]) # fuzzy logic tri_not = fuzz.fuzzy_not(trimf) trap_not = fuzz.fuzzy_not(trapmf) x3, tri_trap_and = fuzz.fuzzy_and(x1, trimf, x2, trapmf) x3, tri_trap_or = fuzz.fuzzy_or(x1, trimf, x2, trapmf) # Defuzzify centroid_x = fuzz.defuzz(x3, tri_trap_or, "centroid") centroid_y = fuzz.interp_membership(x3, tri_trap_or, centroid_x) bisector_x = fuzz.defuzz(x3, tri_trap_or, "bisector") bisector_y = fuzz.interp_membership(x3, tri_trap_or, bisector_x) mom_x = fuzz.defuzz(x3, tri_trap_or, "mom") mom_y = fuzz.interp_membership(x3, tri_trap_or, mom_x) som_x = fuzz.defuzz(x3, tri_trap_or, "som") som_y = fuzz.interp_membership(x3, tri_trap_or, som_x) lom_x = fuzz.defuzz(x3, tri_trap_or, "lom") lom_y = fuzz.interp_membership(x3, tri_trap_or, lom_x)