From 0acbdfe6436d903add43a6f0222ace090082188d Mon Sep 17 00:00:00 2001
From: R0slyn <devnull@localhost>
Date: Tue, 8 Nov 2016 15:35:12 +0000
Subject: [PATCH] incorporating trade barriers. trade barriers only affect
 exports. rather than calculate global export calculate country export (global
 export * scaler for country). Parameter in GAMS still called
 worldExportPrice. Perhaps needs to be changed to country.

---
 .../lurg/country/gams/GamsCountryInput.java   | 21 ++++++++++++-------
 .../country/gams/GamsLocationOptimiser.java   |  7 ++++---
 .../lurg/country/gams/GamsLocationTest.java   | 15 ++++++++++++-
 .../country/gams/GamsRasterOptimiser.java     |  2 +-
 .../ed/lurg/country/gams/GamsRasterTest.java  |  2 +-
 5 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java
index a8192511..92b8b96e 100644
--- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java
+++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java
@@ -9,6 +9,7 @@ import ac.ed.lurg.country.GlobalPrice;
 import ac.ed.lurg.country.ImportExportConstraint;
 import ac.ed.lurg.types.CommodityType;
 import ac.ed.lurg.types.CropType;
+import ac.ed.lurg.utils.LogWriter;
 
 public class GamsCountryInput {
 
@@ -18,15 +19,15 @@ public class GamsCountryInput {
 	private Map<CropType, ImportExportConstraint> importConstraints;
 	private Map<CropType, Double> cropAdjustments;
 	private boolean calibrateToObserved;
+	private Map<CropType, Double> tradeBarriers;
 	
 /*	private double maxLandUseChange;
 	private double meatEfficiency;
 	private double minFeedRate;
-	private double tradeBarrier;
 	private double landChangeEnergy;*/
 	
 	
-	private GamsCountryInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, GlobalPrice> worldPrices,
+	private GamsCountryInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, Double> tradeBarriers, Map<CropType, GlobalPrice> worldPrices,
 			Map<CropType, ImportExportConstraint> importConstraints, Map<CropType, Double> cropAdjustments, boolean calibrateToObserved) {
 		super();
 		this.country = country;
@@ -35,13 +36,14 @@ public class GamsCountryInput {
 		this.importConstraints = importConstraints;
 		this.cropAdjustments = cropAdjustments;
 		this.calibrateToObserved = calibrateToObserved;
+		this.tradeBarriers = tradeBarriers;
 	}
 	
 	public GamsCountryInput(GamsCountryInput gamsInput, Map<CropType, Double> cropAdjs) {
-		this(gamsInput.country, gamsInput.projectedDemand, gamsInput.worldPrices, gamsInput.importConstraints, cropAdjs, false);
+		this(gamsInput.country, gamsInput.projectedDemand, gamsInput.tradeBarriers, gamsInput.worldPrices, gamsInput.importConstraints, cropAdjs, false);
 	}
 	
-	public static GamsCountryInput createInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, GlobalPrice> worldPrices,
+	public static GamsCountryInput createInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, Double> tradeBarriers, Map<CropType, GlobalPrice> worldPrices,
 			Map<CropType, Double> baseNetImport, Map<CropType, Double> maxOfProdOrSupply, Map<CropType, Double> cropAdjustments, boolean calibrateToObserved) {
 			
 		double allowedImportChange = calibrateToObserved ? 0.0 : ModelConfig.MAX_IMPORT_CHANGE;		
@@ -53,7 +55,7 @@ public class GamsCountryInput {
 			importConstraints.put(c, new ImportExportConstraint(entry.getValue() - change, entry.getValue() + change));
 		}
 
-		return new GamsCountryInput(country, projectedDemand, worldPrices, importConstraints, cropAdjustments, calibrateToObserved);
+		return new GamsCountryInput(country, projectedDemand, tradeBarriers, worldPrices, importConstraints, cropAdjustments, calibrateToObserved);
 	}
 	
 
@@ -116,16 +118,21 @@ public class GamsCountryInput {
 	public Map<CropType, Double> getWorldImportPrices() {
 		Map<CropType, Double> prices = new HashMap<CropType, Double>();
 		for (Map.Entry<CropType, GlobalPrice> entry : worldPrices.entrySet()) {
+			
 			prices.put(entry.getKey(), entry.getValue().getImportPrice());
 		}
 		return prices;
 	}
 	
-	public Map<CropType, Double> getWorldExportPrices() {
+	public Map<CropType, Double> getCountryExportPrices() {
 		Map<CropType, Double> prices = new HashMap<CropType, Double>();
 		for (Map.Entry<CropType, GlobalPrice> entry : worldPrices.entrySet()) {
-			prices.put(entry.getKey(), entry.getValue().getExportPrice());
+			
+			double tradeBarrier = tradeBarriers.get(entry.getKey());
+			prices.put(entry.getKey(), entry.getValue().getExportPrice()*(1-tradeBarrier));
+			
 		}
 		return prices;
 	}
 }
+
diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
index 9493c5ed..b7dff38d 100644
--- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
@@ -168,7 +168,7 @@ public class GamsLocationOptimiser {
 		}
 
 		if (DEBUG) { 
-			LogWriter.println("\nImport-export, min net imports, max net imports,  import price,   export price");
+			LogWriter.println("\nImport-export, min net imports, max net imports, global import price, global export price");
 			for (CropType crop : CropType.getImportedTypes()) {
 				ImportExportConstraint iec = countryInput.getImportConstraints().get(crop);
 				double minNetImport = 0, maxNetImport = 0;
@@ -187,8 +187,9 @@ public class GamsLocationOptimiser {
 		addItemMapParm(inDB.addParameter("minNetImport", 1), countryInput.getMinNetImport(), false);
 		addItemMapParm(inDB.addParameter("maxNetImport", 1), countryInput.getMaxNetImport(), false);
 		addItemMapParm(inDB.addParameter("worldImportPrices", 1), countryInput.getWorldImportPrices(), false);
-		addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getWorldExportPrices(), false);
-
+		addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getCountryExportPrices(), false);
+//the above is not really world export price because it is scaled by country tradeBarriers, might want to change
+//parameter name in GAMS i.e. countryExportPrices
 		LogWriter.print("\n");
 		addScalar(inDB, "meatEfficency", countryInput.getMeatEfficiency());		
 		addScalar(inDB, "fertiliserUnitCost", ModelConfig.FERTILISER_MAX_COST);
diff --git a/src/ac/ed/lurg/country/gams/GamsLocationTest.java b/src/ac/ed/lurg/country/gams/GamsLocationTest.java
index b5867c0d..33ad6ae7 100644
--- a/src/ac/ed/lurg/country/gams/GamsLocationTest.java
+++ b/src/ac/ed/lurg/country/gams/GamsLocationTest.java
@@ -22,7 +22,7 @@ public class GamsLocationTest {
 	}
 	
 	private void run() {
-		GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), null, null, null, null, true);
+		GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getTradeBarriers(), null, null, null, null, true);
 		GamsLocationInput gamsInput = new GamsLocationInput(new Timestep(0), getYields(), getPreviousArea(), getIrrigationCosts(), countryLevelInputs);
 		
 		GamsLocationOptimiser opti = new GamsLocationOptimiser(gamsInput);		
@@ -39,6 +39,19 @@ public class GamsLocationTest {
 		dummyMap.put(CommodityType.MEAT, 480.0);
 		return dummyMap;
 	}
+	
+	Map<CropType, Double> getTradeBarriers() {
+		Map<CropType, Double> dummyTradeMap = new HashMap<CropType, Double>();
+		dummyTradeMap.put(CropType.WHEAT, 300.0);
+		dummyTradeMap.put(CropType.MAIZE, 50.0);
+		dummyTradeMap.put(CropType.RICE, 50.0);
+		dummyTradeMap.put(CropType.OILCROPS, 50.0);
+		dummyTradeMap.put(CropType.PULSES, 60.0);
+		dummyTradeMap.put(CropType.STARCHY_ROOTS, 150.0);
+		dummyTradeMap.put(CropType.MEAT, 480.0);
+
+		return dummyTradeMap;
+	}
 
 	
 	Map<Integer, YieldResponsesItem> getYields() {
diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java
index 18c12dc2..0cc19a4b 100644
--- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java
@@ -283,7 +283,7 @@ public class GamsRasterOptimiser {
 			CropType crop = CropType.WHEAT;
 
 			if (yresp != null && (yresp.getYieldMax(crop) < yresp.getYieldFertOnly(crop) || yresp.getYieldMax(crop) < yresp.getYieldIrrigOnly(crop))) {
-				logErrorWithCoord(String.format("Inconsistency: F only: %.4f, I only %.4f, max %.4f at ", yresp.getYieldFertOnly(crop), yresp.getYieldIrrigOnly(crop), yresp.getYieldMax(crop)), key, yieldRaster);
+				logErrorWithCoord("Inconsistency F only:" + yresp.getYieldFertOnly(crop) + ", I only" + yresp.getYieldIrrigOnly(crop) + ", max " + yresp.getYieldMax(crop) + " at ", key, yieldRaster);
 			}
 		}
 
diff --git a/src/ac/ed/lurg/country/gams/GamsRasterTest.java b/src/ac/ed/lurg/country/gams/GamsRasterTest.java
index d67e8122..cc4548d7 100644
--- a/src/ac/ed/lurg/country/gams/GamsRasterTest.java
+++ b/src/ac/ed/lurg/country/gams/GamsRasterTest.java
@@ -18,7 +18,7 @@ public class GamsRasterTest extends GamsLocationTest {
 	}
 	
 	private void run() {
-		GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), null, null, null, null, true);
+		GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getTradeBarriers(), null, null, null, null, true);
 		GamsRasterInput input = new GamsRasterInput(new Timestep(0), getYieldRaster(), getPreviousAreaRaster(), getIrrigationCost(), countryLevelInputs);
 		
 		GamsRasterOptimiser opti = new GamsRasterOptimiser(input);		
-- 
GitLab