📄 tfuzzyset.h
字号:
////////////////////////////////////////////////////////////////////////////////
#ifndef TFuzzySet_h
#define TFuzzySet_h
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Copyright (c)1998 Ali Adams, Bournemouth University, UK.
////////////////////////////////////////////////////////////////////////////////
//
// FuzzyLogic Class Library
//
// TVariable
// TFuzzySet eHedge
// TPremise eOperator
// TPredicate TConsequent
// TFuzzyRule
// TFuzzyController
// TFuzzySystem
//
// TVariable == variable as base for a fuzzyset (Temperature, in Celsius)
// TFuzzySet == fuzzy value set of a variable
// eHedge == linguistic modifier for a fuzzy set (e.g. VERY, SOMEWHAT, ...)
// scalar == input variable's value or defuzzified output variable's value set
// TPremise == scalar is (member of) fuzzyset (e.g. Temperature is HOT)
// eOperator == premises connector {AND, OR}
// TPredicate == Premise eOperator Premise eOperator Premise ...
// TConsequent == Premise
// TFuzzyRule == Predicate or if Predicate then Consequent
// TFuzzyController== FuzzyRules for a single output variable
// TFuzzySystem == all FuzzyControllers in the system
//
//
////////////////////////////////////////////////////////////////////////////////
// Fuzzy Logic Processing Sequence:
//
// 1. Aggregation of predicate truths for each rule
// 2. Correlation of predicate truths for each rule to consequent
// 3. Composition of weighted-consequents of all rules
// 4. Defuzzification of composition's result into a crisp value
////////////////////////////////////////////////////////////////////////////////
//
//
// HELP YOURSELF BY HELPING OTHERS
//
// ThanQ for using this software, feel free to modify, enhance, extend, and/or
// redistribute it as long as you include this header with no modification(s),
// you may add your own header after all other headers of other people (if any).
//
// No warranty of any kind explicit or implicit is given, this software is
// provided "as is" and is only to be used at your own risk, and the author
// does not except any responsibility or liability to any kind of defects,
// damages, or injuries resulting from the (in)direct use of this software.
//
// However, I worked HARD and LONG to get this software to this level of
// usability, readability, and maintainability. The author appreciates any
// reciprocal help from users of this software. This version of the software is
// released as a shareware and any income resulting from it will help sustain
// the author's ability to produce useful, and usable software shared by all.
////////////////////////////////////////////////////////////////////////////////
//
// Please email bugs/fixes/suggestions to: aliadams@bournemouth.ac.uk
//
////////////////////////////////////////////////////////////////////////////////
// References:
// 1. The Fuzzy Systems Handbook, by Earl Cox, 1994. ISBN 0-12-194270-8
// 2. The CFuzzySet classes (C) Timothy C. Lee 1996
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include<iostream.h> // istream, ostream
#include<string.h> // strlen(), strcpy()
#include<vcl\graphics.hpp> // TCanvas, TColor
#include<math.h> // pow, sqrt, ...
#include<values.h> // MAXDOUBLE, ...
#define TOLERANCE 0.0001
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////
#define min(x,y) ((x)<(y))?(x):(y)
#define max(x,y) ((x)>(y))?(x):(y)
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
class TFuzzySet; //forward decleration
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
#define DOMAIN_WIDTH 100 // number of divisions in domain
#define MAX_FUZZYSETS_PER_VARIABLE 9 // max number of fuzzy values for variable
////////////////////////////////////////////////////////////////////////////////
// TVariable == variable as base for a fuzzyset (Temperature, in Celsius)
////////////////////////////////////////////////////////////////////////////////
//
class TVariable
{
public:
char* name; // variable name e.g. Temperature
char* unit; // measure unit e.g. Celsius
double begin; // domain begin e.g. -10
double end; // domain end e.g. +40
TFuzzySet* fuzzysets[MAX_FUZZYSETS_PER_VARIABLE];
public:
TVariable(char* _name=NULL, char* _unit=NULL, double _begin=0.0*DOMAIN_WIDTH, double _end=1.0*DOMAIN_WIDTH);
virtual ~TVariable();
void set_name(char* _name) { if (name != NULL) delete[] name; name = new char[strlen(_name)+1]; strcpy(name, _name); }
void set_unit(char* _unit) { if (unit != NULL) delete[] unit; unit = new char[strlen(_unit)+1]; strcpy(unit, _unit); }
void clear_fuzzysets();
void add_fuzzyset(TFuzzySet* fuzzyset);
void remove_fuzzyset(TFuzzySet* fuzzyset);
bool operator == (TVariable& variable2);
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
#define DOMAIN_POINTS DOMAIN_WIDTH + 1 // number of domain points for DOMAIN_WIDTH
#define INVALID_DOMAIN -MAXDOUBLE // for all points outside min .. max
#define ZERO_TRUTH 0.0 // for all points outside min .. max
//
// A fuzzy set's domain lies inside its variable's domain (begin..end), and is
// divided into 4 regions using five points (min, mid1, mid, mid2, max).
//
// |......|-------------|--------|-----|------------|.............|
// begin min mid1 mid mid2 max end
//
// In order to keep to one general class for all shapes, these five points are
// required by constructor, even if some aren't used. You can howvever, pass
// only the first neccssory few arguments, and let the others take the default
// valus specified in the constructor.
//
// The variable's domain will be sampled using DOMAIN_POINTS points.
//
// The truth value of the DOMAIN_POINTS ponits will be interpolated using
// 1. the above five points,
// 2. the minTruth & maxTruth values, and
// 3. the shape of the fuzzy set.
// This will represent the membership function for the fuzzy set.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// truth function's shape directions
////////////////////////////////////////////////////////////////////////////////
#define UP 0
#define DOWN 1
#define LEFT DOWN // for SHOULDER shapes
#define RIGHT UP // for SHOULDER shapes
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// truth function's shapes
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//enum eHedge {NO_SHAPE, SINGLETON, PLATEAU, LINEAR, TRIANGLE, TRAPEZOIDAL, SHOULDER, S_CURVE, PI_CURVE, BETA_CURVE, GAUSSIAN_CURVE, IRREGULAR};
//
#define NO_SHAPE 0 // FuzzySet ()
// parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // truth(x) = 0.0
// typically, create empty set with NO_SHAPE as an output set, use it to store the result of fuzzyset operations
#define SINGLETON 1 // FuzzySinglton ( mid, type=UP, minTruth=0.0, maxTruth=1.0)
// parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for mid > x --> truth(x) = minTruth
// | // for mid == x+-TOLERANCE--> truth(x) = maxTruth
// | // for mid > x --> truth(x) = minTruth
// -----------|----------
#define PLATEAU 2 // FuzzyPlateau (min, max, type=UP, minTruth=0.0, maxTruth=1.0)
#define BOOLEAN PLATEAU
// mid1,mid,mid2,type parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for min > x --> truth(x) = minTruth
// |---------------------| // min <= x <= max --> truth(x) = maxTruth
// | | // x > max --> truth(x) = minTruth
// | |
#define LINEAR 3 // FuzzyLine (min, max, type=UP, minTruth=0.0, maxTruth=1.0)
// mid1,mid,mid2 parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for min > x --> truth(x) = minTruth
// _ * // min <= x <= max --> truth(x) = x * ((maxTruth-minTruth)/(max-min))
// _ * UP // x > max --> truth(x) = maxTruth
// *
// mid1,mid,mid2 parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // 1 - LINE_UP
// * _ //
// * _ DOWN //
// *
#define TRIANGLE 4 // FuzzyTriangle (min, mid, max, type=UP, minTruth=0.0, maxTruth=1.0)
// mid1,mid2 parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for min > x --> truth(x) = minTruth
// * // min <= x <= mid --> truth(x) = x * ((maxTruth-minTruth)/(mid-min))
// * * // mid <= x <= max --> truth(x) = 1.0 - x * ((maxTruth-minTruth)/(max-mid))
// * * // x > max --> truth(x) = minTruth
// * *
// * *
#define TRAPEZOIDAL 5 // FuzzyTrapezodal (min, mid1, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0)
// mid parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for min > x --> truth(x) = minTruth
// *---------* // min <= x <= mid1 --> truth(x) = x * ((maxTruth-minTruth)/(mid1-min))
// * * // mid1 <= x <= mid2 --> truth(x) = maxTruth
// * * // mid2 <= x <= max --> truth(x) = 1.0 - x * ((maxTruth-minTruth)/(max-mid2))
// * * // x > max --> truth(x) = minTruth
#define SHOULDER 6 // FuzzyShoulder (min, , mid, , max, type=UP, minTruth=0.0, maxTruth=1.0)
// mid1,mid2 parameters ignored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // narrow(LINE_UP, 0.25) + shift_right(narrow(PLATEAU, 0.25), 0.25)
// *-----
// type=UP=RIGHT *
// *
// -----*
// * type=DOWN=LEFT
// *
#define S_CURVE 7 // FuzzySCurve (min, flex, max, type=UP, minTruth=0.0, maxTruth=1.0)
// mid1,mid2 parameters ignored, flex=mid (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for x <= min --> truth(x) = minTruth
// . ` ` // min <= x <= mid --> truth(x) = minTruth + 2*(((x-min)/((mid-min)*2))^2)
// type=UP * // mid <= x <= max --> truth(x) = maxTruth + 2*(((max-x)/((max-mid)*2))^2)
// * // x >= max --> truth(x) = maxTruth
// *
// . . `
// mid1,mid1 parameters ignored, flex=mid (min, mid1, mid, mid2, max, type=DOWN, minTruth=0.0, maxTruth=1.0);// 1 - S_CURVE_UP
// ` ` .
// *
// type=DOWN *
// *
// ` . .
#define PI_CURVE 8 // FuzzyPICurve (min, mid, max, type=UP, minTruth=0.0, maxTruth=1.0)
// type parameter ingored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // narrow(S_CURVE_UP, 0.5) + shift_right(narrow(S_CURVE_DOWN, 0.5), 0.5)
// . `` . //
// * * //
// * * //
// * * //
// . ` ` .
#define BETA_CURVE 9 // FuzzyBetaCurve (min, flex1,mid, flex2,max, type=UP, minTruth=0.0, maxTruth=1.0)
// min,max,type parameters ingored (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for min >= x --> approaching -infinity
// . `` . // min <= x <= mid --> truth(x) = 1 / ( 1 + ((mid-x)/mid1)^2 )
// * * // mid <= x <= max --> truth(x) = 1 / ( 1 + ((x-mid)/mid2)^2 )
// * * // x >= max --> approaching +infinity
// * *
// ` ` ` `
#define GAUSSIAN_CURVE 10 // FuzzyGaussianCurve(centre, width, type=UP, minTruth=0.0, maxTruth=1.0)
// centre=mid, width=mid-min (min, mid1, mid, mid2, max, type=UP, minTruth=0.0, maxTruth=1.0); // for x <= min --> truth(x) = minTruth
// . `` . // min <= x <= mid --> truth(x) = e ^ ( -(mid-min) * (mid-x)^2 )
// * * // mid <= x <= max --> truth(x) = e ^ ( -(max-mid) * (x-mid)^2 )
// * * // x >= max --> truth(x) = minTruth
// * *
// .` `.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// linguistic hedges
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_HEDGES 16 // maximum number of hedges allowed per set
#define INVALID_HEDGE -1 // for all empty elements in hedges[]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -