Esempio n. 1
0
def convert(model, domains):
    '''
        Return a JFML FIS corresponding to this ANFIS model.
        Note you must supply domains (min/max vals) for each input variable.
    '''
    jfml_fis = FuzzyInferenceSystem(model.description)
    jfml_kb = KnowledgeBaseType()
    jfml_fis.setKnowledgeBase(jfml_kb)
    local_jfml = _LocalJFML()
    # Input variables and MFs:
    invars = model.input_variables()
    for varname, fv in invars:
        var = local_jfml.set_in_variable(varname, domains[varname])
        for mfname, mfdef in fv.members():
            local_jfml.set_in_term(varname, mfname, mfdef)
        jfml_kb.addVariable(var)
    # Output variables and rule coefficients:
    onames = model.output_variables()
    for outnum, outvar in enumerate(onames):
        var = local_jfml.set_out_variable(outvar)
        for rnum in range(model.coeff.shape[0]):
            mfname = _out_mf_name(outnum, rnum)
            local_jfml.set_out_term(outvar, mfname, model.coeff[rnum][outnum])
        jfml_kb.addVariable(var)
    # Rules (all in one rule block)
    jfml_rb = TskRuleBaseType(name="Rulebase",
                              tskRuleBaseType=FuzzySystemRuleBase.TYPE_TSK)
    jfml_rb.setActivationMethod(value="PROD")
    rule_ants = _mk_antecedents(local_jfml, model.layer['rules'], invars)
    rule_cons = _mk_consequents(local_jfml, model.layer['consequent'], onames)
    for rnum, rule in enumerate(model.coeff):
        jfml_rule = TskFuzzyRuleType(name="rule{}".format(rnum),
                                     connector="and",
                                     connectorMethod="MIN",
                                     weight=1.0)
        jfml_rule.setAntecedent(value=rule_ants[rnum])
        jfml_rule.setTskConsequent(value=rule_cons[rnum])
        jfml_rb.addTskRule(rule=jfml_rule)
    jfml_fis.addRuleBase(jfml_rb)
    return jfml_fis
from py4jfml.knowledgebase.KnowledgeBaseType import KnowledgeBaseType
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.rule.AntecedentType import AntecedentType
from py4jfml.rule.ClauseType import ClauseType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rule.FuzzyRuleType import FuzzyRuleType
from py4jfml.rulebase.MamdaniRuleBaseType import MamdaniRuleBaseType
from py4jfml.term.FuzzyTerm import FuzzyTerm
from py4jfml.term.FuzzyTermType import FuzzyTermType

# FuzzyInference
iris = FuzzyInferenceSystem("iris - MAMDANI")

# KnowledgeBase
kb = KnowledgeBaseType()
iris.setKnowledgeBase(kb)

# FUZZY VARIABLE SepalLength
sl = FuzzyVariableType("SepalLength", 4.3, 7.9)

# FUZZY TERM low
sl_low = FuzzyTermType("low", FuzzyTerm.TYPE_trapezoidShape, [4.3, 4.3, 5.019, 6.048])
sl.addFuzzyTerm(sl_low)

# FUZZY TERM  medium
sl_medium = FuzzyTermType("medium", FuzzyTerm.TYPE_triangularShape, [5.019, 6.048, 7.05])
sl.addFuzzyTerm(sl_medium)

# FUZZY TERM high
sl_high = FuzzyTermType("high", FuzzyTerm.TYPE_trapezoidShape, [6.048, 7.05, 7.9, 7.9])
Esempio n. 3
0
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.knowledgebasevariable.TsukamotoVariableType import TsukamotoVariableType
from py4jfml.membershipfunction.PointSetMonotonicShapeType import PointSetMonotonicShapeType
from py4jfml.membershipfunction.PointType import PointType
from py4jfml.rule.AntecedentType import AntecedentType
from py4jfml.rule.ClauseType import ClauseType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rule.FuzzyRuleType import FuzzyRuleType
from py4jfml.rulebase.TsukamotoRuleBaseType import TsukamotoRuleBaseType
from py4jfml.term.FuzzyTermType import FuzzyTermType
from py4jfml.term.TsukamotoTermType import TsukamotoTermType

