Java Code Examples for net.sourceforge.jFuzzyLogic.FIS#evaluate()

The following examples show how to use net.sourceforge.jFuzzyLogic.FIS#evaluate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EngineSoundSet.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
private void updateVolume (int track, FIS fuzzyEngine, float load, float rpm) {
	fuzzyEngine.setVariable("load", load);
	fuzzyEngine.setVariable("rpm", rpm);
	fuzzyEngine.evaluate();
	float volume = ((float)fuzzyEngine.getVariable("volume").getValue() / 100f);

	if (volume >= 0 && volume <= 1) {
		setVolume(track, volume * SoundManager.SfxVolumeMul);

		// dbg
		// if (track == 1 || track == 4) {
		// setVolume(track, (float)volume * SoundManager.SfxVolumeMul);
		// } else {
		// setVolume(track, 0);
		// }
		// dbg
	}
}
 
Example 2
Source File: TestCaseJfuzzy.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Test method a fuzzy system that showed NA values due to 'Triangle' membership function bug
 * Bug report and FCL code by Shashankrao Wankhede
 */
@Test
public void testNAmembership() {
	Gpr.debug("Test");

	// FCL.debug = true;
	FIS fis = FIS.load("./tests/junit_shashankrao.fcl", true);
	if (verbose) System.out.println(fis);

	// This set of values used to produce a 'NaN' output
	double ra = 0.5;
	double ad = 0.0;
	fis.setVariable("ra", ra);
	fis.setVariable("ad", ad);
	fis.evaluate();

	// Right output should be 0.5
	double ta = fis.getVariable("ta").getValue();
	if (Double.isNaN(ta) || Double.isInfinite(ta) || (Math.abs(ta - 0.5) > EPSILON)) fail("System's output should be 0.5, but it's " + ta + "\n" + fis.getVariable("ta"));
}
 
Example 3
Source File: TestTipperJava.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Show animation
 * @param fis
 * @throws Exception
 */
static void animateFis(FIS fis) throws Exception {
	if (JFuzzyChart.UseMockClass) {
		Gpr.debug("Using mock class");
		return; // Nothing done
	}

	// Create a plot
	JDialogFis jdf = new JDialogFis(fis, 800, 600);

	// Set different values for 'food' and 'service'. Evaluate the system and show variables
	//		for( double service = 0.0, food = 1; service <= 10; service += 0.1 ) {
	for (double service = 0.0, food = 1; service <= 10; service += 0.1) {
		food = service;
		// Evaluate system using these parameters
		fis.getVariable("service").setValue(service);
		fis.getVariable("food").setValue(food);
		fis.evaluate();

		// Print result & update plot
		System.out.println(String.format("Service: %2.2f\tfood:%2.2f\t=> tip: %2.2f %%", service, food, fis.getVariable("tip").getValue()));
		jdf.repaint();

		// Small delay
		Thread.sleep(100);
	}

}
 
Example 4
Source File: TestCaseJfuzzy.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Verify that De Morgan's laws are OK
 */
void checkDeMorgan(FIS fis) {
	// De Morgan's laws test
	FunctionBlock fb = fis.getFunctionBlock(null);
	RuleBlock rb = fb.getRuleBlocks().values().iterator().next();
	List<Rule> rules = rb.getRules();
	Rule r1 = rules.get(0);
	Rule r2 = rules.get(1);
	Rule r3 = rules.get(2);
	Rule r4 = rules.get(3);

	r1.getDegreeOfSupport();

	// Set different values for 'food' and 'service'. Evaluate the system and show variables
	//		for( double service = 0.0, food = 1; service <= 10; service += 0.1 ) {
	for (double x1 = 0; x1 <= 1.0; x1 += 0.01) {
		for (double x2 = 0; x2 <= 1.0; x2 += 0.01) {
			// Evaluate system using these parameters
			fis.getVariable("x1").setValue(x1);
			fis.getVariable("x2").setValue(x2);
			fis.evaluate();

			// DeMorgan law: NOT(x1 IS small OR x2 IS small) == NOT(x1 IS small) AND NOT(x2 IS small)
			double diff = Math.abs(r1.getDegreeOfSupport() - r2.getDegreeOfSupport());
			if (diff > EPSILON) throw new RuntimeException(String.format("x1: %2.2f\tx2:%2.2f\t=> r1: %2.2f\tr2: %2.2f", x1, x2, r1.getDegreeOfSupport(), r2.getDegreeOfSupport()));

			// DeMorgan law: NOT(x1 IS small OR x2 IS small) == NOT(x1 IS small) AND NOT(x2 IS small)
			diff = Math.abs(r3.getDegreeOfSupport() - r4.getDegreeOfSupport());
			if (diff > EPSILON) throw new RuntimeException(String.format("x1: %2.6f\tx2:%2.6f\t=> r3: %2.6f\tr4: %2.6f", x1, x2, r3.getDegreeOfSupport(), r4.getDegreeOfSupport()));

		}
	}
}
 
