Java源码示例:net.sourceforge.jFuzzyLogic.FIS
示例1
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
}
}
示例2
public static void main(String[] args) {
// Load from 'FCL' file
String fileName = "fcl/Blob.fcl";
FIS fis = FIS.load(fileName, true);
if (fis == null) {
System.err.println("Can't load file: '" + fileName + "'");
System.exit(1);
}
// Get default function block
FunctionBlock fb = fis.getFunctionBlock(null);
JFuzzyChart.get().chart(fb);
int [] lcom = {26,27,27,28,60,320,26,39}; // 25,40
int [] nom = {17,17,18,19,17,21,27,22}; // 14.5,22
int [] noa = {9,9,10,10,17,10,13,13}; // 8.5,13
JFuzzyChart.get().chart(fb);
for (int i = 0; i< lcom.length;i++){
fb.setVariable("lack_of_cohesion_in_methods", lcom[i]);
fb.setVariable("number_of_methods", nom[i]);
fb.setVariable("number_of_attributes", noa[i]);
fb.evaluate();
JFuzzyChart.get().chart(fb.getVariable("res"),fb.getVariable("res").getDefuzzifier(),true);
System.out.println("Res ("+lcom[i]+","+nom[i]+","+noa[i]+"): " + fb.getVariable("res").getValue());
}
}
示例3
public void createTxtFile() {
// Load from 'FCL' file
String fileName = "tests/tipper.fcl";
FIS fis = FIS.load(fileName, true);
// Show ruleset
FunctionBlock functionBlock = fis.getFunctionBlock(null);
// Set inputs
for (double service = 0; service <= 10; service += 1.0)
for (double food = 0; food <= 10; food += 1.0) {
// Set inputs
functionBlock.setVariable("service", service);
functionBlock.setVariable("food", food);
// Evaluate
functionBlock.evaluate();
// Get output
double tip = functionBlock.getVariable("tip").getValue();
// Show
System.out.println(service + "\t" + food + "\t" + tip);
}
}
示例4
/**
* Test method generating a string and parsing it
*/
@Test
public void testToString() {
Gpr.debug("Test");
// Load system
String fileName = "tests/tipper.fcl";
FIS fis = FIS.load(fileName, true);
// Parse FCL code generated by fis.toString()
FIS fis2;
try {
fis2 = FIS.createFromString(fis.toString(), false);
} catch (RecognitionException e) {
throw new RuntimeException("Could not parse FCL code generated by fis.toString(). This should never happen!!!");
}
// Compare both fis (should be identical)
boolean ok = fis.toString().equals(fis2.toString());
if (verbose) System.out.println("Are both fis equal?: " + ok);
if (!ok) throw new RuntimeException("FCL code for both fis is not the same.");
}
示例5
/**
* Test method for membership function
*/
public void checkMembershipFunction(String title, FIS fis, String membershipFunctionFile) {
Gpr.debug("Test");
int mem[][] = loadMembershipFile(membershipFunctionFile);
if (verbose) System.out.println(fis);
FunctionBlock fb = fis.getFunctionBlock(null);
for (int ind = 1; ind < mem.length; ind++) {
double value = int100ToDouble(mem[ind][0]);
fb.setVariable("inVar", value);
int poor = doubleToInt100(fb.getVariable("inVar").getMembership("poor"));
int good = doubleToInt100(fb.getVariable("inVar").getMembership("good"));
int excellent = doubleToInt100(fb.getVariable("inVar").getMembership("excellent"));
if (poor != mem[ind][1]) fail("Membership function " + title + ", poor(" + value + ") should be " + int100ToDouble(mem[ind][1]) + ", but it is " + int100ToDouble(poor));
if (good != mem[ind][2]) fail("Membership function " + title + ", good(" + value + ") should be " + int100ToDouble(mem[ind][2]) + ", but it is " + int100ToDouble(good));
if (excellent != mem[ind][3]) fail("Membership function " + title + ", excellent(" + value + ") should be " + int100ToDouble(mem[ind][3]) + ", but it is " + int100ToDouble(excellent));
}
}
示例6
/**
* Test method for membership function
*/
public void checkMembershipFunction(String title, String fclFile, String membershipFunctionFile) {
int mem[][] = loadMembershipFile(membershipFunctionFile);
FIS fis = FIS.load(fclFile);
if (verbose) System.out.println(fis);
FunctionBlock fb = fis.getFunctionBlock(null);
for (int ind = 1; ind < mem.length; ind++) {
double value = int100ToDouble(mem[ind][0]);
fb.setVariable("inVar", value);
int poor = doubleToInt100(fb.getVariable("inVar").getMembership("poor"));
int good = doubleToInt100(fb.getVariable("inVar").getMembership("good"));
int excellent = doubleToInt100(fb.getVariable("inVar").getMembership("excellent"));
if (poor != mem[ind][1]) fail("Membership function " + title + ", poor(" + value + ") should be " + int100ToDouble(mem[ind][1]) + ", but it is " + int100ToDouble(poor));
if (good != mem[ind][2]) fail("Membership function " + title + ", good(" + value + ") should be " + int100ToDouble(mem[ind][2]) + ", but it is " + int100ToDouble(good));
if (excellent != mem[ind][3]) fail("Membership function " + title + ", excellent(" + value + ") should be " + int100ToDouble(mem[ind][3]) + ", but it is " + int100ToDouble(excellent));
}
}
示例7
/**
* 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"));
}
示例8
@Test
public void test() {
Gpr.debug("Test");
// Prepare command line
String fileName = "tests/tipper.fcl";
String args[] = { "-noCharts", "-e", fileName, "8.5", "9" };
// Run
JFuzzyLogic jFuzzyLogic = new JFuzzyLogic(args);
jFuzzyLogic.run();
FIS fis = jFuzzyLogic.getFis();
// Check input variables
Assert.assertEquals(fis.getVariable("food").getValue(), 8.5, EPSILON);
Assert.assertEquals(fis.getVariable("service").getValue(), 9, EPSILON);
}
示例9
public EngineSoundSet () {
rpm = 0;
gear = 1;
//@off
feIdle = FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolIdle.fcl", FileType.Internal).read(), true);
feOnLow = FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOnLow.fcl", FileType.Internal).read(), true);
feOnMid= FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOnMid.fcl", FileType.Internal).read(), true);
feOnHigh = FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOnHigh.fcl", FileType.Internal).read(), true);
feOffLow = FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOffLow.fcl", FileType.Internal).read(), true);
feOffMid= FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOffMid.fcl", FileType.Internal).read(), true);
feOffHigh = FIS.load(Gdx.files.getFileHandle("data/audio/car-engine/fuzzy/engineVolOffHigh.fcl", FileType.Internal).read(), true);
//@on
}
示例10
@Override
public void initialize() {
numberOfHost=SimSettings.getInstance().getNumOfEdgeHosts();
try {
fis1 = FIS.createFromString(FCL_definition.fclDefinition1, false);
fis2 = FIS.createFromString(FCL_definition.fclDefinition2, false);
fis3 = FIS.createFromString(FCL_definition.fclDefinition3, false);
} catch (RecognitionException e) {
SimLogger.printLine("Cannot generate FIS! Terminating simulation...");
e.printStackTrace();
System.exit(0);
}
}
示例11
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (cl:Class) WHERE cl.lack_of_cohesion_in_methods >" + high_lcom + " AND cl.number_of_methods > " + high_nom + " AND cl.number_of_attributes > " + high_noa + " RETURN cl.app_key as app_key,cl.lack_of_cohesion_in_methods as lack_of_cohesion_in_methods,cl.number_of_methods as number_of_methods, cl.number_of_attributes as number_of_attributes";
if(details){
query += ",cl.name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int lcom,noa,nom;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
lcom = (int) res.get("lack_of_cohesion_in_methods");
noa = (int) res.get("number_of_attributes");
nom = (int) res.get("number_of_methods");
if(lcom >= veryHigh_lcom && noa >= veryHigh_noa && nom >= veryHigh_nom){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("lack_of_cohesion_in_methods",lcom);
fb.setVariable("number_of_attributes",noa);
fb.setVariable("number_of_methods",nom);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_BLOB.csv");
}
}
示例12
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (c:Class{is_broadcast_receiver:true})-[:CLASS_OWNS_METHOD]->(m:Method{name:'onReceive'}) WHERE m.number_of_instructions > "+high_noi+" AND m.cyclomatic_complexity>"+high_cc+" return m.app_key as app_key,m.cyclomatic_complexity as cyclomatic_complexity, m.number_of_instructions as number_of_instructions";
if(details){
query += ",m.full_name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int noi,cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("cyclomatic_complexity");
noi = (int) res.get("number_of_instructions");
if(cc >= veryHigh_cc && noi >= veryHigh_noi){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("cyclomatic_complexity",cc);
fb.setVariable("number_of_instructions",noi);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_HBR.csv");
}
}
示例13
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (cl:Class) WHERE HAS(cl.is_interface) AND cl.number_of_methods > " + high + " RETURN cl.app_key as app_key,cl.number_of_methods as number_of_methods";
if(details){
query += ",cl.name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("number_of_methods");
if(cc >= veryHigh){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("number_of_methods",cc);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_SAK.csv");
}
}
示例14
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (m:Method) WHERE m.number_of_instructions >" + high + " RETURN m.app_key as app_key,m.number_of_instructions as number_of_instructions";
if(details){
query += ",m.full_name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("number_of_instructions");
if(cc >= veryHigh){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("number_of_instructions",cc);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_LM.csv");
}
}
示例15
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (c:Class{parent_name:'android.os.AsyncTask'})-[:CLASS_OWNS_METHOD]->(m:Method) WHERE (m.name='onPreExecute' OR m.name='onProgressUpdate' OR m.name='onPostExecute') AND m.number_of_instructions >"+high_noi+" AND m.cyclomatic_complexity > "+high_cc+" return m.app_key as app_key,m.cyclomatic_complexity as cyclomatic_complexity, m.number_of_instructions as number_of_instructions";
if(details){
query += ",m.full_name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int noi,cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("cyclomatic_complexity");
noi = (int) res.get("number_of_instructions");
if(cc >= veryHigh_cc && noi >= veryHigh_noi){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("cyclomatic_complexity",cc);
fb.setVariable("number_of_instructions",noi);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_HAS.csv");
}
}
示例16
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (cl:Class) WHERE cl.class_complexity > " + high + " RETURN cl.app_key as app_key, cl.class_complexity as class_complexity";
if(details){
query += ",cl.name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("class_complexity");
if(cc >= veryHigh){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("class_complexity",cc);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_CC.csv");
}
}
示例17
public void executeFuzzy(boolean details) throws CypherException, IOException {
Result result;
try (Transaction ignored = graphDatabaseService.beginTx()) {
String query = "MATCH (c:Class{is_service:true})-[:CLASS_OWNS_METHOD]->(m:Method{name:'onStartCommand'}) WHERE m.number_of_instructions > "+high_noi+" AND m.cyclomatic_complexity>"+high_cc+" return m.app_key as app_key,m.cyclomatic_complexity as cyclomatic_complexity, m.number_of_instructions as number_of_instructions";
if(details){
query += ",m.full_name as full_name";
}
result = graphDatabaseService.execute(query);
List<String> columns = new ArrayList<>(result.columns());
columns.add("fuzzy_value");
int noi,cc;
List<Map> fuzzyResult = new ArrayList<>();
File fcf = new File(fclFile);
//We look if the file is in a directory otherwise we look inside the jar
FIS fis;
if(fcf.exists() && !fcf.isDirectory()){
fis = FIS.load(fclFile, false);
}else{
fis = FIS.load(getClass().getResourceAsStream(fclFile),false);
}
FunctionBlock fb = fis.getFunctionBlock(null);
while(result.hasNext()){
HashMap res = new HashMap(result.next());
cc = (int) res.get("cyclomatic_complexity");
noi = (int) res.get("number_of_instructions");
if(cc >= veryHigh_cc && noi >= veryHigh_noi){
res.put("fuzzy_value", 1);
}else {
fb.setVariable("cyclomatic_complexity",cc);
fb.setVariable("number_of_instructions",noi);
fb.evaluate();
res.put("fuzzy_value", fb.getVariable("res").getValue());
}
fuzzyResult.add(res);
}
queryEngine.resultToCSV(fuzzyResult,columns,"_HSS.csv");
}
}
示例18
public static void main(String[] args) throws Exception {
// Load from 'FCL' file
String fileName = "fcl/tipper.fcl";
FIS fis = FIS.load(fileName, true);
if (fis == null) { // Error while loading?
System.err.println("Can't load file: '" + fileName + "'");
return;
}
// Show ruleset
FunctionBlock functionBlock = fis.getFunctionBlock(null);
JFuzzyChart.get().chart(functionBlock);
// Set inputs
functionBlock.setVariable("service", 3);
functionBlock.setVariable("food", 7);
// Evaluate
functionBlock.evaluate();
// Show output variable's chart
Variable tip = functionBlock.getVariable("tip");
JFuzzyChart.get().chart(tip, tip.getDefuzzifier(), true);
Gpr.debug("poor[service]: " + functionBlock.getVariable("service").getMembership("poor"));
// Print ruleSet
System.out.println(functionBlock);
System.out.println("TIP:" + functionBlock.getVariable("tip").getValue());
}
示例19
/**
* 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);
}
}
示例20
void initFisDebugPanel() {
// Create a plot
if (fisPanel != null) {
System.out.println(" Remove existing fis panel");
fuzzyLayout.getTabPanel().remove(fisPanel);
}
FIS fis = fuzzyController.getFis();
List<Variable> list = fuzzyController.getVariables();
fisPanel = new DemoPanelFis(fuzzyController.getVariables(),
list.size(), 1);
fuzzyLayout.getTabPanel().add("Graphs", fisPanel);
}
示例21
public void reload(String str) {
FIS newfis;
try {
newfis = FIS.createFromString(str, true);
fis = newfis;
fisString = str;
// functionBlock = fis.getFunctionBlock(null);
init();
} catch(RecognitionException ex) {
Logger.getLogger(FuzzyController.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例22
public void init(FIS fis, int width, int height) {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); // The program may still run when the window is closed (that's why we don't use JFrame.EXIT_ON_CLOSE)
BoxLayout layout = new BoxLayout(getContentPane(), 0);
getContentPane().setLayout(layout);
pack();
panel = new JPanelFis(fis);
setSize(width, height);
setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
}
示例23
public List<Double> execute(String fileName, String[] inputVariables, int steps, int stepSize) {
// Load FIS
FIS fis = FIS.load(fileName, false);
if( fis == null ) { // Error while loading?
System.err.println("Can't load file: '" + fileName + "'");
return null;
}
FunctionBlock functionBlock = fis.getFunctionBlock(null);
// Time recording
List<Double> timeRecords = new ArrayList<Double>();
long startTime;
int curStep = 0;
// Test
for( int i = 0; i <= steps; i++, curStep += stepSize ) {
startTime = System.currentTimeMillis();
for( int j = 0; j <= curStep; j++ ) {
// Set inputs
for( int k = 0; k < inputVariables.length; k++ )
functionBlock.setVariable(inputVariables[k], Math.random() * 5);
// Evaluate fuzzy set
functionBlock.evaluate();
}
timeRecords.add(new Double(System.currentTimeMillis() - startTime));
if( debug ) Gpr.debug("Evaluate " + fileName + "\ti:" + i + "\tcurStep: " + curStep);
}
return timeRecords;
}
示例24
@Test
public void test() {
Gpr.debug("Test");
// Load from 'FCL' file
String fileName = "tests/tipper.fcl";
FIS fis = FIS.load(fileName, true);
// Show ruleset
FunctionBlock functionBlock = fis.getFunctionBlock(null);
// Read results file
String lines[] = Gpr.readFile("tests/tipper.txt").split("\n");
// Iterate
for (String line : lines) {
// Parse line
String recs[] = line.split("\t");
double service = Gpr.parseDoubleSafe(recs[0]);
double food = Gpr.parseDoubleSafe(recs[1]);
double tip = Gpr.parseDoubleSafe(recs[2]);
// Set inputs
functionBlock.setVariable("service", service);
functionBlock.setVariable("food", food);
// Evaluate
functionBlock.evaluate();
// Get output
double tipEv = functionBlock.getVariable("tip").getValue();
// Show
Assert.assertEquals(tip, tipEv, EPSILON);
}
}
示例25
/**
* Test method 'tipper' fuzzy system
*/
@Test
public void testTipper() {
Gpr.debug("Test");
// Load tipper fuzzy system
FIS fis = FIS.load("./tests/junit_tipper.fcl", true);
FunctionBlock fb = fis.getFunctionBlock(null);
// Load stored results
int mem[][] = loadMembershipFile("./tests/junit_tipper.txt");
// Compare running the system vs. stored results
for (int ind = 1; ind < mem.length; ind++) {
// Get input variables from stores results
double service = int100ToDOuble(mem[ind][0]);
double food = int100ToDOuble(mem[ind][1]);
// Set variables and run the system
fb.setVariable("service", service);
fb.setVariable("food", food);
fb.evaluate();
// Get output variable
double tip = fb.getVariable("tip").getLatestDefuzzifiedValue();
// Compare output variable to stored result
if (doubleToInt100(tip) != mem[ind][2]) fail("Tipper output tip(service=" + service + ", food=" + food + ") should be " + int100ToDOuble(mem[ind][2]) + ", but it is " + tip);
}
}
示例26
/**
* 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()));
}
}
}
示例27
/**
* Check that De Morgan laws are correct for all AND/OR combinations
*/
@Test
public void testDeMorgan() {
Gpr.debug("Test");
if (verbose) System.out.println("Testing De Morgan's law: AND=MIN / OR=MAX");
FIS fis = FIS.load("fcl/testDeMorgan_1.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
if (verbose) System.out.println("Testing De Morgan's law: AND=PROD / OR=ASUM (a.k.a. PROB_OR)");
fis = FIS.load("fcl/testDeMorgan_2.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
if (verbose) System.out.println("Testing De Morgan's law: AND=BDIF / OR=BSUM");
fis = FIS.load("fcl/testDeMorgan_3.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
if (verbose) System.out.println("Testing De Morgan's law: AND=DMIN / OR=DMAX");
fis = FIS.load("fcl/testDeMorgan_4.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
if (verbose) System.out.println("Testing De Morgan's law: AND=NIPMIN / OR=NIPMAX");
fis = FIS.load("fcl/testDeMorgan_5.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
if (verbose) System.out.println("Testing De Morgan's law: AND=HAMACHER / OR=EINSTEIN");
fis = FIS.load("fcl/testDeMorgan_6.fcl");
if (verbose) System.out.println(fis);
checkDeMorgan(fis);
}
示例28
/**
* Test method for {@link net.sourceforge.jFuzzyLogic.FIS#load(java.lang.String)}.
*/
@Test
public void testFileParsing1() {
Gpr.debug("Test");
FIS fis = FIS.load("./tests/junit1.fcl", true);
if (verbose) System.out.println(fis);
separator();
}
示例29
/**
* Test method for {@link net.sourceforge.jFuzzyLogic.FIS#load(java.lang.String)}.
*/
@Test
public void testFileParsing2() {
Gpr.debug("Test");
FIS fis = FIS.load("./tests/junit2.fcl", true);
if (verbose) System.out.println(fis);
separator();
}
示例30
/**
* Test method for {@link net.sourceforge.jFuzzyLogic.FIS#load(java.lang.String)}.
*/
@Test
public void testFileParsing3() {
Gpr.debug("Test");
FIS fis = FIS.load("./tests/junit3.fcl", true);
if (verbose) System.out.println(fis);
separator();
}