diff --git a/src/fuzzy_logic/FuzzyLogicCalculator.java b/src/fuzzy_logic/FuzzyLogicCalculator.java index 4b145166..d770d30c 100644 --- a/src/fuzzy_logic/FuzzyLogicCalculator.java +++ b/src/fuzzy_logic/FuzzyLogicCalculator.java @@ -32,6 +32,7 @@ public class FuzzyLogicCalculator { Farm farm; // farm associated with this calculator double fuzzy_size = 0; // set size of set to return int ranking_version = 0; // what ranking version to use + String strat; // which strategy "optimize" or "imitate" we are using. This is affected later when we select our set size. /** * Constructor for decision calculator for a specific farm based on the network. @@ -72,8 +73,8 @@ public FuzzyLogicCalculator(Farm farm, List farms) { public List getImitationActivities() { double[] c1 = new double[this.P.size()]; int len = c1.length; - double[][] matrix = new double[len-2][len-2]; // matrix of all activities against all activities - + double[][] matrix = new double[len-2][len-2]; // matrix of all activities against all activities + this.strat = "imitate"; this.fuzzy_size = this.farm.getP_imt_fuzzy_size(); // use imitation fuzzy size for this selection for (int i = 0; i < this.P.size(); i++) { @@ -107,8 +108,8 @@ public List getImitationActivities() { for (int i = 0; i< len - 2; i++) { ND.add(ND(i,matrix)); // add ordered values to list } - - List list = activityList((ND)); // cluster algorithm returns optimal activity list + + List list = activityList(ND); // cluster algorithm returns optimal activity list return list; } @@ -123,7 +124,7 @@ public List getOptimizationActivities() { double[] c2 = new double[this.L.size()]; int len = c1.length; double[][] matrix = new double[len-2][len-2]; // matrix of all activities against all activities - + this.strat = "optimize"; this.fuzzy_size = this.farm.getP_opt_fuzzy_size(); // for optimization activities we use this fuzzy size for (int i = 0; i < this.P.size(); i++) { @@ -158,7 +159,7 @@ public List getOptimizationActivities() { // fuzzy logic and clustering functions /** - * given a vector of non-domination scores (doubles) return an optimized activity list based on the desired fuzzy set size OR the natural clustering + * given a vector of non-domination scores (doubles) return an optimized activity list based on the desired fuzzy set size OR a default cluster size * @param x :: original ND vector * @return activityList :: list of activity names */ @@ -166,18 +167,18 @@ private List activityList(List x){ List activityList = new ArrayList(); // final activity list List originalND = new ArrayList(x); // backup list List sortedND = new ArrayList(x); // sorted list - List cluster = new ArrayList(); // final selected activity values from clustering algorithm + List selectedActivityList = new ArrayList(); // final selected activity values from clustering algorithm int index = 0; // index of ND value used to get specific activity indicated by index Collections.sort(sortedND); // returns ascending order 0->1 - cluster = activityOptimalSelection(sortedND); + selectedActivityList = activitySelection(sortedND); // turn ranking values into activity list by sorting the ND array, and then finding the corresponding match in the original list based on the index. // Ex: orig = [0.8,0.3,1.0,0.8] --> sorted = [1.0,0.8,0.8,0.3] // Take 1.0 from the sorted list and match to index value 2 from the original list. Then find activity with index 2 in the activity list of the system // Then take 0.8 and match that to index 0 and find corresponding activity[index = 0]. - for (int i = 0; i< cluster.size(); i++) { - index = originalND.indexOf(cluster.get(i)); + for (int i = 0; i< selectedActivityList.size(); i++) { + index = originalND.indexOf(selectedActivityList.get(i)); activityList.add(this.farm.getPreferences().getDataElementName().get(index)); originalND.set(index, -1.0); // duplicate values exist in array, so 'remove' when used to get next duplicate value } @@ -186,42 +187,51 @@ private List activityList(List x){ } /** - * if there are 1.0 values in the sorted list, then we have optimal values to return. If there are no 1.0 values then we select the best values based on the fuzzy size.
+ * Given a list of ND values corresponding to each activity, return a list of values that correspond to the best selection. + * We have two choices here. 1 - use the default fuzzy size and take the top n values. 2 - select all the 1.0 (max) values from the list.
+ * If there are 1.0 values in the sorted list, then we have optimal values to return. If there are no 1.0 values then we select the best values based on the fuzzy size.
* a list of [1.0, 0.8, 1.0, 0.9, 0.2, 0.3] will return [1.0,1.0]. * a list of [0.9, 0.8, 0.63, 0.9, 0.2, 0.3] will return [0.9,0.9,0.8] assuming fuzzy set size of 3. * @param sorted :: original list to cluster * @return list of preferred activity scores */ - private List activityOptimalSelection(List sorted) { + private List activitySelection(List sorted) { List cluster = new ArrayList(); - List cluster_smaller = new ArrayList(); + List selected_cluster = new ArrayList(); int fuzzy_size = 0; - if (farm.getP_ranking_version() == 0) { - for (int i = 0; i< sorted.size(); i++) { - if (sorted.get(i) == 1.0) { - fuzzy_size++; - } - } + if (this.strat == "optimize") { + fuzzy_size = (int) this.fuzzy_size; + cluster = sorted; - // if no 1.0 optimal activities are present than use the default size and select the best option. - if (fuzzy_size == 0) { - fuzzy_size = (int) this.fuzzy_size; + for (int i = 0; i< fuzzy_size; i++) { + selected_cluster.add(cluster.get(cluster.size() - i - 1)); } } - else if (farm.getP_ranking_version() == 1 ) { - fuzzy_size = (int) this.fuzzy_size; - } - - cluster = sorted; - if (cluster.size() > fuzzy_size) { + + else if (this.strat == "imitate") { + if (farm.getP_ranking_version() == 0) { + for (int i = 0; i< sorted.size(); i++) { + if (sorted.get(i) == 1.0) { + fuzzy_size++; + } + } + // if no 1.0 optimal activities are present than use the default size and select the best option. + if (fuzzy_size == 0) { + fuzzy_size = (int) this.fuzzy_size; + } + } + else if (farm.getP_ranking_version() == 1 ) { + fuzzy_size = (int) this.fuzzy_size; + } + + cluster = sorted; for (int i = 0; i< fuzzy_size; i++) { - cluster_smaller.add(cluster.get(cluster.size() - i - 1)); + selected_cluster.add(cluster.get(cluster.size() - i - 1)); } - return cluster_smaller; - } + } - return cluster; + return selected_cluster; } /** diff --git a/src/main/Consumat.java b/src/main/Consumat.java index 7e94b83b..ea5e27d5 100644 --- a/src/main/Consumat.java +++ b/src/main/Consumat.java @@ -37,7 +37,7 @@ public class Consumat { public static void main(String[] args) { initializeLogging(); - LOGGER.info("Starting FARMIND: version number: 0.9.5"); + LOGGER.info("Starting FARMIND: version number: 0.10.0"); Properties cmd = parseInput(args,false); // parse input arguments from control.properties ReadData reader = new ReadData(cmd); // read all input data files diff --git a/src/testing/FarmTests.java b/src/testing/FarmTests.java index 336a5be1..c52e8193 100644 --- a/src/testing/FarmTests.java +++ b/src/testing/FarmTests.java @@ -101,6 +101,7 @@ public void testImitationDecisionSize1() { farm.setSatisfaction(100); farm.setActivity_Dissimilarity(0); farm.setIncome_Dissimilarity(10); + // ranking version 0 selects all the 1.0 values from ND activity array List x = farm.decideActivitySet(allFarms, cmd); assertEquals(x.size(), 14); @@ -113,7 +114,7 @@ public void testImitationDecisionSize2() { farm.setSatisfaction(100); farm.setActivity_Dissimilarity(0); farm.setIncome_Dissimilarity(10); - farm.setP_ranking_version(1); + farm.setP_ranking_version(1); // ranking version 1 selects the fuzzy set size List x = farm.decideActivitySet(allFarms, cmd); assertEquals(x.size(), 5); @@ -155,21 +156,20 @@ public void testOptimizationDecisionSize1() { cmd.setProperty("modelName", "WEEDCONTROL"); List x = farm.decideActivitySet(allFarms, cmd); - assertEquals(x.size(), 14); + assertEquals(x.size(), 72); } @Test public void testOptimizationDecisionSize2() { - Farm farm = allFarms.get(0); + Farm farm = allFarms.get(1); farm.setSatisfaction(-1); farm.setActivity_Dissimilarity(0); farm.setIncome_Dissimilarity(0); cmd.setProperty("modelName", "WEEDCONTROL"); - farm.setP_ranking_version(1); List x = farm.decideActivitySet(allFarms, cmd); - assertEquals(x.size(), 72); + assertEquals(x.size(), 15); } @Test diff --git a/test_data/farm_parameters.csv b/test_data/farm_parameters.csv index 129999b2..780330c1 100644 --- a/test_data/farm_parameters.csv +++ b/test_data/farm_parameters.csv @@ -1,5 +1,5 @@ ,latitude,longitude,year,education,memory,beta_l,beta_s,beta_p,reference_income,aspiration_coef,tolerance_income,tolerance_activity,lambda,alpha_plus,alpha_minus,phi_plus,phi_minus,opt_fuzzy_size,imt_fuzzy_size,ranking_version,learning_rate Ahaus,0,0,1951,9,5,0.91,0.44,0.5,572,0,0.9,0.69,0.7,0.65,0.65,0.7,0.7,72,5,0,0 -Beckum,0,0,1951,5,5,0.97,0.54,0.5,443,0,0.89,0.6,0.7,0.65,0.65,0.7,0.7,72,5,0,0 +Beckum,0,0,1951,5,5,0.97,0.54,0.5,443,0,0.89,0.6,0.7,0.65,0.65,0.7,0.7,15,5,0,0 Bruehl,0,0,1951,7,5,0.61,0.65,0.5,265,0,0.72,0.9,0.7,0.65,0.65,0.7,0.7,72,5,0,0 Hoevelhof,0,0,1951,6,5,0.88,0.57,0.5,900,0,0.83,0.97,0.7,0.65,0.65,0.7,0.7,72,5,0,0