From d4e4398ebaee3be4fc1ff7be3ae029e19ac8181f Mon Sep 17 00:00:00 2001
From: R0slyn <roslyn.henry.08@aberdeen.ac.uk>
Date: Tue, 25 Jun 2019 12:56:27 +0100
Subject: [PATCH] Weighed price changes.

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

diff --git a/src/ac/ed/lurg/country/AbstractCountryAgent.java b/src/ac/ed/lurg/country/AbstractCountryAgent.java
index 2a2a2cc3..ac9bd99f 100644
--- a/src/ac/ed/lurg/country/AbstractCountryAgent.java
+++ b/src/ac/ed/lurg/country/AbstractCountryAgent.java
@@ -39,19 +39,7 @@ public abstract class AbstractCountryAgent {
 		currentMinDemandFract = getMinDemandFraction();
 	}
 
-	private void calculateCountryPrices(Map<CropType, GlobalPrice> worldPrices){
-		Map<CropType, CountryPrice> countryPrices = new HashMap <CropType, CountryPrice>();
-
-		for (CropType c : CropType.getImportedTypes()) {
-			GlobalPrice worldPrice = worldPrices.get(c);
-			Double tb = tradeBarriers.get(c);
-			LogWriter.println(worldPrice + " " + c);
-			CountryPrice prices = new CountryPrice(worldPrice.getCountryImportPrice(tb==null ? 0 : tb, currentTimestep), worldPrice.getExportPrice());
-			countryPrices.put(c, prices);
-		}
-
-		currentCountryPrices = countryPrices;
-	}
+	protected abstract void calculateCountryPrices(Map<CropType, GlobalPrice> worldPrices);
 	
 	private void calculateDemand() {
 		Map<CommodityType, Double> producerPrices = getProducerCommodityPrices();
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 01797409..192c5278 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -59,6 +59,34 @@ public class CountryAgent extends AbstractCountryAgent {
 	public RasterSet<IntegerRasterItem> getYieldClusters() {
 		return yieldClusters;
 	}
+	
+	@Override
+	protected void calculateCountryPrices(Map<CropType, GlobalPrice> worldPrices) {
+		Map<CropType, CountryPrice> countryPrices = new HashMap <CropType, CountryPrice>();
+		
+		Map<CropType, CropUsageData> cropUsageMap = previousGamsRasterOutput.getCropUsageData();
+		
+		for (CropType c : CropType.getImportedTypes()) {
+			GlobalPrice worldPrice = worldPrices.get(c);
+			Double tb = tradeBarriers.get(c);
+			CropUsageData cropUsageData = cropUsageMap.get(c);
+			LogWriter.println(worldPrice + " " + c);
+			double importsPostShock = cropUsageData.getShockedNetImports();
+			double productionPostShock = cropUsageData.getProductionExpected()-cropUsageData.getProductionShock();
+			double weighting = importsPostShock/(productionPostShock+importsPostShock);
+				
+			double importPrice = worldPrice.getCountryImportPrice(tb==null ? 0 : tb, currentTimestep) * weighting;
+			double exportPrice = worldPrice.getExportPrice() * (1-weighting);
+	
+			double newImportPrice = importPrice + exportPrice;
+	
+			CountryPrice prices = new CountryPrice(newImportPrice, worldPrice.getExportPrice());
+			countryPrices.put(c, prices);
+		}
+
+		currentCountryPrices = countryPrices;
+		
+	}
 
 	private RasterSet<IntegerRasterItem> calcYieldClusters(RasterSet<IrrigationItem> irrigData, YieldRaster countryYieldSurfaces) {
 
diff --git a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
index cbdb684c..e81408fa 100644
--- a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
+++ b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
@@ -6,10 +6,14 @@ import java.util.Map.Entry;
 
 import ac.ed.lurg.country.AbstractCountryAgent;
 import ac.ed.lurg.country.CompositeCountry;
+import ac.ed.lurg.country.CountryPrice;
 import ac.ed.lurg.country.GlobalPrice;
+import ac.ed.lurg.country.SingleCountry;
 import ac.ed.lurg.demand.AbstractDemandManager;
 import ac.ed.lurg.landuse.CropUsageData;
+import ac.ed.lurg.types.CommodityType;
 import ac.ed.lurg.types.CropType;
+import ac.ed.lurg.utils.LogWriter;
 
 /** 
  * Country agent that is interface over data transferred to and from CRAFTY.
@@ -38,4 +42,20 @@ public class CraftyCountryAgent extends AbstractCountryAgent {
 		
 		updateNetImportsFromProdAndDemand(currentProjectedDemand, currentMinDemandFract, cropUsageData);
 	}
+	
+	@Override
+	protected void calculateCountryPrices(Map<CropType, GlobalPrice> worldPrices) {
+		Map<CropType, CountryPrice> countryPrices = new HashMap <CropType, CountryPrice>();
+
+		for (CropType c : CropType.getImportedTypes()) {
+			GlobalPrice worldPrice = worldPrices.get(c);
+			Double tb = tradeBarriers.get(c);
+			LogWriter.println(worldPrice + " " + c);
+			CountryPrice prices = new CountryPrice(worldPrice.getCountryImportPrice(tb==null ? 0 : tb, currentTimestep), worldPrice.getExportPrice());
+			countryPrices.put(c, prices);
+		}
+
+		currentCountryPrices = countryPrices;
+		
+	}
 }
\ No newline at end of file
-- 
GitLab