• No se han encontrado resultados

Acciones del dueño del terreno invadido frente a una construcción extralimitada.

In document La accesión invertida (página 44-55)

Let’s develop C++ code to create a simple fuzzifier. A class called category is defined in Listing 3.1. This class encapsulates the data that we need to define, the categories in Figure 3.2. There are three private data members called lowval, midval, and highval. These

represent the values on the graph that define the category triangle. In the tight category, the lowval is 5.0, the midval is 8.5, and the highval is 12.0. The category class allows you to instantiate a category object and assign parameters to it to define it. Also, there is a string called name that identifies the category, e.g. “tight.” Various member functions are used to interface to the private data members. There is setval(), for example, which lets you set the value of the three parameters, while gethighval() returns the value of the parameter

highval. The function getshare() returns the relative value of membership in a category given an input. In the example discussed earlier, with the number 8.0 as the Fed discount rate and the category tight defined according to the graph in Figure 3.2, getshare() would return 0.8. Note that this is not yet normalized. Following this example, the getshare() value from the accommodative category would also be used to determine the membership weights. These weights define a probability in a given category. A random number

generator is used to define a value that is used to select a fuzzy category based on the probabilities defined.

Listing 3.1 fuzzfier.h

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

// fuzzfier.h V. Rao, H. Rao // 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);

Previous Table of Contents Next Copyright © IDG Books Worldwide, Inc.

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

C++ Neural Networks and Fuzzy Logic

by Valluru B. Rao

M&T Books, IDG Books Worldwide, Inc. ISBN: 1558515526 Pub Date: 06/01/95

Previous Table of Contents Next

Let’s look at the implementation file in Listing 3.2.

Listing 3.2 fuzzfier.cpp

// fuzzfier.cpp V. Rao, H. Rao // 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() file:///H:/edonkey/docs/c/(ebook-pdf)%20-%20mathem..._Neural_Networks_and_Fuzzy_Logic/ch03/037-043.html (1 of 6) [21/11/02 21:56:55]

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic { return lowval; } float category::getmidval() { return midval; } float category::gethighval() { return highval; }

float category::getshare(const 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 file:///H:/edonkey/docs/c/(ebook-pdf)%20-%20mathem..._Neural_Networks_and_Fuzzy_Logic/ch03/037-043.html (2 of 6) [21/11/02 21:56:55]

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

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=0,randnum; float l,m,h, inval=1.0; char input[30]=" "; category * ptr[10]; float relprob[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";

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

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, separated 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 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);

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

total+=relprob[j]; }

if (total==0) {

cout << "data out of range\n"; exit(1); } randnum=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"; } }

cout << "\n\nAll done. Have a fuzzy day !\n"; }

This program first sets up all the categories you define. These could be for the example we choose or any example you can think of. After the categories are defined, you can start entering data to be fuzzified. As you enter data you see the probability aspect come into play. If you enter the same value twice, you may end up with different categories! You will

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

see sample output shortly, but first a technical note on how the weighted probabilities are set up. The best way to explain it is with an example. Suppose that you have defined three categories, A, B, and C. Suppose that category A has a relative membership of 0.8,

category B of 0.4, and category C of 0.2. In the program, these numbers are first multiplied by 100, so you end up with A=80, B=40, and C=20. Now these are stored in a vector with an index j initialized to point to the first category. Let’s say that these three numbers represent three adjacent number bins that are joined together. Now pick a random number to index into the bin that has its maximum value of (80+40+20). If the number is 100, then it is greater than 80 and less than (80+40), you end up in the second bin that represents B. Does this scheme give you weighted probabilities? Yes it does, since the size of the bin (given a uniform distribution of random indexes into it) determines the probability of falling into the bin. Therefore, the probability of falling into bin A is 80/(80+40+20).

Previous Table of Contents Next

Copyright © IDG Books Worldwide, Inc.

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

C++ Neural Networks and Fuzzy Logic

by Valluru B. Rao

M&T Books, IDG Books Worldwide, Inc.

ISBN: 1558515526 Pub Date: 06/01/95

Previous Table of Contents Next

Sample output from the program is shown below. Our input is in italic; computer output is not. The categories defined by the graph in Figure 3.2 are entered in this example. Once the categories are set up, the first data entry of 4.0 gets fuzzified to the accommodative category. Note that the memberships are also presented in each category. The same value is entered again, and this time it gets fuzzified to the very accommodative category. For the last data entry of 12.5, you see that only the very tight category holds membership for this value. In all cases you will note that the memberships add up to 1.0.

fuzzfier

Please type in a category name, e.g. Cool Enter one word without spaces

When you are done, type `done' :

v.accommodative

Type in the lowval, midval and highval for each category, separated by spaces e.g. 1.0 3.0 5.0 :

0 3 6

Please type in a category name, e.g. Cool Enter one word without spaces

When you are done, type `done' :

accommodative

Type in the lowval, midval and highval for each category, separated by spaces e.g. 1.0 3.0 5.0 :

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

3 6 9

Please type in a category name, e.g. Cool Enter one word without spaces

When you are done, type `done' :

tight

Type in the lowval, midval and highval for each category, separated by spaces e.g. 1.0 3.0 5.0 :

5 8.5 12

Please type in a category name, e.g. Cool Enter one word without spaces

When you are done, type `done' :

v.tight

Type in the lowval, midval and highval for each category, separated by spaces e.g. 1.0 3.0 5.0 :

10 12 14

Please type in a category name, e.g. Cool Enter one word without spaces

When you are done, type `done' :

done

=================================== ==Fuzzifier is ready for data== =================================== input a data value, type 0 to terminate

4.0

Output fuzzy category is ==> accommodative<== category membership

---

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic

v.accommodative 0.666667 accommodative 0.333333 tight 0

v.tight 0

input a data value, type 0 to terminate

4.0

Output fuzzy category is ==> v.accommodative<== category membership --- v.accommodative 0.666667 accommodative 0.333333 tight 0 v.tight 0

input a data value, type 0 to terminate

7.5

Output fuzzy category is ==> accommodative<== category membership --- v.accommodative 0 accommodative 0.411765 tight 0.588235 v.tight 0

input a data value, type 0 to terminate

11.0

Output fuzzy category is ==> tight<== category membership --- v.accommodative 0 accommodative 0 tight 0.363636 v.tight 0.636364

input a data value, type 0 to terminate

12.5

Output fuzzy category is ==> v.tight<==

C++ Neural Networks and Fuzzy Logic:A Look at Fuzzy Logic category membership --- v.accommodative 0 accommodative 0 tight 0 v.tight 1

input a data value, type 0 to terminate

0

All done. Have a fuzzy day !

In document La accesión invertida (página 44-55)

Documento similar