如何运行模糊控制语言

时间:2017-04-10 15:38:14

标签: fuzzy-logic fuzzy

我想了解模糊控制语言,然后我们如何使用jfuzzy库在Java应用程序中使用它。 如何运行FCL文件? 此致

1 个答案:

答案 0 :(得分:0)

前段时间我写了一个系统,可以帮助患者根据他们在急诊室出现的症状进行分类。我将在这里提供它的片段,希望它能帮助你开始。

这都假定您已在项目的lib文件夹中安装了jFuzzyLogic工具箱。

您特别询问FCL文件。我把它们放在我的主项目文件夹中,一个这样的文件看起来像这样

// Block definition (there may be more than one block per file)
FUNCTION_BLOCK back_pain
// Define input variables
VAR_INPUT
    airway : REAL;
    oxygenation : REAL;
    sweating: REAL;
    color: REAL;
    heart_rate: REAL;
    blood_pressure: REAL;
    conscious_level: REAL;
    abdominal_pain: REAL;
    pain: REAL;
    mechanism_of_injury: REAL;
    life_stage: REAL;
    direct_trauma: REAL;
    skin_temperature: REAL;
    neurological_deficit: REAL;
    unable_to_walk: REAL;
    colicky_pain: REAL;
    onset: REAL;
    untruthful_story: REAL;
    onset_of_symptoms: REAL;
END_VAR

// Define output variable
VAR_OUTPUT
    situation : REAL;
END_VAR

//Fuzzify input variables
FUZZIFY airway
    TERM not_maintained := 0; 
    TERM maintained := 1;
END_FUZZIFY

FUZZIFY oxygenation
TERM inadequate := trape 0 0 21 36;
TERM adequate_but_very_low := trape 20 35 41 56;
TERM adequate_but_low := trape 40 55 61 76;
TERM adequate := trape 60 78 100 100;
END_FUZZIFY

FUZZIFY sweating
TERM little_to_none := trape 0 0 21 36;
TERM some := trape 20 35 41 56;
TERM significant := trape 40 55 61 76;
TERM exceptional := trape 60 75 100 100;
END_FUZZIFY

FUZZIFY color
TERM pale := trape 0 0 27 45;
TERM somewhat_lacking := trape 30 48 57 75;
TERM full := trape 60 78 100 100;
END_FUZZIFY

FUZZIFY heart_rate
TERM light := trape 0 0 21 36;
TERM moderate := trape 20 35 41 56;
TERM vigorous := trape 40 55 61 76;
TERM very_vigorous := trape 60 75 100 100;
END_FUZZIFY

FUZZIFY blood_pressure
TERM low := trape 0 0 27 45;
TERM normal := trape 30 48 57 75;
TERM high_normal := trape 60 78 100 100;
END_FUZZIFY

FUZZIFY conscious_level
TERM unresponsive := trape 0 0 21 36;
TERM reduced := trape 20 35 41 56;
TERM slightly_reduced := trape 40 55 61 76;
TERM normal := trape 60 75 100 100;
END_FUZZIFY

FUZZIFY life_stage
TERM infant := trape 0 0 21 31;
TERM toddler := trape 17 28 34 45;
TERM child := trape 32 44 50 62;
TERM adult := trape 60 70 100 100;
END_FUZZIFY

FUZZIFY mechanism_of_injury
TERM minor := trape 0 0 27 45;
TERM moderate := trape 56 77 80 101;
TERM significant := trape 60 81 100 100;
END_FUZZIFY

FUZZIFY pain
TERM little_to_none := trape 0 0 21 36;
TERM mild := trape 20 38 38 56;
TERM moderate := trape 40 58 58 76;
TERM severe := trape 60 78 100 100;
END_FUZZIFY

FUZZIFY abdominal_pain
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY direct_trauma
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY neurological_deficit
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY onset
TERM distant := trape 0 0 17 23;
TERM recent := trape 17 25 30 38;
TERM acute := trape 32 39 44 52;
TERM rapid := trape 43 49 54 60;
TERM abrupt := trape 60 73 100 100;
END_FUZZIFY

FUZZIFY unable_to_walk
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY skin_temperature
TERM cold := trape 0 0 15 30;
TERM normal := trape 24 34 45 55;
TERM warm := trape 39 49 60 70;
TERM hot := trape 53 65 70 83;
TERM very_hot := trape 60 72 100 100;
END_FUZZIFY

FUZZIFY untruthful_story
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY colicky_pain
    TERM not_present := 0;
    TERM present := 1;
END_FUZZIFY

FUZZIFY onset_of_symptoms
TERM distant := trape 0 0 17 23;
TERM recent := trape 17 25 30 38;
TERM acute := trape 32 39 44 52;
TERM rapid := trape 43 49 54 60;
TERM abrupt := trape 60 73 100 100;
END_FUZZIFY

// Defuzzify output variable 'situation'
DEFUZZIFY situation
    TERM immediate := trape 0 0 2.5 5;
    TERM very_urgent := trian 3 6.5 10;
    TERM urgent := trian 8 34 60;
    TERM standard := trian 50 85 120;
    TERM non_urgent := trape 100 170 240 240;
    // Use 'Right-most Maximum' defuzzification method
    METHOD : RM;
    // Default value is 240 (if no rule activates defuzzifier)
    DEFAULT := 240;
END_DEFUZZIFY