tipper = FuzzyInferenceSystem("tipper - TSUKAMOTO")

#KNOWLEDGE BASE
kb = KnowledgeBaseType()
tipper.setKnowledgeBase(kb)

#FUZZY VARIABLE food
food = FuzzyVariableType(name="food", domainLeft=0.0, domainRight=10.0)

#FUZZY TERM rancid
rancid = FuzzyTermType(name="rancid",
                       type_java=FuzzyTermType.TYPE_triangularShape,
                       param=[0.0, 2.0, 5.5])
food.addFuzzyTerm(rancid)

#FUZZY TERM delicious
delicious = FuzzyTermType(name="delicious",
                          type_java=FuzzyTermType.TYPE_rightLinearShape,
                          param=[5.5, 10.0])
import os

from py4jfml.knowledgebase.KnowledgeBaseType import KnowledgeBaseType
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.rule.AntecedentType import AntecedentType
from py4jfml.rule.ClauseType import ClauseType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rule.FuzzyRuleType import FuzzyRuleType
from py4jfml.rulebase.MamdaniRuleBaseType import MamdaniRuleBaseType
from py4jfml.term.FuzzyTermType import FuzzyTermType

japaneseDietAssessment = FuzzyInferenceSystem(
    "japaneseDietAssessment - MAMDANI")

# KNOWLEDGE BASE
kb = KnowledgeBaseType()
japaneseDietAssessment.setKnowledgeBase(kb)

# FUZZY VARIABLE PCC(Percentage of Calories from Carbohydrate)
pcc = FuzzyVariableType("PCC", 0., 100.)

# FUZZY TERM low
pcc_low = FuzzyTermType("low", FuzzyTermType.TYPE_trapezoidShape,
                        [0., 0., 55., 60.])
pcc.addFuzzyTerm(pcc_low)

# FUZZY TERM medium
pcc_medium = FuzzyTermType("medium", FuzzyTermType.TYPE_trapezoidShape,
                           [55., 60., 65., 70.])
pcc.addFuzzyTerm(pcc_medium)
from py4jfml.Py4Jfml import Py4jfml
import os

from py4jfml.knowledgebase.KnowledgeBaseType import KnowledgeBaseType
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.rule.AntecedentType import AntecedentType
from py4jfml.rule.ClauseType import ClauseType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rule.FuzzyRuleType import FuzzyRuleType
from py4jfml.rulebase.MamdaniRuleBaseType import MamdaniRuleBaseType
from py4jfml.term.FuzzyTermType import FuzzyTermType

invertedPendulum = FuzzyInferenceSystem(name="invertedPendulum - MAMDANI")

#KNOWLEDGE BASE
kb = KnowledgeBaseType()
invertedPendulum.setKnowledgeBase(value=kb)

#FUZZY VARIABLE Angle
ang = FuzzyVariableType(name="Angle", domainLeft=0.0, domainRight=255.0)

#FUZZY TERM VNEG
ang_vneg = FuzzyTermType(name="very negative", type_java=FuzzyTermType.TYPE_trapezoidShape, param=[0.0, 0.0, 48.0, 88.0])
ang.addFuzzyTerm(ft=ang_vneg)

#FUZZY TERM NEG
ang_neg = FuzzyTermType(name="negative", type_java=FuzzyTermType.TYPE_triangularShape, param=[48.0, 88.0, 128.0])
ang.addFuzzyTerm(ft=ang_neg)

#FUZZY TERM NEU
ang_neu = FuzzyTermType(name="zero",  type_java=FuzzyTermType.TYPE_triangularShape, param=[88.0, 128.0, 168.0])
from py4jfml.knowledgebase.KnowledgeBaseType import KnowledgeBaseType
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.membershipfunction.CircularDefinitionType import CircularDefinitionType
from py4jfml.operator.OrLogicalType import OrLogicalType
from py4jfml.rule.AntecedentType import AntecedentType
from py4jfml.rule.ClauseType import ClauseType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rule.FuzzyRuleType import FuzzyRuleType
from py4jfml.rulebase.MamdaniRuleBaseType import MamdaniRuleBaseType
from py4jfml.term.FuzzyTermType import FuzzyTermType

