⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fuzzy.cc.bak

📁 To increase life time fuzzy using
💻 BAK
字号:
#include <fstream>#include "fuzzy.h"fuzzy::fuzzy(const char* fname){	read_fuzzy(fname);}fuzzy::~fuzzy(){	delete this;}void fuzzy::add_input(input* in){    inputs.push_back(in);}void fuzzy::add_output(output* out){    outputs.push_back(out);}void fuzzy::add_rule(rule* ru){    rules.push_back(ru);}float fuzzy::calc(float x,int flag){    float sum=0.0;    float tmu=0.0;    inputs[0]->calc(x); // mu of the inputs    for(int i=0; i<rules.size(); i++)	rules[i]->calc(inputs,flag); // mu of the rules    for(int i=0; i<rules.size(); i++)	if(((outputs[rules[i]->o])->getm())<rules[i]->getm())	    (outputs[rules[i]->o])->setm(rules[i]->getm());    // calculate all ouput with max    for(int i=0; i<outputs.size(); i++)	if(outputs[i]->getm()>0){	    sum+=outputs[i]->getm()*outputs[i]->getv();	    tmu+=outputs[i]->getm();	    outputs[i]->setm(0.0);	}    // calculate defuzzification    if (sum == 0.0){    	return (0);    }    else {    	return(sum/tmu);    }}float fuzzy::calc(float x1,float x2,int flag){    float sum=0.0;    float tmu=0.0;    inputs[0]->calc(x1); // mu of the first input    inputs[1]->calc(x2); // mu of the second input    for(int i=0; i<rules.size(); i++)	rules[i]->calc(inputs,flag); // mu of the rules    for(int i=0; i<rules.size(); i++){	cout << "***" << i << " " << (outputs[rules[i]->o])->setm(rules[i]->getm()) <<endl;	if(((outputs[rules[i]->o])->getm())<rules[i]->getm()){	    (outputs[rules[i]->o])->setm(rules[i]->getm());	}	cout << "***" << endl;    }    // calculate all ouput with max    for(int i=0; i<outputs.size(); i++)	if(outputs[i]->getm()>0){	    sum+=outputs[i]->getm()*outputs[i]->getv();	    tmu+=outputs[i]->getm();	    outputs[i]->setm(0.0);	}    // calculate defuzzification    if (sum == 0.0){    	return (0);    }    else {    	return(sum/tmu);    }}float fuzzy::calc(float x1,float x2, float x3,int flag){//    cerr << "fuzzy3 " << x1 << " " << x2 << " " << x3 << " ";    float sum=0.0;    float tmu=0.0;    inputs[0]->calc(x1); // mu of the first input    inputs[1]->calc(x2); // mu of the second input    inputs[2]->calc(x3); // mu of the third input    for(int i=0; i<rules.size(); i++)	rules[i]->calc(inputs,flag); // mu of the rules    for(int i=0; i<rules.size(); i++)	if(((outputs[rules[i]->o])->getm())<rules[i]->getm())	    (outputs[rules[i]->o])->setm(rules[i]->getm());    // calculate all ouput with max    for(int i=0; i<outputs.size(); i++)	if(outputs[i]->getm()>0){	    sum+=outputs[i]->getm()*outputs[i]->getv();	    tmu+=outputs[i]->getm();	    outputs[i]->setm(0.0);	}    // calculate defuzzification    return(sum/tmu);}int fuzzy::read_fuzzy(const char* fname){    ifstream f(fname);    if(!f){	cerr << " can not open input file: " << fname << endl;	exit(1);    }    string buf,name,art;    float a,b,c,d,cf;    int anz;    int line=0;    int ninputs=0;    int x,y,z,o;    input *ip;    output *op;    rule *rp;    f >> buf >> name >> anz;    cerr << buf << " " << name << " " << anz << endl;    line++;    if(buf != "input"){	cerr << "error in inputfile at line: " << line 	     << " first name must be input" << endl;	exit(1);    } __inputs:      // new input    ip = new input();    ip->set_name(name);    ninputs++;    // new membership    for(int i=0; i<anz; i++){	f >> buf >> name >> art;	line++;	cerr << buf << " " << name << " " << art << endl;	if(buf != "member"){	    cerr << "error in inputfile at line: " << line 		 << " first name must be member" << endl;	    exit(1);	}	if(art != "trapeze" && art != "triangular"){	    cerr << "error in inputfile at line: " << line 		 << " type must be triangel or trapeze" << endl;	    exit(1);	}	if(art == "triangular"){	    f >> a >> b >> c;	    cerr << a << " " << b << " " << c << endl;	    ip->set_member(name,a,b,c);	    line++;	}	else{	    f >> a >> b >> c >> d;	    cerr << a << " " << b << " " << c << " " << d << endl;	    ip->set_member(name,a,b,c,d);	    line++;	}	    }    this->add_input(ip);  // add new input in fuzzy    f >> buf >> name >> anz;    line++;    cerr << buf << " " << name << " " << anz << endl;    if(buf == "input") goto __inputs;    for(int i=0; i<anz; i++){	f >> buf >> name >> a;	op = new output(a);  // make new output	op->set_name(name);  // add name to output	this->add_output(op);   // add output in fuzzy	line++;	cerr << buf << " " << name << " " << a << endl;    }        f >> buf >> anz;    line++;    cerr << buf << " " << anz << endl;    if(buf != "rules"){	cerr << "error in inputfile at line: " << line 	     << " name must be rules" << endl;	exit(1);    }    for(int i=0; i<anz; i++){	if(ninputs==1){	    f >> x >> o >> cf ;	    rp = new rule(x,0,0,o,cf); // rule x _ _ y cf	    this->add_rule(rp);	    line++;	    cerr << x << " "  << o  << " " << cf << endl;	}	if(ninputs==2){	    f >> x >> y >> o >> cf ;	    rp = new rule(x,y,0,o,cf); // rule x x1 _ y cf	    this->add_rule(rp);	    line++;	    cerr << x << " " << y << " " << o  << " " << cf << endl;	}	if(ninputs==3){	    f >> x >> y >> z >> o >> cf ;	    rp = new rule(x,y,z,o,cf); // rule x x1 x2 y cf	    this->add_rule(rp);	    line++;	    cerr << x << " " << y << " " << z << " " 		 << o  << " " << cf << endl;	}    }        f.close();    return(ninputs);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -