home Forums Feature Requests Reading/Writing FIS files using FuzzyLite

Tagged: 

This topic contains 10 replies, has 3 voices, and was last updated by CF Kwong CF Kwong 2 years, 10 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #830
    CF Kwong
    CF Kwong
    Participant

    Hello again Juan,

    I”m working on a fuzzy logic problem that requires me to integrate the original code into the Network Simulator 3, or commonly known as NS-3. Previously I’ve modified the original C code from MATLAB’s Fuzzy Logic toolbox written by Roger Jang (i.e. “fismain.c”). The file “fis.c” is left unmodified, and it works perfectly. However the problem is the NS-3 is a C++ platform which requires different coding style, which I am not entirely familiar with. I just need 3 input to produce a single output using Mamdani or Sugeno type fuzzy engine.

    Do FuzzyLite has a simpler version just like “fis.c” and “fismain.c“?

    Many, many thanks in advance.

    CF Kwong

    The modified code from “fismain.c” is as follows.

    #include <fis.c>
    #include <stdio.h>

    double SS = 0.5;
    double NC = 0.5;
    double BER= 0.5;

    FuzzyDataFile = fopen(“Fuzzy_Logic.dat”,”w+”);
    fprintf (FuzzyDataFile, “%f %f %f\n”, SS, NC, BER);
    fclose(FuzzyDataFile);

    /* Writing a Data Matrix file */
    dataMatrix = returnDataMatrix(“Fuzzy_Logic.dat”, &data_row_n, &data_col_n);

    /* Fuzzy Inference System (FIS) File */
    fisMatrix = returnFismatrix(“Handover_Decision.fis”, &fis_row_n, &fis_col_n);

    /* Build FIS data structure */
    fis = (FIS *)fisCalloc(1, sizeof(FIS));
    fisBuildFisNode(fis, fisMatrix, fis_col_n, MF_POINT_N);

    /* Error Checking */
    if (data_col_n < fis->in_n)
    {
    PRINTF(“Given FIS is a %d-input %d-output system.\n”,fis->in_n, fis->out_n);
    PRINTF(“Given data file does not have enough input entries.\n”);
    fisFreeMatrix((void **)dataMatrix, data_row_n);
    fisFreeMatrix((void **)fisMatrix, fis_row_n);
    fisFreeFisNode(fis);
    }

    /* Create output matrix */
    outputMatrix = (DOUBLE **)fisCreateMatrix(data_row_n, fis->out_n, sizeof(DOUBLE));

    /* Evaluate FIS on each input vector */
    for (i = 0; i < data_row_n; i++)
    getFisOutput(dataMatrix[i], fis, outputMatrix[i]);

    /* Output vector */
    for (i = 0; i < data_row_n; i++)
    {
    for (j = 0; j < fis->out_n; j++)
    {
    results = outputMatrix[i][j]; // Here is the output after defuzzification
    }
    }

    /* clean up FIS memory */
    fisFreeFisNode(fis);
    fisFreeMatrix((void **)dataMatrix, data_row_n);
    fisFreeMatrix((void **)fisMatrix, fis_row_n);
    fisFreeMatrix((void **)outputMatrix, data_row_n);

    #833
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    Hi,

    I do not know what the goal is for fis.c or fismain.c. It seems to me that you want to have the data in FLD format stored in a two-dimensional matrix. You can do that if you parse the results obtained with the FldExporter.

    However, if you want custom input vectors, you can have two matrices, one for inputs and the other for outputs. Then, you can easily create both matrices yourself using fuzzylite. You do not need the fis.c or fismain.c to do it for you, roughly, you can do it as follows.

    for every input vector v:
       for every input variable i:
         i.setInputValue(v[i]);
       engine.process();
     for every output variable o:
       w[o] = o.defuzzify();

    where v is a row of the input matrix, w is a row of the output matrix.

    #841
    CF Kwong
    CF Kwong
    Participant

    Thank you, much appreciated.

    Here’s another question:

    Let say if I have a string of data store inside, say InputData.txt with three column format “SS BER QOS” where SS, BER and QOS is the crisp data input before the fuzzier, and I want to feed those data into fuzzylite.

    The output will be stored inside a file, say OutputData.txt, Can fuzzylite do such or I need to do some minor modification of the codes? I am using MATLAB generated FIS data, i.e. HD.fis.

    Hope to hear from you soon.

    Cheers!

    CF Kwong

    #843
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    Hi,

    fuzzylite does not deal with Files, and I think I will keep it that way for the foreseeable future. However, I will consider using custom vectors for the next version.

    You need to investigate how to read and write files from your computer using C++. I have done reading files using std::ifstream and std::getline. For every line read, you can use std::istringstream as a tokenizer that reads the line and gets each of the values in the line and stores them in a matrix. Afterwards:
    1) traverse the matrix using the values to prepare the input variables (using InputVariable::setInputValue)
    2) call method Engine::process
    3) defuzzify the values of each output variable and store them in the matrix
    4) Write to file the matrix or part of the matrix

    Also, I have changed the title of the post to reflect better its contents.

    Cheers.

    #870
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    Hi CF,

    Another person has asked for the same fis.c and fismain.c file, but now I understand what you want to do!
    I will definitely add that feature to fuzzylite soonish and let you know.

    Cheers,

    #872
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    What you require is not too hard and you do not need to build fuzzylite from source to achieve so.

    Roughly, in SomeFile.cpp:

    
    #include <fl/Headers.h>
    
    int main(int argc, char** argv){
       std::string fisEngine = readFromSomeFile("xxx.fis");
       std::vector<std::vector<fl::scalar> > inputs = readFromSomeFileAndParse("xxx.dat");
       fl::Engine* engine = FisImporter().fromString(fisEngine);
       for (std::size_t i = 0 ; i < inputs.size(); ++i){
           for (std::size_t x = 0 ; x < inputs.at(i).size(); ++x){
                engine->getInputVariable(x)->setInputValue(inputs.at(i).at(x));
           }
           engine->process();
           for (int i = 0 ; i < engine->numberOfOutputVariables(); ++i){
              fl::scalar output = engine->getOutputVariable(i)->defuzzify(); 
              writeToSomeFile(output + " ");
           }
           writeToSomeFile(output + "\n");
    }
    

    You have to implement yourself the read and write functions and fix some details.

    g++ SomeFile.cpp -I/path/to/fuzzylite/fuzzylite -L/path/to/fuzzylite/fuzzylite/bin -lfuzzylite

    #1029

    hacmachdien
    Participant

    Hi Juan,
    I have noticed that you’ve included “import from Fuzzy Inference System (FIS)” feature to fuzzylite 4.0 GUI but it seems that no explicit instruction on how to use the feature is available. Could you provide an example to clarify the matter?

    #1030
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    Hi hacmachdien,

    If you have a fuzzy engine in “fis” format (check examples), then you can copy and paste the contents of the “fis” file into the import window.

    For example, if you copy and paste the following block into the import window, you will have a fuzzy engine ready to use.

    
    [System]
    Name='simple-dimmer'
    Type='mamdani'
    NumInputs=1
    NumOutputs=1
    NumRules=3
    AndMethod=''
    OrMethod=''
    ImpMethod='min'
    AggMethod='max'
    DefuzzMethod='centroid'
    
    [Input1]
    Enabled=1
    Name='Ambient'
    Range=[0.000 1.000]
    NumMFs=3
    MF1='DARK':'trimf',[0.000 0.250 0.500]
    MF2='MEDIUM':'trimf',[0.250 0.500 0.750]
    MF3='BRIGHT':'trimf',[0.500 0.750 1.000]
    
    [Output1]
    Enabled=1
    Name='Power'
    Range=[0.000 1.000]
    Default=nan
    LockValid=0
    LockRange=0
    NumMFs=3
    MF1='LOW':'trimf',[0.000 0.250 0.500]
    MF2='MEDIUM':'trimf',[0.250 0.500 0.750]
    MF3='HIGH':'trimf',[0.500 0.750 1.000]
    
    [Rules]
    1.000 , 3.000 (1.000) : 1
    2.000 , 2.000 (1.000) : 1
    3.000 , 1.000 (1.000) : 1
    
    #1036

    hacmachdien
    Participant

    Thanks a lot, Juan
    The reply was a great help to me. Just another curious notion about fuzzylite 4.0 C++ codes: Is it easily applicable in hardware implementation? I using your fuzzy GUI and C++ code generator to create a real-time application on FPGA environment. I guess it would take me tons of time to convert the C++ codes to synthesizable Fixed-Point (if the conversion is even possible).

    #1037
    Juan Rada-Vilela
    Juan Rada-Vilela
    Keymaster

    I am not sure about hardware implementation, but later this year I expect to export to pure C, for which it should be compatible for hardware implementation.

    #1061
    CF Kwong
    CF Kwong
    Participant

    Hi Juan,

    Just saw your reply here. Many thanks for your reply. I will see if the code is working on my situation or not.

    Cheers!

    CF Kwong

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.