From f551212407073a49b10f03c898b1403045acb9bf Mon Sep 17 00:00:00 2001
From: R0slyn <roslyn.henry.08@aberdeen.ac.uk>
Date: Wed, 26 Jun 2019 17:19:11 +0100
Subject: [PATCH] Corrected changes to weighted producer prices.

---
 .../ed/lurg/country/AbstractCountryAgent.java | 12 ++---------
 src/ac/ed/lurg/country/CountryAgent.java      | 18 +++++++++++++++++
 .../country/crafty/CraftyCountryAgent.java    | 20 +++++++++++++++----
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/src/ac/ed/lurg/country/AbstractCountryAgent.java b/src/ac/ed/lurg/country/AbstractCountryAgent.java
index 2a2a2cc3..25283e15 100644
--- a/src/ac/ed/lurg/country/AbstractCountryAgent.java
+++ b/src/ac/ed/lurg/country/AbstractCountryAgent.java
@@ -18,7 +18,7 @@ public abstract class AbstractCountryAgent {
 	protected Map<CropType, Double> tradeBarriers;
 	protected Map<CommodityType, Double> currentProjectedDemand;
 	protected Map<CropType, CountryPrice> currentCountryPrices;
-	private Map<CommodityType, Map<CropType, Double>> baseDemandFact;
+	protected Map<CommodityType, Map<CropType, Double>> baseDemandFact;
 	protected Timestep currentTimestep;
 	protected Map<CommodityType, Map<CropType, Double>> currentMinDemandFract;
 	
@@ -78,15 +78,7 @@ public abstract class AbstractCountryAgent {
 		return prices;
 	}
 	
-	private double getCommPriceFromCropPrice(CommodityType commodity) {
-		double commPricePlum = 0;
-		Map<CropType, Double> demandFract = baseDemandFact.get(commodity);
-
-		for (CropType crop : commodity.getCropTypes()) {
-			commPricePlum += currentCountryPrices.get(crop).getImportPrice() * demandFract.get(crop);  // weight price by base demand of each cereal crop
-		}
-		return commPricePlum;
-	}
+	protected abstract double getCommPriceFromCropPrice(CommodityType commodity);
 
 	public Map<CommodityType, Double> getCurrentProjectedDemand() {
 		return currentProjectedDemand;
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 01797409..06736d5c 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -207,6 +207,24 @@ public class CountryAgent extends AbstractCountryAgent {
 
 		return input;
 	}
+	
+	@Override
+	protected double getCommPriceFromCropPrice(CommodityType commodity) {
+		double commPricePlum = 0;
+		Map<CropType, Double> demandFract = baseDemandFact.get(commodity);
+		Map<CropType, CropUsageData> cropUsageMap = previousGamsRasterOutput.getCropUsageData();
+		
+		for (CropType crop : commodity.getCropTypes()) {
+			CropUsageData cropUsageData = cropUsageMap.get(crop);
+	
+			double weighting = Math.max(0, cropUsageData.getShockedNetImports()/(cropUsageData.getProductionExpected()+cropUsageData.getNetImportsExpected()));
+			double newImportPrice = currentCountryPrices.get(crop).getImportPrice()*weighting + currentCountryPrices.get(crop).getExportPrice()*(1-weighting);
+			
+			commPricePlum += newImportPrice * demandFract.get(crop);  // weight price by base demand of each cereal crop
+		}
+		
+		return commPricePlum;
+	}
 
 	public RasterSet<LandUseItem> getLandUses() {
 		return previousGamsRasterOutput.getLandUses();
diff --git a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
index cbdb684c..7ab4740c 100644
--- a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
+++ b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
@@ -9,14 +9,15 @@ import ac.ed.lurg.country.CompositeCountry;
 import ac.ed.lurg.country.GlobalPrice;
 import ac.ed.lurg.demand.AbstractDemandManager;
 import ac.ed.lurg.landuse.CropUsageData;
+import ac.ed.lurg.types.CommodityType;
 import ac.ed.lurg.types.CropType;
 
-/** 
+/**
  * Country agent that is interface over data transferred to and from CRAFTY.
- * Need to configure country mapping to that there is 
+ * Need to configure country mapping to that there is
  * */
 public class CraftyCountryAgent extends AbstractCountryAgent {
-	
+
 	private Map<CropType, CropUsageData> cropUsageData;
 
 	public CraftyCountryAgent(AbstractDemandManager demandManager,CompositeCountry country, Map<CropType, Double> tradeBarriers) {
@@ -35,7 +36,18 @@ public class CraftyCountryAgent extends AbstractCountryAgent {
 			CropUsageData cropusage = new CropUsageData(entry.getValue());
 			cropUsageData.put(entry.getKey(), cropusage);
 		}
-		
+
 		updateNetImportsFromProdAndDemand(currentProjectedDemand, currentMinDemandFract, cropUsageData);
 	}
+
+	@Override
+ 	protected double getCommPriceFromCropPrice(CommodityType commodity) {
+ 		double commPricePlum = 0;
+ 		Map<CropType, Double> demandFract = baseDemandFact.get(commodity);
+ 
+ 		for (CropType crop : commodity.getCropTypes()) {
+ 			commPricePlum += currentCountryPrices.get(crop).getImportPrice() * demandFract.get(crop);  // weight price by base demand of each cereal crop
+ 		}
+ 		return commPricePlum;
+ 	}
 }
\ No newline at end of file
-- 
GitLab