# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

fuzzyset.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 #include "nel/ai/fuzzy/fuzzyset.h"
00026 
00027 namespace NLAIFUZZY
00028 {
00029         
00030         void IFuzzySet::addFact(double value)
00031         {
00032                 _Facts.push_back( value );
00033         }
00034 
00035         double IFuzzySet::agregate()
00036         {
00037                 if ( _Facts.size() )
00038                 {
00039                         // min
00040                         double min = 1;
00041                         while ( _Facts.size() )
00042                         {
00043                                 if ( _Facts.front() < min )
00044                                         min = _Facts.front();
00045                                 _Facts.pop_front();
00046                         }
00047                         _Value = min;
00048                         return min;
00049                 }
00050                 else 
00051                 {       
00052                         _Value = 0.0;
00053                         return 0.0;
00054                 }
00055         }
00056 
00057         double IFuzzySet::getValue()
00058         {
00059                 return _Value;
00060         }
00061 
00062         CFuzzyInterval::CFuzzyInterval(char *name,double min, double max) : IFuzzySet(name)
00063         {
00064                 _Min = min;
00065                 _Max = max;
00066         }
00067 
00068         CFuzzyInterval::CFuzzyInterval(const CFuzzyInterval &cp) : IFuzzySet(cp._Name)
00069         {
00070                 _Min = cp._Min;
00071                 _Max = cp._Max;
00072         }
00073 
00074         void CFuzzyInterval::init(NLAIAGENT::IObjectIA *params)
00075         {
00076                 if ( ((NLAIAGENT::IBaseGroupType *)params)->size() != 3 ) 
00077                 {
00078                 //      throw Exc::
00079                 }
00080                 NLAIAGENT::IObjectIA  * arg = (NLAIAGENT::IObjectIA  *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00081                 _Min = (double) ((NLAIAGENT::DDigitalType *) arg )->getValue();
00082                 arg->release();
00083                 arg = (NLAIAGENT::IObjectIA  *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00084                 _Max = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00085                 arg->release();
00086         }
00087 
00088         double CFuzzyInterval::membership(double val)
00089         {
00090                 if ( val >= _Min && val <= _Max )
00091                         return 1.0;
00092                 else
00093                         return 0.0;
00094         }
00095 
00096         bool CFuzzyInterval::isIn(double val)
00097         {
00098                 if ( val >= _Min && val <= _Max )
00099                         return true;
00100                 else
00101                         return false;
00102         }
00103 
00104         double CFuzzyInterval::surface()
00105         {
00106                 return ( _Max - _Min );
00107         }
00108 
00109         double CFuzzyInterval::center()
00110         {
00111                 return ( _Min + _Max ) /2;
00112         }
00113 
00114 
00115         const NLAIC::IBasicType *CFuzzyInterval::clone() const 
00116         {
00117                 CFuzzyInterval *clone = new CFuzzyInterval( *this );
00118                 return clone;
00119         }
00120 
00121         const NLAIC::IBasicType *CFuzzyInterval::newInstance() const
00122         {
00123                 NLAIC::IBasicType *instance = new CFuzzyInterval( *this );
00124 
00125                 return instance;
00126         }
00127 
00128         const NLAIC::CIdentType &CFuzzyInterval::getType() const
00129         {
00130                 return IdFuzzyInterval;
00131         }
00132 
00133         void CFuzzyInterval::save(NLMISC::IStream &os)
00134         {
00135                 IFuzzySet::save(os);
00136                 os.serial( (double &) _Min );
00137                 os.serial( (double &) _Max );
00138         }
00139 
00140         void CFuzzyInterval::load(NLMISC::IStream &is)
00141         {
00142         }
00143 
00144         void CFuzzyInterval::getDebugString(std::string &txt) const
00145         {
00146                 txt += NLAIC::stringGetBuild("%s [%f , %f]", _Name, _Min, _Max);
00147         }
00148 
00149         bool CFuzzyInterval::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00150         {
00151                 return ( ((CFuzzyInterval &)a)._Min == _Min && ((CFuzzyInterval &)a)._Max == _Max );
00152         }
00153         
00154         const NLAIAGENT::IObjectIA::CProcessResult &CFuzzyInterval::run()
00155         {
00156                 return NLAIAGENT::IObjectIA::ProcessRun;
00157         }
00158 
00161 
00162 
00163         CRightFuzzySet::CRightFuzzySet(char *name, double x1,double x2,double x3)  : IFuzzySet(name)
00164         {
00165                 _X1 = x1;
00166                 _X2 = x2;
00167                 _X3 = x3; 
00168         }
00169 
00170         CRightFuzzySet::CRightFuzzySet(const CRightFuzzySet &cp) : IFuzzySet(cp._Name)
00171         {
00172                 _X1 = cp._X1;
00173                 _X2 = cp._X2;
00174                 _X3 = cp._X3;
00175         }
00176 
00177         void CRightFuzzySet::init(NLAIAGENT::IObjectIA *params)
00178         {
00179                 if ( ((NLAIAGENT::IBaseGroupType *)params)->size() != 3 ) 
00180                 {
00181                 //      throw Exc::
00182                 }
00183                 NLAIAGENT::IObjectIA *arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00184                 _X1 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00185                 arg->release();
00186                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00187                 _X2 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00188                 arg->release();
00189                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00190                 _X3 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00191                 arg->release();
00192         }
00193 
00194 
00195         double CRightFuzzySet::membership(double val)
00196         {
00197                 if ( val >= _X2 && val <= _X3 )
00198                         return 1.0;
00199 
00200                 if ( val > _X1 && val < _X2 ) 
00201                 {
00202                         return ( ( val - _X1 ) / ( _X2 - _X1 ) );
00203                 }
00204 
00205                 return 0.0;
00206         }
00207 
00208         bool CRightFuzzySet::isIn(double val)
00209         {
00210                 return ( val >= _X1 && val <= _X3 );
00211         }
00212 
00213         double CRightFuzzySet::surface()
00214         {
00215                 return (_X2 - _X1) /2 + (_X3 - _X2);
00216         }
00217         
00218         double CRightFuzzySet::center()
00219         {
00220                 return ( ( (_X1 + _X2) /4 + (_X2 + _X3) /2 ) /1.5);
00221         }
00222 
00223         const NLAIC::IBasicType *CRightFuzzySet::clone() const
00224         { 
00225                 CRightFuzzySet *clone = new CRightFuzzySet( *this );
00226                 return clone;
00227         }
00228         const NLAIC::IBasicType *CRightFuzzySet::newInstance() const
00229         {
00230                 NLAIC::IBasicType *instance = new CRightFuzzySet( *this );
00231                 return instance;
00232         }
00233 
00234         const NLAIC::CIdentType &CRightFuzzySet::getType() const
00235         {
00236                 return IdRightFuzzySet;
00237         }
00238 
00239         void CRightFuzzySet::save(NLMISC::IStream &os)
00240         {
00241                 IFuzzySet::save(os);
00242                 os.serial( (double &) _X1 );
00243                 os.serial( (double &) _X2 );
00244                 os.serial( (double &) _X3);
00245         }
00246 
00247         void CRightFuzzySet::load(NLMISC::IStream &is)
00248         {
00249         }
00250 
00251         void CRightFuzzySet::getDebugString(std::string &txt) const
00252         {
00253                 txt += NLAIC::stringGetBuild("CRightFuzzySet %s [%f , %f , %f]", _Name, _X1, _X2, _X3);
00254         }
00255         
00256         bool CRightFuzzySet::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00257         {
00258                 return ( ((CRightFuzzySet &)a)._X1 == _X1 && 
00259                                  ((CRightFuzzySet &)a)._X2 == _X2 && 
00260                                  ((CRightFuzzySet &)a)._X3 == _X3 );
00261         }
00262 
00263         const NLAIAGENT::IObjectIA::CProcessResult &CRightFuzzySet::run()
00264         {
00265                 return NLAIAGENT::IObjectIA::ProcessRun;
00266         }
00267 
00268 
00269 
00270         NLAIAGENT::tQueue IFuzzySet::isMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA &params) const
00271         {
00272 
00273 #ifdef NL_DEBUG 
00274         std::string nameP;
00275         std::string nameM;
00276         funcName->getDebugString(nameM);
00277         params.getDebugString(nameP);
00278 
00279         const char *dbg_class_name = (const char *) getType();
00280 #endif
00281                 NLAIAGENT::tQueue r;
00282                 if(className == NULL)
00283                 {
00284                         if( (*funcName) == NLAIAGENT::CStringVarName( "Constructor" ) )
00285                         {                                       
00286                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CTriangleFuzzySet::IdTriangleFuzzySet ) );
00287                                 r.push( NLAIAGENT::CIdMethod( 0 + IObjectIA::getMethodIndexSize(), 0.0, NULL, c) );
00288                         }
00289 
00290                         if( (*funcName) == NLAIAGENT::CStringVarName( "Membership" ) )
00291                         {                                       
00292                                 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIAGENT::DigitalType::IdDigitalType ) );
00293                                 r.push( NLAIAGENT::CIdMethod( 1 + IObjectIA::getMethodIndexSize(), 0.0, NULL, c) );
00294                         }
00295 
00296                 }
00297 
00298                 if ( r.empty() )
00299                         return IObjectIA::isMember(className, funcName, params);
00300                 else
00301                         return r;
00302         }
00303 
00304         NLAIAGENT::IObjectIA::CProcessResult IFuzzySet::runMethodeMember(sint32 index, NLAIAGENT::IObjectIA *p)
00305         {
00306                 switch(index - IObjectIA::getMethodIndexSize() )
00307                 {
00308                 case 0:
00309                         {                                       
00310                                 init(p);
00311                         }
00312                         break;
00313 
00314                 case 1:
00315                         {
00316                                 NLAIAGENT::IObjectIA::CProcessResult r;
00317                                 const NLAIAGENT::IObjectIA *obj = ( (NLAIAGENT::IBaseGroupType *) p )->get();
00318                                 float val = float(( (NLAIAGENT::DDigitalType *) obj)->getNumber());
00319                                 r.Result = new NLAIAGENT::DDigitalType( membership( val ) );
00320                                 return r;
00321                         }
00322                 }
00323 
00324                 return NLAIAGENT::IObjectIA::CProcessResult();
00325         }
00326 
00327 
00328 
00329 /*
00330         const NLAIC::CIdentType CompositeiFuzzySet::idCompositeiFuzzySet = NLAIC::CIdentType("CompositeiFuzzySet",NLAIC::CSelfClassCFactory(CompositeiFuzzySet()),
00331                 NLAIC::CTypeOfObject::tObject,
00332                 NLAIC::CTypeOfOperator::opAdd |
00333                 NLAIC::CTypeOfOperator::opSub |
00334                 NLAIC::CTypeOfOperator::opEq );
00335         
00336         CompositeiFuzzySet::CompositeiFuzzySet()
00337         {
00338         }
00339         
00340         CompositeiFuzzySet::CompositeiFuzzySet(const CompositeiFuzzySet &cp)
00341         {
00342                 list<IFuzzySet *>::const_iterator it_f = cp._iFuzzySets.begin();
00343                 while ( it_f  != cp._iFuzzySets.end() )
00344                 {
00345                         _iFuzzySets.push_back( (IFuzzySet *) (*it_f)->clone() );
00346                         it_f++;
00347                 }
00348         }
00349 
00350         void CompositeiFuzzySet::addiFuzzySet(IFuzzySet *f)
00351         {
00352                 _iFuzzySets.push_back(f);
00353         }
00354 
00355         double CompositeiFuzzySet::membership(double val)
00356         {
00357                 list<IFuzzySet *>::iterator it_f = _iFuzzySets.begin();
00358                 while ( it_f  != _iFuzzySets.end() )
00359                 {
00360                         if ( (*it_f)->isIn( val ) )
00361                                 return (*it_f)->membership( val );
00362                         it_f++;
00363                 }
00364                 return 0.0;
00365         }
00366 
00367         bool CompositeiFuzzySet::isIn(double val )
00368         {
00369                 list<IFuzzySet *>::iterator it_f = _iFuzzySets.begin();
00370                 while ( it_f  != _iFuzzySets.end() )
00371                 {
00372                         if ( (*it_f)->isIn( val ) )
00373                                 return true;
00374                         it_f++;
00375                 }
00376                 return false;
00377         }
00378 
00379         double CompositeiFuzzySet::center()
00380         {
00381                 // TODO
00382                 return 0.0;
00383         }
00384 
00385         bool CompositeiFuzzySet::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00386         {
00387                 return false;
00388         }
00389 
00390         const NLAIC::IBasicType *CompositeiFuzzySet::clone() const 
00391         {
00392                 return new CompositeiFuzzySet( *this );
00393         }
00394 
00395         const NLAIC::IBasicType *CompositeiFuzzySet::newInstance() const
00396         {
00397                 return new CompositeiFuzzySet( *this ); 
00398         }
00399 
00400         const NLAIC::CIdentType &CompositeiFuzzySet::getType() const
00401         {
00402                 return idCompositeiFuzzySet;
00403         }
00404 
00405         void CompositeiFuzzySet::save(NLMISC::IStream &os)
00406         {
00407         }
00408 
00409         void CompositeiFuzzySet::load(NLMISC::IStream &is)
00410         {
00411         }
00412 
00413         void CompositeiFuzzySet::getDebugString(char *txt) const
00414         {
00415                 sprintf(txt,"CompositeiFuzzySet");
00416         }
00417 
00418         const TProcessStatement &CompositeiFuzzySet::run()
00419         {
00420                 return NLAIAGENT::IObjectIA::AgentIdle;
00421         }
00422 */
00425 
00426 
00427 
00428         CTriangleFuzzySet::CTriangleFuzzySet(char *name, double x1,double x2,double x3)  : IFuzzySet(name)
00429         {
00430                 _X1 = x1;
00431                 _X2 = x2;
00432                 _X3 = x3;
00433         }
00434 
00435         CTriangleFuzzySet::CTriangleFuzzySet(const CTriangleFuzzySet &cp) : IFuzzySet(cp._Name)
00436         {
00437                 _X1 = cp._X1;
00438                 _X2 = cp._X2;
00439                 _X3 = cp._X3;
00440         }
00441 
00442         void CTriangleFuzzySet::init(NLAIAGENT::IObjectIA *params)
00443         {
00444 #ifdef NL_DEBUG
00445                 std::string buf;
00446                 params->getDebugString(buf);
00447 #endif
00448 
00449                 sint32 i = ((NLAIAGENT::IBaseGroupType *)params)->size();
00450                 if ( i != 3 ) 
00451                 {
00452                 //      throw Exc::
00453                 }
00454                 NLAIAGENT::IObjectIA *arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00455                 _X1 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00456                 arg->release();
00457 
00458                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00459                 _X2 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00460                 arg->release();
00461 
00462                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00463                 _X3 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00464                 arg->release();
00465         }
00466 
00467 
00468         double CTriangleFuzzySet::membership(double val)
00469         {
00470                 if ( val > _X1 && val < _X2 )
00471                 {
00472                         return ( ( val - _X1 ) / ( _X2 - _X1 ) );
00473                 }
00474 
00475                 if ( val > _X2 && val < _X3 )
00476                 {
00477                         return ( ( val - _X2 ) / ( _X3 - _X2 ) );
00478                 }
00479 
00480                 return 0.0;
00481         }
00482 
00483         bool CTriangleFuzzySet::isIn(double val)
00484         {
00485                 return ( val > _X1 && val < _X2 );
00486         }
00487 
00488         double CTriangleFuzzySet::surface()
00489         {
00490                 return (_X3 - _X2) /2;
00491         }
00492 
00493         double CTriangleFuzzySet::center()
00494         {
00495                 return ( ( (_X1 + _X2) /2 * (_X2 - _X1) ) +
00496                                  ( (_X2 + _X3) /2 * (_X3 - _X2) ) ) 
00497                                  / (_X3 - _X1);
00498         }
00499 
00500         const NLAIC::IBasicType *CTriangleFuzzySet::clone() const
00501         {
00502                 CTriangleFuzzySet *clone = new CTriangleFuzzySet( *this );
00503                 return clone;
00504         }
00505         const NLAIC::IBasicType *CTriangleFuzzySet::newInstance() const
00506         {
00507                 NLAIC::IBasicType *instance = new CTriangleFuzzySet( *this );
00508                 return instance;
00509         }
00510 
00511         const NLAIC::CIdentType &CTriangleFuzzySet::getType() const
00512         {
00513                 return IdTriangleFuzzySet;
00514         }
00515         void CTriangleFuzzySet::save(NLMISC::IStream &os)
00516         {
00517                 IFuzzySet::save(os);
00518                 os.serial( (double &) _X1 );
00519                 os.serial( (double &) _X2 );
00520                 os.serial( (double &) _X3 );
00521         }
00522 
00523         void CTriangleFuzzySet::load(NLMISC::IStream &is)
00524         {
00525                 is.serial( _X1 );
00526                 is.serial( _X2 );
00527                 is.serial( _X3 );
00528         }
00529 
00530         void CTriangleFuzzySet::getDebugString(std::string &txt) const
00531         {
00532                 txt += NLAIC::stringGetBuild("CTriangleFuzzySet %s [%f , %f , %f]", _Name, _X1, _X2, _X3);
00533         }
00534         
00535         bool CTriangleFuzzySet::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00536         {
00537                 return ( ((CTriangleFuzzySet &)a)._X1 == _X1 && ((CTriangleFuzzySet &)a)._X2 == _X2 && ((CTriangleFuzzySet &)a)._X3 == _X3 );
00538         }
00539 
00540         const NLAIAGENT::IObjectIA::CProcessResult &CTriangleFuzzySet::run()
00541         {
00542                 return NLAIAGENT::IObjectIA::ProcessRun;
00543         }
00544 
00547         
00548         
00549         CLeftFuzzySet::CLeftFuzzySet(char *name, double p1,double p2,double p3) : IFuzzySet(name)
00550         {
00551                 _X1 = p1;
00552                 _X2 = p2;
00553                 _X3 = p3;
00554         }
00555 
00556         CLeftFuzzySet::CLeftFuzzySet( const CLeftFuzzySet &cp ) : IFuzzySet(cp._Name)
00557         {
00558                 _X1 = cp._X1;
00559                 _X2 = cp._X2;
00560                 _X3 = cp._X3;
00561         }
00562 
00563         void CLeftFuzzySet::init(NLAIAGENT::IObjectIA *params)
00564         {
00565                 if ( ((NLAIAGENT::IBaseGroupType *)params)->size() != 3 ) 
00566                 {
00567                 //      throw Exc::
00568                 }
00569                 
00570                 NLAIAGENT::IObjectIA *arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00571                 _X1 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00572                 arg->release();
00573 
00574                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00575                 _X2 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00576                 arg->release();
00577 
00578                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00579                 _X3 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00580                 arg->release();
00581         }
00582 
00583         CLeftFuzzySet::~CLeftFuzzySet()
00584         {
00585         }
00586 
00587         double CLeftFuzzySet::membership(double val)
00588         {
00589                 if ( val >= _X1 && val <= _X2 )
00590                         return 1.0;
00591 
00592                 if ( val > _X2 && val < _X3 )
00593                 {
00594                         return ( ( val - _X2 ) / ( _X3 - _X2 ) );
00595                 }
00596 
00597                 return 0.0;
00598         }
00599 
00600         bool CLeftFuzzySet::isIn(double val)
00601         {
00602                 return ( val >= _X1 && val <= _X3 );
00603         }
00604 
00605         double CLeftFuzzySet::surface()
00606         {
00607                 return (_X2 - _X1) + (_X3 - _X2) /2;
00608         }
00609 
00610         double CLeftFuzzySet::center()
00611         {
00612                 return ( ( (_X1 + _X2) /2 + (_X2 + _X3) /4 ) /1.5);
00613         }
00614 
00615         const NLAIC::IBasicType *CLeftFuzzySet::clone() const
00616         { 
00617                 CLeftFuzzySet *clone = new CLeftFuzzySet( *this );
00618                 return clone;
00619         }
00620         const NLAIC::IBasicType *CLeftFuzzySet::newInstance() const
00621         {
00622                 NLAIC::IBasicType *instance = new CLeftFuzzySet( *this );
00623                 return instance;
00624         }
00625 
00626         const NLAIC::CIdentType &CLeftFuzzySet::getType() const
00627         {
00628                 return IdLeftFuzzySet;
00629         }
00630 
00631         void CLeftFuzzySet::save(NLMISC::IStream &os)
00632         {
00633                 IFuzzySet::save(os);
00634                 os.serial( (double &) _X1 );
00635                 os.serial( (double &) _X2 );
00636                 os.serial( (double &) _X3 );
00637         }
00638 
00639         void CLeftFuzzySet::load(NLMISC::IStream &is)
00640         {
00641         }
00642 
00643         void CLeftFuzzySet::getDebugString(std::string &txt) const
00644         {
00645                 txt += NLAIC::stringGetBuild("CLeftFuzzySet %s [%f , %f , %f]", _Name, _X1, _X2, _X3);
00646         }
00647         
00648         bool CLeftFuzzySet::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00649         {
00650                 return ( ((CLeftFuzzySet &)a)._X1 == _X1 && ((CLeftFuzzySet &)a)._X2 == _X2 && ((CLeftFuzzySet &)a)._X3 == _X3 );
00651         }
00652 
00653         const NLAIAGENT::IObjectIA::CProcessResult &CLeftFuzzySet::run()
00654         {
00655                 return NLAIAGENT::IObjectIA::ProcessRun;
00656         }
00657 
00660 
00661 
00662 
00663         CTrapezeFuzzySet::CTrapezeFuzzySet(char *name, double p1,double p2,double p3, double p4) : IFuzzySet(name)
00664         {
00665                 _X1 = p1;
00666                 _X2 = p2;
00667                 _X3 = p3;
00668                 _X4 = p4;
00669         }
00670  
00671         CTrapezeFuzzySet::CTrapezeFuzzySet(const CTrapezeFuzzySet &cp) : IFuzzySet(cp._Name)
00672         {
00673                 _X1 = cp._X1;
00674                 _X2 = cp._X2;
00675                 _X3 = cp._X3;
00676                 _X4 = cp._X4;
00677         }
00678 
00679         void CTrapezeFuzzySet::init(NLAIAGENT::IObjectIA *params)
00680         {
00681                 if ( ((NLAIAGENT::IBaseGroupType *)params)->size() != 3 ) 
00682                 {
00683                 //      throw Exc::
00684                 }
00685                 NLAIAGENT::IObjectIA *arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00686                 _X1 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00687                 arg->release();
00688                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00689                 _X2 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00690                 arg->release();
00691                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00692                 _X3 = (double) ((NLAIAGENT::DDigitalType *) arg)->getValue();
00693                 arg->release();
00694                 arg = (NLAIAGENT::IObjectIA *) ((NLAIAGENT::IBaseGroupType *)params)->popFront();
00695                 _X4 = (double) ((NLAIAGENT::DDigitalType *)arg)->getValue();
00696                 arg->release();
00697 
00698         }
00699 
00700         CTrapezeFuzzySet::~CTrapezeFuzzySet()
00701         {
00702         }
00703 
00704         double CTrapezeFuzzySet::membership(double val)
00705         {
00706                 if ( val >= _X2 && val <= _X3 )
00707                         return 1.0;
00708 
00709                 if ( val > _X1 && val < _X2 )
00710                 {
00711                         return ( ( val - _X1 ) / ( _X2 - _X1 ) );
00712                 }
00713 
00714                 if ( val > _X3 && val < _X2 )
00715                 {
00716                         return ( ( val - _X3 ) / ( _X4 - _X3 ) );
00717                 }
00718 
00719                 return 0.0;
00720         }
00721 
00722         bool CTrapezeFuzzySet::isIn(double val)
00723         {
00724                 return ( val > _X1 && val < _X4 );
00725         }
00726 
00727         double CTrapezeFuzzySet::surface()
00728         {
00729                 return (_X2 - _X1) /2 + (_X3 - _X2) + (_X4 - _X3) / 2;
00730         }
00731 
00732         double CTrapezeFuzzySet::center()
00733         {
00734                 return (_X1 + _X2) /2 + (_X2 + _X3) /4;
00735         }
00736 
00737         const NLAIC::IBasicType *CTrapezeFuzzySet::clone() const
00738         { 
00739                 CTrapezeFuzzySet *clone = new CTrapezeFuzzySet( *this );
00740                 return clone;
00741         }
00742         const NLAIC::IBasicType *CTrapezeFuzzySet::newInstance() const
00743         {
00744                 NLAIC::IBasicType *instance = new CTrapezeFuzzySet( *this );
00745                 return instance;
00746         }
00747 
00748         const NLAIC::CIdentType &CTrapezeFuzzySet::getType() const
00749         {
00750                 return IdTrapezeFuzzySet;
00751         }
00752 
00753         void CTrapezeFuzzySet::save(NLMISC::IStream &os)
00754         {
00755                 IFuzzySet::save(os);
00756                 os.serial( (double &) _X1 );
00757                 os.serial( (double &) _X2 );
00758                 os.serial( (double &) _X3 );
00759                 os.serial( (double &) _X4 );
00760         }
00761 
00762         void CTrapezeFuzzySet::load(NLMISC::IStream &is)
00763         {
00764         }
00765 
00766         void CTrapezeFuzzySet::getDebugString(std::string &txt) const
00767         {
00768                 txt += NLAIC::stringGetBuild("CTrapezeFuzzySet %s [%f , %f , %f , %f]", _Name, _X1, _X2, _X3, _X4);
00769         }
00770         
00771         bool CTrapezeFuzzySet::isEqual(const NLAIAGENT::IBasicObjectIA &a) const
00772         {
00773                 return ( ((CTrapezeFuzzySet &)a)._X1 == _X1 && 
00774                                  ((CTrapezeFuzzySet &)a)._X2 == _X2 && 
00775                                  ((CTrapezeFuzzySet &)a)._X3 == _X3 &&
00776                                  ((CTrapezeFuzzySet &)a)._X4 == _X4 );
00777         }
00778 
00779         const NLAIAGENT::IObjectIA::CProcessResult &CTrapezeFuzzySet::run()
00780         {
00781                 return NLAIAGENT::IObjectIA::ProcessRun;
00782         }
00783 }