From 6b64bf6284d935791a1d8d69a75cfa2c8ee915f5 Mon Sep 17 00:00:00 2001
From: Roslyn Henry <roslyn.henry@ed.ac.uk>
Date: Wed, 26 May 2021 15:48:04 +0100
Subject: [PATCH] Check to make sure rebased demand is affordable.

---
 .../ed/lurg/demand/ElasticDemandManager.java  | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/ac/ed/lurg/demand/ElasticDemandManager.java b/src/ac/ed/lurg/demand/ElasticDemandManager.java
index eca6b8d8..f5fbece8 100755
--- a/src/ac/ed/lurg/demand/ElasticDemandManager.java
+++ b/src/ac/ed/lurg/demand/ElasticDemandManager.java
@@ -96,6 +96,28 @@ public class ElasticDemandManager extends AbstractSSPDemandManager {
 			double d = cpcAdj * population;
 			foodDemands.put(commodity, d);
 		}
+		
+		double foodSpend = 0;
+		for (CommodityType commodity : CommodityType.getAllFoodItems()) {
+			double consumerPrice = consumerPrices.get(commodity);
+			double rebasedDemand = foodDemands.get(commodity);
+			double caloriesPerT = kcalPerT.get(commodity);
+			
+			foodSpend += consumerPrice*rebasedDemand/population/365/2000*caloriesPerT;
+		}
+		
+		LogWriter.println("foodSpend " + foodSpend + " budget " + gdpPc*ModelConfig.MAX_INCOME_PROPORTION_FOOD_SPEND);
+		
+		if(foodSpend > gdpPc*ModelConfig.MAX_INCOME_PROPORTION_FOOD_SPEND) {
+			double overspend= 1/(foodSpend/(gdpPc*ModelConfig.MAX_INCOME_PROPORTION_FOOD_SPEND));
+			LogWriter.println("overspend " + overspend);
+			for (Map.Entry<CommodityType, Double> entry : foodPc.entrySet()) {
+			CommodityType commodity = entry.getKey();
+			double rebasedDemand = foodDemands.get(commodity);
+			double affordableDemand = rebasedDemand*overspend;
+			foodDemands.put(commodity, affordableDemand);
+			}
+		}
 
 		if (outputGamsDemand) writeGamsDemandOutputs(inputData, gamsOutput, foodDemands, population);
 
-- 
GitLab