From 05bfae9a06f142611feffc89e8864a664a1b1aff Mon Sep 17 00:00:00 2001
From: alexanpe <devnull@localhost>
Date: Wed, 27 Feb 2019 15:00:31 +0000
Subject: [PATCH] Some improvements in demand related to nonfood

---
 .../ed/lurg/country/AbstractCountryAgent.java |  1 -
 .../country/gams/GamsCommodityDemand.java     |  5 ++
 .../country/gams/GamsDemandOptimiser.java     | 63 ++++++++-----------
 src/ac/ed/lurg/demand/BaseConsumpManager.java |  3 +-
 4 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/src/ac/ed/lurg/country/AbstractCountryAgent.java b/src/ac/ed/lurg/country/AbstractCountryAgent.java
index e6ca1af5..ede041ef 100644
--- a/src/ac/ed/lurg/country/AbstractCountryAgent.java
+++ b/src/ac/ed/lurg/country/AbstractCountryAgent.java
@@ -72,7 +72,6 @@ public abstract class AbstractCountryAgent {
 			LogWriter.println(commodity.getGamsName() + ": " + commPrice);
 		}
 		
-		prices.put(CommodityType.NONFOOD, Double.NaN);
 		return prices;
 	}
 
diff --git a/src/ac/ed/lurg/country/gams/GamsCommodityDemand.java b/src/ac/ed/lurg/country/gams/GamsCommodityDemand.java
index 162d5879..2b3d8195 100644
--- a/src/ac/ed/lurg/country/gams/GamsCommodityDemand.java
+++ b/src/ac/ed/lurg/country/gams/GamsCommodityDemand.java
@@ -33,4 +33,9 @@ public class GamsCommodityDemand {
 	public CommodityType getCommodity() {
 		return commodity;
 	}
+
+	@Override
+	public String toString() {
+		return String.format("%14s:  subs=%.3f,\tdisc=%.3f,\tdemand(kcal)=%.1f,\tdemand(plum)= %.3f", commodity, subsistence, discretionary, getKcalTotal(), getPlumDemand()); 
+	}
 }