RULEBLOCK No1
    // Use 'min' for 'and' (also implicit use 'max'
    // for 'or' to fulfill DeMorgan's Law)
    AND : MIN;
    // Use 'min' activation method
    ACT : MIN;
    // Use 'max' accumulation method
    ACCU : MAX;

    RULE 1 : IF airway IS NOT maintained 
                THEN situation IS immediate WITH 1.0;

    RULE 2 : IF oxygenation IS inadequate 
                THEN situation IS immediate WITH 1.0; 

    RULE 3 : IF (sweating IS significant OR sweating IS exceptional)
             AND (color IS pale)
             AND (heart_rate IS vigorous OR heart_rate IS very_vigorous)
             AND (blood_pressure IS low)
             AND (conscious_level IS reduced OR conscious_level IS unresponsive) 
                THEN situation IS immediate WITH 1.0;
    RULE 4 : IF pain IS severe
                THEN situation IS very_urgent WITH 0.8;
    RULE 5 : IF mechanism_of_injury IS significant
                THEN situation IS very_urgent WITH 0.8;
    RULE 6 : IF neurological_deficit IS present 
             AND (onset IS abrupt OR onset IS rapid OR onset IS acute)
                THEN situation IS very_urgent WITH 0.8;
    RULE 7 : IF (life_stage IS infant OR life_stage IS toddler OR life_stage IS child)
             AND (skin_temperature IS hot OR skin_temperature IS very_hot)
                THEN situation IS very_urgent WITH 0.8;
    RULE 8 : IF life_stage IS adult
             AND skin_temperature IS very_hot
                THEN situation IS very_urgent WITH 0.8;
    RULE 9 : IF abdominal_pain IS present
                THEN situation IS very_urgent WITH 0.8;
    RULE 10 : IF neurological_deficit IS present
              AND onset IS recent
                THEN situation IS urgent WITH 0.6;
    RULE 11 : IF untruthful_story IS present
                THEN situation IS urgent WITH 0.6;
    RULE 12 : IF unable_to_walk IS present
                THEN situation IS urgent WITH 0.6;
    RULE 13 : IF skin_temperature IS hot 
              AND life_stage IS adult
                THEN situation IS urgent WITH 0.6;
    RULE 14 : IF pain IS moderate
                THEN situation IS urgent WITH 0.6;
    RULE 15 : IF colicky_pain IS present
                THEN situation IS urgent WITH 0.6;
    RULE 16 : IF direct_trauma IS present
                THEN situation IS urgent WITH 0.6;  
    RULE 17 : IF pain IS mild
                THEN situation IS standard WITH 0.4;
    RULE 18 : IF onset_of_symptoms IS recent
                THEN situation IS standard WITH 0.4;
END_RULEBLOCK

END_FUNCTION_BLOCK

这是定义模糊术语的地方,以及它们的隶属函数(我使用了梯形隶属函数)和模糊规则。

在我的src文件夹中,我存储了使用这种FCL文件的java文件。这是一个例子

package com.project.triage;

import net.sourceforge.jFuzzyLogic.FIS;

/**
 * Test parsing an FCL file
 * @author pcingola@users.sourceforge.net
 */
public class FuzzyChestPain {
    double waitingTime;
    public FuzzyChestPain(boolean aw, int oxy, 
            int sweat, int col, int hr, int bp, int cl,
            int pain, boolean cardiac_pain, 
            boolean shortness_of_breath, 
            boolean irregular_rhythm, boolean pleuritic_pain,
            int history_of_cardiac, int onset_of_symptoms,
            int onset, int vomiting_no_blood) {
        // Load from 'FCL' file
        String fileName = "chest_pain.fcl";
        FIS fis = FIS.load(fileName,true);
        // Error while loading?
        if( fis == null ) { 
            System.err.println("Can't load file: '" 
                                   + fileName + "'");
            return;
        }

        // Show 
        //fis.chart();

        // Set inputs
        fis.setVariable("airway", convertBoolean(!aw));
        fis.setVariable("oxygenation", oxy);
        fis.setVariable("sweating", sweat);
        fis.setVariable("color", col);
        fis.setVariable("heart_rate", hr);
        fis.setVariable("blood_pressure", bp);
        fis.setVariable("conscious_level", cl);
        fis.setVariable("pain", pain);
        fis.setVariable("cardiac_pain", convertBoolean(cardiac_pain));
        fis.setVariable("shortness_of_breath", convertBoolean(shortness_of_breath));
        fis.setVariable("irregular_rhythm", convertBoolean(irregular_rhythm));
        fis.setVariable("pleuritic_pain", convertBoolean(pleuritic_pain));
        fis.setVariable("history_of_cardiac", history_of_cardiac);
        fis.setVariable("onset_of_symptoms", onset_of_symptoms);
        fis.setVariable("onset", onset);
        fis.setVariable("vomiting_no_blood", vomiting_no_blood);

       // Evaluate
        fis.evaluate();

        // Show output variable's chart 
        fis.getVariable("situation").chartDefuzzifier(true);
        //System.out.println("Patient should wait no longer than " + fis.getVariable("situation").getLatestDefuzzifiedValue());
        // Print ruleSet
        //System.out.println(fis);
        setWaitingTime(fis.getVariable("situation").getLatestDefuzzifiedValue());
    }

    private void setWaitingTime(double time) {
        waitingTime = time;
    }

    public double getWaitingTime() {
        return waitingTime;
    }



    //Convert boolean variables into 1s and 0s
    public int convertBoolean(boolean myVar) {
        if (myVar == true) {
            return 1;
        }
        else {
            return 0;
        }
    }
}

像这样的一个类可以用来将清晰的输入传递给你的模糊变量,在我的例子中,对输出进行去模糊化,时间可以安全地在急诊室等待。您可以在此类中看到我加载上面显示的FCL文件。

希望这个小例能引导您朝着正确的方向前进,但请随时询问您是否有任何疑问。复活节快乐。