From ff1b7c3ebc4b0935683a75ed0735c852ed83eeab Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Wed, 28 Jun 2017 20:58:30 +0100
Subject: [PATCH] Cluster include water avaliablitity

---
 src/ac/ed/lurg/ModelConfig.java             |  2 +-
 src/ac/ed/lurg/country/CountryAgent.java    | 11 +++++++----
 src/ac/ed/lurg/yield/YieldClusterPoint.java | 17 +++++++++++------
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java
index f90b8fa9..5c5c4d26 100644
--- a/src/ac/ed/lurg/ModelConfig.java
+++ b/src/ac/ed/lurg/ModelConfig.java
@@ -250,6 +250,6 @@ public class ModelConfig {
 	public static final double PASTURE_MAX_IRRIGATION_RATE = getDoubleProperty("DEFAULT_MAX_IRRIGATION_RATE", 50.0); // shouldn't need this but some areas crops don't have a value, but was causing them to be selected
 	public static final int LPJG_TIMESTEP_SIZE = 5;
 	
-	public static final int NUM_YIELD_CLUSTERS = getIntProperty("NUM_YIELD_CLUSTERS", 500);
+	public static final int NUM_YIELD_CLUSTERS = getIntProperty("NUM_YIELD_CLUSTERS", 5000);
 	public static final long RANDOM_SEED = getIntProperty("RANDOM_SEED", 1974329);  // any number will do
 }
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index f2293887..586809c8 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -62,7 +62,7 @@ public class CountryAgent {
 		return yieldClusters;
 	}
 	
-	private RasterSet<IntegerRasterItem> calcYieldClusters(YieldRaster countryYieldSurfaces) {
+	private RasterSet<IntegerRasterItem> calcYieldClusters(RasterSet<IrrigationItem> irrigData, YieldRaster countryYieldSurfaces) {
 		
 		LogWriter.println("calcYieldClusters: for " + ModelConfig.NUM_YIELD_CLUSTERS + " clusters");	
 		
@@ -70,8 +70,11 @@ public class CountryAgent {
 		Collection<YieldClusterPoint> clusteringPoints = new HashSet<YieldClusterPoint>();
 		for (Entry<RasterKey, YieldResponsesItem> entry : countryYieldSurfaces.entrySet()) {
 			YieldResponsesItem yieldresp = entry.getValue();
-			if (yieldresp != null)
-				clusteringPoints.add(new YieldClusterPoint(entry.getKey(), yieldresp));
+			if (yieldresp != null) {
+				RasterKey key = entry.getKey();
+				IrrigationItem irrigItem = irrigData.get(key);
+				clusteringPoints.add(new YieldClusterPoint(key, yieldresp, irrigItem));
+			}
 		}
 				
 		// do the clustering
@@ -114,7 +117,7 @@ public class CountryAgent {
 		}
 		else {			
 			if (yieldClusters==null)
-				yieldClusters = calcYieldClusters(countryYieldSurfaces);  // this should only be on the first timestep
+				yieldClusters = calcYieldClusters(irrigData, countryYieldSurfaces);  // this should only be on the first timestep
 			
 			// optimize areas and intensity 
 			GamsRasterInput input = getGamsRasterInput(irrigData, countryYieldSurfaces, globalGen2EcIncrease);
diff --git a/src/ac/ed/lurg/yield/YieldClusterPoint.java b/src/ac/ed/lurg/yield/YieldClusterPoint.java
index 4b965f71..c04a9171 100644
--- a/src/ac/ed/lurg/yield/YieldClusterPoint.java
+++ b/src/ac/ed/lurg/yield/YieldClusterPoint.java
@@ -3,6 +3,7 @@ package ac.ed.lurg.yield;
 import java.util.Arrays;
 import java.util.Collection;
 
+import ac.ed.lurg.landuse.IrrigationItem;
 import ac.ed.lurg.types.CropType;
 import ac.ed.lurg.utils.LogWriter;
 import ac.ed.lurg.utils.cluster.ClusteringPoint;
@@ -13,31 +14,34 @@ public class YieldClusterPoint implements ClusteringPoint<String> {
 	private final static String PASTURE = "pas";
 	private final static String WHEAT_MIN = "wheatMin";
 	private final static String WHEAT_MAX = "wheatMax";
-	private final static String RICE_MIN = "riceMin";
+	private final static String RICE_MAX = "riceMin";
 	private final static String MAIZE_MIN = "maizeMin";
 	private final static String MAIZE_MAX = "maizeMax";
 	private final static String ROOTS_MIN = "rootsMin";
+	private final static String IRRIG_WATER_AVAL = "irrigWaterAval";
 
 	private RasterKey rasterKey;
 	private double wheatMin;
 	private double wheatMax;
-	private double riceMin;
+	private double riceMax;
 	private double maizeMin;
 	private double maizeMax;
 	private double rootsMin;
 	private double pasture;
+	private double irrigWaterAval;
 
-	public YieldClusterPoint(RasterKey rasterKey, YieldResponsesItem yields) {
+	public YieldClusterPoint(RasterKey rasterKey, YieldResponsesItem yields, IrrigationItem irrigItem) {
 		this.rasterKey = rasterKey;
 		
 		// not sure if we be better to get a reference to the YieldResponsesItem, rather than caching these values?
 		this.wheatMin = yields.getYieldNone(CropType.WHEAT);
-		this.riceMin = yields.getYieldNone(CropType.RICE);
+		this.riceMax = yields.getYieldMax(CropType.RICE);
 		this.maizeMin = yields.getYieldNone(CropType.MAIZE);
 		this.rootsMin = yields.getYieldNone(CropType.STARCHY_ROOTS);
 		this.pasture = yields.getYieldNone(CropType.PASTURE);
 		this.wheatMax = yields.getYieldMax(CropType.WHEAT);
 		this.maizeMax = yields.getYieldMax(CropType.MAIZE);
+		this.irrigWaterAval = irrigItem.getIrrigConstraint();
 	}
 
 	public RasterKey getRasterKey() {
@@ -51,10 +55,11 @@ public class YieldClusterPoint implements ClusteringPoint<String> {
 			case PASTURE:  return pasture;
 			case WHEAT_MIN:  return wheatMin;
 			case WHEAT_MAX:  return wheatMax;
-			case RICE_MIN:  return riceMin;
+			case RICE_MAX:  return riceMax;
 			case MAIZE_MIN:  return maizeMin;
 			case MAIZE_MAX:  return maizeMax;
 			case ROOTS_MIN:  return rootsMin;
+			case IRRIG_WATER_AVAL:  return irrigWaterAval;
 		}
 		LogWriter.printlnError("YieldClusterPoint.getClusteringValue: got unknown value " + key);
 		return Double.NaN;
@@ -62,7 +67,7 @@ public class YieldClusterPoint implements ClusteringPoint<String> {
 
 	@Override
 	public Collection<String> getAllClusteringKeys() {
-		return Arrays.asList(PASTURE, WHEAT_MIN, WHEAT_MAX, RICE_MIN, MAIZE_MIN, MAIZE_MAX, ROOTS_MIN);
+		return Arrays.asList(PASTURE, WHEAT_MIN, WHEAT_MAX, MAIZE_MIN, MAIZE_MAX, RICE_MAX, IRRIG_WATER_AVAL);
 	}
 	
 	public String toString() {
-- 
GitLab