\ No newline at end of file
diff --git a/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java b/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
index a3b8a5e2..b9c356c1 100644
--- a/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsDemandOptimiser.java
@@ -70,33 +70,31 @@ public class GamsDemandOptimiser {
 
 		for (Map.Entry<CommodityType, Double> entry : inputData.getPrices().entrySet()) {
 			CommodityType comm = entry.getKey();
-			Vector<String> v = new Vector<String>();
-			v.add(comm.getGamsName());
-			double priceComm, prevSubs = 0.1, prevDisc = 0.1;
-			
-			if (comm == CommodityType.NONFOOD) {
-				prevSubs = 5;
-				prevDisc = 1;
-				priceComm = 45.376+53.036*gdpPc/inputData.getUsaGdpPc();
-			}
-			else
-				priceComm = entry.getValue() * 1000 / comm.getkcalPerT()*2000*365;  //price per calorie of commodity x per year assuming 2000 kcal a day for whole year
-			
-			if (previousGamsDemands != null) {
-				GamsCommodityDemand demand = previousGamsDemands.getGamsDemands(comm);
-				if (demand == null)
-					LogWriter.printlnError("Null GamsCommodityDemand for " + comm);
-				else {
-					prevSubs = demand.getSubsistence();
-					prevDisc = demand.getDiscretionary();
-				}
-			}
-			
-			LogWriter.println(String.format("%14s:  price=%.4f, previousSubs=%.4f, previousDisc=%.4f", comm.getGamsName(), priceComm, prevSubs, prevDisc));
-			setGamsParamValue(priceP.addRecord(v), priceComm, 4);
-			setGamsParamValue(prevSubsP.addRecord(v), prevSubs, 4);
-			setGamsParamValue(prevDiscP.addRecord(v), prevDisc, 4);
+			double priceComm = entry.getValue() * 1000 / comm.getkcalPerT()*2000*365;  //price per calorie of commodity x per year assuming 2000 kcal a day for whole year
+			doCommodity(inDB, priceP, prevSubsP, prevDiscP, gdpPc, previousGamsDemands, comm, priceComm, 0.1, 0.1);
 		}
+		
+		double nonfoodPrice = 45.376+53.036*gdpPc/inputData.getUsaGdpPc();
+		doCommodity(inDB, priceP, prevSubsP, prevDiscP, gdpPc, previousGamsDemands, CommodityType.NONFOOD, nonfoodPrice, 5, 1);
+	}
+
+	private void doCommodity(GAMSDatabase inDB, GAMSParameter priceP, GAMSParameter prevSubsP, GAMSParameter prevDiscP, double gdpPc, 
+			GamsDemandOutput previousGamsDemands, CommodityType comm, double price, double defaultSubs, double defaultDisc) {
+		
+		Vector<String> v = new Vector<String>();
+		v.add(comm.getGamsName());
+		double prevSubs = defaultSubs, prevDisc = defaultDisc;
+		
+		if (previousGamsDemands != null) {
+			GamsCommodityDemand demand = previousGamsDemands.getGamsDemands(comm);
+			prevSubs = demand.getSubsistence();
+			prevDisc = demand.getDiscretionary();
+		}
+		
+		LogWriter.println(String.format("%14s:  price=%.4f, previousSubs=%.4f, previousDisc=%.4f", comm.getGamsName(), price, prevSubs, prevDisc));
+		setGamsParamValue(priceP.addRecord(v), price, 4);
+		setGamsParamValue(prevSubsP.addRecord(v), prevSubs, 4);
+		setGamsParamValue(prevDiscP.addRecord(v), prevDisc, 4);
 	}
 
 	private double setGamsParamValue(GAMSParameterRecord param, double val, int places) {
@@ -129,16 +127,9 @@ public class GamsDemandOptimiser {
 			else {
 				double subsGams = rec.getLevel();
 				double discGams = varDisc.findRecord(commodityName).getLevel();
-
-				if (commodity == CommodityType.NONFOOD) { // we don't want to consider non-food, so not added to demandMap
-					LogWriter.println(String.format("%14s:  subs=%.3f, disc=%.3f", commodityName, subsGams, discGams)); 
-				}
-				else {	//gams is calculating demand in number of 2000 kcal servings per day so * by 2000 and * 365 to get kcal per year
-					GamsCommodityDemand demand = new GamsCommodityDemand(commodity, subsGams, discGams);
-					
-					LogWriter.println(String.format("%14s:  subs=%.3f, disc=%.3f, demand(kcal)=%.1f, demand(plum)= %.3f", commodityName, subsGams, discGams, demand.getKcalTotal(), demand.getPlumDemand())); 
-					demandMap.add(demand);
-				}
+				GamsCommodityDemand demand = new GamsCommodityDemand(commodity, subsGams, discGams);
+				LogWriter.println(demand.toString());
+				demandMap.add(demand);
 			}
 		}
 		
diff --git a/src/ac/ed/lurg/demand/BaseConsumpManager.java b/src/ac/ed/lurg/demand/BaseConsumpManager.java
index 35b3f678..b768c741 100644
--- a/src/ac/ed/lurg/demand/BaseConsumpManager.java
+++ b/src/ac/ed/lurg/demand/BaseConsumpManager.java
@@ -79,7 +79,8 @@ public class BaseConsumpManager {
 		Map<CommodityType, Double> commodityMap = baseConsumpMap.get(country);
 
 		if (commodityMap == null || !commodityMap.containsKey(commodity)) {
-			LogWriter.printlnError("BaseConsumpManager: can't get value for " + commodity + ", " + country);
+			if (commodity != CommodityType.NONFOOD)  // don't report this, as expected for nonfood
+				LogWriter.printlnError("BaseConsumpManager: can't get value for " + commodity + ", " + country);
 			return 0.0;
 		}
 		
-- 
GitLab