From bbd4df4d5132e356ae27c3508b03cbb3e9b0b87c Mon Sep 17 00:00:00 2001 From: R0slyn <devnull@localhost> Date: Wed, 9 Nov 2016 17:32:56 +0000 Subject: [PATCH] Changed getSuitable function. Change trade barriers to affect imports rather than exports. Created 'TradeManager' to allow for switching on and off of barriers from file. --- .classpath | 17 ++-- GAMS/IntExtOpt.gms | 8 +- src/ac/ed/lurg/ModelConfig.java | 6 +- src/ac/ed/lurg/ModelMain.java | 14 +-- .../ed/lurg/country/TradeBarriersReader.java | 93 ------------------- .../lurg/country/gams/GamsCountryInput.java | 16 ++-- .../country/gams/GamsLocationOptimiser.java | 4 +- src/ac/ed/lurg/landuse/LandUseItem.java | 22 +---- 8 files changed, 42 insertions(+), 138 deletions(-) delete mode 100644 src/ac/ed/lurg/country/TradeBarriersReader.java diff --git a/.classpath b/.classpath index 5cf8c1de..22226a36 100644 --- a/.classpath +++ b/.classpath @@ -1,8 +1,9 @@ -<?xml version="1.0" encoding="UTF-8"?> -<classpath> - <classpathentry kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> - <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> - <classpathentry kind="lib" path="/Applications/GAMS24.7/sysdir/apifiles/Java/api/GAMSJavaAPI.jar"/> - <classpathentry kind="output" path="bin"/> -</classpath> +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> + <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> + <classpathentry kind="lib" path="C:/GAMS/win64/24.7/apifiles/Java/api/GAMSJavaAPI.jar"/> + <classpathentry kind="lib" path="C:/GAMS/win64/24.7/apifiles/Java/api/gams.jar"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms index 81cb9de9..627ece29 100644 --- a/GAMS/IntExtOpt.gms +++ b/GAMS/IntExtOpt.gms @@ -20,7 +20,7 @@ PARAMETER irrigParam(crop, location) yield response to irrigation parameter; PARAMETER demand(all_types) in t; PARAMETER worldExportPrices(import_crop) prices for exports; - PARAMETER worldImportPrices(import_crop) prices for imports; + PARAMETER countryImportPrices(import_crop) prices for imports; PARAMETER maxNetImport(import_crop) maximum net import for each crop based on world market; PARAMETER minNetImport(import_crop) minimum net import for each crop based on world market; PARAMETER irrigCost(location) irrigation cost in cost per 1000 Mlitre or Mha for each litre per m2; @@ -40,7 +40,7 @@ $gdxin %gdxincname% $load location, suitableLandArea, previousArea, demand, landChangeCost $load yieldNone, yieldFertOnly, yieldIrrigOnly, yieldBoth -$load fertParam, irrigParam, otherIParam, worldExportPrices, worldImportPrices, maxNetImport, minNetImport, unhandledCropArea +$load fertParam, irrigParam, otherIParam, worldExportPrices, countryImportPrices, maxNetImport, minNetImport, unhandledCropArea $load meatEfficency, minFeedRate, otherICost, irrigCost, irrigMaxRate, irrigConstraint, fertiliserUnitCost, domesticPriceMarkup $gdxin @@ -182,7 +182,7 @@ $gdxin ) * landChangeCost ) * domesticPriceMarkup + - SUM(import_crop, importAmount(import_crop) * worldImportPrices(import_crop) - exportAmount(import_crop) * worldExportPrices(import_crop)) + SUM(import_crop, importAmount(import_crop) * countryImportPrices(import_crop) - exportAmount(import_crop) * worldExportPrices(import_crop)) ) / 1000000; MODEL LAND_USE /ALL/ ; @@ -218,7 +218,7 @@ $gdxin totalProdCost(crop) = sum(location, unitCost.l(crop, location) * area.l(crop, location)); totalCropland(location) = sum(crop_less_pasture, area.l(crop_less_pasture, location)); - netImportCost(import_crop) = importAmount.l(import_crop) * worldImportPrices(import_crop) - exportAmount.l(import_crop) * worldExportPrices(import_crop); + netImportCost(import_crop) = importAmount.l(import_crop) * countryImportPrices(import_crop) - exportAmount.l(import_crop) * worldExportPrices(import_crop); netImportAmount(import_crop) = importAmount.l(import_crop) - exportAmount.l(import_crop); feedCost(feed_crop)$(netImportAmount(feed_crop) gt 0) = (totalProdCost(feed_crop) + netImportCost(feed_crop)) * feedAmount.l(feed_crop) / (totalProd(feed_crop) + netImportAmount(feed_crop)); diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java index e04b2ca1..ef7a56b2 100644 --- a/src/ac/ed/lurg/ModelConfig.java +++ b/src/ac/ed/lurg/ModelConfig.java @@ -96,7 +96,7 @@ public class ModelConfig { public static final String COUNTRY_DATA_FILE = DATA_DIR + File.separator + "country_data.csv"; public static final String COMMODITY_DATA_FILE = DATA_DIR + File.separator + "con_prod_c_and_m.csv"; public static final String BIOENERGY_DEMAND_FILE = DATA_DIR + File.separator + "bio_demand.csv"; - public static final String TRADE_BARRIERS_FILE = DATA_DIR + File.separator + "trade_barriers.csv"; + public static final String TRADE_BARRIERS_FILE = DATA_DIR + File.separator + "trade_barriers_dummy.csv"; // yield data @@ -171,8 +171,10 @@ public class ModelConfig { public static final double DOMESTIC_PRICE_MARKUP = getDoubleProperty("DOMESTIC_PRICE_MARKUP", 1.0); public static final double TRANSPORT_LOSSES = getDoubleProperty("TRANSPORT_LOSSES", 0.1); // in international trade - //public static final double TRADE_BARRIER_FACTOR = getDoubleProperty("TRADE_BARRIER_FACTOR", 1.5); // price factor in international trade, transport cost and real trade barriers + public static final double TRADE_BARRIER_FACTOR_DEFAULT = getDoubleProperty("TRADE_BARRIER_FACTOR", 0.2); // price factor in international trade, transport cost and real trade barriers + public static final boolean ACTIVE_TRADE_BARRIERS = getBooleanProperty("ACTIVE_TRADE_BARRIERS", false); // if set to true read in barrier information from file, otherwise use default as above + public static final int NUM_CEREAL_CATEGORIES = getIntProperty("NUM_CEREAL_CATEGORIES", 5); public static final int NUM_PASTURE_CATEGORIES = getIntProperty("NUM_PASTURE_CATEGORIES", 1); diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index 746596cb..4fa81ae5 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -17,7 +17,7 @@ import ac.ed.lurg.country.CountryAgent; import ac.ed.lurg.country.CountryBoundaryRaster; import ac.ed.lurg.country.CountryBoundaryReader; import ac.ed.lurg.country.GlobalPrice; -import ac.ed.lurg.country.TradeBarriersReader; +import ac.ed.lurg.country.TradeManager; import ac.ed.lurg.country.gams.GamsRasterOutput; import ac.ed.lurg.demand.BaseConsumpManager; import ac.ed.lurg.demand.DemandManager; @@ -37,6 +37,7 @@ import ac.ed.lurg.types.CropToDoubleMap; import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.LandCoverType; import ac.ed.lurg.types.ModelFitType; +import ac.ed.lurg.utils.LazyHashMap; import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.yield.LPJYieldResponseMapReader; import ac.ed.lurg.yield.YieldRaster; @@ -53,6 +54,7 @@ public class ModelMain { private Collection<CountryAgent> countryAgents; private CountryBoundaryRaster countryBoundaryRaster; private DemandManager demandManager; + private TradeManager tradeManager; private CompositeCountryManager compositeCountryManager; private RasterHeaderDetails desiredProjection; @@ -73,6 +75,7 @@ public class ModelMain { BaseConsumpManager baseConsumpManager = new BaseConsumpManager(); compositeCountryManager = new CompositeCountryManager(baseConsumpManager); demandManager = new DemandManager(ModelFitType.LOGISTIC, ModelConfig.SSP_SCENARIO, baseConsumpManager, compositeCountryManager); + tradeManager = new TradeManager(compositeCountryManager); currentIrrigationData = getFixedIrrigationData(); countryBoundaryRaster = getCountryBoundaryRaster(); @@ -338,10 +341,8 @@ public class ModelMain { RasterSet<LandCoverItem> initLC = getInitialLandCover(); Map<CompositeCountry, Map<CropType, CropUsageData>> cropUsageDataMap = new CropUsageReader(compositeCountryManager).getCommodityData(); - Map<CompositeCountry, CropToDoubleMap> tradeBarriersMap = new TradeBarriersReader(compositeCountryManager).getTradeBarriers(); - - + for (CompositeCountry cc : countryGrouping) { // DEBUG code @@ -354,9 +355,10 @@ public class ModelMain { List<RasterKey> keys = countryBoundaryRaster.getKeysFor(cc); RasterSet<LandCoverItem> initCountryLC = initLC.createSubsetForKeys(keys); Map<CropType, CropUsageData> countryCommodityData = cropUsageDataMap.get(cc); - Map<CropType, Double> countryTradeBarriers = tradeBarriersMap.get(cc); - + Map<CropType, Double> countryTradeBarriers = tradeManager.getTradeBarriers(cc); + //Map<CropType, Double> countryTradeBarriers = (ModelConfig.ACTIVE_TRADE_BARRIERS) ? tradeBarriersMap.get(cc): getDefaultTradeBarriers(); + if (countryCommodityData == null) { LogWriter.printlnError("No commodities data for " + cc + ", so skipping"); } diff --git a/src/ac/ed/lurg/country/TradeBarriersReader.java b/src/ac/ed/lurg/country/TradeBarriersReader.java deleted file mode 100644 index 28ea881a..00000000 --- a/src/ac/ed/lurg/country/TradeBarriersReader.java +++ /dev/null @@ -1,93 +0,0 @@ -package ac.ed.lurg.country; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.Map; -import java.util.Map.Entry; - -import ac.ed.lurg.ModelConfig; -import ac.ed.lurg.types.CropType; -import ac.ed.lurg.utils.LazyHashMap; -import ac.ed.lurg.utils.LogWriter; -import ac.ed.lurg.types.CropToDoubleMap; - - -public class TradeBarriersReader { - - private static final int COUNTRY_COL = 0; - private static final int ITEM_COL = 1; - private static final int TRADE_BARRIER_COL = 2; - - private CompositeCountryManager compositeCountryManager; - - public TradeBarriersReader(CompositeCountryManager compositeCountryManager) { - // super(); - this.compositeCountryManager = compositeCountryManager; - } - - public Map<CompositeCountry, CropToDoubleMap> getTradeBarriers() { - String filename = ModelConfig.TRADE_BARRIERS_FILE; - - @SuppressWarnings("serial") - LazyHashMap<CompositeCountry, CropToDoubleMap> tradeMap = new LazyHashMap<CompositeCountry, CropToDoubleMap>() { - protected CropToDoubleMap createValue() { return new CropToDoubleMap(); } - }; -// - - try { - BufferedReader reader = new BufferedReader(new FileReader(filename)); - String line, countryName, itemName; - double barrierValue; - reader.readLine(); // read header - - while ((line=reader.readLine()) != null) { - String[] tokens = line.split(","); - - if (tokens.length < 3) - LogWriter.printlnError("Too few columns in " + filename + ", " + line); - - try { - - countryName = tokens[COUNTRY_COL]; - itemName = tokens[ITEM_COL]; - barrierValue = Double.parseDouble(tokens[TRADE_BARRIER_COL]); - - SingleCountry country = CountryManager.getForName(countryName); - - CropType crop = CropType.getForFaoName(itemName); - - CompositeCountry cc = compositeCountryManager.getForSingleCountry(country); - - CropToDoubleMap countryData = tradeMap.lazyGet(cc); - - countryData.incrementValue(crop, barrierValue); - - } catch (Exception e) { - LogWriter.printlnError("Failed in processing trade barriers line " + line); - LogWriter.print(e); - } - } - reader.close(); - - } catch (Exception e) { - LogWriter.printlnError("Failed in reading trade barriers data file"); - LogWriter.print(e); - } - LogWriter.println("Processed " + filename + ", create " + tradeMap.size() + " barriers entries"); - - for (Entry<CompositeCountry, CropToDoubleMap> entry : tradeMap.entrySet()) { - -// LogWriter.println("country name " + entry.getKey()); -// LogWriter.println("crops " + entry.getValue()); - - entry.getValue().divideBy(compositeCountryManager.getAllForCompositeCountry(entry.getKey()).size()); - -// LogWriter.println("number of countries " + compositeCountryManager.getAllForCompositeCountry(entry.getKey()).size()); -// LogWriter.println("crops " + entry.getValue()); - - } - - return tradeMap; - } - -} diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java index 92b8b96e..d31d9f84 100644 --- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -115,21 +115,25 @@ public class GamsCountryInput { return netImport; } - public Map<CropType, Double> getWorldImportPrices() { + 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()) { - - prices.put(entry.getKey(), entry.getValue().getImportPrice()); + + tradeBarrier = tradeBarriers.get(entry.getKey()); + baseImportPrice = entry.getValue().getImportPrice(); + prices.put(entry.getKey(), baseImportPrice+baseImportPrice*tradeBarrier); } return prices; } - public Map<CropType, Double> getCountryExportPrices() { + public Map<CropType, Double> getWorldExportPrices() { Map<CropType, Double> prices = new HashMap<CropType, Double>(); for (Map.Entry<CropType, GlobalPrice> entry : worldPrices.entrySet()) { - double tradeBarrier = tradeBarriers.get(entry.getKey()); - prices.put(entry.getKey(), entry.getValue().getExportPrice()*(1-tradeBarrier)); + + 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 b7dff38d..b6cbb46a 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java @@ -186,8 +186,8 @@ public class GamsLocationOptimiser { addItemMapParm(inDB.addParameter("minNetImport", 1), countryInput.getMinNetImport(), false); addItemMapParm(inDB.addParameter("maxNetImport", 1), countryInput.getMaxNetImport(), false); - addItemMapParm(inDB.addParameter("worldImportPrices", 1), countryInput.getWorldImportPrices(), false); - addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getCountryExportPrices(), false); + addItemMapParm(inDB.addParameter("countryImportPrices", 1), countryInput.getCountryImportPrices(), false); + addItemMapParm(inDB.addParameter("worldExportPrices", 1), countryInput.getWorldExportPrices(), 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/landuse/LandUseItem.java b/src/ac/ed/lurg/landuse/LandUseItem.java index f4a3d817..14594356 100644 --- a/src/ac/ed/lurg/landuse/LandUseItem.java +++ b/src/ac/ed/lurg/landuse/LandUseItem.java @@ -202,25 +202,13 @@ public class LandUseItem implements InterpolatingRasterItem<LandUseItem> { double d = 0; double protectedTotal = protectedArea; - double forestArea = getLandCoverArea(LandCoverType.FOREST); - double naturalArea = getLandCoverArea(LandCoverType.OTHER_NATURAL); - double totalNatural = forestArea+naturalArea; + double totalNatural = getLandCoverArea(LandCoverType.OTHER_NATURAL) + getLandCoverArea(LandCoverType.FOREST); - if (totalNatural >= protectedTotal){ - - double unavailableNatural = naturalArea/totalNatural*protectedTotal; - unavailableNatural = (Double.isNaN(unavailableNatural) || Double.isInfinite(unavailableNatural)) ? 0.0 : unavailableNatural; - double unavailableForest = forestArea/totalNatural*protectedTotal; - unavailableForest = (Double.isNaN(unavailableForest) || Double.isInfinite(unavailableForest)) ? 0.0 : unavailableForest; - - d += naturalArea - unavailableNatural + forestArea - unavailableForest; + if (totalNatural >= protectedTotal) + d += totalNatural - protectedTotal; - } - - for (LandCoverType l : LandCoverType.values()) { - if (LandCoverType.PASTURE.equals(l)|| LandCoverType.CROPLAND.equals(l) ) // barren land is not suitable - d += getLandCoverArea(l); - } + d += getLandCoverArea(LandCoverType.PASTURE); + d += getLandCoverArea(LandCoverType.CROPLAND); return d; } -- GitLab