📄 tfuzzyset.cpp
字号:
{
// if (abs(truths[x] - maxTruth) < TOLERANCE)
if (truths[x] == maxTruth)
{
if (MaxLeft == INVALID_DOMAIN)
{
MaxLeft = x; // first x of maxTruth
MaxRight = x; // in case there is only one max point
}
else
{
MaxRight = x; // overright with last x of maxTruth
}
}
}
// if (abs(truths[(int)( (MaxLeft + MaxRight)/(2) )] - maxTruth) < TOLERANCE) // multi plateaus
if (truths[(int)( (MaxLeft + MaxRight)/(2) )] == maxTruth) // multi plateaus
scalar = (MaxLeft + MaxRight) / 2;
else
scalar = MaxLeft;
}
break;
case NEAR_EDGE: //find x at the left edge of usually a single-edged plateau
{
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];
}
for (int x=0; x<DOMAIN_POINTS; x++)
{
// if (abs(truths[x] - maxTruth) < TOLERANCE)
if (truths[x] == maxTruth)
{
scalar = x; // first x of maxTruth
break;
}
}
}
break;
case FAR_EDGE: //find x at the right edge of usually a single-edged plateau
{
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];
}
for (int x=0; x<DOMAIN_POINTS; x++)
{
// if (abs(truths[x] - maxTruth) < TOLERANCE)
if (truths[x] == maxTruth)
{
scalar = x; // last x of maxTruth
}
}
}
break;
case CENTROID_SUPPORT_SET: //similar to CENTROID, but for only the part of the domain with truth(X) > alphaCut (STRONG)
{
// NOT IMPLEMENTED YET
}
break;
case AVERAGE_MAXIMUM: //similar to MAXIMUM_HEIGHT, but for only the highest plateau, or singlton
{
// NOT IMPLEMENTED YET
}
break;
case CENTRE_OF_MAXIMUMS: //similar to MAXIMUM_HEIGHT, but for only the two highest plateaus, or singltons
{
// NOT IMPLEMENTED YET
}
break;
case CENTRE_OF_MASS: //find x at the centre of the region supported by the maximum number of rules or evidence
//case BEST_EVIDENCE:
{
// NOT IMPLEMENTED YET
}
break;
case LEAST_ENTROPY: //find x for the least degree of fuzzyness (information entropy)
{
// NOT IMPLEMENTED YET
}
break;
}
scalar = (scalar * (variable->end - variable->begin))/DOMAIN_WIDTH + variable->begin;
return scalar;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// move and resize functions
////////////////////////////////////////////////////////////////////////////////
void TFuzzySet::shift_left (double percent)
{
double temp[DOMAIN_POINTS];
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
temp[x] = truths[x];
}
else
{
temp[x] = ZERO_TRUTH;
}
}
double shift = (max - min) * percent;
min -= shift;
mid1 -= shift;
mid -= shift;
mid2 -= shift;
max -= shift;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
truths[x] = temp[(int)(x+shift)];
}
else
{
truths[x] = ZERO_TRUTH;
}
}
}
void TFuzzySet::shift_right (double percent)
{
double temp[DOMAIN_POINTS];
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
temp[x] = truths[x];
}
else
{
temp[x] = ZERO_TRUTH;
}
}
double shift = (max - min) * percent;
min += shift;
mid1 += shift;
mid += shift;
mid2 += shift;
max += shift;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
truths[x] = temp[(int)(x-shift)];
}
else
{
truths[x] = ZERO_TRUTH;
}
}
}
void TFuzzySet::shift_up (double percent)
{
double shift = (maxTruth - minTruth) * percent;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] != ZERO_TRUTH)
{
truths[x] += shift;
}
}
minTruth += shift;
maxTruth += shift;
}
void TFuzzySet::shift_down (double percent)
{
double shift = (maxTruth - minTruth) * percent;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] != ZERO_TRUTH)
{
truths[x] -= shift;
}
}
minTruth -= shift;
maxTruth -= shift;
}
void TFuzzySet::narrow (double percent)
{
double temp[DOMAIN_POINTS];
int x, j;
for (x=min,j=0; x<=max; x=x+(1/percent),j++)
{
temp[j] = truths[x];
}
double _min = mid - percent * (mid-min );
double _mid1 = mid - percent * (mid-mid1);
double _mid = mid - percent * (mid-mid );
double _mid2 = mid - percent * (mid-mid2);
double _max = mid - percent * (mid-max );
min = _min;
mid1 = _mid1;
mid = _mid;
mid2 = _mid2;
max = _max;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
truths[x] = temp[(int)(x-min)];
}
else
{
truths[x] = ZERO_TRUTH;
}
}
}
void TFuzzySet::widen (double factor)
{
double temp[DOMAIN_POINTS];
for (int x=min; x<max; x++)
{
for (int j=0; j<factor; j++)
{
temp[(int)((x-min)*factor) + j] = truths[x];
}
}
double _min = mid - factor * (mid-min );
double _mid1 = mid - factor * (mid-mid1);
double _mid = mid - factor * (mid-mid );
double _mid2 = mid - factor * (mid-mid2);
double _max = mid - factor * (mid-max );
min = _min;
mid1 = _mid1;
mid = _mid;
mid2 = _mid2;
max = _max;
for (int x=0; x<DOMAIN_POINTS; x++)
{
if ( (x>=min) && (x<=max) )
{
truths[x] = temp[(int)(x-min)];
}
else
{
truths[x] = ZERO_TRUTH;
}
}
}
void TFuzzySet::scale_up (double factor)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] != ZERO_TRUTH)
{
truths[x] *= factor;
}
}
minTruth *= factor;
maxTruth *= factor;
}
void TFuzzySet::scale_down (double percent)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
if (truths[x] != ZERO_TRUTH)
{
truths[x] *= percent;
}
}
minTruth *= percent;
maxTruth *= percent;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// logical operators (Zadeh and non_Zadeh)
////////////////////////////////////////////////////////////////////////////////
TFuzzySet& TFuzzySet::and(TFuzzySet& set2, int type)
{
TFuzzySet* result = new TFuzzySet(*this);
switch(type)
{
case ZADEH: // min(s1,s2)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = (truths[x] < set2.truths[x]) ? truths[x] : set2.truths[x];
}
result->minTruth = (minTruth < set2.minTruth) ? minTruth : set2.minTruth;
result->maxTruth = (maxTruth < set2.maxTruth) ? maxTruth : set2.maxTruth;
}
break;
case MEAN: // (s1+s2)/2
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = (truths[x] + set2.truths[x]) / 2.0;
}
result->minTruth = (minTruth + set2.minTruth) / 2.0;
result->maxTruth = (maxTruth + set2.maxTruth) / 2.0;
}
break;
case MEANSQUARE: // ((s1+s2)/2)^2
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = pow((truths[x] + set2.truths[x]) / 2.0, 2);
}
result->minTruth = pow((minTruth + set2.minTruth) / 2.0, 2);
result->maxTruth = pow((maxTruth + set2.maxTruth) / 2.0, 2);
}
break;
case MEANROOT: // ((s1+s2)/2)^.5
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = pow((truths[x] + set2.truths[x]) / 2.0, 0.5);
}
result->minTruth = pow((minTruth + set2.minTruth) / 2.0, 0.5);
result->maxTruth = pow((maxTruth + set2.maxTruth) / 2.0, 0.5);
}
break;
case PRODUCT: // s1*s2
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = truths[x] * set2.truths[x];
}
result->minTruth = minTruth * set2.minTruth;
result->maxTruth = maxTruth * set2.maxTruth;
}
break;
case BOUNDEDSUM: // max(0,(s1+s2-1))
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = (0.0 > (truths[x] + set2.truths[x] - 1)) ? 0.0 : (truths[x] + set2.truths[x] - 1);
}
result->minTruth = (0.0 > (minTruth + set2.minTruth - 1)) ? 0.0 : (minTruth + set2.minTruth - 1);
result->maxTruth = (0.0 > (maxTruth + set2.maxTruth - 1)) ? 0.0 : (maxTruth + set2.maxTruth - 1);
}
break;
}
return *result;
}
TFuzzySet& TFuzzySet::or(TFuzzySet& set2, int type)
{
TFuzzySet* result = new TFuzzySet(*this);
switch(type)
{
case ZADEH: // max(s1,s2)
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = (truths[x] > set2.truths[x]) ? truths[x] : set2.truths[x];
}
result->minTruth = (minTruth > set2.minTruth) ? minTruth : set2.minTruth;
result->maxTruth = (maxTruth > set2.maxTruth) ? maxTruth : set2.maxTruth;
}
break;
case MEAN: // (2*min(s1,s2)+4*max(s1,s2))/6
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = (
2 * ((truths[x] < set2.truths[x]) ? truths[x] : set2.truths[x])
+
4 * ((truths[x] > set2.truths[x]) ? truths[x] : set2.truths[x])
) / 6.0;
}
result->minTruth = (
2 * ((minTruth < set2.minTruth) ? minTruth : set2.minTruth)
+
4 * ((minTruth > set2.minTruth) ? minTruth : set2.minTruth)
) / 6.0;
result->maxTruth = (
2 * ((maxTruth < set2.maxTruth) ? maxTruth : set2.maxTruth)
+
4 * ((maxTruth > set2.maxTruth) ? maxTruth : set2.maxTruth)
) / 6.0;
}
break;
case MEANSQUARE: // ((2*min(s1,s2)+4*max(s1,s2))/6)^2
{
for (int x=0; x<DOMAIN_POINTS; x++)
{
result->truths[x] = pow(((
2 * ((truths[x] < set2.truths[x]) ? truths[x] : set2.truths[x])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -