From 7ee903f1b93926f0874cfe94a8fd80c2cbeedd5b Mon Sep 17 00:00:00 2001
From: Peter Alexander <>
Date: Thu, 2 Jul 2020 14:42:48 +0100
Subject: [PATCH] Allow prices to be determined using prod cost instead of
 export price

---
 .project                                 |  9 +++++++--
 src/ac/ed/lurg/ModelConfig.java          |  1 +
 src/ac/ed/lurg/ModelMain.java            |  7 ++++++-
 src/ac/ed/lurg/country/CountryAgent.java | 16 ++++++++++++++--
 src/ac/ed/lurg/utils/WatchForFile.java   |  1 +
 5 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/.project b/.project
index 126b7b24..68492159 100644
--- a/.project
+++ b/.project
@@ -15,10 +15,15 @@
 			<arguments>
 			</arguments>
 		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.statet.r.resourceProjects.RBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
 	</buildSpec>
 	<natures>
-		<nature>de.walware.statet.base.StatetNature</nature>
-		<nature>de.walware.statet.r.RNature</nature>
 		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.statet.ide.resourceProjects.Statet</nature>
+		<nature>org.eclipse.statet.r.resourceProjects.R</nature>
 	</natures>
 </projectDescription>
diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java
index fd4e9ec0..015b641d 100755
--- a/src/ac/ed/lurg/ModelConfig.java
+++ b/src/ac/ed/lurg/ModelConfig.java
@@ -155,6 +155,7 @@ public class ModelConfig {
 	public static final boolean DONT_REBASE_DEMAND = getBooleanProperty("DONT_REBASE_DEMAND", false);;
 	public static final String DEMAND_CURVES_FILE = getProperty("DEMAND_CURVES_FILE", DATA_DIR + File.separator + "com_curves.csv");  // either DEMAND_CURVES_FILE or DEMAND_CONSUMPTION_FILE is used, but not both
 	public static final String DEMAND_CONSUMPTION_FILE = getProperty("DEMAND_CONSUMPTION_FILE", DATA_DIR + File.separator + "hist_comsump.csv");
+	public static final boolean DEMAND_PRICE_IMPORT_AND_PROD_COST = getBooleanProperty("DEMAND_PRICE_IMPORT_AND_PROD_COST", false);
 	public static final String SSP_FILE = DATA_DIR + File.separator + "ssp.csv";	
 	public static final String BASELINE_CONSUMP_FILE = DATA_DIR + File.separator + "base_consump.csv";
 	public static final String CALORIE_PER_T_FILE = DATA_DIR + File.separator + "calories_per_t.csv";
diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java
index 903986b8..9eab1581 100644
--- a/src/ac/ed/lurg/ModelMain.java
+++ b/src/ac/ed/lurg/ModelMain.java
@@ -258,7 +258,12 @@ public class ModelMain {
 				double demandAmount = 0;
 
 				for (AbstractCountryAgent country : countryAgents.getAll()) {
-					Double d = country.getCurrentProjectedDemand().get(comm);
+					Map<CommodityType, Double> demands = country.getCurrentProjectedDemand();
+					if (demands == null) {
+						LogWriter.printlnError(country.getCountry() + " " + comm);
+					}
+					
+					Double d = demands.get(comm);
 					if (d != null) {
 						demandAmount += d.doubleValue();
 						LogWriter.println(String.format("%s,%s,%.4f", country.getCountry(), comm.getGamsName(), d));
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 93bdb467..db6bf93e 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -237,9 +237,21 @@ public class CountryAgent extends AbstractCountryAgent {
 			CropUsageData cropUsageData = cropUsageMap.get(crop);
 	
 			double weighting = Math.max(0, cropUsageData.getShockedNetImports()/(cropUsageData.getProductionExpected()+cropUsageData.getNetImportsExpected()));
-			double newImportPrice = currentCountryPrices.get(crop).getImportPrice()*weighting + currentCountryPrices.get(crop).getExportPrice()*(1-weighting);
 			
-			commPricePlum += newImportPrice * demandFract.get(crop);  // weight price by base demand of each cereal crop
+			double importPrice = currentCountryPrices.get(crop).getImportPrice();
+			double exportPrice = currentCountryPrices.get(crop).getExportPrice();
+			double prodCost = cropUsageData.getProdCost();
+			double exportOrProdCost = Double.isNaN(prodCost) || prodCost <= 0.0 ? exportPrice : prodCost;
+			double newCropPrice;
+			
+			if (ModelConfig.DEMAND_PRICE_IMPORT_AND_PROD_COST)
+				newCropPrice = importPrice*weighting + exportOrProdCost*(1-weighting);
+			else 
+				newCropPrice = importPrice*weighting + exportPrice*(1-weighting);
+
+			LogWriter.println(String.format("   Crop %s: import %.4f, export %.4f, prodcost %.4f, newCropPrice %.4f", crop, importPrice, exportPrice, prodCost, newCropPrice));
+			
+			commPricePlum += newCropPrice * demandFract.get(crop);  // weight price by base demand of each cereal crop
 		}
 		
 		return commPricePlum;
diff --git a/src/ac/ed/lurg/utils/WatchForFile.java b/src/ac/ed/lurg/utils/WatchForFile.java
index cd360015..d099f475 100644
--- a/src/ac/ed/lurg/utils/WatchForFile.java
+++ b/src/ac/ed/lurg/utils/WatchForFile.java
@@ -8,6 +8,7 @@ import java.nio.file.StandardWatchEventKinds;
 import java.nio.file.WatchEvent.Kind;
 import java.util.Observable;
 
+@SuppressWarnings("deprecation")
 public class WatchForFile extends Observable implements WatchDir.Callback {
 
 	private File fileToWaitFor;
-- 
GitLab