From 37773d29d1f968571364ac1c17d09c25e076558b Mon Sep 17 00:00:00 2001
From: Bart Arendarczyk <s1924442@ed.ac.uk>
Date: Fri, 19 May 2023 12:13:06 +0100
Subject: [PATCH] Fixed population and GDP adjustment bug and issues when
 forestry off.

---
 GAMS/IntExtOpt.gms                            |  6 ++---
 .../country/gams/GamsDemandOptimiser.java     |  4 ++--
 .../country/gams/GamsWoodDemandOptimiser.java |  4 ++--
 .../ed/lurg/demand/AbstractDemandManager.java | 23 +++++++++++++++++--
 src/ac/ed/lurg/demand/SspManager.java         | 16 +++++++------
 5 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms
index 4fefa8d8..524c692d 100644
--- a/GAMS/IntExtOpt.gms
+++ b/GAMS/IntExtOpt.gms
@@ -117,7 +117,7 @@ $gdxin
               fruitveg          4.0
               sugar             3.0
               energyCrops       0.3
-              pasture           0.52     / ;
+              pasture           0.38     / ;
               
  PARAMETER baseCost(crop)  base cost per hectare
         /     wheat             0.10
@@ -146,8 +146,8 @@ $gdxin
  previousNetImport(import_types) = previousImportAmount(import_types) - previousExportAmount(import_types);
  
  PARAMETER loggingCost(wood_type)
-    /   roundwood   0.1
-        fuelwood    0.05
+    /   roundwood   0.05
+        fuelwood    0.03
     /;
  
 
diff --git a/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java b/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
index dcf2b005..dd6e0f9c 100755
--- a/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
@@ -213,14 +213,14 @@ public class GamsDemandOptimiser {
 				subsGams = subsMap.get(commodity);
 			} else {
 				subsGams = 0;
-				LogWriter.printlnWarning(commodity + " not found in Gams output so adding with zero demand for subsistence");
+				LogWriter.println(commodity + " not found in Gams output so adding with zero demand for subsistence", 3);
 			}
 
 			if (discMap.containsKey(commodity)) {
 				discGams = discMap.get(commodity);
 			} else {
 				discGams = 0;
-				LogWriter.printlnWarning(commodity + " not found in Gams output so adding with zero demand for discretionary");
+				LogWriter.println(commodity + " not found in Gams output so adding with zero demand for discretionary", 3);
 			}
 
 			GamsCommodityDemand demand = new GamsCommodityDemand(commodity, subsGams, discGams, inputData.getKcalPerT(commodity));
diff --git a/src/ac/ed/lurg/country/gams/GamsWoodDemandOptimiser.java b/src/ac/ed/lurg/country/gams/GamsWoodDemandOptimiser.java
index 9bfc4704..4d7098ef 100644
--- a/src/ac/ed/lurg/country/gams/GamsWoodDemandOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsWoodDemandOptimiser.java
@@ -170,14 +170,14 @@ public class GamsWoodDemandOptimiser {
                 subsGams = subsMap.get(commodity);
             } else {
                 subsGams = 0;
-                LogWriter.printlnWarning(commodity + " not found in Gams output so adding with zero demand for subsistence");
+                LogWriter.println(commodity + " not found in Gams output so adding with zero demand for subsistence", 3);
             }
 
             if (discMap.containsKey(commodity)) {
                 discGams = discMap.get(commodity);
             } else {
                 discGams = 0;
-                LogWriter.printlnWarning(commodity + " not found in Gams output so adding with zero demand for discretionary");
+                LogWriter.println(commodity + " not found in Gams output so adding with zero demand for discretionary", 3);
             }
 
             GamsWoodCommodityDemand demand = new GamsWoodCommodityDemand(commodity, subsGams, discGams);