Example 5
Source File: TipperAnimation.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {
	System.out.println("Start: TipperAnimation");

	// Create FCL system
	String fcl = "FUNCTION_BLOCK tipper\n" + //
			"\n" + //
			"VAR_INPUT\n" + //
			"   service : REAL;\n" + //
			"   food : REAL;\n" + //
			"END_VAR\n" + //
			"\n" + //
			"VAR_OUTPUT\n" + //
			"   tip : REAL;\n" + //
			"END_VAR\n" + //
			"\n" + //
			"FUZZIFY service\n" + //
			"   TERM poor := (0, 1) (4, 0) ; \n" + //
			"   TERM good := (1, 0) (4,1) (6,1) (9,0);\n" + //
			"   TERM excellent := (6, 0) (9, 1) (10,1);\n" + //
			"END_FUZZIFY\n" + //
			"\n" + //
			"FUZZIFY food\n" + //
			"   TERM rancid := (0, 1) (1, 1) (3,0) ;\n" + //
			"   TERM delicious := (7,0) (9,1) (10,1);\n" + //
			"END_FUZZIFY\n" + //
			"\n" + //
			"DEFUZZIFY tip\n" + //
			"   TERM cheap := (0,0) (5,1) (10,0);\n" + //
			"   TERM average := (10,0) (15,1) (20,0);\n" + //
			"   TERM generous := (20,0) (25,1) (30,0);\n" + //
			"   METHOD : COG;\n" + //
			"   DEFAULT := 0;\n" + //
			"END_DEFUZZIFY\n" + //
			"\n" + //
			"RULEBLOCK No1\n" + //
			"   ACCU : MAX;\n" + //
			"   AND : MIN;\n" + //
			"   ACT : MIN;\n" + //
			"\n" + //
			"   RULE 1 : IF service IS poor OR food is rancid THEN tip IS cheap;\n" + //
			"   RULE 2 : IF service IS good THEN tip IS average; \n" + //
			"   RULE 3 : IF service IS excellent AND food IS delicious THEN tip is generous;\n" + //
			"END_RULEBLOCK\n" + //
			"\n" + //
			"END_FUNCTION_BLOCK\n";

	FIS fis = FIS.createFromString(fcl, true);
	System.out.println(fis);

	// Create a plot
	JDialogFis jdf = null;
	if (!JFuzzyChart.UseMockClass) jdf = new JDialogFis(fis, 800, 600);

	// Set different values for 'food' and 'service'. Evaluate the system and show variables
	for (double service = 0.0, food = 1; service <= 10; service += 0.1) {
		food = service;
		// Evaluate system using these parameters
		fis.getVariable("service").setValue(service);
		fis.getVariable("food").setValue(food);
		fis.evaluate();

		// Print result & update plot
		System.out.println(String.format("Service: %2.2f\tfood:%2.2f\t=> tip: %2.2f %%", service, food, fis.getVariable("tip").getValue()));
		if (jdf != null) jdf.repaint();

		// Small delay
		Thread.sleep(100);
	}

	System.out.println("End: TipperAnimation");
}
 
Example 6
Source File: TestCaseJfuzzy.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testMembershipWithVariables() {
	Gpr.debug("Test");

	// FCL.debug = true;
	FIS fis = FIS.load("./tests/membershipWithVariables.fcl", true);

	Variable var = fis.getVariable("out");
	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}

	//---
	// Part 1
	//---
	double low = 1, mid = 2, high = 3;
	fis.setVariable("low", low);
	fis.setVariable("mid", mid);
	fis.setVariable("high", high);
	fis.evaluate();

	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}
	Assert.assertTrue(Math.abs(0.0 - var.getUniverseMin()) < EPSILON);
	Assert.assertTrue(Math.abs(3.0 - var.getUniverseMax()) < EPSILON);

	//---
	// Part 2:Re-assign variables and make sure that the 'universe' is properly recalculated
	//---
	low = 2;
	mid = 4;
	high = 6;
	fis.setVariable("low", low);
	fis.setVariable("mid", mid);
	fis.setVariable("high", high);
	fis.evaluate();

	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}
	Assert.assertTrue(Math.abs(0.0 - var.getUniverseMin()) < EPSILON);
	Assert.assertTrue(Math.abs(6.0 - var.getUniverseMax()) < EPSILON);
}
 
Example 7
Source File: TestCaseZzz.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testMembershipWithVariables() {
	Gpr.debug("Test");

	// FCL.debug = true;
	FIS fis = FIS.load("./tests/membershipWithVariables.fcl", true);

	System.out.println(fis);
	Variable var = fis.getVariable("out");
	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}

	//---
	// Part 1
	//---
	double low = 1, mid = 2, high = 3;
	fis.setVariable("low", low);
	fis.setVariable("mid", mid);
	fis.setVariable("high", high);
	fis.evaluate();

	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}
	Assert.assertTrue(Math.abs(0.0 - var.getUniverseMin()) < EPSILON);
	Assert.assertTrue(Math.abs(3.0 - var.getUniverseMax()) < EPSILON);

	//---
	// Part 2:Re-assign variables and make sure that the 'universe' is properly recalculated
	//---
	low = 2;
	mid = 4;
	high = 6;
	fis.setVariable("low", low);
	fis.setVariable("mid", mid);
	fis.setVariable("high", high);
	fis.evaluate();

	if (verbose) {
		System.out.println(var);
		System.out.println("Universe:\t[" + var.getUniverseMin() + " , " + var.getUniverseMax() + "]: " + var.getValue());
	}
	Assert.assertTrue(Math.abs(0.0 - var.getUniverseMin()) < EPSILON);
	Assert.assertTrue(Math.abs(6.0 - var.getUniverseMax()) < EPSILON);
}