diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms index 2da56a774f6274ddb0d45075e6e4136b06a865f8..a6c3795e50f81cfaf79d433d7352fc0509c2c7ba 100644 --- a/GAMS/IntExtOpt.gms +++ b/GAMS/IntExtOpt.gms @@ -10,8 +10,8 @@ PARAMETER yieldFertOnly(crop, location) yield in t per ha; PARAMETER yieldIrrigOnly(crop, location) yield in t per ha; PARAMETER yieldBoth(crop, location) yield in t per ha; - PARAMETER fertParam(crop) yield response to fertilizer parameter; - PARAMETER irrigParam(crop) yield response to irrigation parameter; + PARAMETER fertParam(crop, location) yield response to fertilizer parameter; + PARAMETER irrigParam(crop, location) yield response to irrigation parameter; PARAMETER demand(crop) in t; PARAMETER world_input_energy(crop) average input energy from world exports used to determine if we should import or export; PARAMETER maxNetImport(crop) maximum net import for each crop based on world market; @@ -73,17 +73,17 @@ $gdxin YIELD_EQ(crop, location) .. yield(crop, location) =E= ( yieldNone(crop, location) + - (yieldFertOnly(crop, location) - yieldNone(crop, location)) * ((fertI(crop, location) + delta) ** fertParam(crop)) + - (yieldIrrigOnly(crop, location) - yieldNone(crop, location)) * ((irrigI(crop, location) + delta) ** irrigParam(crop)) + + (yieldFertOnly(crop, location) - yieldNone(crop, location)) * ((fertI(crop, location) + delta) ** fertParam(crop, location)) + + (yieldIrrigOnly(crop, location) - yieldNone(crop, location)) * ((irrigI(crop, location) + delta) ** irrigParam(crop, location)) + (yieldBoth(crop, location) + yieldNone(crop, location) - yieldFertOnly(crop, location) - yieldIrrigOnly(crop, location)) * - ((fertI(crop, location) + delta) ** fertParam(crop)) * ((irrigI(crop, location) + delta) ** irrigParam(crop)) + ((fertI(crop, location) + delta) ** fertParam(crop, location)) * ((irrigI(crop, location) + delta) ** irrigParam(crop, location)) ) * otherIntensity(crop, location); * YIELD_EQ(crop, location) .. yield(crop, location) =E= ( * yieldNone(crop, location) + * (yieldFertOnly(crop, location) - yieldNone(crop, location)) * (1 - exp(-10*fertI(crop, location)) + * (yieldIrrigOnly(crop, location) - yieldNone(crop, location)) * (1 - exp(-5*irrigI(crop, location)) + -* (yieldBoth(crop, location) + yieldNone(crop, location) - yieldFertOnly(crop, location) - yieldIrrigOnly(crop, location)) * (1 - exp(-fertParam*fertI(crop, location))) * (1 - exp(-irrigParam*irrigI(crop, location))) +* (yieldBoth(crop, location) + yieldNone(crop, location) - yieldFertOnly(crop, location) - yieldIrrigOnly(crop, location)) * (1 - exp(-fertParam(crop, location)*fertI(crop, location))) * (1 - exp(-irrigParam(crop, location)*irrigI(crop, location))) * ) * otherIntensity(crop, location); CROP_DEMAND_CONSTRAINT(crop_less_pasture) .. sum(location, area(crop_less_pasture, location) * yield(crop_less_pasture, location)) - feedAmount(crop_less_pasture) =G= diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java index a0dc812fb8f727c7042d574d75f4acb4b0791a38..1208fb42039c11dd82e12f274eaa6f81b0c818ca 100644 --- a/src/ac/ed/lurg/ModelConfig.java +++ b/src/ac/ed/lurg/ModelConfig.java @@ -99,5 +99,7 @@ public class ModelConfig { public static final int START_TIMESTEP = getIntProperty("START_TIMESTEP", 0); public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 1); public static final int BASE_YEAR = getIntProperty("BASE_YEAR", 2010); + + public static final int NUM_LOCATIONS_PER_COUNTRY = 5; } \ No newline at end of file diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index af3df404cfe5566ee35e1599b4846192364bd14b..5344ba1dae8ea936a2d86821f9b530a3b896b58a 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -69,48 +69,56 @@ public class CountryAgent implements GamsInputData { } @Override - public Map<CropType, Double> getYieldNone() { - return currentRefYield; + public Map<Integer, Map<CropType, Double>> getYieldNone() { + Map<Integer, Map<CropType, Double>> returnMap = new HashMap<Integer, Map<CropType, Double>>(); + + for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) + returnMap.put(i, currentRefYield); + + return returnMap; } @Override - public Map<CropType, Double> getYieldFertOnly() { - return currentRefYield; + public Map<Integer, Map<CropType, Double>> getYieldFertOnly() { + return getYieldNone(); } @Override - public Map<CropType, Double> getYieldIrrigOnly() { - return currentRefYield; + public Map<Integer, Map<CropType, Double>> getYieldIrrigOnly() { + return getYieldNone(); } @Override - public Map<CropType, Double> getYieldBoth() { - return currentRefYield; + public Map<Integer, Map<CropType, Double>> getYieldBoth() { + return getYieldNone(); } - @Override - public Map<CropType, Double> getFertParam() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); - + private Map<Integer, Map<CropType, Double>> getLocationCropMapForValue(double d) { + Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); for (CropType crop : CropType.getAllItems()) - dummyMaxExports.put(crop, 0.4); - - return dummyMaxExports; + dummyMap.put(crop, d); + + Map<Integer, Map<CropType, Double>> returnMap = new HashMap<Integer, Map<CropType, Double>>(); + + for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) + returnMap.put(i, dummyMap); + + return returnMap; } @Override - public Map<CropType, Double> getIrrigParam() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); + public Map<Integer, Map<CropType, Double>> getFertParam() { + return getLocationCropMapForValue(0.6); + } - for (CropType crop : CropType.getAllItems()) - dummyMaxExports.put(crop, 0.4); - - return dummyMaxExports; + @Override + public Map<Integer, Map<CropType, Double>> getIrrigParam() { + return getLocationCropMapForValue(0.5); } @Override - public Map<CropType, Double> getPreviousCropArea() { + public Map<Integer, Map<CropType, Double>> getPreviousCropArea() { Map<CropType, Double> previousCropAreas = new HashMap<CropType, Double>(); int previousTimestep = currentTimestep==0 ? 0 : currentTimestep-1; CropAreas cd = cropAreasTimeseries.get(previousTimestep); @@ -118,7 +126,12 @@ public class CountryAgent implements GamsInputData { for (CropType crop : CropType.getAllItems()) previousCropAreas.put(crop, cd.getCropArea(crop)); - return previousCropAreas; + Map<Integer, Map<CropType, Double>> returnMap = new HashMap<Integer, Map<CropType, Double>>(); + + for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) + returnMap.put(i, previousCropAreas); + + return returnMap; } @Override diff --git a/src/ac/ed/lurg/country/gams/GamsInputData.java b/src/ac/ed/lurg/country/gams/GamsInputData.java index 8bfb24e5df20119ac48b310eec197905b4a51da7..4ea87e1a0b188f7c2586cbf82b0402cfe123ff12 100644 --- a/src/ac/ed/lurg/country/gams/GamsInputData.java +++ b/src/ac/ed/lurg/country/gams/GamsInputData.java @@ -5,23 +5,25 @@ import java.util.Map; import ac.ed.lurg.types.CropType; public interface GamsInputData { - + + Map<Integer, Map<CropType, Double>> getYieldNone(); + Map<Integer, Map<CropType, Double>> getYieldFertOnly(); + Map<Integer, Map<CropType, Double>> getYieldIrrigOnly(); + Map<Integer, Map<CropType, Double>> getYieldBoth(); + Map<Integer, Map<CropType, Double>> getPreviousCropArea(); + + Map<Integer, Map<CropType, Double>> getFertParam(); + Map<Integer, Map<CropType, Double>> getIrrigParam(); + Map<CropType, Double> getProjectedDemand(); - Map<CropType, Double> getYieldNone(); - Map<CropType, Double> getYieldFertOnly(); - Map<CropType, Double> getYieldIrrigOnly(); - Map<CropType, Double> getYieldBoth(); - Map<CropType, Double> getPreviousCropArea(); Map<CropType, Double> getWorldInputEnergy(); Map<CropType, Double> getMaxNetImport(); Map<CropType, Double> getMinNetImport(); + double getMeatEfficiency(); double getMaxLandUseChange(); double getMaxIntensity(); double getTradeBarrier(); double getLandChangeEnergy(); double getMinFeedRate(); - Map<CropType, Double> getFertParam(); - Map<CropType, Double> getIrrigParam(); - } diff --git a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java index 0c6e04845a283a7490dcc33f8f7815b4eac30394..70402dfb1e8ad3700b631cbdcb7830d12bad2abd 100644 --- a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java @@ -38,57 +38,50 @@ public class GamsLandUseOptimiser { GAMSWorkspace ws = new GAMSWorkspace(wsInfo); GAMSDatabase db = ws.addDatabase(); - - int numLocations = 5; LogWriter.println("\nPrevious areas"); - GAMSParameter parm = db.addParameter("previous_area", 2, "the previous area for each land use"); - for (int i= 1; i<=numLocations; i++) - addLocationMapParm(parm, inputData.getPreviousCropArea(), i); + GAMSParameter param = db.addParameter("previous_area", 2, "the previous area for each land use"); + addLocationMapParm(param, inputData.getPreviousCropArea()); LogWriter.println("\nDemand"); - parm = db.addParameter("demand", 1, "demand for crop"); - addItemMapParm(parm, inputData.getProjectedDemand()); + param = db.addParameter("demand", 1, "demand for crop"); + addItemMapParm(param, inputData.getProjectedDemand()); LogWriter.println("\nYieldNone"); - parm = db.addParameter("yieldNone", 2, "ref yield t per ha"); - for (int i= 1; i<=numLocations; i++) - addLocationMapParm(parm, inputData.getYieldNone(), i); + param = db.addParameter("yieldNone", 2, "ref yield t per ha"); + addLocationMapParm(param, inputData.getYieldNone()); LogWriter.println("\nYieldFertOnly"); - parm = db.addParameter("yieldFertOnly", 2, "ref yield t per ha"); - for (int i= 1; i<=numLocations; i++) - addLocationMapParm(parm, inputData.getYieldFertOnly(), i); + param = db.addParameter("yieldFertOnly", 2, "ref yield t per ha"); + addLocationMapParm(param, inputData.getYieldFertOnly()); LogWriter.println("\nYyieldIrrigOnly"); - parm = db.addParameter("yieldIrrigOnly", 2, "ref yield t per ha"); - for (int i= 1; i<=numLocations; i++) - addLocationMapParm(parm, inputData.getYieldIrrigOnly(), i); + param = db.addParameter("yieldIrrigOnly", 2, "ref yield t per ha"); + addLocationMapParm(param, inputData.getYieldIrrigOnly()); LogWriter.println("\nYieldBoth"); - parm = db.addParameter("yieldBoth", 2, "ref yield t per ha"); - for (int i= 1; i<=numLocations; i++) - addLocationMapParm(parm, inputData.getYieldBoth(), i); + param = db.addParameter("yieldBoth", 2, "ref yield t per ha"); + addLocationMapParm(param, inputData.getYieldBoth()); LogWriter.println("\nfertParam"); - parm = db.addParameter("fertParam", 1); - addItemMapParm(parm, inputData.getFertParam()); + param = db.addParameter("fertParam", 2); + addLocationMapParm(param, inputData.getFertParam()); LogWriter.println("\nirrigParam"); - parm = db.addParameter("irrigParam", 1); - addItemMapParm(parm, inputData.getIrrigParam()); + param = db.addParameter("irrigParam", 2); + addLocationMapParm(param, inputData.getIrrigParam()); LogWriter.println("\nWorld input energy"); - parm = db.addParameter("world_input_energy", 1, "average input energy from world exports used to determine if we should import or export energy per t"); - addItemMapParm(parm, inputData.getWorldInputEnergy()); + param = db.addParameter("world_input_energy", 1, "average input energy from world exports used to determine if we should import or export energy per t"); + addItemMapParm(param, inputData.getWorldInputEnergy()); LogWriter.println("\nMax Net Import"); - parm = db.addParameter("maxNetImport", 1, "max net imports for each crop based on world market"); - addItemMapParm(parm, inputData.getMaxNetImport()); + param = db.addParameter("maxNetImport", 1, "max net imports for each crop based on world market"); + addItemMapParm(param, inputData.getMaxNetImport()); LogWriter.println("\nMin Net Import"); - parm = db.addParameter("minNetImport", 1, "min net imports for each crop based on world market"); - addItemMapParm(parm, inputData.getMinNetImport()); + param = db.addParameter("minNetImport", 1, "min net imports for each crop based on world market"); + addItemMapParm(param, inputData.getMinNetImport()); addScalar(db.addParameter("meatEfficency", 0, "efficency of converting feed and pasture into animal products"), inputData.getMeatEfficiency()); addScalar(db.addParameter("maxLandUseChange", 0, "max rate of land use change"), inputData.getMaxLandUseChange()); @@ -154,19 +147,20 @@ public class GamsLandUseOptimiser { } } - private void addLocationMapParm(GAMSParameter parm, Map<CropType, Double> itemMap, int locationId) { - for (Map.Entry<CropType, Double> entry : itemMap.entrySet()) { - LogWriter.println(String.format(" %15s,\t %.1f", entry.getKey().getGamsName(), entry.getValue())); - Vector<String> v = new Vector<String>(); - v.add(entry.getKey().getGamsName()); - v.add(Integer.toString(locationId)); - // if (locationId== 2 & entry.getKey().getGamsName().equals("cereals")) - // parm.addRecord(v).setValue(2.5); - // else + private void addLocationMapParm(GAMSParameter parm, Map<Integer, Map<CropType, Double>> locationItemMap) { + for (Map.Entry<Integer, Map<CropType, Double>> cropsForALocation : locationItemMap.entrySet()) { + Integer locationId = cropsForALocation.getKey(); + Map<CropType, Double> itemMap = cropsForALocation.getValue(); + + for (Map.Entry<CropType, Double> entry : itemMap.entrySet()) { + LogWriter.println(String.format(" %15s,\t %.1f", entry.getKey().getGamsName(), entry.getValue())); + Vector<String> v = new Vector<String>(); + v.add(entry.getKey().getGamsName()); + v.add(Integer.toString(locationId)); parm.addRecord(v).setValue(entry.getValue()); + } } } - @SuppressWarnings("unused") private void cleanup(String directory) { @@ -186,4 +180,4 @@ public class GamsLandUseOptimiser { e.printStackTrace(); } } -} +} \ No newline at end of file diff --git a/src/ac/ed/lurg/country/gams/GamsTest.java b/src/ac/ed/lurg/country/gams/GamsTest.java index 1121e726c495d1bfe88c73e7c92ebbae98dc3be8..384f920e5a22e96ccb6ad6167f29f7c2adf420d3 100644 --- a/src/ac/ed/lurg/country/gams/GamsTest.java +++ b/src/ac/ed/lurg/country/gams/GamsTest.java @@ -3,6 +3,7 @@ package ac.ed.lurg.country.gams; import java.util.HashMap; import java.util.Map; +import ac.ed.lurg.ModelConfig; import ac.ed.lurg.country.CropAreas; import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; @@ -23,72 +24,50 @@ public class GamsTest implements GamsInputData { @Override public Map<CropType, Double> getProjectedDemand() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 100.0); + dummyMap.put(CropType.CEREALS, 300.0); dummyMap.put(CropType.FRUIT, 50.0); dummyMap.put(CropType.OILCROPS, 50.0); - dummyMap.put(CropType.STARCHY_ROOTS, 50.0); + dummyMap.put(CropType.STARCHY_ROOTS, 150.0); dummyMap.put(CropType.PULSES, 60.0); dummyMap.put(CropType.VEGETABLES, 20.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 80.0); + dummyMap.put(CropType.MEAT_OR_PASTURE, 480.0); dummyMap.put(CropType.TREENUTS, 10.0); return dummyMap; } - @Override - public Map<CropType, Double> getYieldNone() { + private Map<Integer, Map<CropType, Double>> getLocationCropMapForValue(double d) { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 1.0); - dummyMap.put(CropType.FRUIT, 1.0); - dummyMap.put(CropType.OILCROPS, 1.0); - dummyMap.put(CropType.STARCHY_ROOTS, 1.0); - dummyMap.put(CropType.PULSES, 1.0); - dummyMap.put(CropType.VEGETABLES, 1.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 1.0); - dummyMap.put(CropType.TREENUTS, 1.0); - return dummyMap; + for (CropType crop : CropType.getAllItems()) + dummyMap.put(crop, d); + + Map<Integer, Map<CropType, Double>> returnMap = new HashMap<Integer, Map<CropType, Double>>(); + + for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) + returnMap.put(i, dummyMap); + + return returnMap; } + - public Map<CropType, Double> getYieldFertOnly() { - Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 3.0); - dummyMap.put(CropType.FRUIT, 3.0); - dummyMap.put(CropType.OILCROPS, 3.0); - dummyMap.put(CropType.STARCHY_ROOTS, 3.0); - dummyMap.put(CropType.PULSES, 3.0); - dummyMap.put(CropType.VEGETABLES, 3.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 2.0); - dummyMap.put(CropType.TREENUTS, 3.0); - return dummyMap; + @Override + public Map<Integer, Map<CropType, Double>> getYieldNone() { + return getLocationCropMapForValue(1); } - public Map<CropType, Double> getYieldIrrigOnly() { - Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 2.0); - dummyMap.put(CropType.FRUIT, 2.0); - dummyMap.put(CropType.OILCROPS, 2.0); - dummyMap.put(CropType.STARCHY_ROOTS, 2.0); - dummyMap.put(CropType.PULSES, 2.0); - dummyMap.put(CropType.VEGETABLES, 2.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 2.0); - dummyMap.put(CropType.TREENUTS, 2.0); - return dummyMap; + public Map<Integer, Map<CropType, Double>> getYieldFertOnly() { + return getLocationCropMapForValue(3); } - public Map<CropType, Double> getYieldBoth() { - Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); - dummyMap.put(CropType.CEREALS, 4.0); - dummyMap.put(CropType.FRUIT, 4.0); - dummyMap.put(CropType.OILCROPS, 4.0); - dummyMap.put(CropType.STARCHY_ROOTS, 4.0); - dummyMap.put(CropType.PULSES, 4.0); - dummyMap.put(CropType.VEGETABLES, 4.0); - dummyMap.put(CropType.MEAT_OR_PASTURE, 4.0); - dummyMap.put(CropType.TREENUTS, 4.0); - return dummyMap; + public Map<Integer, Map<CropType, Double>> getYieldIrrigOnly() { + return getLocationCropMapForValue(2); + } + + public Map<Integer, Map<CropType, Double>> getYieldBoth() { + return getLocationCropMapForValue(3); } @Override - public Map<CropType, Double> getPreviousCropArea() { + public Map<Integer, Map<CropType, Double>> getPreviousCropArea() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); dummyMap.put(CropType.CEREALS, 9.0); dummyMap.put(CropType.FRUIT, 5.0); @@ -98,7 +77,13 @@ public class GamsTest implements GamsInputData { dummyMap.put(CropType.VEGETABLES, 20.0); dummyMap.put(CropType.MEAT_OR_PASTURE, 80.0); dummyMap.put(CropType.TREENUTS, 5.0); - return dummyMap; + + Map<Integer, Map<CropType, Double>> returnMap = new HashMap<Integer, Map<CropType, Double>>(); + + for (int i= 1; i<=ModelConfig.NUM_LOCATIONS_PER_COUNTRY; i++) + returnMap.put(i, dummyMap); + + return returnMap; } @Override @@ -174,22 +159,12 @@ public class GamsTest implements GamsInputData { } @Override - public Map<CropType, Double> getFertParam() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); - - for (CropType crop : CropType.getAllItems()) - dummyMaxExports.put(crop, 0.6); - - return dummyMaxExports; + public Map<Integer, Map<CropType, Double>> getFertParam() { + return getLocationCropMapForValue(0.6); } @Override - public Map<CropType, Double> getIrrigParam() { - Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); - - for (CropType crop : CropType.getAllItems()) - dummyMaxExports.put(crop, 0.5); - - return dummyMaxExports; + public Map<Integer, Map<CropType, Double>> getIrrigParam() { + return getLocationCropMapForValue(0.4); } }