From 1932dcce8abb59342097f83f3a833c7a141db738 Mon Sep 17 00:00:00 2001
From: Bart Arendarczyk <s1924442@ed.ac.uk>
Date: Thu, 6 Apr 2023 16:51:57 +0100
Subject: [PATCH] Export adjustment bug fixes.

---
 src/ac/ed/lurg/country/CountryAgent.java | 69 ++++++++++++------------
 src/ac/ed/lurg/country/GlobalPrice.java  | 10 ++--
 2 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 728939e7..75c4dfd5 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -175,22 +175,25 @@ public class CountryAgent extends AbstractCountryAgent {
 				changeUp = changeDown = 0;
 			} else {
 				changeDown = changeUp = maxOfProdOrSupply * ModelConfig.MAX_IMPORT_CHANGE;
-			}
 
-			if (crop.isImportedCrop()) {
-				// Guide export level towards import level
-				if (baseTrade < 0) {
-					double ratio = currentWorldPrices.get(crop).getExportAdjustment(crop.getStockToUseRatio());
-					baseTrade *= ratio;
-					cropUsage.updateNetImports(cropUsage.getNetImportsExpected() * ratio);
-				}
-
-				// If stock about to go negative temporarily increase import price
-				if (currentWorldPrices.get(crop).isStockCritical()) {
-					currentCountryPrices.get(crop).adjustImportPrice(2.0);
+				if (crop.isImportedCrop()) {
+					// Guide export level towards import level
+					if (baseTrade < 0) {
+						if (crop.equals(CropType.RUMINANTS)) {
+							int foo = 1;
+						}
+						double ratio = currentWorldPrices.get(crop).getExportAdjustment(crop.getStockToUseRatio());
+						baseTrade *= ratio;
+						cropUsage.updateNetImports(cropUsage.getNetImportsExpected() * ratio);
+					}
+
+					// If stock about to go negative temporarily increase import price
+					if (currentWorldPrices.get(crop).isStockCritical()) {
+						currentCountryPrices.get(crop).adjustImportPrice(2.0);
+					}
 				}
 			}
-			
+
 			if (CropType.ENERGY_CROPS.equals(crop) && baseTrade == 0) { // could apply this logic for all crops?
 				changeDown = changeUp = ModelConfig.MAX_IMPORT_CHANGE * currentGen2EcDemand;
 			}
@@ -227,18 +230,18 @@ public class CountryAgent extends AbstractCountryAgent {
 			} else {
 				double maxOfProdOrSupply = Math.max(carbonUsageData.getCarbonCredits() + Math.max(baseTrade, 0), 1);
 				changeUp = changeDown = maxOfProdOrSupply * ModelConfig.MAX_IMPORT_CHANGE;
-			}
 
-			// Guide export level towards import level
-			if (baseTrade < 0) {
-				double ratio = globalCarbonPrice.getExportAdjustment(ModelConfig.DEFAULT_STOCK_USE_RATIO);
-				baseTrade *= ratio;
-				carbonUsageData.updateNetImports(carbonUsageData.getNetCarbonImport() * ratio);
-			}
+				// Guide export level towards import level
+				if (baseTrade < 0) {
+					double ratio = globalCarbonPrice.getExportAdjustment(ModelConfig.DEFAULT_STOCK_USE_RATIO);
+					baseTrade *= ratio;
+					carbonUsageData.updateNetImports(carbonUsageData.getNetCarbonImport() * ratio);
+				}
 
-			// If stock about to go negative temporarily increase import price
-			if (globalCarbonPrice.isStockCritical()) {
-				currentCarbonPrice.adjustImportPrice(2.0);
+				// If stock about to go negative temporarily increase import price
+				if (globalCarbonPrice.isStockCritical()) {
+					currentCarbonPrice.adjustImportPrice(2.0);
+				}
 			}
 
 			carbonTradeConstraint = new TradeConstraint(baseTrade - changeDown, baseTrade + changeUp);
@@ -269,18 +272,18 @@ public class CountryAgent extends AbstractCountryAgent {
 				changeUp = changeDown = 0.0;
 			} else {
 				changeUp = changeDown = maxOfProdOrSupply * ModelConfig.MAX_IMPORT_CHANGE;
-			}
 
-			// Guide export level towards import level
-			if (baseTrade < 0) {
-				double ratio = globalWoodPrices.get(woodType).getExportAdjustment(ModelConfig.DEFAULT_STOCK_USE_RATIO);
-				baseTrade *= ratio;
-				woodUsageData.get(woodType).updateNetImports(woodUsageData.get(woodType).getNetImport() * ratio);
-			}
+				// Guide export level towards import level
+				if (baseTrade < 0) {
+					double ratio = globalWoodPrices.get(woodType).getExportAdjustment(ModelConfig.DEFAULT_STOCK_USE_RATIO);
+					baseTrade *= ratio;
+					woodUsageData.get(woodType).updateNetImports(woodUsageData.get(woodType).getNetImport() * ratio);
+				}
 
-			// If stock about to go negative temporarily increase import price
-			if (globalWoodPrices.get(woodType).isStockCritical()) {
-				currentWoodPrices.get(woodType).adjustImportPrice(2.0);
+				// If stock about to go negative temporarily increase import price
+				if (globalWoodPrices.get(woodType).isStockCritical()) {
+					currentWoodPrices.get(woodType).adjustImportPrice(2.0);
+				}
 			}
 
 			woodTradeConstraints.put(woodType, new TradeConstraint(baseTrade - changeDown, baseTrade + changeUp));
diff --git a/src/ac/ed/lurg/country/GlobalPrice.java b/src/ac/ed/lurg/country/GlobalPrice.java
index 84256e7c..6c47365e 100644
--- a/src/ac/ed/lurg/country/GlobalPrice.java
+++ b/src/ac/ed/lurg/country/GlobalPrice.java
@@ -176,9 +176,13 @@ public class GlobalPrice implements Serializable {
 	}
 
 	public double getExportAdjustment(double targetStockUseRatio) {
-		double surplus = stockLevel - (stockLevel / stockUseRatio) * targetStockUseRatio;
-		double ratio = importAmount / (ModelConfig.EXPORT_ADJUSTMENT_RATE * exportAmountBeforeLoss
-				+ ModelConfig.STOCK_ADJUSTMENT_RATE * surplus);
+		double targetStockLevel = (stockLevel / stockUseRatio) * targetStockUseRatio;
+		double surplus = stockLevel - targetStockLevel;
+		double targetExport = exportAmountBeforeLoss
+				- ModelConfig.EXPORT_ADJUSTMENT_RATE * (exportAmountBeforeLoss - importAmount)
+				- ModelConfig.STOCK_ADJUSTMENT_RATE * surplus;
+		targetExport = Math.max(targetExport, 0); // can't be negative
+		double ratio = exportAmountBeforeLoss > 0 ? targetExport / exportAmountBeforeLoss : 1;
 		return ratio;
 	}
 
-- 
GitLab