【人工智能】模糊隶属函数

理论部分
【人工智能】模糊推理

矩形模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : RectangleMembershipFunction  //类名
 * @Description : 矩形模糊隶属函数  //描述
 * @Author : 主教 //作者
 * @Date: 2023/4/14  9:12
 */
public class RectangleMembershipFunction {
    
    
    private double a;
    private double b;

    public RectangleMembershipFunction(double a, double b) {
    
    
        this.a = a;
        this.b = b;
    }

    public double compute(double x) {
    
    
        if (x >= a && x <= b) {
    
    
            return 1;
        } else {
    
    
            return 0;
        }
    }
    public Function<Double, Double> createRectangleMembershipFunction(){
    
    
        return this::compute;
    }

}

梯形模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : TrapezoidMembershipFunction  //类名
 * @Description : 梯型模糊隶属函数  //描述
 * @Author : 主教 //作者
 * @Date: 2023/4/14  9:09
 */
public class TrapezoidMembershipFunction {
    
    
    private double a;
    private double b;
    private double c;
    private double d;

    public TrapezoidMembershipFunction(double a, double b, double c, double d) {
    
    
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public double compute(double x) {
    
    
        if (x < a || x > d) {
    
    
            return 0;
        } else if (x >= b && x <= c) {
    
    
            return 1;
        } else if (x < b) {
    
    
            return (x - a) / (b - a);
        } else {
    
    
            return (d - x) / (d - c);
        }
    }
    public Function<Double, Double> createTrapezoidMembershipFunction(){
    
    
        return this::compute;
    }

}

k次抛物型模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : ParabolicMembershipFunction  //类名
 * @Description : k次抛物型模糊隶属函数  //描述
 * @Author : 主教 //作者
 * @Date: 2023/4/14  9:17
 */
public class ParabolicMembershipFunction {
    
    
    private double a;
    private double b;
    private double c;
    private double d;
    private int k;

    public ParabolicMembershipFunction(double a, double b, double c, double d, int k) {
    
    
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.k = k;
    }

    public double compute(double x) {
    
    
        if (x <= a || x >= d) {
    
    
            return 0;
        } else if (x > a && x <= b) {
    
    
            return Math.pow((x - a) / (b - a), k);
        } else if (x > b && x <= c) {
    
    
            return 1;
        } else {
    
    
            return Math.pow((d - x) / (d - c), k);
        }
    }
    public Function<Double, Double> createParabolicMembershipFunction(){
    
    
        return this::compute;
    }

}

T型模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : TMembershipFunction  //类名
 * @Description : T型模糊隶属函数  //描述
 * @Author : 86138 //作者
 * @Date: 2023/4/14  9:25
 */
public class TMembershipFunction {
    
    
    private double a;
    private double m;
    private double n;

    public TMembershipFunction(double a, double m, double n) {
    
    
        this.a = a;
        this.m = m;
        this.n = n;
    }

    public double compute(double x) {
    
    
        if (x <= a) {
    
    
            return 0;
        } else if (x > a && x <= m) {
    
    
            return (x - a) / (m - a);
        } else if (x >= m && x < n) {
    
    
            return 1;
        } else {
    
    
            return (n - x) / (n - m);
        }
    }
    public Function<Double, Double> createTMembershipFunction(){
    
    
        return this::compute;
    }
}

正态型模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : GaussianMembershipFunction  //类名
 * @Description : 正态型模糊隶属函数  //描述
 * @Author : 86138 //作者
 * @Date: 2023/4/14  9:27
 */
public class GaussianMembershipFunction {
    
    
    private double mu;
    private double sigma;

    public GaussianMembershipFunction(double mu, double sigma) {
    
    
        this.mu = mu;
        this.sigma = sigma;
    }

    public double compute(double x) {
    
    
        return (1 / (sigma * Math.sqrt(2 * Math.PI))) * Math.exp(-((x - mu) * (x - mu)) / (2 * sigma * sigma));
    }
    public Function<Double, Double> createGaussianMembershipFunction(){
    
    
        return this::compute;
    }
}

柯西型模糊隶属函数

import java.util.function.Function;

/**
 * @ClassName : CauchyMembershipFunction  //类名
 * @Description : 柯西型模糊隶属函数  //描述
 * @Author : 86138 //作者
 * @Date: 2023/4/14  9:29
 */
public class CauchyMembershipFunction {
    
    
    private double x0;
    private double gamma;