invertedPendulum = FuzzyInferenceSystem("invertedPendulum - MAMDANI")

# KNOWLEDGE BASE
kb = KnowledgeBaseType()
invertedPendulum.setKnowledgeBase(kb)

# FUZZY VARIABLE
ang = FuzzyVariableType("Angle", 0.0, 255.0)

# FUZZY TERM VNEG
ang_vneg = FuzzyTermType("very negative", FuzzyTermType.TYPE_trapezoidShape, [0.0, 0.0, 48.0, 88.0])
ang.addFuzzyTerm(ang_vneg)

#FUZZY TERM NEG
ang_neg = FuzzyTermType("negative", FuzzyTermType.TYPE_triangularShape,[48.0, 88.0, 128.0])
ang.addFuzzyTerm(ang_neg)

# FUZZY TERM NEU
ang_neu = FuzzyTermType("zero", FuzzyTermType.TYPE_triangularShape,[88.0, 128.0, 168.0])
Esempio n. 7
0
average = TskTermType(name="average", order=TskTerm._ORDER_0, coeff=[1.6])
tskVar.addTskTerm(average)
if tskVar.getTerms()[0].getName() == average.getName():
    print("TskVariableType test case 1 (term name) passed.")
else:
    print("TskVariableType test case 1 (term name) failed.")
tskVar.setType(output)
if tskVar.getType() == output:
    print("TskVariableType test case 2 (type) passed.")
else:
    print("TskVariableType test case 2 (type) failed.")
if tskVar.getCombination() == comb:
    print("TskVariableType test case 3 (combination) passed.")
else:
    print("TskVariableType test case 3 (combination) failed.")
kb = KnowledgeBaseType()
kb.addVariable(aggregatedFuzzyVar)
kb.addVariable(anyaVar)
kb.addVariable(tsukamotoVar)
kb.addVariable(tskVar)
kb.addVariable(force)
if kb.getVariable("tip").getCombination() == "WA":
    print("KnowledgeBaseType test case passed.")
else:
    print("KnowledgeBaseType test case failed.")

# FUZZY VARIABLE
ang = FuzzyVariableType("Angle", 0.0, 255.0)
ang_vneg = FuzzyTermType("very negative", FuzzyTermType.TYPE_trapezoidShape,
                         [0.0, 0.0, 48.0, 88.0])
ang.addFuzzyTerm(ang_vneg)
from py4jfml.Py4Jfml import Py4jfml
import os

from py4jfml.knowledgebase.KnowledgeBaseType import KnowledgeBaseType
from py4jfml.knowledgebasevariable.AnYaDataCloudType import AnYaDataCloudType
from py4jfml.knowledgebasevariable.FuzzyVariableType import FuzzyVariableType
from py4jfml.rule.AnYaAntecedentType import AnYaAntecedentType
from py4jfml.rule.AnYaRuleType import AnYaRuleType
from py4jfml.rule.ConsequentType import ConsequentType
from py4jfml.rulebase.AnYaRuleBaseType import AnYaRuleBaseType
from py4jfml.term.FuzzyTermType import FuzzyTermType

tipper = FuzzyInferenceSystem("tipper - AnYa")

#KNOWLEDGE BASE
kb = KnowledgeBaseType()
tipper.setKnowledgeBase(kb)

#CLOUD food
datumFood = [1.0,1.7,4.0,3.2]
cloudFood = AnYaDataCloudType(name="food", terms=datumFood)

kb.addVariable(var=cloudFood)

#CLOUD service
datumService = [6.0,5.7,7.0,4.6]
cloudService = AnYaDataCloudType(name="service", terms=datumService)

kb.addVariable(var=cloudService)

#CLOUD service