/*
JFCM (Java Fuzzy Congnitive Maps)
Copyright (C) 2009 De Franciscis Dimitri - www.megadix.it
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package trust.jfcm.examples;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Locale;
import trust.jfcm.*;
import trust.jfcm.utils.FcmIO;
import trust.jfcm.utils.FcmRunner;
import trust.jfcm.utils.SimpleFcmRunner;
/**
* Modified example inspired by:
* http://www.ochoadeaspuru.com/fuzcogmap/wildlifepark.php
*
* by Guillermo Ochoa de Aspuru
*
*/
public class WildlifePark {
FcmRunner runner;
CognitiveMap map;
public WildlifePark() {
//map = FcmIO.loadXml(getClass().getResourceAsStream("WildlifePark.fcm.xml")).get(0);
map = FcmIO.loadXml(getClass().getResourceAsStream("WildlifePark.SIGMOID.fcm.xml")).get(0);
runner = new SimpleFcmRunner(map, 0.1, 1000);
}
public void test_scenario_1() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.1);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.1);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 1", runner.converge());
}
public void test_scenario_2() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.1);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.5);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 2", runner.converge());
}
public void test_scenario_3() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.1);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.9);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 3", runner.converge());
}
public void test_scenario_4() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.5);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.1);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 4", runner.converge());
}
public void test_scenario_5() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.5);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.5);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 5", runner.converge());
}
public void test_scenario_6() throws Exception {
resetMap();
// FIXED VALUES
map.getConcepts().get("Rain").setOutput(0.5);
map.getConcepts().get("Rain").setFixedOutput(true);
map.getConcepts().get("Rangers").setOutput(0.9);
map.getConcepts().get("Rangers").setFixedOutput(true);
// start from zero
map.getConcepts().get("Grassland").setOutput(0.0);
map.getConcepts().get("Poachers").setOutput(0.0);
map.getConcepts().get("Erbivores").setOutput(0.0);
map.getConcepts().get("Predators").setOutput(0.0);
showResults("Scenario 6", runner.converge());
}
public void run() {
try {
System.out.print("Scenario\tConverged");
for (Concept c : map.getConcepts().values()) {
System.out.print("\t" + c.getName());
}
System.out.println();
test_scenario_1();
test_scenario_2();
test_scenario_3();
test_scenario_4();
test_scenario_5();
test_scenario_6();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void resetMap() {
Iterator<Concept> iter = map.getConceptsIterator();
while (iter.hasNext()) {
Concept concept = iter.next();
concept.setOutput(0.0);
concept.setPrevOutput(null);
concept.setFixedOutput(false);
}
}
void showResults(String scenario, boolean converged) {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setMaximumFractionDigits(8);
System.out.print(scenario + "\t" + converged);
for (Concept c : map.getConcepts().values()) {
System.out.print("\t");
System.out.print(c.getOutput() != null ? nf.format(c.getOutput()) : "");
}
System.out.println();
}
public static void main(String[] args) {
WildlifePark example = new WildlifePark();
example.run();
}
}