    public CauchyMembershipFunction(double x0, double gamma) {
    
    
        this.x0 = x0;
        this.gamma = gamma;
    }

    public double compute(double x) {
    
    
        return (1 / Math.PI) * (gamma / ((x - x0) * (x - x0) + gamma * gamma));
    }
    public Function<Double, Double> createCauchyMembershipFunction(){
    
    
        return this::compute;
    }
}

使用方法

第一种 直接使用:Function.compute()

Function f = new Function(...props);
double membership = f.compute(x);

第二种 搭配模糊变量定义函数:createGaussianMembershipFunction()

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @ClassName : FuzzyVariable  //类名
 * @Description : 隶属函数  //描述
 * @Author : 86138 //作者
 * @Date: 2023/4/12  15:58
 */
public class FuzzyVariable {
    
    
    private String name;    //模糊变量名称
    private Map<String, Function<Double, Double>> terms;  // 术语和隶属度函数

    public FuzzyVariable(String name) {
    
    
        this.terms = new HashMap<>();
        this.name = name;
    }
    // 向变量中添加新的术语及其隶属度函数
    public void addTerm(String trem, Function<Double,Double> membershipFunc){
    
    
        this.terms.put(trem,membershipFunc);
    }
    // 输入值模糊化
    public Map<String, Double> fuzzify(double value){
    
    
        Map<String, Double> memberships = new HashMap<>();
        for(Map.Entry<String, Function<Double,Double>> entry : terms.entrySet()){
    
    
            String trem = entry.getKey();
            Function<Double, Double> membershipFunc = entry.getValue();
            double membership = membershipFunc.apply(value);
            memberships.put(trem,membership);
        }
        return memberships;
    }

    public String getName() {
    
    
        return name;
    }

    public Map<String, Function<Double, Double>> getTerms() {
    
    
        return terms;
    }

    @Override
    public String toString() {
    
    
        return "FuzzyVariable{" +
                "name='" + name + '\'' +
                ", terms=" + terms +
                '}';
    }
}

使用示例Test:

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : test  //类名
 * @Description : 测试类  //描述
 * @Author : 86138 //作者
 * @Date: 2023/4/12  16:32
 */
public class test {
    
    
    public static void main(String[] args) {
    
    
        FuzzyVariable temperature_fuzzy_variable = new FuzzyVariable("气温");
        temperature_fuzzy_variable.addTerm("温度低",new TriangularMembershipFunction(0.0,0.0,40.0).createTriangularFunction());
        temperature_fuzzy_variable.addTerm("温度适中",new TriangularMembershipFunction(20.0,50.0,80.0).createTriangularFunction());
        temperature_fuzzy_variable.addTerm("温度高",new TriangularMembershipFunction(60.0,100.0,100.0).createTriangularFunction());


        FuzzyVariable humidity_fuzzy_variable = new FuzzyVariable("湿度");
        humidity_fuzzy_variable.addTerm("湿度低",new TriangularMembershipFunction(0.0,0.0,25.0).createTriangularFunction());
        humidity_fuzzy_variable.addTerm("湿度适中",new TriangularMembershipFunction(15.0,30.0,45.0).createTriangularFunction());
        humidity_fuzzy_variable.addTerm("湿度高",new TriangularMembershipFunction(35.0,60.0,60.0).createTriangularFunction());



        Map<String, Double> a = temperature_fuzzy_variable.fuzzify(64);
        Map<String, Double> b = humidity_fuzzy_variable.fuzzify(22);
        System.out.println(a);
        System.out.println(b);

        Map<String, Double> result = new HashMap<>();

        for (Map.Entry<String, Double> entryA : a.entrySet()) {
    
    
            for (Map.Entry<String, Double> entryB : b.entrySet()) {
    
    
                String key = entryA.getKey() + "AND" + entryB.getKey();
                double value = Math.min(entryA.getValue(),entryB.getValue());
                result.put(key, value);
            }
        }
        System.out.println(result);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_25218219/article/details/130146092