Skip to content

Commit

Permalink
minor coment updates and refactoring
Browse files Browse the repository at this point in the history
Also added some new tests to account for the extra activity selections
  • Loading branch information
Keller Kevin committed Feb 25, 2019
1 parent 0dbf92a commit edd0b09
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
74 changes: 42 additions & 32 deletions src/fuzzy_logic/FuzzyLogicCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -72,8 +73,8 @@ public FuzzyLogicCalculator(Farm farm, List<Farm> farms) {
public List<String> 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++) {
Expand Down Expand Up @@ -107,8 +108,8 @@ public List<String> getImitationActivities() {
for (int i = 0; i< len - 2; i++) {
ND.add(ND(i,matrix)); // add ordered values to list
}

List<String> list = activityList((ND)); // cluster algorithm returns optimal activity list
List<String> list = activityList(ND); // cluster algorithm returns optimal activity list

return list;
}
Expand All @@ -123,7 +124,7 @@ public List<String> 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++) {
Expand Down Expand Up @@ -158,26 +159,26 @@ public List<String> 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
*/
private List<String> activityList(List<Double> x){
List<String> activityList = new ArrayList<String>(); // final activity list
List<Double> originalND = new ArrayList<Double>(x); // backup list
List<Double> sortedND = new ArrayList<Double>(x); // sorted list
List<Double> cluster = new ArrayList<Double>(); // final selected activity values from clustering algorithm
List<Double> selectedActivityList = new ArrayList<Double>(); // 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
}
Expand All @@ -186,42 +187,51 @@ private List<String> activityList(List<Double> 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. <br>
* 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. <br>
* 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. <br>
* 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<Double> activityOptimalSelection(List<Double> sorted) {
private List<Double> activitySelection(List<Double> sorted) {
List<Double> cluster = new ArrayList<Double>();
List<Double> cluster_smaller = new ArrayList<Double>();
List<Double> selected_cluster = new ArrayList<Double>();
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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/Consumat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/testing/FarmTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> x = farm.decideActivitySet(allFarms, cmd);
assertEquals(x.size(), 14);
Expand All @@ -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<String> x = farm.decideActivitySet(allFarms, cmd);
assertEquals(x.size(), 5);
Expand Down Expand Up @@ -155,21 +156,20 @@ public void testOptimizationDecisionSize1() {
cmd.setProperty("modelName", "WEEDCONTROL");

List<String> 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<String> x = farm.decideActivitySet(allFarms, cmd);
assertEquals(x.size(), 72);
assertEquals(x.size(), 15);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion test_data/farm_parameters.csv
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit edd0b09

Please sign in to comment.