Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tevong

Fuzzylogic

This topic is 6094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone give me a quick brief or source code in c++ of fuzzylogic? I know it involves taking 2 FLV and using a FAM to output a behavior but I''m not sure how it''s supposed to be written.

Share this post


Link to post
Share on other sites
Advertisement
These two file listings give you a fuzzifier. I got them out of a book on neural nets. I forget the book title, I remember it was something about C++ and neural nets. But this is fuzzy logic, don't worry.

BTW, this doesn't really sound like what you're asking for, it doesn't output a behavior or anything like that... :/

    
// fuzzfier.h
// program to fuzzify data

class category
{
private:
char name[30];
float lowval, highval, midval

public:
category(){};
void setname(char *);
char * getname();
void setval(float&, float&, float&);
float getlowval;
float getmidval();
float gethighval();

float getshare(const float&);

~category(){};
};

int randnum(int);



...

        
// fuzzfier.cpp
// program to fuzzify data

#include <iostream.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include "fuzzfier.h"


void category::setname(char *n)
{
strcpy(name,n);
}

char * category::getname()
{
return name;
}

void category::setval(float &h, float &m; float &l)
{
highval = h;
midval = m;
lowval = l;
}

float category::getlowval()
{
return lowval;
}

float category::getmidval()
{
return midval;
}

float category::gethighval()
{
return highval;
}

float category::getshare(conts float & input)
{
// this member function returns the relative membership
// of an input in a category, with a maximum of 1.0

float output;
float midlow, highmid;

midlow = midval-lowval;
highmid = highval - midval;

// if outside the range, then output = 0
if((input <= lowval) || (input >= highval))
output = 0;
else
{
if (input > midval)
output = (highval - input)/highmid;
else
if (input == midval)
output = 1.0;
else
output = (input - lowval)/midlow;
}

return output;
}

int randomnum(int maxval)
{
// random number generator
// will return an integer up to maxval

srand((unsigned)time(NULL));
return rand() % maxval;
}

void main()
{
// a fuzzifier program that takes category information:
// lowval, midval and highval and category name
// and fuzzifies an input based on the total number of
// categories and the membership in each category

int i=0,j=0,numcat,randnum;
float l,m,h, inval=1.0;

char input[30]=" ";
category * ptr[10];
float relprog[10];
float total=0,runtotal=0;

// input the category information; terminate with 'done'
while(1)
{
cout << "\nPlease type in a category name, e.g. Cool\n";
cout << "Enter one word without spaces\n";
cout << "When you are done, type 'done':\n\n";

ptr[i] = new category;
cin >> input;

if ((input[0]=='d' && input[1] == 'o' && input[2] == 'n' && input[3] == 'e')) break;

ptr[i]->setname(input);

cout << "\nType in the lowval, midval and highval\n";
cout << "for each category, seperated by spaces\n";
cout << " e.g. 1.0 3.0 5.0 :\n\n";

cin >> l >> m >> h;
ptr[i]->setval(h,m,l);

i++
}

numcat=i; // number of categories

// Categories are now set up: Now input the data to fuzzify
cout << "\n\n";
cout << "===========================================\n";
cout << "== Fuzzifier is ready for data ==\n";
cout << "===========================================\n";

while (1)
{
cout << "\ninput a data value, type 0 to terminate\n";

cin >> inval;

if (inval ==0) break;

// calculate relative probabilities of input being in each category
total = 0;

for (j=0;j<numcat;j++)
{
relprob[j]=100*ptr[j]->getshare(inval);
total+=relprob[j];
}

if (total == 0)
{
cout << "data out of range\n";
exit(1);
}

randnumb=randomnum((int)total);

j=0;
runtotal=relprob[0];

while ((runtotal<randnum)&&(j<numcat))
{
j++
runtotal += relprob[j];
}

cout << "\nOutput fuzzy category is ==> " << ptr[j]->getname()<<"<==\n";
cout << "category\t"<<"membership\n";
cout << "---------------\n";

for (j=0;j<numcat;j++)
{
cout << ptr[j]->getname() << "\t\t" << (relprob[j]/total) << "\n";
}
}
}



[EDIT: changed code tags to source tags... didn't like the italics.]
[EDIT: some things just didn't seem right... like a bunch of #includes one next to another on the same line...]

[EDIT: nearly 4 years later (3 days short of it, in fact), noticed an error and fixed it.]

Edited by - coldacid on October 14, 2001 10:37:44 AM
Edited by - coldacid on October 14, 2001 10:43:09 AM


[Edited by - coldacid on October 11, 2005 3:28:23 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!