Beispiel #1
0
TEST(Fuzzy, addFuzzyRule){
  Fuzzy* fuzzy = new Fuzzy();

  FuzzyRuleAntecedent* seTemperatureLowOrpressureSmall = new FuzzyRuleAntecedent();
  FuzzySet* temperatureLow = new FuzzySet(0, 10, 10, 20);
  temperatureLow->setPertinence(0.5);
  FuzzySet* pressureSmall = new FuzzySet(0, 10, 10, 20);
  pressureSmall->setPertinence(0.5);
  seTemperatureLowOrpressureSmall->joinWithOR(temperatureLow, pressureSmall);

  FuzzyRuleAntecedent* andRiskMinimum = new FuzzyRuleAntecedent();
  FuzzySet* riskMinimum = new FuzzySet(0, 10, 10, 20);
  riskMinimum->setPertinence(0.7);
  andRiskMinimum->joinSingle(riskMinimum);

  FuzzyRuleAntecedent* seTemperatureLowOrpressureSmallandRiskMinimum = new FuzzyRuleAntecedent();
  seTemperatureLowOrpressureSmallandRiskMinimum->joinWithAND(seTemperatureLowOrpressureSmall, andRiskMinimum);

  FuzzyRuleConsequent* thenDangerLow = new FuzzyRuleConsequent();

  FuzzySet* dangerLow = new FuzzySet(0, 10, 10, 20);

  thenDangerLow->addOutput(dangerLow);

  FuzzyRule* fuzzyRule = new FuzzyRule(1, seTemperatureLowOrpressureSmallandRiskMinimum, thenDangerLow);
  
  bool result = fuzzy->addFuzzyRule(fuzzyRule);

  EXPECT_TRUE(result);
}
Beispiel #2
0
TEST(FuzzyRuleAntecedent, joinSingle){
  FuzzyRuleAntecedent* fuzzyRuleAntecedent = new FuzzyRuleAntecedent();

  FuzzySet* fuzzySet = new FuzzySet(0, 10, 10, 20);

  bool result = fuzzyRuleAntecedent->joinSingle(fuzzySet);

  EXPECT_TRUE(result);
}
Beispiel #3
0
TEST(Fuzzy, setInputAndFuzzifyAndDefuzzify08){
  Fuzzy* fuzzy = new Fuzzy();

  FuzzyInput* age = new FuzzyInput(1);

  FuzzySet* months = new FuzzySet(-360, -6, 5, 1200);
  age->addFuzzySet(months);

  fuzzy->addFuzzyInput(age);

  FuzzyOutput* quality = new FuzzyOutput(1);

  FuzzySet* avaliation = new FuzzySet(0, 1, 1, 1);
  quality->addFuzzySet(avaliation);

  fuzzy->addFuzzyOutput(quality);

  //-------------------- Montando as regras Fuzzy
  FuzzyRuleAntecedent* ifMonths = new FuzzyRuleAntecedent();
  ifMonths->joinSingle(months);

  FuzzyRuleConsequent* thenAvaliation = new FuzzyRuleConsequent();
  thenAvaliation->addOutput(avaliation);
  
  // Instanciando um objeto FuzzyRule
  FuzzyRule* fuzzyRule01 = new FuzzyRule(1, ifMonths, thenAvaliation);
  fuzzy->addFuzzyRule(fuzzyRule01);

  fuzzy->setInput(1, 50);

  fuzzy->fuzzify();
  // cout << "Pertinência (months): " << months->getPertinence() << endl;

  // cout << "Pertinência (avaliation): " << avaliation->getPertinence() << endl;

  float fuzzyAntecedentEvaluate = ifMonths->evaluate();

  // cout << "Entrada: " << dist << ", Saida: " << output << endl;

  EXPECT_EQ(months->getPertinence(), fuzzyAntecedentEvaluate);
}
int main(int argc, char *argv[]) {
	Fuzzy* fuzzy = new Fuzzy();

	// FuzzyInput
	FuzzyInput* distance = new FuzzyInput(1);

	FuzzySet* close = new FuzzySet(0, 20, 20, 40);
	distance->addFuzzySet(close);
	FuzzySet* safe = new FuzzySet(30, 50, 50, 70);
	distance->addFuzzySet(safe);
	FuzzySet* distante = new FuzzySet(60, 80, 100, 100);
	distance->addFuzzySet(distante);

	fuzzy->addFuzzyInput(distance);

	// FuzzyInput
	FuzzyInput* inputSpeed = new FuzzyInput(2);

	FuzzySet* stoped = new FuzzySet(0, 0, 0, 0);
	inputSpeed->addFuzzySet(stoped);
	FuzzySet* slow = new FuzzySet(1, 10, 10, 20);
	inputSpeed->addFuzzySet(slow);
	FuzzySet* normal = new FuzzySet(15, 30, 30, 50);
	inputSpeed->addFuzzySet(normal);
	FuzzySet* quick = new FuzzySet(45, 60, 70, 70);
	inputSpeed->addFuzzySet(quick);

	fuzzy->addFuzzyInput(inputSpeed);

	// FuzzyInput
	FuzzyInput* temperature = new FuzzyInput(3);

	FuzzySet* cold = new FuzzySet(-30, -30, -20, -10);
	temperature->addFuzzySet(cold);
	FuzzySet* good = new FuzzySet(-15, 0, 0, 15);
	temperature->addFuzzySet(good);
	FuzzySet* hot = new FuzzySet(10, 20, 30, 30);
	temperature->addFuzzySet(hot);

	fuzzy->addFuzzyInput(temperature);

	// FuzzyOutput
	FuzzyOutput* risk = new FuzzyOutput(1);

	FuzzySet* minimum = new FuzzySet(0, 20, 20, 40);
	risk->addFuzzySet(minimum);
	FuzzySet* average = new FuzzySet(30, 50, 50, 70);
	risk->addFuzzySet(average);
	FuzzySet* maximum = new FuzzySet(60, 80, 80, 100);
	risk->addFuzzySet(maximum);

	fuzzy->addFuzzyOutput(risk);

	// FuzzyOutput
	// adicionando speed como output também
	FuzzyOutput* outputSpeed = new FuzzyOutput(2);

	FuzzySet* stopedOut = new FuzzySet(0, 0, 0, 0);
	outputSpeed->addFuzzySet(stopedOut);
	FuzzySet* slowOut = new FuzzySet(1, 10, 10, 20);
	outputSpeed->addFuzzySet(slowOut);
	FuzzySet* normalOut = new FuzzySet(15, 30, 30, 50);
	outputSpeed->addFuzzySet(normalOut);
	FuzzySet* quickOut = new FuzzySet(45, 60, 70, 70);
	outputSpeed->addFuzzySet(quickOut);

	fuzzy->addFuzzyOutput(outputSpeed);

	// Building FuzzyRule
	FuzzyRuleAntecedent* distanceCloseAndSpeedQuick = new FuzzyRuleAntecedent();
	distanceCloseAndSpeedQuick->joinWithAND(close, quick);
	FuzzyRuleAntecedent* temperatureCold = new FuzzyRuleAntecedent();
	temperatureCold->joinSingle(cold);
	FuzzyRuleAntecedent* ifDistanceCloseAndSpeedQuickOrTemperatureCold = new FuzzyRuleAntecedent();
	ifDistanceCloseAndSpeedQuickOrTemperatureCold->joinWithOR(distanceCloseAndSpeedQuick, temperatureCold);

	FuzzyRuleConsequent* thenRisMaximumAndSpeedSlow = new FuzzyRuleConsequent();
	thenRisMaximumAndSpeedSlow->addOutput(maximum);
	thenRisMaximumAndSpeedSlow->addOutput(slowOut);

	FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifDistanceCloseAndSpeedQuickOrTemperatureCold, thenRisMaximumAndSpeedSlow);
	fuzzy->addFuzzyRule(fuzzyRule1);

	// Building FuzzyRule
	FuzzyRuleAntecedent* distanceSafeAndSpeedNormal = new FuzzyRuleAntecedent();
	distanceSafeAndSpeedNormal->joinWithAND(safe, normal);
	FuzzyRuleAntecedent* ifDistanceSafeAndSpeedNormalOrTemperatureGood = new FuzzyRuleAntecedent();
	ifDistanceSafeAndSpeedNormalOrTemperatureGood->joinWithOR(distanceSafeAndSpeedNormal, good);

	FuzzyRuleConsequent* thenRiskAverageAndSpeedNormal = new FuzzyRuleConsequent();
	thenRiskAverageAndSpeedNormal->addOutput(average);
	thenRiskAverageAndSpeedNormal->addOutput(normalOut);

	FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifDistanceSafeAndSpeedNormalOrTemperatureGood, thenRiskAverageAndSpeedNormal);
	fuzzy->addFuzzyRule(fuzzyRule2);

	// Building FuzzyRule
	FuzzyRuleAntecedent* distanceDistanteAndSpeedSlow = new FuzzyRuleAntecedent();
	distanceDistanteAndSpeedSlow->joinWithAND(distante, slow);
	FuzzyRuleAntecedent* ifDistanceDistanteAndSpeedSlowOrTemperatureHot = new FuzzyRuleAntecedent();
	ifDistanceDistanteAndSpeedSlowOrTemperatureHot->joinWithOR(distanceDistanteAndSpeedSlow, hot);

	FuzzyRuleConsequent* thenRiskMinimumSpeedQuick = new FuzzyRuleConsequent();
	thenRiskMinimumSpeedQuick->addOutput(minimum);
	thenRiskMinimumSpeedQuick->addOutput(quickOut);

	FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifDistanceDistanteAndSpeedSlowOrTemperatureHot, thenRiskMinimumSpeedQuick);
	fuzzy->addFuzzyRule(fuzzyRule3);

	fuzzy->setInput(1, 10);
	fuzzy->setInput(2, 30);
	fuzzy->setInput(3, -15);

	fuzzy->fuzzify();

	pwOut << "Distância: " << close->getPertinence() << ", " << safe->getPertinence() << ", " << distante->getPertinence() << '\n';
	pwOut << "Velocidade: " << stoped->getPertinence() << ", " << slow->getPertinence() << ", " << normal->getPertinence() << ", " << quick->getPertinence() << '\n';
	pwOut << "Temperatura: " << cold->getPertinence() << ", " << good->getPertinence() << ", " << hot->getPertinence() << '\n';

	pwOut << "regra1: " << fuzzyRule1->isFired() << ", regra2: " << fuzzyRule2->isFired() << ", regra3: " << fuzzyRule3->isFired() << '\n';

	float output1 = fuzzy->defuzzify(1);
	float output2 = fuzzy->defuzzify(2);

	pwOut << "Saída Risco: " << output1 << ", Saída Velocidade: " << output2 << '\n';

	return 0;
}
Beispiel #5
0
int main(int argc, char *argv[]) {
	// Instanciando um objeto da biblioteca
	Fuzzy* fuzzy = new Fuzzy();

	// Criando o FuzzyInput distancia
	FuzzyInput* distance = new FuzzyInput(1);
	// Criando os FuzzySet que compoem o FuzzyInput distancia 
	FuzzySet* small = new FuzzySet(0, 20, 20, 40); // Distancia pequena
	distance->addFuzzySet(small); // Adicionando o FuzzySet small em distance
	FuzzySet* safe = new FuzzySet(30, 50, 50, 70); // Distancia segura
	distance->addFuzzySet(safe); // Adicionando o FuzzySet safe em distance
	FuzzySet* big = new FuzzySet(60, 80, 80, 80); // Distancia grande
	distance->addFuzzySet(big); // Adicionando o FuzzySet big em distance

	fuzzy->addFuzzyInput(distance); // Adicionando o FuzzyInput no objeto Fuzzy

	// Criando o FuzzyOutput velocidade
	FuzzyOutput* velocity = new FuzzyOutput(1);
	// Criando os FuzzySet que compoem o FuzzyOutput velocidade 
	FuzzySet* slow = new FuzzySet(0, 10, 10, 20); // Velocidade lenta
	velocity->addFuzzySet(slow); // Adicionando o FuzzySet slow em velocity
	FuzzySet* average = new FuzzySet(10, 20, 30, 40); // Velocidade normal
	velocity->addFuzzySet(average); // Adicionando o FuzzySet average em velocity
	FuzzySet* fast = new FuzzySet(30, 40, 40, 50); // Velocidade alta
	velocity->addFuzzySet(fast); // Adicionando o FuzzySet fast em velocity

	fuzzy->addFuzzyOutput(velocity); // Adicionando o FuzzyOutput no objeto Fuzzy

	//-------------------- Montando as regras Fuzzy
	// FuzzyRule "SE distancia = pequena ENTAO velocidade = lenta"
	FuzzyRuleAntecedent* ifDistanceSmall = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
	ifDistanceSmall->joinSingle(small); // Adicionando o FuzzySet correspondente ao objeto Antecedente
	FuzzyRuleConsequent* thenVelocitySlow = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
	thenVelocitySlow->addOutput(slow);// Adicionando o FuzzySet correspondente ao objeto Consequente
	// Instanciando um objeto FuzzyRule
	FuzzyRule* fuzzyRule01 = new FuzzyRule(1, ifDistanceSmall, thenVelocitySlow); // Passando o Antecedente e o Consequente da expressao
	fuzzy->addFuzzyRule(fuzzyRule01); // Adicionando o FuzzyRule ao objeto Fuzzy

	// FuzzyRule "SE distancia = segura ENTAO velocidade = normal"
	FuzzyRuleAntecedent* ifDistanceSafe = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
	ifDistanceSafe->joinSingle(safe); // Adicionando o FuzzySet correspondente ao objeto Antecedente
	FuzzyRuleConsequent* thenVelocityAverage = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
	thenVelocityAverage->addOutput(average);// Adicionando o FuzzySet correspondente ao objeto Consequente
	// Instanciando um objeto FuzzyRule
	FuzzyRule* fuzzyRule02 = new FuzzyRule(2, ifDistanceSafe, thenVelocityAverage); // Passando o Antecedente e o Consequente da expressao
	fuzzy->addFuzzyRule(fuzzyRule02); // Adicionando o FuzzyRule ao objeto Fuzzy

	// FuzzyRule "SE distancia = grande ENTAO velocidade = alta"
	FuzzyRuleAntecedent* ifDistanceBig = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
	ifDistanceBig->joinSingle(big); // Adicionando o FuzzySet correspondente ao objeto Antecedente
	FuzzyRuleConsequent* thenVelocityFast = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
	thenVelocityFast->addOutput(fast);// Adicionando o FuzzySet correspondente ao objeto Consequente
	// Instanciando um objeto FuzzyRule
	FuzzyRule* fuzzyRule03 = new FuzzyRule(3, ifDistanceBig, thenVelocityFast); // Passando o Antecedente e o Consequente da expressao
	fuzzy->addFuzzyRule(fuzzyRule03); // Adicionando o FuzzyRule ao objeto Fuzzy

	for(int dist = 0; dist < 100; dist++){
		fuzzy->setInput(1, dist);

		fuzzy->fuzzify();

		float output = fuzzy->defuzzify(1);
		cout << "Entrada: " << dist << ", Saida: " << output << endl;
	}
	return 0;
}
/*
 *  Class written by taking the online eFLL library example as model.
 *  The values of the fuzzy sets were determined by doing some physical
 *  experiments.
 */
