Skip to content

Instantly share code, notes, and snippets.

@Ronnasayd
Last active October 13, 2020 14:40
Show Gist options
  • Save Ronnasayd/9278406 to your computer and use it in GitHub Desktop.
Save Ronnasayd/9278406 to your computer and use it in GitHub Desktop.
Exemplo de teste da biblioteca FLL
#include "Fuzzy.h"
#include "Fuzzy.cpp"
#include "FuzzyComposition.h"
#include "FuzzyComposition.cpp"
#include "FuzzyInput.h"
#include "FuzzyInput.cpp"
#include "FuzzyIO.h"
#include "FuzzyIO.cpp"
#include "FuzzyOutput.h"
#include "FuzzyOutput.cpp"
#include "FuzzyRule.h"
#include "FuzzyRule.cpp"
#include "FuzzyRuleAntecedent.h"
#include "FuzzyRuleAntecedent.cpp"
#include "FuzzyRuleConsequent.h"
#include "FuzzyRuleConsequent.cpp"
#include "FuzzySet.h"
#include "FuzzySet.cpp"
#include<iostream.h>
#include <fstream>
// setting the maximum and minimum values
#define MAX_DISTANCE_FRONT 50
#define MIN_DISTANCE_FRONT 1
#define MAX_DISTANCE_LATERAL 20
#define MIN_DISTANCE_LATERAL 1
#define MAX_SPEED 320
#define MIN_SPEED 0
using namespace std;
int main(int argc, char const *argv[])
{
Fuzzy* fuzzy = new Fuzzy();
FuzzyInput* frontDistanceSensor = new FuzzyInput(1);
FuzzyInput* lateralDistanceSensor = new FuzzyInput(2);
// create the FuzzySets that copose the FuzzyInput objects
FuzzySet* smallFrontDistance = new FuzzySet(MIN_DISTANCE_FRONT,MIN_DISTANCE_FRONT,MAX_DISTANCE_FRONT/4,MAX_DISTANCE_FRONT/2);
frontDistanceSensor->addFuzzySet(smallFrontDistance);// add the FuzzySet in the FuzzyInput object
FuzzySet* smallLateralDistance = new FuzzySet(MIN_DISTANCE_LATERAL,MIN_DISTANCE_LATERAL,MAX_DISTANCE_LATERAL/4,MAX_DISTANCE_LATERAL/2);
lateralDistanceSensor->addFuzzySet(smallLateralDistance);
FuzzySet* safeFrontDistance = new FuzzySet(MAX_DISTANCE_FRONT/4,MAX_DISTANCE_FRONT/2,MAX_DISTANCE_FRONT/2,MAX_DISTANCE_FRONT*3/4);
frontDistanceSensor->addFuzzySet(safeFrontDistance);
FuzzySet* safeLateralDistance = new FuzzySet(MAX_DISTANCE_LATERAL/4,MAX_DISTANCE_LATERAL/2,MAX_DISTANCE_LATERAL/2,MAX_DISTANCE_LATERAL*3/4);
lateralDistanceSensor->addFuzzySet(safeLateralDistance);
FuzzySet* bigFrontDistance = new FuzzySet(MAX_DISTANCE_FRONT/2,MAX_DISTANCE_FRONT*3/4,MAX_DISTANCE_FRONT,MAX_DISTANCE_FRONT);
frontDistanceSensor->addFuzzySet(bigFrontDistance);
FuzzySet* bigLateralDistance = new FuzzySet(MAX_DISTANCE_LATERAL/2,MAX_DISTANCE_LATERAL*3/4,MAX_DISTANCE_LATERAL,MAX_DISTANCE_LATERAL);
lateralDistanceSensor->addFuzzySet(bigLateralDistance);
// add the FuzzyInputs in the Fuzzy Object
fuzzy->addFuzzyInput(lateralDistanceSensor);
fuzzy->addFuzzyInput(frontDistanceSensor);
// create two new FuzzyOutput objects
FuzzyOutput* rightWheelSpeed = new FuzzyOutput(1);
FuzzyOutput* leftWheelSpeed = new FuzzyOutput(2);
// create the FuzzySets that copose the FuzzyOutput objects
FuzzySet* lowerRightSpeed = new FuzzySet(MIN_SPEED,MIN_SPEED,MAX_SPEED/4,MAX_SPEED/2);
rightWheelSpeed->addFuzzySet(lowerRightSpeed);// add the FuzzySet in the FuzzyInput object
FuzzySet* lowerLeftSpeed = new FuzzySet(MIN_SPEED,MIN_SPEED,MAX_SPEED/4,MAX_SPEED/2);
leftWheelSpeed->addFuzzySet(lowerLeftSpeed);
FuzzySet* averageRightSpeed = new FuzzySet(MAX_SPEED/4,MAX_SPEED/2,MAX_SPEED/2,MAX_SPEED*3/4);
rightWheelSpeed->addFuzzySet(averageRightSpeed);
FuzzySet* averageLeftSpeed = new FuzzySet(MAX_SPEED/4,MAX_SPEED/2,MAX_SPEED/2,MAX_SPEED*3/4);
leftWheelSpeed->addFuzzySet(averageLeftSpeed);
FuzzySet* fastRightSpeed = new FuzzySet(MAX_SPEED/2,MAX_SPEED*3/4,MAX_SPEED,MAX_SPEED);
rightWheelSpeed->addFuzzySet(fastRightSpeed);
FuzzySet* fastLeftSpeed = new FuzzySet(MAX_SPEED/2,MAX_SPEED*3/4,MAX_SPEED,MAX_SPEED);
leftWheelSpeed->addFuzzySet(fastLeftSpeed);
// add the FuzzyOutputs in the Fuzzy object
fuzzy->addFuzzyOutput(rightWheelSpeed);
fuzzy->addFuzzyOutput(leftWheelSpeed);
// building the antecedents fuzzy rules
FuzzyRuleAntecedent* smallFrontDistanceAndSmallLateralDistance = new FuzzyRuleAntecedent();
smallFrontDistanceAndSmallLateralDistance->joinWithAND(smallFrontDistance,smallLateralDistance);
FuzzyRuleAntecedent* smallFrontDistanceAndSafeLateralDistance = new FuzzyRuleAntecedent();
smallFrontDistanceAndSafeLateralDistance->joinWithAND(smallFrontDistance,safeLateralDistance);
FuzzyRuleAntecedent* smallFrontDistanceAndBigLateralDistance = new FuzzyRuleAntecedent();
smallFrontDistanceAndBigLateralDistance->joinWithAND(smallFrontDistance,bigLateralDistance);
FuzzyRuleAntecedent* safeFrontDistanceAndSmallLateralDistance = new FuzzyRuleAntecedent();
safeFrontDistanceAndSmallLateralDistance->joinWithAND(safeFrontDistance,smallLateralDistance);
FuzzyRuleAntecedent* safeFrontDistanceAndSafeLateralDistance = new FuzzyRuleAntecedent();
safeFrontDistanceAndSafeLateralDistance->joinWithAND(safeFrontDistance,safeLateralDistance);
FuzzyRuleAntecedent* safeFrontDistanceAndBigLateralDistance = new FuzzyRuleAntecedent();
safeFrontDistanceAndBigLateralDistance->joinWithAND(safeFrontDistance,bigLateralDistance);
FuzzyRuleAntecedent* bigFrontDistanceAndSmallLateralDistance = new FuzzyRuleAntecedent();
bigFrontDistanceAndSmallLateralDistance->joinWithAND(bigFrontDistance,smallLateralDistance);
FuzzyRuleAntecedent* bigFrontDistanceAndSafeLateralDistance = new FuzzyRuleAntecedent();
bigFrontDistanceAndSafeLateralDistance->joinWithAND(bigFrontDistance,safeLateralDistance);
FuzzyRuleAntecedent* bigFrontDistanceAndBigLateralDistance = new FuzzyRuleAntecedent();
bigFrontDistanceAndBigLateralDistance->joinWithAND(bigFrontDistance,bigLateralDistance);
// building the consequent fuzzy rules
FuzzyRuleConsequent* fastRightSpeedWheel = new FuzzyRuleConsequent();
fastRightSpeedWheel->addOutput(fastRightSpeed);
FuzzyRuleConsequent* averageRightSpeedWheel = new FuzzyRuleConsequent();
averageRightSpeedWheel->addOutput(averageRightSpeed);
FuzzyRuleConsequent* lowerRightSpeedWheel = new FuzzyRuleConsequent();
lowerRightSpeedWheel->addOutput(lowerRightSpeed);
FuzzyRuleConsequent* fastLeftSpeedWheel = new FuzzyRuleConsequent();
fastLeftSpeedWheel->addOutput(fastLeftSpeed);
FuzzyRuleConsequent* averageLeftSpeedWheel = new FuzzyRuleConsequent();
averageLeftSpeedWheel->addOutput(averageLeftSpeed);
FuzzyRuleConsequent* lowerLeftSpeedWheel = new FuzzyRuleConsequent();
lowerLeftSpeedWheel->addOutput(lowerLeftSpeed);
// connection between antecedent and consequent rules
FuzzyRule* fuzzyRule01 = new FuzzyRule(1,smallFrontDistanceAndSmallLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule01);
FuzzyRule* fuzzyRule02 = new FuzzyRule(2,smallFrontDistanceAndSmallLateralDistance,lowerLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule02);
FuzzyRule* fuzzyRule03 = new FuzzyRule(3,smallFrontDistanceAndSafeLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule03);
FuzzyRule* fuzzyRule04 = new FuzzyRule(4,smallFrontDistanceAndSafeLateralDistance,lowerLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule04);
FuzzyRule* fuzzyRule05 = new FuzzyRule(5,smallFrontDistanceAndBigLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule05);
FuzzyRule* fuzzyRule06 = new FuzzyRule(6,smallFrontDistanceAndBigLateralDistance,lowerLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule06);
FuzzyRule* fuzzyRule07 = new FuzzyRule(7,safeFrontDistanceAndSmallLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule07);
FuzzyRule* fuzzyRule08 = new FuzzyRule(8,safeFrontDistanceAndSmallLateralDistance,averageLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule08);
FuzzyRule* fuzzyRule09 = new FuzzyRule(9,safeFrontDistanceAndSafeLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule09);
FuzzyRule* fuzzyRule10 = new FuzzyRule(10,safeFrontDistanceAndSafeLateralDistance,fastLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule10);
FuzzyRule* fuzzyRule11 = new FuzzyRule(11,safeFrontDistanceAndBigLateralDistance,averageRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule11);
FuzzyRule* fuzzyRule12 = new FuzzyRule(12,safeFrontDistanceAndBigLateralDistance,fastLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule12);
FuzzyRule* fuzzyRule13 = new FuzzyRule(13,bigFrontDistanceAndSmallLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule13);
FuzzyRule* fuzzyRule14 = new FuzzyRule(14,bigFrontDistanceAndSmallLateralDistance,averageLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule14);
FuzzyRule* fuzzyRule15 = new FuzzyRule(15,bigFrontDistanceAndSafeLateralDistance,fastRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule15);
FuzzyRule* fuzzyRule16 = new FuzzyRule(16,bigFrontDistanceAndSafeLateralDistance,fastLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule16);
FuzzyRule* fuzzyRule17 = new FuzzyRule(17,bigFrontDistanceAndBigLateralDistance,lowerRightSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule17);
FuzzyRule* fuzzyRule18 = new FuzzyRule(18,bigFrontDistanceAndBigLateralDistance,fastLeftSpeedWheel);
fuzzy->addFuzzyRule(fuzzyRule18);
ofstream myfile;
myfile.open ("out6.txt");
int i,j;
for(i = 1 ; i < 100; i++)
for(j = 1 ; j < 100; j++){
fuzzy->setInput(1,i);
fuzzy->setInput(2,j);
fuzzy->fuzzify();
float ve = fuzzy->defuzzify(2);
float vd = fuzzy->defuzzify(1);
//myfile <<"SF: "<<i<<" SR: "<<j<<" VR: "<< int(vd)<<" VL: "<<int(ve)<<"\n";
myfile <<i<<" "<<j<<" "<< int(vd)<<" "<<int(ve)<<"\n";
}
myfile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment