FuzzyVariable.fromJSON()
方法将从JSON格式中恢复模糊变量对象。模糊变量包含模糊集合和模糊规则。
FuzzyVariable.fromJSON(jsonString)
jsonString
: 要恢复的模糊变量对象的JSON字符串。
FuzzyVariable对象
const jsonString = '{"name":"temperature","min":0,"max":100,"sets":[{"name":"cold","type":"triangle","params":[0,0,25]},{"name":"moderate","type":"triangle","params":[0,25,50]},{"name":"hot","type":"triangle","params":[25,50,100]}],"rules":[{"antecedent":[{"set":"cold","variable":"temperature"}],"consequent":{"set":"low","variable":"fan_speed"}},{"antecedent":[{"set":"moderate","variable":"temperature"}],"consequent":{"set":"medium","variable":"fan_speed"}},{"antecedent":[{"set":"hot","variable":"temperature"}],"consequent":{"set":"high","variable":"fan_speed"}}]}';
const fuzzyVariable = FuzzyVariable.fromJSON(jsonString);
console.log(fuzzyVariable);
上述代码将输出以下内容:
{
name: 'temperature',
min: 0,
max: 100,
sets: [
{ name: 'cold', type: 'triangle', params: [ 0, 0, 25 ] },
{ name: 'moderate', type: 'triangle', params: [ 0, 25, 50 ] },
{ name: 'hot', type: 'triangle', params: [ 25, 50, 100 ] }
],
rules: [
{
antecedent: [ { set: 'cold', variable: 'temperature' } ],
consequent: { set: 'low', variable: 'fan_speed' }
},
{
antecedent: [ { set: 'moderate', variable: 'temperature' } ],
consequent: { set: 'medium', variable: 'fan_speed' }
},
{
antecedent: [ { set: 'hot', variable: 'temperature' } ],
consequent: { set: 'high', variable: 'fan_speed' }
}
]
}
JSON字符串应该以与FuzzyVariable.toJSON()
输出的字符串相同的格式编写。否则,恢复过程可能会失败。
在开发中,我们建议使用FuzzyVariable.toJSON()
方法生成JSON字符串,并使用FuzzyVariable.fromJSON()
方法将其恢复为对象。