FuzzyController::FuzzyController()
{
  _fuzzy = new Fuzzy();

  // Input RPM
  FuzzySet*         low_RPM = new FuzzySet(                RPM_MIN,                 RPM_MIN, RPM_ZERO - 2 * RPM_STEP, RPM_ZERO -     RPM_STEP);
  FuzzySet*  little_low_RPM = new FuzzySet(RPM_ZERO - 2 * RPM_STEP, RPM_ZERO -     RPM_STEP, RPM_ZERO -     RPM_STEP,                RPM_ZERO);
  FuzzySet*       right_RPM = new FuzzySet(RPM_ZERO -     RPM_STEP,                RPM_ZERO,                RPM_ZERO, RPM_ZERO +     RPM_STEP);
  FuzzySet* little_high_RPM = new FuzzySet(               RPM_ZERO, RPM_ZERO +     RPM_STEP, RPM_ZERO +     RPM_STEP, RPM_ZERO + 2 * RPM_STEP);
  FuzzySet*        high_RPM = new FuzzySet(RPM_ZERO +     RPM_STEP, RPM_ZERO + 2 * RPM_STEP,                 RPM_MAX,                 RPM_MAX);
  
  FuzzyInput* RPM = new FuzzyInput(1);
  RPM->addFuzzySet(        low_RPM);
  RPM->addFuzzySet( little_low_RPM);
  RPM->addFuzzySet(      right_RPM);
  RPM->addFuzzySet(little_high_RPM);
  RPM->addFuzzySet(       high_RPM);
  
  _fuzzy->addFuzzyInput(RPM);

  // Output PWM
  FuzzySet*         low_PWM = new FuzzySet(                PWM_MIN,                 PWM_MIN, PWM_ZERO - 2 * PWM_STEP, PWM_ZERO -     PWM_STEP);
  FuzzySet*  little_low_PWM = new FuzzySet(PWM_ZERO - 2 * PWM_STEP, PWM_ZERO -     PWM_STEP, PWM_ZERO -     PWM_STEP,                PWM_ZERO);
  FuzzySet*       right_PWM = new FuzzySet(PWM_ZERO -     PWM_STEP,                PWM_ZERO,                PWM_ZERO, PWM_ZERO +     PWM_STEP);
  FuzzySet* little_high_PWM = new FuzzySet(               PWM_ZERO, PWM_ZERO +     PWM_STEP, PWM_ZERO +     PWM_STEP, PWM_ZERO + 2 * PWM_STEP);
  FuzzySet*        high_PWM = new FuzzySet(PWM_ZERO +     PWM_STEP, PWM_ZERO + 2 * PWM_STEP,                 PWM_MAX,                 PWM_MAX);
  
  FuzzyOutput* PWM = new FuzzyOutput(1);
  PWM->addFuzzySet(       high_PWM);
  PWM->addFuzzySet(little_high_PWM);
  PWM->addFuzzySet(      right_PWM);
  PWM->addFuzzySet( little_low_PWM);
  PWM->addFuzzySet(        low_PWM);
  
  _fuzzy->addFuzzyOutput(PWM);

  // Build the rules
  // 1. If         low_RPM then        high_PWM
  // 2. If  little_low_RPM then little_high_PWM
  // 3. If       right_RPM then       right_PWM
  // 4. If little_high_RPM then  little_low_PWM
  // 5. It        high_RPM then         low_PWM

  // Rule no. 1
  FuzzyRuleAntecedent* ifRPMLow = new FuzzyRuleAntecedent();
  ifRPMLow->joinSingle(low_RPM);
  
  FuzzyRuleConsequent* thenPWMHigh = new FuzzyRuleConsequent();
  thenPWMHigh->addOutput(high_PWM);
  
  FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifRPMLow, thenPWMHigh);  
  _fuzzy->addFuzzyRule(fuzzyRule1);


  // Rule no. 2
  FuzzyRuleAntecedent* ifRPMLittleLow = new FuzzyRuleAntecedent();
  ifRPMLittleLow->joinSingle(little_low_RPM);
  
  FuzzyRuleConsequent* thenPWMLittleHigh = new FuzzyRuleConsequent();
  thenPWMLittleHigh->addOutput(little_high_PWM);
  
  FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifRPMLittleLow, thenPWMLittleHigh);  
  _fuzzy->addFuzzyRule(fuzzyRule2);


  // Rule no. 3
  FuzzyRuleAntecedent* ifRPMRight = new FuzzyRuleAntecedent();
  ifRPMRight->joinSingle(right_RPM);
  
  FuzzyRuleConsequent* thenPWMRight = new FuzzyRuleConsequent();
  thenPWMRight->addOutput(right_PWM);
  
  FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifRPMRight, thenPWMRight);  
  _fuzzy->addFuzzyRule(fuzzyRule3);


  // Rule no. 4
  FuzzyRuleAntecedent* ifRPMLittleHigh = new FuzzyRuleAntecedent();
  ifRPMLittleHigh->joinSingle(little_high_RPM);
  
  FuzzyRuleConsequent* thenPWMLittleLow = new FuzzyRuleConsequent();
  thenPWMLittleLow->addOutput(little_low_PWM);
  
  FuzzyRule* fuzzyRule4 = new FuzzyRule(4, ifRPMLittleHigh, thenPWMLittleLow);  
  _fuzzy->addFuzzyRule(fuzzyRule4);


  // Rule no. 5
  FuzzyRuleAntecedent* ifRPMHigh = new FuzzyRuleAntecedent();
  ifRPMHigh->joinSingle(high_RPM);
  
  FuzzyRuleConsequent* thenPWMLow = new FuzzyRuleConsequent();
  thenPWMLow->addOutput(low_PWM);
  
  FuzzyRule* fuzzyRule5 = new FuzzyRule(5, ifRPMHigh, thenPWMLow);  
  _fuzzy->addFuzzyRule(fuzzyRule5);
}
Beispiel #7
0
int main(int argc, char *argv[])
{
    // Set a seed to rand
    srand((unsigned)time(0));

    // Instantiating a Fuzzy object
    Fuzzy *fuzzy = new Fuzzy();

    // Instantiating a FuzzyInput object
    FuzzyInput *distance = new FuzzyInput(1);
    // Instantiating a FuzzySet object
    FuzzySet *small = new FuzzySet(0, 20, 20, 40);
    // Including the FuzzySet into FuzzyInput
    distance->addFuzzySet(small);
    // Instantiating a FuzzySet object
    FuzzySet *safe = new FuzzySet(30, 50, 50, 70);
    // Including the FuzzySet into FuzzyInput
    distance->addFuzzySet(safe);
    // Instantiating a FuzzySet object
    FuzzySet *big = new FuzzySet(60, 80, 80, 80);
    // Including the FuzzySet into FuzzyInput
    distance->addFuzzySet(big);
    // Including the FuzzyInput into Fuzzy
    fuzzy->addFuzzyInput(distance);

    // Instantiating a FuzzyOutput objects
    FuzzyOutput *speed = new FuzzyOutput(1);
    // Instantiating a FuzzySet object
    FuzzySet *slow = new FuzzySet(0, 10, 10, 20);
    // Including the FuzzySet into FuzzyOutput
    speed->addFuzzySet(slow);
    // Instantiating a FuzzySet object
    FuzzySet *average = new FuzzySet(10, 20, 30, 40);
    // Including the FuzzySet into FuzzyOutput
    speed->addFuzzySet(average);
    // Instantiating a FuzzySet object
    FuzzySet *fast = new FuzzySet(30, 40, 40, 50);
    // Including the FuzzySet into FuzzyOutput
    speed->addFuzzySet(fast);
    // Including the FuzzyOutput into Fuzzy
    fuzzy->addFuzzyOutput(speed);

    // Building FuzzyRule "IF distance = small THEN speed = slow"
    // Instantiating a FuzzyRuleAntecedent objects
    FuzzyRuleAntecedent *ifDistanceSmall = new FuzzyRuleAntecedent();
    // Creating a FuzzyRuleAntecedent with just a single FuzzySet
    ifDistanceSmall->joinSingle(small);
    // Instantiating a FuzzyRuleConsequent objects
    FuzzyRuleConsequent *thenSpeedSlow = new FuzzyRuleConsequent();
    // Including a FuzzySet to this FuzzyRuleConsequent
    thenSpeedSlow->addOutput(slow);
    // Instantiating a FuzzyRule objects
    FuzzyRule *fuzzyRule01 = new FuzzyRule(1, ifDistanceSmall, thenSpeedSlow);
    // Including the FuzzyRule into Fuzzy
    fuzzy->addFuzzyRule(fuzzyRule01);

    // Building FuzzyRule "IF distance = safe THEN speed = average"
    // Instantiating a FuzzyRuleAntecedent objects
    FuzzyRuleAntecedent *ifDistanceSafe = new FuzzyRuleAntecedent();
    // Creating a FuzzyRuleAntecedent with just a single FuzzySet
    ifDistanceSafe->joinSingle(safe);
    // Instantiating a FuzzyRuleConsequent objects
    FuzzyRuleConsequent *thenSpeedAverage = new FuzzyRuleConsequent();
    // Including a FuzzySet to this FuzzyRuleConsequent
    thenSpeedAverage->addOutput(average);
    // Instantiating a FuzzyRule objects
    FuzzyRule *fuzzyRule02 = new FuzzyRule(2, ifDistanceSafe, thenSpeedAverage);
    // Including the FuzzyRule into Fuzzy
    fuzzy->addFuzzyRule(fuzzyRule02);

    // Building FuzzyRule "IF distance = big THEN speed = high"
    // Instantiating a FuzzyRuleAntecedent objects
    FuzzyRuleAntecedent *ifDistanceBig = new FuzzyRuleAntecedent();
    // Creating a FuzzyRuleAntecedent with just a single FuzzySet
    ifDistanceBig->joinSingle(big);
    // Instantiating a FuzzyRuleConsequent objects
    FuzzyRuleConsequent *thenSpeedFast = new FuzzyRuleConsequent();
    // Including a FuzzySet to this FuzzyRuleConsequent
    thenSpeedFast->addOutput(fast);
    // Instantiating a FuzzyRule objects
    FuzzyRule *fuzzyRule03 = new FuzzyRule(3, ifDistanceBig, thenSpeedFast);
    // Including the FuzzyRule into Fuzzy
    fuzzy->addFuzzyRule(fuzzyRule03);

    // get a random value
    int input = random(0, 100);
    // Printing something
    cout << "\n\n\nEntrance: \n\t\t\tDistance: " << input << endl;
    // Set the random value as an input
    fuzzy->setInput(1, input);
    // Running the Fuzzification
    fuzzy->fuzzify();
    // Running the Defuzzification
    float output = fuzzy->defuzzify(1);
    // Printing something
    cout << "Result: \n\t\t\tSpeed: " << output << endl;
    return 0;
}
Beispiel #8
0
TEST(Fuzzy, setInputAndFuzzifyAndDefuzzify06){
  Fuzzy* fuzzy = new Fuzzy();

  // FuzzyInput
  FuzzyInput* distance = new FuzzyInput(1);

  FuzzySet* close = new FuzzySet(0, 20, 20, 40);
  distance->addFuzzySet(close);
  FuzzySet* safe = new FuzzySet(30, 50, 50, 70);
  distance->addFuzzySet(safe);
  FuzzySet* distante = new FuzzySet(60, 80, 100, 100);
  distance->addFuzzySet(distante);

  fuzzy->addFuzzyInput(distance);

  // FuzzyInput
  FuzzyInput* inputSpeed = new FuzzyInput(2);

  FuzzySet* stoped = new FuzzySet(0, 0, 0, 0);
  inputSpeed->addFuzzySet(stoped);
  FuzzySet* slow = new FuzzySet(1, 10, 10, 20);
  inputSpeed->addFuzzySet(slow);
  FuzzySet* normal = new FuzzySet(15, 30, 30, 50);
  inputSpeed->addFuzzySet(normal);
  FuzzySet* quick = new FuzzySet(45, 60, 70, 70);
  inputSpeed->addFuzzySet(quick);

  fuzzy->addFuzzyInput(inputSpeed);

  // FuzzyInput
  FuzzyInput* temperature = new FuzzyInput(3);

  FuzzySet* cold = new FuzzySet(-30, -30, -20, -10);
  temperature->addFuzzySet(cold);
  FuzzySet* good = new FuzzySet(-15, 0, 0, 15);
  temperature->addFuzzySet(good);
  FuzzySet* hot = new FuzzySet(10, 20, 30, 30);
  temperature->addFuzzySet(hot);

  fuzzy->addFuzzyInput(temperature);

  // FuzzyOutput
  FuzzyOutput* risk = new FuzzyOutput(1);

  FuzzySet* minimum = new FuzzySet(0, 20, 20, 40);
  risk->addFuzzySet(minimum);
  FuzzySet* average = new FuzzySet(30, 50, 50, 70);
  risk->addFuzzySet(average);
  FuzzySet* maximum = new FuzzySet(60, 80, 80, 100);
  risk->addFuzzySet(maximum);

  fuzzy->addFuzzyOutput(risk);

  // FuzzyOutput
  // adicionando speed como output também
  FuzzyOutput* outputSpeed = new FuzzyOutput(2);

  FuzzySet* stopedOut = new FuzzySet(0, 0, 0, 0);
  outputSpeed->addFuzzySet(stopedOut);
  FuzzySet* slowOut = new FuzzySet(1, 10, 10, 20);
  outputSpeed->addFuzzySet(slowOut);
  FuzzySet* normalOut = new FuzzySet(15, 30, 30, 50);
  outputSpeed->addFuzzySet(normalOut);
  FuzzySet* quickOut = new FuzzySet(45, 60, 70, 70);
  outputSpeed->addFuzzySet(quickOut);

  fuzzy->addFuzzyOutput(outputSpeed);

  // Building FuzzyRule
  FuzzyRuleAntecedent* distanceCloseAndSpeedQuick = new FuzzyRuleAntecedent();
  distanceCloseAndSpeedQuick->joinWithAND(close, quick);
  FuzzyRuleAntecedent* temperatureCold = new FuzzyRuleAntecedent();
  temperatureCold->joinSingle(cold);
  FuzzyRuleAntecedent* ifDistanceCloseAndSpeedQuickOrTemperatureCold = new FuzzyRuleAntecedent();
  ifDistanceCloseAndSpeedQuickOrTemperatureCold->joinWithOR(distanceCloseAndSpeedQuick, temperatureCold);

  FuzzyRuleConsequent* thenRisMaximumAndSpeedSlow = new FuzzyRuleConsequent();
  thenRisMaximumAndSpeedSlow->addOutput(maximum);
  thenRisMaximumAndSpeedSlow->addOutput(slowOut);

  FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifDistanceCloseAndSpeedQuickOrTemperatureCold, thenRisMaximumAndSpeedSlow);
  fuzzy->addFuzzyRule(fuzzyRule1);

  // Building FuzzyRule
  FuzzyRuleAntecedent* distanceSafeAndSpeedNormal = new FuzzyRuleAntecedent();
  distanceSafeAndSpeedNormal->joinWithAND(safe, normal);
  FuzzyRuleAntecedent* ifDistanceSafeAndSpeedNormalOrTemperatureGood = new FuzzyRuleAntecedent();
  ifDistanceSafeAndSpeedNormalOrTemperatureGood->joinWithOR(distanceSafeAndSpeedNormal, good);

  FuzzyRuleConsequent* thenRiskAverageAndSpeedNormal = new FuzzyRuleConsequent();
  thenRiskAverageAndSpeedNormal->addOutput(average);
  thenRiskAverageAndSpeedNormal->addOutput(normalOut);

  FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifDistanceSafeAndSpeedNormalOrTemperatureGood, thenRiskAverageAndSpeedNormal);
  fuzzy->addFuzzyRule(fuzzyRule2);

  // Building FuzzyRule
  FuzzyRuleAntecedent* distanceDistanteAndSpeedSlow = new FuzzyRuleAntecedent();
  distanceDistanteAndSpeedSlow->joinWithAND(distante, slow);
  FuzzyRuleAntecedent* ifDistanceDistanteAndSpeedSlowOrTemperatureHot = new FuzzyRuleAntecedent();
  ifDistanceDistanteAndSpeedSlowOrTemperatureHot->joinWithOR(distanceDistanteAndSpeedSlow, hot);

  FuzzyRuleConsequent* thenRiskMinimumSpeedQuick = new FuzzyRuleConsequent();
  thenRiskMinimumSpeedQuick->addOutput(minimum);
  thenRiskMinimumSpeedQuick->addOutput(quickOut);

  FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifDistanceDistanteAndSpeedSlowOrTemperatureHot, thenRiskMinimumSpeedQuick);
  fuzzy->addFuzzyRule(fuzzyRule3);

  fuzzy->setInput(1, 10);
  fuzzy->setInput(2, 30);
  fuzzy->setInput(3, -15);

  fuzzy->fuzzify();

  float output1 = fuzzy->defuzzify(1);
  float output2 = fuzzy->defuzzify(2);

  EXPECT_EQ(80.0, output1);
  EXPECT_EQ(10.5, output2);
}
Beispiel #9
0
TEST(Fuzzy, setInputAndFuzzifyAndDefuzzify01){
  Fuzzy* fuzzy = new Fuzzy();

  // FuzzyInput
  FuzzyInput* temperature = new FuzzyInput(1);

  FuzzySet* low = new FuzzySet(0, 10, 10, 20);
  temperature->addFuzzySet(low);
  FuzzySet* mean = new FuzzySet(10, 20, 30, 40);
  temperature->addFuzzySet(mean);
  FuzzySet* high = new FuzzySet(30, 40, 40, 50);
  temperature->addFuzzySet(high);

  fuzzy->addFuzzyInput(temperature);

  // FuzzyOutput
  FuzzyOutput* climate = new FuzzyOutput(1);

  FuzzySet* cold = new FuzzySet(0, 10, 10, 20);
  climate->addFuzzySet(cold);
  FuzzySet* good = new FuzzySet(10, 20, 30, 40);
  climate->addFuzzySet(good);
  FuzzySet* hot = new FuzzySet(30, 40, 40, 50);
  climate->addFuzzySet(hot);

  fuzzy->addFuzzyOutput(climate);

  // Building FuzzyRule
  FuzzyRuleAntecedent* ifTemperatureLow = new FuzzyRuleAntecedent();
  ifTemperatureLow->joinSingle(low);
  FuzzyRuleConsequent* thenClimateCold = new FuzzyRuleConsequent();
  thenClimateCold->addOutput(cold);

  FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifTemperatureLow, thenClimateCold);
  fuzzy->addFuzzyRule(fuzzyRule1);

  // Building FuzzyRule
  FuzzyRuleAntecedent* ifTemperatureMean = new FuzzyRuleAntecedent();
  ifTemperatureMean->joinSingle(mean);
  FuzzyRuleConsequent* thenClimateGood = new FuzzyRuleConsequent();
  thenClimateGood->addOutput(good);

  FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifTemperatureMean, thenClimateGood);
  fuzzy->addFuzzyRule(fuzzyRule2);

  // Building FuzzyRule
  FuzzyRuleAntecedent* ifTemperatureHigh = new FuzzyRuleAntecedent();
  ifTemperatureHigh->joinSingle(low);
  FuzzyRuleConsequent* thenClimateHot = new FuzzyRuleConsequent();
  thenClimateHot->addOutput(cold);

  FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifTemperatureHigh, thenClimateHot);
  fuzzy->addFuzzyRule(fuzzyRule3);

  bool result1 = fuzzy->setInput(1, 15);

  bool result2 = fuzzy->fuzzify();

  float output = fuzzy->defuzzify(1);

  EXPECT_TRUE(result1);
  EXPECT_TRUE(result2);
  EXPECT_GT(output, 0.0);
}