pcingola / jfuzzylogic Goto Github PK
View Code? Open in Web Editor NEWThe most complete fuzzy logic library in Java. The de-facto standard for research and industrial applications.
License: GNU Lesser General Public License v3.0
The most complete fuzzy logic library in Java. The de-facto standard for research and industrial applications.
License: GNU Lesser General Public License v3.0
Hello, I have been searching for a Fuzzy Logic related Java project and found this.
I have trouble with this API, I can´t really understand it well.
I have my data stored in about 10 excels (and growing each week), each of these has 4k rows and 55 columns.
What kind of data format does this API use? And more important, Can I use this API to create rules giving it some data?
Thank you in advance.
Hello, I have a problem with using jFuzzyLogic with maven. The maven repository https://mvnrepository.com/artifact/net.sourceforge.jFuzzyLogic/jFuzzyLogic/1.2.1 seems to not have all the functionalities of the current library version. Namely, the functionBlock class is not a part of the maven build, are there any other maven repositories that I can use.
Thanks for any advice.
Hello using JAVA API i want to build a FIS with a lot of rules. Using "addAntecedent()" function there is a limitation for only 2 antecendents for a single rule. I also read there are more limitations regarding the total number of rules for each FIS. This is not stated in the documentation. Is this right?
Is this a bug?
I'm using: JFuzzyLogic 3.3 (build 2015-04-09), c1.fcl is a renamed tipper.fcl (included at the bottom).
c:\...>java -jar jFuzzyLogic.jar -e c1.fcl 8.1 8.1
jFuzzyLogic version JFuzzyLogic 3.3 (build 2015-04-09), by Pablo Cingolani.
FUNCITON_BLOCK c1
VAR_INPUT food = 8.100000
VAR_INPUT service = 8.100000
VAR_OUTPUT tip = 21.099399
RULE_BLOCK No1
Support Rule name Rule
0.000000 1 IF (service IS poor) OR (food IS rancid) THEN tip IS cheap;
0.300000 2 IF service IS good THEN tip IS average;
0.550000 3 IF (service IS excellent) AND (food IS delicious) THEN tip IS generous;
Then,
c:\...>java -jar jFuzzyLogic.jar -c c1.fcl >c1.cpp
compile and run with args 8.1 8.1 as above:
Function block c1:
Input food : 8.100000
food_delicious : 0.550000
food_rancid : 0.000000
Input service : 8.100000
service_excellent : 0.700000
service_good : 0.300000
service_poor : 0.000000
Output tip : 19.387872
Why is the output tip different?
The files:
c1.fcl
FUNCTION_BLOCK c1 // Block definition (there may be more than one block per file)
VAR_INPUT // Define input variables
service : REAL;
food : REAL;
END_VAR
VAR_OUTPUT // Define output variable
tip : REAL;
END_VAR
FUZZIFY service // Fuzzify input variable 'service': {'poor', 'good' , 'excellent'}
TERM poor := (0, 1) (4, 0) ;
TERM good := (1, 0) (4,1) (6,1) (9,0);
TERM excellent := (6, 0) (9, 1);
END_FUZZIFY
FUZZIFY food // Fuzzify input variable 'food': { 'rancid', 'delicious' }
TERM rancid := (0, 1) (1, 1) (3,0) ;
TERM delicious := (7,0) (9,1);
END_FUZZIFY
DEFUZZIFY tip // Defzzzify output variable 'tip' : {'cheap', 'average', 'generous' }
TERM cheap := (0,0) (5,1) (10,0);
TERM average := (10,0) (15,1) (20,0);
TERM generous := (20,0) (25,1) (30,0);
METHOD : COG; // Use 'Center Of Gravity' defuzzification method
DEFAULT := 0; // Default value is 0 (if no rule activates defuzzifier)
END_DEFUZZIFY
RULEBLOCK No1
AND : MIN; // Use 'min' for 'and' (also implicit use 'max' for 'or' to fulfill DeMorgan's Law)
ACT : MIN; // Use 'min' activation method
ACCU : MAX; // Use 'max' accumulation method
RULE 1 : IF service IS poor OR food IS rancid THEN tip IS cheap;
RULE 2 : IF service IS good THEN tip IS average;
RULE 3 : IF service IS excellent AND food IS delicious THEN tip IS generous;
END_RULEBLOCK
END_FUNCTION_BLOCK
c1.cpp
//--------------------------------------------------------------------------------
// Code generated by jFuzzyLogic
// jFuzzyLogic Version : JFuzzyLogic 3.3 (build 2015-04-09), by Pablo Cingolani
// jFuzzyLogic creted by Pablo Cingolani
//--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
double ruleAccumulationMethod_max(double defuzzifierValue, double valueToAggregate) { return ( defuzzifierValue > valueToAggregate ? defuzzifierValue : valueToAggregate ); }
double ruleActivationMethod_min(double degreeOfSupport, double membership) { return (degreeOfSupport < membership ? degreeOfSupport : membership); }
double ruleConnectionMethod_or(double antecedent1, double antecedent2) { return (antecedent1 > antecedent2 ? antecedent1 : antecedent2); }
double ruleConnectionMethod_and(double antecedent1, double antecedent2) { return (antecedent1 < antecedent2 ? antecedent1 : antecedent2); }
class FunctionBlock_c1 {
public:
// VAR_INPUT
double food;
double service;
// VAR_OUTPUT
double tip;
private:
// FUZZIFY food
double food_delicious;
double food_rancid;
// FUZZIFY service
double service_excellent;
double service_good;
double service_poor;
// DEFUZZIFY tip
double defuzzify_tip[1000];
public:
FunctionBlock_c1();
void calc();
void print();
private:
void defuzzify();
void fuzzify();
void reset();
double membership_food_delicious(double x);
double membership_food_rancid(double x);
double membership_service_excellent(double x);
double membership_service_good(double x);
double membership_service_poor(double x);
double membership_tip_average(double x);
double membership_tip_cheap(double x);
double membership_tip_generous(double x);
void calc_No1();
};
// Constructor
FunctionBlock_c1::FunctionBlock_c1() {
tip = 0.0;
}
// Calculate function block
void FunctionBlock_c1::calc() {
reset();
fuzzify();
calc_No1();
defuzzify();
}
// RULEBLOCK No1
void FunctionBlock_c1::calc_No1() {
// RULE 1 : IF (service IS poor) OR (food IS rancid) THEN tip IS cheap;
double degreeOfSupport_1 = 1.0 * ( ruleConnectionMethod_or(service_poor , food_rancid) );
if( degreeOfSupport_1 > 0 ) {
for (int i = 0 ; i < 1000 ; i++ ) {
double x = 0.0 + i * 0.03;
double membership = membership_tip_cheap(x);
double y = ruleActivationMethod_min( degreeOfSupport_1 , membership );
defuzzify_tip[i] += ruleAccumulationMethod_max( defuzzify_tip[i], y );
}
}
// RULE 2 : IF service IS good THEN tip IS average;
double degreeOfSupport_2 = 1.0 * ( service_good );
if( degreeOfSupport_2 > 0 ) {
for (int i = 0 ; i < 1000 ; i++ ) {
double x = 0.0 + i * 0.03;
double membership = membership_tip_average(x);
double y = ruleActivationMethod_min( degreeOfSupport_2 , membership );
defuzzify_tip[i] += ruleAccumulationMethod_max( defuzzify_tip[i], y );
}
}
// RULE 3 : IF (service IS excellent) AND (food IS delicious) THEN tip IS generous;
double degreeOfSupport_3 = 1.0 * ( ruleConnectionMethod_and(service_excellent , food_delicious) );
if( degreeOfSupport_3 > 0 ) {
for (int i = 0 ; i < 1000 ; i++ ) {
double x = 0.0 + i * 0.03;
double membership = membership_tip_generous(x);
double y = ruleActivationMethod_min( degreeOfSupport_3 , membership );
defuzzify_tip[i] += ruleAccumulationMethod_max( defuzzify_tip[i], y );
}
}
}
// Defuzzify
void FunctionBlock_c1::defuzzify() {
double sum_tip = 0.0;
double wsum_tip = 0.0;
for (int i = 0; i < 1000 ; i++ ) {
double x = 0.0 + i * 0.03;
sum_tip += defuzzify_tip[i];
wsum_tip += x * defuzzify_tip[i];
}
tip = wsum_tip / sum_tip;
}
// Fuzzify all variables
void FunctionBlock_c1::fuzzify() {
food_delicious = membership_food_delicious(food);
food_rancid = membership_food_rancid(food);
service_excellent = membership_service_excellent(service);
service_good = membership_service_good(service);
service_poor = membership_service_poor(service);
}
// Membership functions
double FunctionBlock_c1::membership_food_delicious(double x) {
if ( x <= 7.0 ) return 0.0;
if ( x > 9.0 ) return 1.0;
if ( x <= 9.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 7.0 ) / ( 9.0 - 7.0 ) );
}
double FunctionBlock_c1::membership_food_rancid(double x) {
if ( x <= 0.0 ) return 1.0;
if ( x > 3.0 ) return 0.0;
if ( x <= 1.0 ) return 1.0 + ( 1.0 - 1.0 ) * ( ( x - 0.0 ) / ( 1.0 - 0.0 ) );
if ( x <= 3.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 1.0 ) / ( 3.0 - 1.0 ) );
}
double FunctionBlock_c1::membership_service_excellent(double x) {
if ( x <= 6.0 ) return 0.0;
if ( x > 9.0 ) return 1.0;
if ( x <= 9.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 6.0 ) / ( 9.0 - 6.0 ) );
}
double FunctionBlock_c1::membership_service_good(double x) {
if ( x <= 1.0 ) return 0.0;
if ( x > 9.0 ) return 0.0;
if ( x <= 4.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 1.0 ) / ( 4.0 - 1.0 ) );
if ( x <= 6.0 ) return 1.0 + ( 1.0 - 1.0 ) * ( ( x - 4.0 ) / ( 6.0 - 4.0 ) );
if ( x <= 9.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 6.0 ) / ( 9.0 - 6.0 ) );
}
double FunctionBlock_c1::membership_service_poor(double x) {
if ( x <= 0.0 ) return 1.0;
if ( x > 4.0 ) return 0.0;
if ( x <= 4.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 0.0 ) / ( 4.0 - 0.0 ) );
}
double FunctionBlock_c1::membership_tip_average(double x) {
if ( x <= 10.0 ) return 0.0;
if ( x > 20.0 ) return 0.0;
if ( x <= 15.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 10.0 ) / ( 15.0 - 10.0 ) );
if ( x <= 20.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 15.0 ) / ( 20.0 - 15.0 ) );
}
double FunctionBlock_c1::membership_tip_cheap(double x) {
if ( x <= 0.0 ) return 0.0;
if ( x > 10.0 ) return 0.0;
if ( x <= 5.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 0.0 ) / ( 5.0 - 0.0 ) );
if ( x <= 10.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 5.0 ) / ( 10.0 - 5.0 ) );
}
double FunctionBlock_c1::membership_tip_generous(double x) {
if ( x <= 20.0 ) return 0.0;
if ( x > 30.0 ) return 0.0;
if ( x <= 25.0 ) return 0.0 + ( 1.0 - 0.0 ) * ( ( x - 20.0 ) / ( 25.0 - 20.0 ) );
if ( x <= 30.0 ) return 1.0 + ( 0.0 - 1.0 ) * ( ( x - 25.0 ) / ( 30.0 - 25.0 ) );
}
// Print
void FunctionBlock_c1::print() {
printf("Function block c1:\n");
printf(" Input %20s : %f\n", "food" , food);
printf(" %20s : %f\n", "food_delicious" , food_delicious);
printf(" %20s : %f\n", "food_rancid" , food_rancid);
printf(" Input %20s : %f\n", "service" , service);
printf(" %20s : %f\n", "service_excellent" , service_excellent);
printf(" %20s : %f\n", "service_good" , service_good);
printf(" %20s : %f\n", "service_poor" , service_poor);
printf(" Output %20s : %f\n", "tip" , tip);
}
// Reset output
void FunctionBlock_c1::reset() {
for( int i=0 ; i < 1000 ; i++ ) { defuzzify_tip[i] = 0.0; }
}
int main(int argc, char *argv[]) {
// Create function blocks
FunctionBlock_c1 c1;
// Parse input
if( argc > 1 ) { c1.food = atof(argv[1]); }
if( argc > 2 ) { c1.service = atof(argv[2]); }
// Calculate
c1.calc();
// Show results
c1.print();
}
Hello
I think jFuzzyLogic doesn't work when I add weight to rule, is that right ?
jFuzzyLogic version JFuzzyLogic 3.3 (build 2015-04-09), by Pablo Cingolani.
line 34:51 extraneous input '(' expecting ID
line 34:87 extraneous input ')' expecting SEMICOLON
line 35:53 extraneous input '(' expecting ID
line 35:89 extraneous input ')' expecting SEMICOLON
line 36:48 extraneous input '(' expecting ID
line 36:75 mismatched input ')' expecting SEMICOLON
Exception in thread "main" java.lang.RuntimeException: Unknown (or unimplemented) ruleblock item : RULE 2: IF (Enemy_Health IS Good) THEN (Aggressiveness IS Run_Away) WITH 1.0;
at net.sourceforge.jFuzzyLogic.rule.RuleBlock.fclTree(RuleBlock.java:188)
at net.sourceforge.jFuzzyLogic.FunctionBlock.fclTree(FunctionBlock.java:135)
at net.sourceforge.jFuzzyLogic.FIS.createFromLexer(FIS.java:79)
at net.sourceforge.jFuzzyLogic.FIS.createFromString(FIS.java:97)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.load(JFuzzyLogic.java:137)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.show(JFuzzyLogic.java:189)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.run(JFuzzyLogic.java:180)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.main(JFuzzyLogic.java:36)
RULEBLOCK first
ACCU:MAX;(*MAX NSUM BSUM*)
OR:MAX;(*Autres operations possibles : ASUM > u1+u2-u1xu2, BSUM > min (1,u1+u2) *)
AND:MIN;(*Autres operations possibles : PROD > u1xu2, BDIF > max(0,u1*u2-1)*)
RULE 0: IF (Our_Health IS Near_Death) THEN (Aggressiveness IS Fight_Defensively) ;
RULE 1: IF (Enemy_Health IS Near_Death) THEN (Aggressiveness IS Fight_Defensively);
RULE 2: IF (Enemy_Health IS Good) THEN (Aggressiveness IS Run_Away) WITH 1.0 ;
END_RULEBLOCK
Does Jfuzzylogic under following repo contains latest source code ? In current github repo, there is no version information. So I am not sure. I checked the code and it is missing some files.
Hello it would be very nice to have documented how to redraw the JFuzzyChart-s and not create only new windows with the graphs.
I was using your TestTipper.java code with some cycles and variable changes and it was creating a new set of windows for every iteration.
Thank you!
Hi,
i have several groups of input, when i do setVariable and evalue like this:
// do one setVariables and evaluate first
// then repeatedly evaluate another group data
for (float[] ins : data) {
fb = fis.getFunctionBlock("scaler");
fis.setVariable("cpu", ins[0]);
fis.setVariable("memory", ins[1]);
fis.evaluate();
// action is the output var
Variable a = fb.getVariable("action");
// a.getValue() here get the before evaluted value
}
the problem is the output value is always the the one before this evaluate...
is there anything others should i do before do a new evaluate?
For example, I worked out by trial and error that I needed the '-e' flag in the arguments to run examples in the fcl folder. However, there is no documentation on running these, such as the values of parameters to pass. I can see the following tantalising samples:
bathTub, cinthia, ip, ip2 membershipFunctionsDemo, qualify (two versions), qurat, robot (two versions) sc2, scarledOLD, testDeMorgan (six versons) and z, as well as tipper (two versions). If a couple more of these could have the treatment tipper did in the provided manual, that would be great!
Hi!
I found your project useful but it's some times hard to use because doesn't have documentation, I think the manual it's great but often is a little hard to tell if exists a method that do something that the developers need.
I think if implements an reference that explain briefly what each method does could be more helpful.
Thanks for jFuzzy!
Hi,
If I use d1.fcl with -e or by itself (just d1.flc for params) it runs, but with -c the result is:
C:\...>java -jar jFuzzyLogic.jar -c d1.fcl
jFuzzyLogic version JFuzzyLogic 3.3 (build 2015-04-09), by Pablo Cingolani.
Exception in thread "main" java.lang.ClassCastException: net.sourceforge.jFuzzyL
ogic.defuzzifier.DefuzzifierCenterOfGravitySingletons cannot be cast to net.sour
ceforge.jFuzzyLogic.defuzzifier.DefuzzifierContinuous
at net.sourceforge.jFuzzyLogic.FunctionBlock.toStringCpp(FunctionBlock.j
ava:685)
at net.sourceforge.jFuzzyLogic.FIS.toStringCpp(FIS.java:299)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.compile(JFuzzyLogic.java:48)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.run(JFuzzyLogic.java:170)
at net.sourceforge.jFuzzyLogic.JFuzzyLogic.main(JFuzzyLogic.java:36)
d1.fcl:
FUNCTION_BLOCK d1
VAR_INPUT
temp : REAL;
pressure : REAL;
END_VAR
VAR_OUTPUT
valve : REAL;
END_VAR
FUZZIFY temp
TERM cold := (3, 1) (27, 0);
TERM hot := (3, 0 ) (27, 1);
END_FUZZIFY
FUZZIFY pressure
TERM low := (55, 1) (95, 0);
TERM high:= (55, 0) (95, 1);
END_FUZZIFY
DEFUZZIFY valve
TERM drainage := -100;
TERM closed := 0;
TERM inlet := 100;
METHOD : COGS;
DEFAULT := 0;
END_DEFUZZIFY
RULEBLOCK No1
AND : MIN;
ACCU : MAX;
RULE 1 : IF temp IS cold AND pressure IS low THEN valve IS inlet;
RULE 2 : IF temp IS cold AND pressure IS high THEN valve IS closed WITH 0.8;
RULE 3 : IF temp IS hot AND pressure IS low THEN valve IS closed;
RULE 4 : IF temp IS hot AND pressure IS high THEN valve IS drainage;
END_RULEBLOCK
END_FUNCTION_BLOCK
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.