📄 temperature.cpp
字号:
//temperature.cpp : the process of thermoregulation
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CONTROL_TEM.h"
#include "temperature.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////////
//Fuzzy Inference Using fuzzy rule
//////////////////////////////////////////////////////////////////////////
#include"dlg.h"
#include<string.h>
#include<math.h>
#include"CONTROL_TEMView.h"
extern double begin,end,de;
temperature::temperature()
{
for(int i=0;i<4;i++)/*规则*/
strcpy(rule[0][i],"pb");
strcpy(rule[0][4],"pm");
strcpy(rule[0][5],"pm");
strcpy(rule[0][6],"ps");
strcpy(rule[1][0],"pb");
strcpy(rule[1][1],"pb");
for(i=2;i<6;i++)
strcpy(rule[1][i],"pm");
strcpy(rule[1][6],"ps");///改为6
strcpy(rule[2][0],"pb");
strcpy(rule[2][1],"pb");
strcpy(rule[2][2],"pm");
for(i=3;i<6;i++)
{
strcpy(rule[2][i],"ps");
}
strcpy(rule[2][6],"ns");
strcpy(rule[3][0],"pb");
strcpy(rule[3][1],"pm");
strcpy(rule[3][2],"ps");
strcpy(rule[3][3],"z0");
strcpy(rule[3][4],"ns");
strcpy(rule[3][5],"nm");
strcpy(rule[3][6],"nb");
for(i=0;i<5;i++)
{
strcpy(rule[4][i],"ns");
}
strcpy(rule[4][5],"nm");
strcpy(rule[4][6],"nb");
strcpy(rule[5][0],"ns");
for(i=1;i<5;i++)
strcpy(rule[5][i],"nm");
strcpy(rule[5][5],"nb");
strcpy(rule[5][6],"nb");
strcpy(rule[6][0],"ns");
strcpy(rule[6][1],"ns");
strcpy(rule[6][3],"nm");
for(i=3;i<7;i++)
strcpy(rule[6][i],"nb");
}
void temperature::putvalue()
{
m_nde=de;
m_ne=begin-end;
}
void temperature::modify(double w)
{
m_nde=m_nde+w*0.1;///功率对温度的影响函数
m_ne+=m_nde;
begin=end+m_ne;
}
double temperature::getde()
{
return m_nde;
}
double temperature::getne()
{
return m_ne;
}
temperature::~temperature()
{
}
/*********各模糊变量的判断函数**********/
void temperature::mz0(double x,element &a)
{
if((x>=-2.0)&&(x<=0))
{
a.m=0.5*x+1;
strcpy(a.var,"z0");
}
else if((x>=0)&&(x<=2.0))
{
a.m=1-0.5*x;
strcpy(a.var,"z0");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mps(double x,element &a)
{
if((x>=0)&&(x<=2.0))
{
a.m=0.5*x;
strcpy(a.var,"ps");
}
else if((x>2.0)&&(x<=4.0))
{
a.m=2-0.5*x;
strcpy(a.var,"ps");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mpm(double x,element &a)
{
if((x>=2.0)&&(x<=4.0))
{
a.m=0.5*x-1;
strcpy(a.var,"pm");
}
else if((x>4.0)&&(x<=6.0))
{
a.m=3-0.5*x;
strcpy(a.var,"pm");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mns(double x,element &a)
{
if((x>=-4.0)&&(x<=-2.0))
{
a.m=0.5*x+2;
strcpy(a.var,"ns");
}
else if((x>-2.0)&&(x<=0))
{
a.m=-0.5*x;
strcpy(a.var,"ns");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mnm(double x,element &a)
{
if((x>=-6.0)&&(x<=-4.0))
{
a.m=0.5*x+3;
strcpy(a.var,"nm");
}
else if((x>-4.0)&&(x<=-2.0))
{
a.m=-0.5*x-1;
strcpy(a.var,"nm");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mnb(double x,element &a)
{
if((x>=-6.0)&&(x<=-4.0))
{
a.m=-0.5*x-2;
strcpy(a.var,"nb");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
void temperature::mpb(double x,element &a)
{
if((x>=4.0)&&(x<=6.0))
{
a.m=0.5*x-2;
strcpy(a.var,"pb");
}
else
{
a.m=0;
strcpy(a.var,"");
}
}
///////////////////////////////////////
//模糊化
///////////////////////////////////////
double temperature::fuzzification()
{
element b[30],tag1[7],tag2[7];
double d,lower[30];
double u,upper[30];
int k=0,j;
mnb(m_ne,tag1[0]);
mnm(m_ne,tag1[1]);
mns(m_ne,tag1[2]);
mz0(m_ne,tag1[3]);
mps(m_ne,tag1[4]);
mpm(m_ne,tag1[5]);
mpb(m_ne,tag1[6]);
mnb(m_nde,tag2[0]);
mnm(m_nde,tag2[1]);
mns(m_nde,tag2[2]);
mz0(m_nde,tag2[3]);
mps(m_nde,tag2[4]);
mpm(m_nde,tag2[5]);
mpb(m_nde,tag2[6]);
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
{
if((tag1[i].m>0)&&(tag2[j].m>0))
{
b[k].m=min(tag1[i].m,tag2[j].m);
strcpy(b[k].var,rule[i][j]);
k++;
}
}
//////////////////////////////////////////////////////////////////////
func fun[10][3]; int t=0,n=0,flag,flag1;
double x,y;
int fcount[10];//10 section for fun
for(n=0;n<10;n++)
fcount[n]=0;
n=0;
for(i=0;i<k;i++)
{
flag=0;
flag1=0;
t=0;
x=-9;
y=-9;
if(strcmp(b[i].var,"z0")==0)
n=1;
if(strcmp(b[i].var,"ps")==0)
n=2;
if(strcmp(b[i].var,"pm")==0)
n=3;
if(strcmp(b[i].var,"ns")==0)
n=4;
if(strcmp(b[i].var,"nm")==0)
n=5;
if(strcmp(b[i].var,"nb")==0)
n=6;
if(strcmp(b[i].var,"pb")==0)
n=7;
switch(n)
{
case 1:
x=(b[i].m-1)/0.5;
if((x>-2)&&(x<0))
{
fun[i][t].down=-2;
fun[i][t].up=x;
fun[i][t].exp=12;
t++;
flag=1;
fcount[i]++;
}
y=(1-b[i].m)/0.5;
if((0<y)&&(y<2))
{
fun[i][t].down=y;
fun[i][t].up=2;
fun[i][t].exp=13;
t++;
flag1=1;
fcount[i]++;
}
if((flag==1)&&(flag1==1))
{
fun[i][t].down=x;
fun[i][t].up=y;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
if((flag==0)&&(flag1==0))
{
fun[i][t].exp=12;
fun[i][t].up=0;
fun[i][t].down=-2;
fcount[i]++;
t++;
fun[i][t].exp=13;
fun[i][t].up=2;
fun[i][t].down=0;
fcount[i]++;
t++;
}
break;
//----------------------------------
case 2:
x=b[i].m/0.5;
if((x>0)&&(x<2))
{
fun[i][t].down=0;
fun[i][t].up=x;
fun[i][t].exp=14;
t++;
flag=1;
fcount[i]++;
}
y=(2-b[i].m)/0.5;
if((2<y)&&(y<4))
{
fun[i][t].down=y;
fun[i][t].up=4;
fun[i][t].exp=15;
t++;
flag1=1;
fcount[i]++;
}
if((flag==1)&&(flag1==1))
{
fun[i][t].down=x;
fun[i][t].up=y;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
if((flag==0)&&(flag1==0))
{
fun[i][t].exp=14;
fun[i][t].up=2;
fun[i][t].down=0;
fcount[i]++;
t++;
fun[i][t].exp=15;
fun[i][t].up=4;
fun[i][t].down=2;
fcount[i]++;
t++;
}
break;
//-------------------------------------
case 3:
x=(b[i].m+1)/0.5;
if((x>2)&&(x<4))
{
fun[i][t].down=2;
fun[i][t].up=x;
fun[i][t].exp=16;
t++;
flag=1;
fcount[i]++;
}
y=(3-b[i].m)/0.5;
if((0<y)&&(y<2))
{
fun[i][t].down=y;
fun[i][t].up=6;
fun[i][t].exp=17;
t++;
flag1=1;
fcount[i]++;
}
if((flag==1)&&(flag1==1))
{
fun[i][t].down=x;
fun[i][t].up=y;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
if((flag==0)&&(flag1==0))
{
fun[i][t].exp=16;
fun[i][t].up=4;
fun[i][t].down=2;
fcount[i]++;
t++;
fun[i][t].exp=17;
fun[i][t].up=6;
fun[i][t].down=4;
fcount[i]++;
t++;
}
break;
//--------------------------------
case 4:
x=(b[i].m-2)/0.5;
if((x>-4)&&(x<-2))
{
fun[i][t].down=-4;
fun[i][t].up=x;
fun[i][t].exp=10;
t++;
flag=1;
fcount[i]++;
}
y=(-b[i].m)/0.5;
if((-2<y)&&(y<=0))
{
fun[i][t].down=y;
fun[i][t].up=0;
fun[i][t].exp=11;
t++;
flag1=1;
fcount[i]++;
}
if((flag==1)&&(flag1==1))
{
fun[i][t].down=x;
fun[i][t].up=y;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
if((flag==0)&&(flag1==0))
{
fun[i][t].exp=10;
fun[i][t].up=-2;
fun[i][t].down=-4;
fcount[i]++;
t++;
fun[i][t].exp=11;
fun[i][t].up=0;
fun[i][t].down=-2;
fcount[i]++;
t++;
}
break;
//-------------------------
case 5:
x=(b[i].m-3)/0.5;
if((x>-6)&&(x<-4))
{
fun[i][t].down=-6;
fun[i][t].up=x;
fun[i][t].exp=8;
t++;
flag=1;
fcount[i]++;
}
y=(1+b[i].m)/-0.5;
if((y>-4)&&(y<-2))
{
fun[i][t].down=y;
fun[i][t].up=-2;
fun[i][t].exp=9;
t++;
flag1=1;
fcount[i]++;
}
if((flag==1)&&(flag1==1))
{
fun[i][t].down=x;
fun[i][t].up=y;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
if((flag==0)&&(flag1==0))
{
fun[i][t].exp=8;
fun[i][t].up=-4;
fun[i][t].down=-6;
fcount[i]++;
t++;
fun[i][t].exp=9;
fun[i][t].up=-2;
fun[i][t].down=-4;
fcount[i]++;
t++;
}
break;
//-------------------------------------
case 6:
x=(b[i].m+2)/(-0.5);
if((-6<x)&&(x<-4))
{
fun[i][t].down=-6;
fun[i][t].up=x;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
fun[i][t].down=x;
fun[i][t].up=-4;
fun[i][t].exp=7;
t++;
fcount[i]++;
}
else
{
fun[i][t].down=-6;
fun[i][t].up=-4;
fun[i][t].exp=7;
fcount[i]++;
t++;
}
break;
//-----------------------------------------
case 7:
x=(b[i].m+2)/0.5;
if((4<x)&&(x<6))
{
fun[i][t].down=4;
fun[i][t].up=x;
fun[i][t].exp=18;
t++;
fcount[i]++;
fun[i][t].down=x;
fun[i][t].up=6;
fun[i][t].exp=b[i].m;
t++;
fcount[i]++;
}
else
{
fun[i][t].down=4;
fun[i][t].up=6;
fun[i][t].exp=18;
fcount[i]++;
t++;
}
break;
//-----------------------------------------
}
}
//----------------to get the max value-----
for(i=0;i<k;i++)//the number of fun
{
n=0;
for(j=1;j<fcount[i];j++)//the section of each fun
{
if(fun[i][n].down>fun[i][j].down)
n=j;
}
lower[i]=fun[i][n].down;
}
d=lower[0];
for(i=1;i<k;i++)
if(lower[i]<d)
d=lower[i];
for(i=0;i<k;i++)//the number of fun
{
n=0;
for(j=1;j<fcount[i];j++)//the section of each fun
{
if(fun[i][n].up<fun[i][j].up)
n=j;
}
upper[i]=fun[i][n].up;
}
u=upper[0];
for(i=1;i<k;i++)
if(upper[i]>u)
u=upper[i];
double wt=0;double wm=0;
for(double q=d;q<=u;q+=(u-d)/1000)
{
double mf=0;
for(i=0;i<k;i++)
for(j=0;j<fcount[i];j++)
{
if(q<=fun[i][j].up&&q>=fun[i][j].down)
{
if(fun[i][j].exp<7)
{
if(fun[i][j].exp>mf)
mf=fun[i][j].exp;
}
else
{
switch(int(fun[i][j].exp))
{
case 7:
element o;
mnb(q,o);
if(mf<o.m)
mf=o.m;
break;
case 8:
case 9:
mnm(q,o);
if(mf<o.m)
mf=o.m;
break;
case 10:
case 11:
mns(q,o);
if(mf<o.m)
mf=o.m;
break;
case 12:
case 13:
mz0(q,o);
if(mf<o.m)
mf=o.m;
break;
case 14:
case 15:
mps(q,o);
if(mf<o.m)
mf=o.m;
break;
case 16:
case 17:
mpm(q,o);
if(mf<o.m)
mf=o.m;
break;
case 18:
mpb(q,o);
if(mf<o.m)
mf=o.m;
break;
}
}
}
}
wt=(u-d)/1000*mf+wt;
wm=(u-d)/1000*mf*q+wm;
}
double w=wm/wt;
return w;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -