📄 tfuzzyset.cpp
字号:
break;
// fuzzify a scalar, or widen
case ABOUT: // fuzzify(scalar), or widen by 1.3
//case AROUND:
//case ROUGHLY:
{
//if(shape == SINGLETON) then become PI Curve, else widen
widen(1.3);
}
break;
// restrict
case ABOVE: // shift_right(50%)
//case MORE_THAN:
{
shift_right(0.5);
}
break;
// restrict
case BELOW: // shift_left(50%)
//case LESS_THAN:
{
shift_left(0.5);
}
break;
// contrast intensification (rotate_anti_clock_wise)
case POSITIVELY: // if truths[x]<=0.5 --> n*(( x)^n) , n=2.0
// if truths[x]> 0.5 --> 1 - n*((1-x)^n) , n=2.0
//case ALMOST:
//case DEFINITELY:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] <= 0.5)
{
truths[x] = ( 2.0 * pow(truths[x], 2.0) );
}
else if (truths[x] > 0.5)
{
truths[x] = 1.0 - ( 2.0 * pow((1.0-truths[x]), 2.0) );
}
}
}
break;
// contrast diffusion (rotate_clock_wise)
case GENERALLY: // if truths[x]<=0.5 --> ((1/n)*( x))^(1/n) , n=2.0
// if truths[x]> 0.5 --> 1 - ((1/n)*(1-x))^(1/n) , n=2.0
//case USUALLY:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] <= 0.5)
{
truths[x] = pow( ((1/2.0)*truths[x]), (1/2.0) );
}
else if (truths[x] > 0.5)
{
truths[x] = 1.0 - pow((1/2.0)*((1.0-truths[x])), (1/2.0) );
}
}
}
break;
// approximate broadly
case IN_THE_VICINITY_OF: // widen to double
{
widen(2.0);
}
break;
// approximate narrowly
case CLOSE_TO: // narrow by half
//case NEIGHBORING:
{
narrow(0.5);
}
break;
// dilute
case SLIGHTLY: // x^(1/n) , n=3.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/3.0);
}
}
break;
case SOMEWHAT: // x^(1/n) , n=2.0
//case RATHER:
//case QUITE:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/2.0);
}
}
break;
// intensify
case VERY: // x^n , n=2.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 2.0);
}
}
break;
case EXTREMELY: // x^n , n=3.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 3.0);
}
}
break;
}
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
break; // for loop
}// end if
}
}
void TFuzzySet::remove_hedge(int type)
{
for(int i=0; i<MAX_HEDGES; i++)
{
if (hedges[i] == type)
{
// shift up
for(int j=i; j<MAX_HEDGES-1; j++)
{
hedges[j] = hedges[j+1];
}
// invalidate last hedge in case it had a valid hedge
hedges[MAX_HEDGES-1] = INVALID_HEDGE;
switch(type)
{
// negation
case NOT: // !
{
*this = !*this;
}
break;
// fuzzify a scalar, or widen
case ABOUT: // fuzzify(scalar), or widen by 1.3
//case AROUND:
//case ROUGHLY:
{
if(shape == SINGLETON)
{
//PI_CURVE_it(mid);
}
else
{
narrow(1/1.3);
}
}
break;
// restrict
case ABOVE: // shift_right(50%)
//case MORE_THAN:
{
shift_left(0.5);
}
break;
// restrict
case BELOW: // shift_left(50%)
//case LESS_THAN:
{
shift_right(0.5);
}
break;
// contrast intensification (rotate_anti_clock_wise)
case POSITIVELY: // if truths[x]<=0.5 --> n*(( x)^n) , n=2.0
// if truths[x]> 0.5 --> 1 - n*((1-x)^n) , n=2.0
//case ALMOST:
//case DEFINITELY:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] <= 0.5)
{
truths[x] = pow( (truths[x]*(1/2.0)), (1/2.0) );
}
else if (truths[x] > 0.5)
{
truths[x] = 1.0 - pow(((1.0-truths[x]))*(1/2.0), (1/2.0) );
}
}
}
break;
// contrast diffusion (rotate_clock_wise)
case GENERALLY: // if truths[x]<=0.5 --> ((1/n)*( x))^(1/n) , n=2.0
// if truths[x]> 0.5 --> 1 - ((1/n)*(1-x))^(1/n) , n=2.0
//case USUALLY:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] <= 0.5)
{
truths[x] = ( 2.0 * pow(truths[x], 2.0) );
}
else if (truths[x] > 0.5)
{
truths[x] = 1.0 - ( 2.0 * pow((1.0-truths[x]), 2.0) );
}
}
}
break;
// approximate broadly
case IN_THE_VICINITY_OF: // widen to double
{
narrow(1/2.0);
}
break;
// approximate narrowly
case CLOSE_TO: // narrow by half
//case NEIGHBORING:
{
widen(1/0.5);
}
break;
// dilute
case SLIGHTLY: // x^(1/n) , n=3.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/(1/3.0));
}
}
break;
case SOMEWHAT: // x^(1/n) , n=2.0
//case RATHER:
//case QUITE:
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/(1/2.0));
}
}
break;
// intensify
case VERY: // x^n , n=2.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/2.0);
}
}
break;
case EXTREMELY: // x^n , n=3.0
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = pow(truths[x], 1/3.0);
}
}
break;
}
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
break; // for loop
}// end if
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// manipulator functions
////////////////////////////////////////////////////////////////////////////////
// multiply all by 1.0/maxTruth() so that maxTruth()=1.0
void TFuzzySet::normalize()
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
truths[x] = truths[x] * (1.0/maxTruth);
}
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
}
// is normalised?
bool TFuzzySet::normalized()
{
return (maxTruth == 1.0);
}
// for all truth(x) > _maxTruth make truth(x) = _maxTruth
void TFuzzySet::truncate(double _maxTruth)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] > _maxTruth)
{
truths[x] = _maxTruth;
}
}
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
}
// STRONG restricts domain to x:truth(x) > alphaCut
// WEAK restricts domain to x:truth(x) >= alphaCut
void TFuzzySet::apply_alphaCut(int type)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (type == STRONG)
{
if (truths[x] <= alphaCut)
{
truths[x] = ZERO_TRUTH;
}
}
else if (type == WEAK)
{
if (truths[x] < alphaCut)
{
truths[x] = ZERO_TRUTH;
}
}
}
double left = INVALID_DOMAIN;
double right = INVALID_DOMAIN;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] != ZERO_TRUTH)
{
if (left == INVALID_DOMAIN)
{
left = x;
right = x; // in case there is only one point above alpha
}
else
{
right = x;
}
}
}
min = left;
max = right;
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
}
// convert to a scalar
double TFuzzySet::defuzzify(int how)
{
double scalar = INVALID_DOMAIN;
switch(how)
{
case CENTROID: //find domain point x at the centre of gravity, {(Xi*Yi)/{Yi
//case COMPOSITE_MOMENTS:
{
double productsum = 0.0;
double D = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
double y = (0.0 > truths[x]) ? 0.0 : truths[x];
productsum += x*y;
D += y;
}
if (D != 0.0)
scalar = productsum / D;
else
scalar = mid;
}
break;
case COMPOSITE_MAXIMUM: //find domain point x with maximum truth, if plateau, then use either AVERAGE_MAXIMUM, or CENTRE_OF_MAXIMUMS
//case MAXIMUM_HEIGHT:
{
minTruth = 1.0;
maxTruth = 0.0;
for (int x=0; x<DOMAIN_POINTS; x++)
{
minTruth = (minTruth < truths[x]) ? minTruth : truths[x];
maxTruth = (maxTruth > truths[x]) ? maxTruth : truths[x];
}
double MaxLeft = INVALID_DOMAIN;
double MaxRight = INVALID_DOMAIN;
for (int x=0; x<DOMAIN_POINTS; x++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -