diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index 9b3f00cf0380d72e50d9893e01bb49a0368aff7c..898d414f41501af704b85256e20423326ecb394c 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -8,6 +8,7 @@ import ac.ed.lurg.country.gams.GamsCountryInput; import ac.ed.lurg.country.gams.GamsRasterInput; import ac.ed.lurg.country.gams.GamsRasterOptimiser; import ac.ed.lurg.country.gams.GamsRasterOutput; +import ac.ed.lurg.country.CountryPrice; import ac.ed.lurg.demand.DemandManager; import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.IrrigationItem; @@ -29,10 +30,11 @@ public class CountryAgent { private Timestep currentTimestep; private YieldRaster countryYieldSurfaces; private Map<CommodityType, Double> currentProjectedDemand; + private Map<CropType, CountryPrice> currentCountryPrices; private Map<CropType, Double> tradeBarriers; public CountryAgent(DemandManager demandManager,CompositeCountry country, RasterSet<LandCoverItem> initialLC, - Map<CropType, CropUsageData> cropUsageData, Map<CropType, Double> tradeBarriers) { + Map<CropType, CropUsageData> cropUsageData, Map<CropType, Double> tradeBarriers) { this.demandManager = demandManager; this.country = country; @@ -69,6 +71,8 @@ public class CountryAgent { // get projected demand currentProjectedDemand = demandManager.getDemand(country, timestep.getYear()); + currentCountryPrices = calculateCountryPrices(worldPrices); + if (currentProjectedDemand.size() == 0) { LogWriter.printlnError("No demand for country " + country + " so skipping it"); @@ -78,7 +82,7 @@ public class CountryAgent { } else { // optimize areas and intensity - GamsRasterInput input = getGamsRasterInput(currentProjectedDemand, tradeBarriers, worldPrices, irrigData); + GamsRasterInput input = getGamsRasterInput(currentProjectedDemand, currentCountryPrices, irrigData); GamsRasterOptimiser opti = new GamsRasterOptimiser(input); LogWriter.println("Running " + country.getName() + ", currentTimestep " + currentTimestep); @@ -94,7 +98,7 @@ public class CountryAgent { return currentProjectedDemand; } - private GamsRasterInput getGamsRasterInput(Map<CommodityType, Double> projectedDemand, Map<CropType, Double> tradeBarriers, Map<CropType, GlobalPrice> worldPrices, RasterSet<IrrigationItem> irrigData) { + private GamsRasterInput getGamsRasterInput(Map<CommodityType, Double> projectedDemand, Map<CropType, CountryPrice> countryPrices, RasterSet<IrrigationItem> irrigData) { //TODO why pass in projectedDemand when currentProjectedDemand belongs to CountryAgent class already? GamsRasterOutput prevOutput; Map<CropType, Double> cropAdjs; @@ -121,9 +125,25 @@ public class CountryAgent { maxOfProdOrSupply.put(entry.getKey(), cropUsage.getProduction() + Math.max(netImports, 0)); } - GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(country, projectedDemand, tradeBarriers, worldPrices, baseNetImport, maxOfProdOrSupply, cropAdjs, calibrate); + GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(country, projectedDemand, countryPrices, baseNetImport, maxOfProdOrSupply, cropAdjs, calibrate); GamsRasterInput input = new GamsRasterInput(currentTimestep, countryYieldSurfaces, prevOutput.getLandUses(), irrigData, countryLevelInputs); return input; } -} \ No newline at end of file + + + Map<CropType, CountryPrice> calculateCountryPrices(Map<CropType, GlobalPrice> worldPrices){ + + Map<CropType, CountryPrice> countryPrices = new HashMap <CropType, CountryPrice>(); + + for (CropType c : CropType.getImportedTypes()) { + GlobalPrice worldPrice = worldPrices.get(c); + CountryPrice prices = new CountryPrice((worldPrice.getImportPrice()+ worldPrice.getImportPrice()*tradeBarriers.get(c).doubleValue()), worldPrice.getExportPrice()); + countryPrices.put(c, prices); + + } + + return countryPrices; + + } +} diff --git a/src/ac/ed/lurg/country/CountryPrice.java b/src/ac/ed/lurg/country/CountryPrice.java new file mode 100644 index 0000000000000000000000000000000000000000..459ea3924f1b9c9a298803880c3b1c566aac94e7 --- /dev/null +++ b/src/ac/ed/lurg/country/CountryPrice.java @@ -0,0 +1,21 @@ +package ac.ed.lurg.country; + +public class CountryPrice { + + double importPrice; + double exportPrice; + + public CountryPrice(double importPrice, double exportPrice){ + + this.importPrice = importPrice; + this.exportPrice = exportPrice; + } + + public double getImportPrice() { + return importPrice; + } + + public double getExportPrice() { + return exportPrice; + } +} diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java index 5a94b21ca17f901c0ac989ae1e04bc23afb51bb6..e73624a3b03fa9b5c290e022f720a7a575032c8e 100644 --- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -5,7 +5,7 @@ import java.util.Map; import ac.ed.lurg.ModelConfig; import ac.ed.lurg.country.CompositeCountry; -import ac.ed.lurg.country.GlobalPrice; +import ac.ed.lurg.country.CountryPrice; import ac.ed.lurg.country.ImportExportConstraint; import ac.ed.lurg.types.CommodityType; import ac.ed.lurg.types.CropType; @@ -14,11 +14,10 @@ public class GamsCountryInput { private CompositeCountry country; // not really required but useful for debugging private Map<CommodityType, Double> projectedDemand; - private Map<CropType, GlobalPrice> worldPrices; private Map<CropType, ImportExportConstraint> importConstraints; private Map<CropType, Double> cropAdjustments; private boolean calibrateToObserved; - private Map<CropType, Double> tradeBarriers; + private Map<CropType, CountryPrice> countryPrices; /* private double maxLandUseChange; private double meatEfficiency; @@ -26,23 +25,22 @@ public class GamsCountryInput { private double landChangeEnergy;*/ - private GamsCountryInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, Double> tradeBarriers, Map<CropType, GlobalPrice> worldPrices, + private GamsCountryInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, CountryPrice> countryPrices, Map<CropType, ImportExportConstraint> importConstraints, Map<CropType, Double> cropAdjustments, boolean calibrateToObserved) { super(); this.country = country; this.projectedDemand = projectedDemand; - this.worldPrices = worldPrices; this.importConstraints = importConstraints; this.cropAdjustments = cropAdjustments; this.calibrateToObserved = calibrateToObserved; - this.tradeBarriers = tradeBarriers; + this.countryPrices = countryPrices; } public GamsCountryInput(GamsCountryInput gamsInput, Map<CropType, Double> cropAdjs) { - this(gamsInput.country, gamsInput.projectedDemand, gamsInput.tradeBarriers, gamsInput.worldPrices, gamsInput.importConstraints, cropAdjs, false); + this(gamsInput.country, gamsInput.projectedDemand, gamsInput.countryPrices,gamsInput.importConstraints, cropAdjs, false); } - public static GamsCountryInput createInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, Double> tradeBarriers, Map<CropType, GlobalPrice> worldPrices, + public static GamsCountryInput createInput(CompositeCountry country, Map<CommodityType, Double> projectedDemand, Map<CropType, CountryPrice> countryPrices, Map<CropType, Double> baseNetImport, Map<CropType, Double> maxOfProdOrSupply, Map<CropType, Double> cropAdjustments, boolean calibrateToObserved) { double allowedImportChange = calibrateToObserved ? 0.0 : ModelConfig.MAX_IMPORT_CHANGE; @@ -54,7 +52,7 @@ public class GamsCountryInput { importConstraints.put(c, new ImportExportConstraint(entry.getValue() - change, entry.getValue() + change)); } - return new GamsCountryInput(country, projectedDemand, tradeBarriers, worldPrices, importConstraints, cropAdjustments, calibrateToObserved); + return new GamsCountryInput(country, projectedDemand, countryPrices, importConstraints, cropAdjustments, calibrateToObserved); } @@ -66,8 +64,8 @@ public class GamsCountryInput { return projectedDemand; } - public Map<CropType, GlobalPrice> getWorldPrices() { - return worldPrices; + public Map<CropType, CountryPrice> getCountryPrices() { + return countryPrices; } public Map<CropType, ImportExportConstraint> getImportConstraints() { @@ -115,22 +113,19 @@ public class GamsCountryInput { } public Map<CropType, Double> getCountryImportPrices() { - double tradeBarrier; - double baseImportPrice; Map<CropType, Double> prices = new HashMap<CropType, Double>(); - for (Map.Entry<CropType, GlobalPrice> entry : worldPrices.entrySet()) { - - tradeBarrier = tradeBarriers.get(entry.getKey()); - baseImportPrice = entry.getValue().getImportPrice(); - prices.put(entry.getKey(), baseImportPrice+baseImportPrice*tradeBarrier); + + for (Map.Entry<CropType, CountryPrice> entry : countryPrices.entrySet()) { + prices.put(entry.getKey(), entry.getValue().getImportPrice()); } return prices; - } + } + - public Map<CropType, Double> getWorldExportPrices() { + public Map<CropType, Double> getCountryExportPrices() { Map<CropType, Double> prices = new HashMap<CropType, Double>(); - for (Map.Entry<CropType, GlobalPrice> entry : worldPrices.entrySet()) { + for (Map.Entry<CropType, CountryPrice> entry : countryPrices.entrySet()) { prices.put(entry.getKey(), entry.getValue().getExportPrice()); } return prices; diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java index b6cbb46ab6173e35e2c6ec6df0d02089bfd4eea6..4968fe01c3f40fb3285373929999a5ca604721a9 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java @@ -20,7 +20,7 @@ import com.gams.api.GAMSWorkspace; import com.gams.api.GAMSWorkspaceInfo; import ac.ed.lurg.ModelConfig; -import ac.ed.lurg.country.GlobalPrice; +import ac.ed.lurg.country.CountryPrice; import ac.ed.lurg.country.ImportExportConstraint; import ac.ed.lurg.landuse.CropUsageData; import ac.ed.lurg.landuse.Intensity; @@ -177,7 +177,7 @@ public class GamsLocationOptimiser { maxNetImport = iec.getMaxNetImport(); } - GlobalPrice gp = countryInput.getWorldPrices().get(crop); + CountryPrice gp = countryInput.getCountryPrices().get(crop); LogWriter.println(String.format(" %15s, \t %5.1f, \t %5.1f, \t %5.3f, \t %5.3f", crop.getGamsName(), minNetImport, maxNetImport, gp.getImportPrice(), gp.getExportPrice())); @@ -187,7 +187,7 @@ public class GamsLocationOptimiser { addItemMapParm(inDB.addParameter("minNetImport", 1), countryInput.getMinNetImport(), false); addItemMapParm(inDB.addParameter("maxNetImport", 1), countryInput.getMaxNetImport(), false); addItemMapParm(inDB.addParameter("countryImportPrices", 1), countryInput.getCountryImportPrices(), false); - addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getWorldExportPrices(), false); + addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getCountryExportPrices(), false); //the above is not really world export price because it is scaled by country tradeBarriers, might want to change //parameter name in GAMS i.e. countryExportPrices LogWriter.print("\n"); diff --git a/src/ac/ed/lurg/country/gams/GamsLocationTest.java b/src/ac/ed/lurg/country/gams/GamsLocationTest.java index 33ad6ae7fc669eb796b9d7b4108890ad8d0041a4..f66d29ce73c2aa7ac789eb2ea3c2e5c72a6e5843 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationTest.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationTest.java @@ -5,6 +5,7 @@ import java.util.Map; import ac.ed.lurg.Timestep; import ac.ed.lurg.country.CompositeCountry; +import ac.ed.lurg.country.CountryPrice; import ac.ed.lurg.landuse.LandUseItem; import ac.ed.lurg.landuse.IrrigationItem; import ac.ed.lurg.types.CommodityType; @@ -22,7 +23,7 @@ public class GamsLocationTest { } private void run() { - GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getTradeBarriers(), null, null, null, null, true); + GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getCountryPrices(), null, null, null, true); GamsLocationInput gamsInput = new GamsLocationInput(new Timestep(0), getYields(), getPreviousArea(), getIrrigationCosts(), countryLevelInputs); GamsLocationOptimiser opti = new GamsLocationOptimiser(gamsInput); @@ -40,19 +41,26 @@ public class GamsLocationTest { return dummyMap; } - Map<CropType, Double> getTradeBarriers() { - Map<CropType, Double> dummyTradeMap = new HashMap<CropType, Double>(); - dummyTradeMap.put(CropType.WHEAT, 300.0); - dummyTradeMap.put(CropType.MAIZE, 50.0); - dummyTradeMap.put(CropType.RICE, 50.0); - dummyTradeMap.put(CropType.OILCROPS, 50.0); - dummyTradeMap.put(CropType.PULSES, 60.0); - dummyTradeMap.put(CropType.STARCHY_ROOTS, 150.0); - dummyTradeMap.put(CropType.MEAT, 480.0); + Map<CropType, CountryPrice> getCountryPrices() { + + Map<CropType, CountryPrice> dummyTradeMap = new HashMap<CropType, CountryPrice>(); + + dummyTradeMap.put(CropType.WHEAT, createCountryPrice(0.15,0.15)); + dummyTradeMap.put(CropType.MAIZE, createCountryPrice(0.15,0.15)); + dummyTradeMap.put(CropType.RICE, createCountryPrice(0.4,0.4)); + dummyTradeMap.put(CropType.OILCROPS, createCountryPrice(0.4,0.4)); + dummyTradeMap.put(CropType.PULSES, createCountryPrice(0.2,0.2)); + dummyTradeMap.put(CropType.STARCHY_ROOTS, createCountryPrice(0.1,0.1)); + dummyTradeMap.put(CropType.MEAT, createCountryPrice(0.2,0.2)); return dummyTradeMap; } + CountryPrice createCountryPrice(double imports, double exports){ + + return new CountryPrice(imports,exports); + + } Map<Integer, YieldResponsesItem> getYields() { diff --git a/src/ac/ed/lurg/country/gams/GamsRasterTest.java b/src/ac/ed/lurg/country/gams/GamsRasterTest.java index cc4548d732e8b9f0e4004c1cf7896ec1479abadc..6a29149d64075265504a56f4cf9f8857f768b0e4 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterTest.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterTest.java @@ -18,7 +18,7 @@ public class GamsRasterTest extends GamsLocationTest { } private void run() { - GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getTradeBarriers(), null, null, null, null, true); + GamsCountryInput countryLevelInputs = GamsCountryInput.createInput(new CompositeCountry("Test"), getProjectedDemand(), getCountryPrices(), null, null, null, true); GamsRasterInput input = new GamsRasterInput(new Timestep(0), getYieldRaster(), getPreviousAreaRaster(), getIrrigationCost(), countryLevelInputs); GamsRasterOptimiser opti = new GamsRasterOptimiser(input);