diff --git a/src/ac/ed/lurg/demand/AbstractDemandManager.java b/src/ac/ed/lurg/demand/AbstractDemandManager.java
index 77ab2bf3..5934a428 100644
--- a/src/ac/ed/lurg/demand/AbstractDemandManager.java
+++ b/src/ac/ed/lurg/demand/AbstractDemandManager.java
@@ -113,6 +113,15 @@ public abstract class AbstractDemandManager implements Serializable {
 	protected abstract Map<WoodType, Double> getWoodDemand(SingleCountry country, int year, Map<WoodType, Double> producerPrices);
 
 	public Map<WoodType, Double> getWoodDemandComposite(CompositeCountry cc, int year, Map<WoodType, CountryPrice> countryWoodPrices) {
+		Map<WoodType, Double> compositeDemandMap = new HashMap<WoodType, Double>();
+
+		if (!ModelConfig.IS_FORESTRY_ON) { // Forestry Off so return dummy values
+			for (WoodType woodType : WoodType.values()) {
+				compositeDemandMap.put(woodType, 0.0);
+			}
+			return compositeDemandMap;
+		}
+
 		if (!ModelConfig.CHANGE_DEMAND_YEAR)
 			year = ModelConfig.BASE_YEAR;
 
@@ -121,7 +130,6 @@ public abstract class AbstractDemandManager implements Serializable {
 			producerPrices.put(entry.getKey(), entry.getValue().getConsumerPrice());
 		}
 
-		Map<WoodType, Double> compositeDemandMap = new HashMap<WoodType, Double>();
 		Map<WoodType, Double> singleDemandMap;
 
 		for (SingleCountry c : CountryManager.getInstance().getAllForCompositeCountry(cc)) {
@@ -160,7 +168,18 @@ public abstract class AbstractDemandManager implements Serializable {
 	}
 
 	public Map<CompositeCountry, Map<WoodType, WoodUsageData>> getInitialWoodUsage() {
-		return woodDemandManager.getInitialWoodUsage();
+		if (ModelConfig.IS_FORESTRY_ON) {
+			return woodDemandManager.getInitialWoodUsage();
+		} else { // Forestry Off so use blank dummy data
+			Map<CompositeCountry, Map<WoodType, WoodUsageData>> countryMap = new HashMap<>();
+			for (CompositeCountry cc : CountryManager.getInstance().getAllCompositeCountries()) {
+				for (WoodType wType : WoodType.values()) {
+					Map<WoodType, WoodUsageData> usageMap = countryMap.computeIfAbsent(cc, k -> new HashMap<>());
+					usageMap.put(wType, new WoodUsageData(0, 0, 0));
+				}
+			}
+			return countryMap;
+		}
 	}
 
 }
diff --git a/src/ac/ed/lurg/demand/SspManager.java b/src/ac/ed/lurg/demand/SspManager.java
index 21b8d7ce..8f56f088 100644
--- a/src/ac/ed/lurg/demand/SspManager.java
+++ b/src/ac/ed/lurg/demand/SspManager.java
@@ -68,9 +68,15 @@ public class SspManager {
 	
 	public SspData get(String scenario, int year, SingleCountry country) {
 		SspData targetYearData = lookup(scenario, year, country);
-		if (targetYearData != null)
-			return targetYearData;
-				
+
+		// Adjust population and GDP for Monte Carlo
+		double popFactor = ModelConfig.getAdjParam("SSP_POPULATION_FACTOR");
+		double gdpFactor = ModelConfig.getAdjParam("SSP_GDP_PC_FACTOR");
+
+		if (targetYearData != null) {
+			return new SspData(targetYearData.getPopulation() * popFactor, targetYearData.getGdp() * gdpFactor);
+		}
+
 		SspData upYearData = null, downYearData = null;
 		int downYear = year-1, upYear = year+1;
 		
@@ -91,10 +97,6 @@ public class SspManager {
 			
 			double pop = interpolate(upYearData.getPopulation(), downYearData.getPopulation(), factor);
 			double gdp = interpolate(upYearData.getGdp(), downYearData.getGdp(), factor);
-
-			// Adjust population and GDP for Monte Carlo
-			double popFactor = ModelConfig.getAdjParam("SSP_POPULATION_FACTOR");
-			double gdpFactor = ModelConfig.getAdjParam("SSP_GDP_PC_FACTOR");
 			
 			return new SspData(pop * popFactor, gdp * gdpFactor);
 		}
-- 
GitLab