Fuzzy and heterogeneous modeling in SIMLIB/C++ David Martinek Faculty of Information Technology Brno University of Technology, Brno, Božetěchova 2, 612 00, Czech Republic Web: http://www.fit.vutbr.cz/~martinek/ E-mail: martinek@fit.vutbr.cz Abstract This paper deals with the formulation of heterogeneous and fuzzy models by means of the simulation library SIMLIB/C++. This library has been developed at the Department of Computer Science and Engineering (the Faculty of Information Technology since 2002). It is designed as an object oriented library for the C++ language. The basic version provides instruments for continuous, discrete and heterogeneous modeling. SIMLIB/C++ was developed as a modular library. Modules for 3D support and for optimizations are already available. The development of the fuzzy module (FuzzySIMLIB/C++) is in progress nowadays. We can formulate the heterogeneous model by conjunction of the continuous, discrete and other modeling concepts. The continuous model is represented by the difference equations, which are held in the tree structure by SIMLIB/C++. Discrete models are in this library represented by processes. The conjunction between these two concepts is carried out by ‘switch-guards’ connected into the continuous model and they monitor some variable. If the value of a variable passes over the zero level, the guard changes values or whole equations. However, the C++ language itself may be used as the ‘glue’ between the concepts. The core of fuzzy model is represented by the fuzzy controller (FC). FC uses the fuzzy sets for the conversion between sharp and fuzzy values. The base of the inference rules causes the action of FC. The behavior of FC appear continuous outward, therefore it can be embedded into the difference equations of the continuous model. 1. Introduction Since the beginning of computer age, the computer aided modeling has undergone a considerable evolution. The continuous modeling was one of the first modeling concepts. This concept comes from the age of the analog computers; therefore, the model seems like an analog circuit diagram. The model consists of blocks called integrators and of functional blocks. These elementary blocks form the circuit, which calculates difference equations. Another modeling concept was starting to assert oneself with coming of digital computers. We call this concept the discrete modeling. Then two different approaches emerged. The first approach represents modeling systems oriented on events (SIMSCRIPT language). There features the event calendar in these systems. Here changes of state of the model are solely bound on occurrences of events in the calendar. The second approach then represents process-oriented modeling languages (GPSS, SOL, SIMULA67), where as the process we understand the chronological order of events. This approach has been adopted outside the area of modeling – by the parallel programming with threads. Both discrete and continuous modeling can describe only some specific cases of real systems. Therefore, both of these concepts began combine together very soon. As the combined models, we understand models created by fusion of continuous and discrete concept. However, nowadays it appears the combined approach does not sufficient for the description of systems. As the time goes, there had emerged a new programming techniques and modeling approaches. It is obvious we are trying utilizing these new techniques for modeling. The research on a knowledge-based modeling area is in progress lately. Amongst these new approaches, we could rank for example a fuzzy logic, a logical programming, neural nets and the like. The models, which with classical modeling concepts use also these new approaches, we call the heterogeneous models. SIMLIB/C++ library was developed since the beginning as a modular library. We can perceive its core as two modules – the continuous one and the discrete one. There has been added a conditional concept also into the library, which provides the communication between the continuous and the discrete part of a model. The module for fuzzy logic has been added to the library for the possibility of experimentation with the knowledge-based modeling. A fuzzy controller of type Mamdani is implemented here. The model, which uses this controller, can use all the other modules of course. 2. Modeling with SIMLIB/C++ The C++ language was used for implementation of the library. This language was found to be one of the most suitable languages for system simulation. When implementing a model in C++ we will appreciate many of the C++ features, e.g. multiple inheritance, automatic object initialization and operator overloading. The C++ language has – except for parallelism – all the features necessary for simulation models description. It seems quite easy to develop a simulation library, which will contain all the facilities for model description and simulation control. This library has been developed mainly for Linux, but it can be recompiled in other Unix systems and also under DOS/Windows. The only limitation is that this library can be used only on the machines, which uses processor from the Intel-x-86 processor family. The reason is a processor dependent implementation of interruption of processes. 2.1. Continuous module As written above, the continuous model originates by interconnection of the functional blocks and the special blocks called integrators. The functional blocks present classical mathematical functions, like sinus, cosinus, logarithm and so on. The integrators make the integration of its own inputs. There must be performed a numerical integration on digital computers. The SIMLIB/C++ library uses the Runge-Kutta numeric integration method. The block declarations define the elements of the model’s continuous part and relations between them. The basic hierarchy of the continuous block classes: class SimObject; // base abstract class class class class class class class class class class class class // abstract block // blocks with continuous output // integrator // status variables // hysteresis // backlash // general relay // general function // constant // variable //parameter (simulation run constant) aBlock; aContiBlock; Integrator; Status; Hyst; Blash; Relay; Function; Constant; Variable; Parameter; Continuous blocks are divided into several classes. There are many possibilities how to join these blocks together in C++, but the most elegant way is to use the overloaded operators in associations with a constructor of model’s main class. As the model main class may be used any of the user defined classes. The special Integrator class describes the integrators. This class uses the operator overloading to make the model description more readable. The ( ) operator is used for the integrator (or another block) input definition in the following way: <identifier_of_integrator> (<input_expression>); The input expression is stored in a tree structure created by overloaded operators. This structure is evaluated in every step of the numeric method. The integrator behavior description is implemented in the simulation library. In addition to the basic function of integrator, it is also possible to set the integrator’s status by the overloaded assign operator. When declaring an object of the class integrator it is possible to set the initial value; the default value is zero. 2.2. Conditional concept Classes of conditions represent a mechanism to describe the communication between the continuous and discrete parts of the model. The inputs to these objects are values whose sign changes are detected and certain actions may be started. The hierarchy of the conditional classes class SimObject; // base abstract class // combined: class aCondition; // state condition base class class Condition; //state event detector (Boolean version) class ConditionUp; // action by FALSE-->TRUE change class ConditionDown;// action by TRUE-->FALSE 2.3. Discrete module To implement parallelism it is necessary to use the model behavior description either by events (which may not be interrupted) or by processes (which may be interrupted). SIMLIB/C++ provides both of these conceptions. Here is the hierarchy of the discrete classes: class SimObject; // base abstract class // discrete: class Link; //list item base class (Simula-like) class Entity; // discrete model entity class Process; //process base class Event; //event base class Sampler;//periodic calls of global function class List; //list of objects of class Link descendants class Queue; //priority queue class Stat; //statistics class TStat; //time dependent statistics class Histogram; //histogram class Facility; //SOL-like facility class Store; //SOL-like store class Barrier; //barrier class Semaphore; //semaphore The Events are used for example in multiple-service systems for a customer (Process) generation. There is used an idea the Event may plan its own invocation itself in a certain (or random) time. The Process describes the chronological order of activities. Between performances of these activities, the Process sleeps. Two commands can put the Process to sleep – wait and wait-until. After calling one of these commands, the Process releases the processor. The library provides also other tools for the synchronization of processes like the Barrier and the Semaphore. There is a collection of classes inspired by SOL language for disposal to create models of multiple-service systems. There are the classes Facility and Store. The Process (or more processes) may occupy these facilities for a certain time. If the capacity of Facility or Store is exceeded, the processes may line up to the Queue. There are also classes Stat, TStat and Histogram for disposal to an evaluation of a simulation. These classes can collect several statistics about the model and synoptically represent them. 3. Heterogeneous models As heterogeneous models, we understand such models that in addition to use the combined approach uses the other modeling techniques. We will focus on the knowledge-based models – especially the fuzzy logic. 3.1. Fuzzy module The FuzzySIMLIB/C++ provides an implementation of Mamdani version of fuzzy controller. It provides classes for fuzzy sets with several shapes of membership functions. There is implemented a basic defuzzyfication methods here – Min of Max, Max of Max, Mean of Max and Discrete Center of Gravity. The module provides a general and an intuitive interface for an inference rule’s notation. However, there is also a specialized interface for controllers with two inputs and only one output. This interface is optimized and may be used to machine generation of models. The last kind of interface uses XML. This interface is used for communication with a graphical fuzzy sets editor – MeFE. 3.2. Fuzzy logic Firstly, we must explain some ideas of a fuzzy logic and of a theory of fuzzy sets. To avoid confusion we will name a classical (not fuzzy) set a sharp set. Every sharp set is defined by a certain characteristic function. This function maps a universum of sharp set into set {0, 1}. The classical logic knows only two values – true (1) and false (0). To the contrary, the fuzzy logic uses a continuous truth-function, which maps the universum into the whole interval <0,1>. We call this projection as the membership function. It is possible to use whichever shape of this function, but considering an effectiveness of calculation there is the most common shapes a triangle, a trapezoid, a singleton and a gaussian curve. Smooth curves have highest computing requirements than triangles or trapezoids, but the output of fuzzy controller may be more fluid. The membership functions are a base of fuzzy sets. Each function in fuzzy set has a name – a word value. This name describes some vague value. For example, we suppose the fuzzy set described a velocity – the word values of membership functions may be Small, Medium and High. The fuzzy set is made of membership functions with word values. The idea of the word value corresponds with the idea of an element, as we know it in sharp sets. When we want to describe the element of fuzzy set we must to know actual values of all the membership functions defined on fuzzy set’s universum. The value 60 km/h may look in fuzzy representation like this speed = {0/Small, 0.8/Medium, 0.2/Hight} Therefore, we must have at disposal a mechanism for transformation of sharp numbers in fuzzy values and back. We call these mechanisms a fuzzyfication and a defuzzyfication. We are just explaining the fuzzyfication. To explain the defuzzyfication we need another definition. The inference rules is a concept, which describes a behavior of the fuzzy controller. Here is an example of one inference rule: if (speed is Medium and surface is wet) then (power is low) Suppose we use only two fuzzy inputs and the only one fuzzy output. After evaluation, all the inference rules we obtain a surface (very simplification) like on figure 1. This figure also depicts the most common used methods for evaluation of the sharp value from the surface. Fig. 1: Defuzzyfication and defuzzyfication methods. 3.3 Implementation of fuzzy logic Here is the base hierarchy of fuzzy classes: class FuzzyMembershipFunction // base membership function class FuzzySingleton class FuzzyTriangle class FuzzyTrapez class FuzzyGauss class FuzzyGauss2 class FuzzySet // fuzzy set class FuzzyVariable // base fuzzy variable class FuzzyInput class FuzzyOutput class FuzzyBlock class FuzzySampledBlock class FuzzyRSBlock // base of each fuzzy controller FuzzySIMLIB/C++ provides classes for work with fuzzy sets in the Mamdani model. The membership functions are represented by descendants of the class FuzzyMembershipFunction. An object of class FuzzySet is created by means of them. From the user’s view, it is a container, which stores membership functions and information about ranges of the universum. Because we want to work with the fuzzy variables, we have the class FuzzyVariable here for disposal. The descendants of this class are class FuzzyInput and class FuzzyOutput. These classes realize a connection between the world of the classical – sharp numbers and the world of fuzzy sets. The class FuzzyInput is an input and the class FuzzyOutput is an output of the fuzzy controller. If we see the descendant of the FuzzyVariable as the variable, we may see the class FuzzySet as a data type. Then, the membership functions are a domain of this variable. As we can see, the fuzzy variables can acquire fuzzy values, as we explain above. Now, we demonstrate in an example how to create the fuzzy variable in C++ language. The most readable way is to define a function, which returns a value of type FuzzyInput: FuzzyInput* createFLength() { // the distance between cars FuzzySet *ftLength = new FuzzySet("length",-5, MAX_LENGTH, FuzzyTrapez ("Z0", -5, -5, 0, 15), FuzzyTriangle("SM", 0, 15, 40), FuzzyTriangle("ME", 15, 40, 65), FuzzyTriangle("BG", 40, 65, 90), FuzzyTrapez ("BB", 65, 90, MAX_LENGTH, MAX_LENGTH) ); return new FuzzyInput((*ftLength)); } This example shows the creation of the one input variable with name “length”. The fuzzy set, which defines this fuzzy variable, is covered by the membership functions with word values: “Z0” – zero, “SM” – small medium, “ME” – medium, “BG” – big and “BB” – big big. These abbreviations are frequently used instead of a boring naming by adjectives. We have several eventualities for the inference rule’s description. The first way is to write the rules to a method behavior() of class FuzzySampledBlock by the way we show above. Then, the class FuzzySampledBlock must be used as the main class of the model. That means we must create a descendant of this class and describe the whole environment of controller inside them (most often by difference equations). There are overloaded logical and assignment operators in the library. These operators make the defuzzyfication during each step of simulation. The other inference rule’s notations are not intended for a manual using, but it is possible to use them to machine generation of model. It is direct derivation tree generation and as next, it is the import from an XML document (which a MeFE editor may produce). These notations can be used only with the class FuzzyRSBlock instead of the class FuzzySampledBlock. As the last example, we introduce the connection between the fuzzy controller and the continuous model. It is a “smart car” model. The smart car tries to hold an approximately constant distance from a first car (for example in a convoy). This “pacemaker” may change speed unpredictably and the smart car must react. Here is a demonstration of declaration of class and constructor: Class IntelligentCar : public FuzzyRSBlock { private: FuzzyInput &speed, &length; FuzzyOutput &acceleration; public: Integrator v, s; // velocity, track Pacemaker &p; // first car public: IntelligentCar(double _v, Pacemaker &pm, FuzzyInput &Speed, FuzzyInput &Length, FuzzyOutput &Acceleration, FuzzyGeneralRules &rules) : FuzzyRSBlock(rules), //import of inference rules speed(Speed), //differential speed between cars length(Length), //distance between cars acceleration(Acceleration), // acceleration of smart car v(acceleration,_v), // v = a´ s(v,0), // s = v´ p(pm) // pacemaker { speed.setInput(pm.v–v);//differential speed between cars length.setInput(Min(pm.s – s,MAX_LENGTH)); //distance // registration of fuzzy variables speed.registerOwner(this); length.registerOwner(this); acceleration.registerOwner(this); }; ... } 3.4. Editor MeFE The manual model creation may be a very lengthy process. This mode of creation is more inclinable to faults, because we lose an overview about the model. Therefore, an editor MeFE (Membership Functions Editor) has been written. This editor is a tool to designing the fuzzy sets and the inference rules. The editor is designed as a wizard. We can see two screenshots on figures 2 and 3. On the figure 2, we can see a dialog window for a fuzzy set design. The figure 3 depicts the design of the inference rules. The designed fuzzy model may be saved into the XML file by pressing the finish button. This XML file may be imported by fuzzy model in FuzzySIMLIB/C++ through a command line parameter. Then, the model will be parameterized by the concrete fuzzy controller. The model design will be much more intuitive and flexible. Fig. 3: Declaration of the input fuzzy variable. Fig. 2: The inference rules. 4. Conclusion The SIMLIB/C++ library and the enhancement FuzzySIMLIB/C++ is a full usable tool for heterogeneous modeling. Thanks to the implementation in the C++ language, has the library very high modeling power, because of their easy extensibility. In addition, the models may use a standard and the other C and C++ libraries. There are a lot of them on the Internet today. However, an impossibility of a verification of models may appear as a disadvantage. Some failures in model design may evince only during the simulation. We plan enhancement the XML support beyond the FuzzySIMLIB/C++ module to the future. It is need to enhance the fuzzy module with the other computational techniques, like the Sugeno model. There is counted on enhancement with a logical programming support for the heterogeneous models area. References [1] Jura, P.: Fuzzy logika pro řízení a modelování. Brno, PC Dir, 1998. [2] Rábová, Z.: Modelování a simulace. Brno, Nakladatelství VUT v Brně, 1992. [3] Peringer, P: The SIMLIB/C++ home page. Last visited at July 8, 2002. http://www.fee.vutbr.cz/~peringer/SIMLIB/