From 04e32681387b52fc3df7e242a6a3e9ab4eaf9bb1 Mon Sep 17 00:00:00 2001 From: s1924442 <b.arendarczyk@sms.ed.ac.uk> Date: Sat, 20 Feb 2021 22:07:28 +0000 Subject: [PATCH] Bug fixes: GAMS database landcover inputs, GAMS syntax errors --- GAMS/IntExtOpt.gms | 4 +- src/ac/ed/lurg/ModelMain.java | 2 + .../country/gams/GamsLocationOptimiser.java | 33 ++++++++++-- .../country/gams/GamsRasterOptimiser.java | 51 ++++++++++++------- src/ac/ed/lurg/landuse/LandUseItem.java | 17 ++++++- 5 files changed, 83 insertions(+), 24 deletions(-) diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms index ea7633f8..66115fcb 100644 --- a/GAMS/IntExtOpt.gms +++ b/GAMS/IntExtOpt.gms @@ -142,7 +142,7 @@ $gdxin POSITIVE VARIABLE cropArea, fertI, irrigI, otherIntensity, ruminantFeed, monogastricFeed, importAmount, exportAmount, agriLandExpansion, cropIncrease, cropDecrease, pastureDecrease, pastureIncrease, totalFeedDM, - landCoverArea, landCoverChange, woodHarvest, carbonFlux; + landCoverArea, landCoverChange, woodHarvest; EQUATIONS UNIT_COST_EQ(crop, location) cost per area @@ -244,7 +244,7 @@ $gdxin LAND_COVER_CHANGE_CONSTRAINT(land_cover, location) .. sum(land_cover_after, landCoverChange(land_cover, land_cover_after, location)) =L= previousLandCoverArea(land_cover, location); -* TOTAL_LAND_COVER_CONSTRAINT(location) .. sum(land_cover, landCoverArea(land_cover, location)) =E= suitableArea(location); +* TOTAL_LAND_COVER_CONSTRAINT(location) .. sum(land_cover, landCoverArea(land_cover, location)) =E= suitableLandArea(location); WOOD_HARVEST_CALC(location) .. woodHarvest(location) =E= sum((land_cover_before, land_cover_after), landCoverChange(land_cover_before, land_cover_after, location) * woodYield(land_cover_before, land_cover_after, location)); diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java index e01d8ac4..0ae21ecd 100644 --- a/src/ac/ed/lurg/ModelMain.java +++ b/src/ac/ed/lurg/ModelMain.java @@ -633,12 +633,14 @@ public class ModelMain { for (Map.Entry<RasterKey, LandCoverItem> entry : initialLC.entrySet()) landUseRaster.put(entry.getKey(), new LandUseItem(entry.getValue())); + /* // Set initial GAMS land covers for (LandUseItem item : landUseRaster.values()) { item.setLandCoverArea(LandCoverType.TIMBER_FOREST, item.getLandCoverArea(LandCoverType.MANAGED_FOREST)); item.setLandCoverArea(LandCoverType.CARBON_FOREST, 0.0); item.setLandCoverArea(LandCoverType.NATURAL, item.getLandCoverArea(LandCoverType.OTHER_NATURAL) + item.getLandCoverArea(LandCoverType.UNMANAGED_FOREST)); } + */ return landUseRaster; } diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java index 616dc301..c8278cd1 100644 --- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java @@ -173,7 +173,7 @@ public class GamsLocationOptimiser { Vector<String> v = new Vector<String>(); v.add(lc.getName()); v.add(locString); - setGamsParamValue(prevLandCoverP.addRecord(v), landUseItem.getLandCoverArea(lc), 3); + setGamsParamValue(prevLandCoverP.addRecord(v), landUseItem.getUnprotectedLandCoverArea(lc), 3); } } @@ -415,7 +415,7 @@ public class GamsLocationOptimiser { if (modelStatus != ModelStat.OPTIMAL_LOCAL) LogWriter.printlnError("Critical!!! Land use incorrectly solved. " + contextString); - GAMSVariable varAreas = outDB.getVariable("area"); + GAMSVariable varAreas = outDB.getVariable("cropArea"); GAMSVariable varFertIntensities = outDB.getVariable("fertI"); GAMSVariable varIrrigIntensities = outDB.getVariable("irrigI"); GAMSVariable varOtherIntensities = outDB.getVariable("otherIntensity"); @@ -549,6 +549,33 @@ public class GamsLocationOptimiser { double change = rec.getLevel(); + if (landCoverChange.containsKey(locId)) { + Map<LandCoverType, Map<LandCoverType, Double>> locMap = landCoverChange.get(locId); + + if (locMap.containsKey(LandCoverType.getForName(fromLC))) { + locMap.get(LandCoverType.getForName(fromLC)).put(LandCoverType.getForName(toLC), change); + } + else { + Map<LandCoverType, Double> toMap = new HashMap<LandCoverType, Double>(); + toMap.put(LandCoverType.getForName(toLC), change); + + //Map<LandCoverType, Map<LandCoverType, Double>> fromMap = new HashMap<LandCoverType, Map<LandCoverType, Double>>(); + //fromMap.put(LandCoverType.getForName(fromLC), toMap); + + landCoverChange.get(locId).put(LandCoverType.getForName(fromLC), toMap); + } + } + else { + Map<LandCoverType, Double> toMap = new HashMap<LandCoverType, Double>(); + toMap.put(LandCoverType.getForName(toLC), change); + + Map<LandCoverType, Map<LandCoverType, Double>> fromMap = new HashMap<LandCoverType, Map<LandCoverType, Double>>(); + fromMap.put(LandCoverType.getForName(fromLC), toMap); + + landCoverChange.put(locId, fromMap); + } + + /* Map<LandCoverType, Double> toMap = new HashMap<LandCoverType, Double>(); toMap.put(LandCoverType.getForName(toLC), change); @@ -556,7 +583,7 @@ public class GamsLocationOptimiser { fromMap.put(LandCoverType.getForName(fromLC), toMap); landCoverChange.put(locId, fromMap); - + */ } GamsLocationOutput results = new GamsLocationOutput(modelStatus, landUses, cropUsageData, landCoverChange); diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java index f5f18ff4..a0b8df7d 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -271,10 +271,7 @@ public class GamsRasterOptimiser { } - - - return newLandUseRaster; } @@ -532,26 +529,44 @@ public class GamsRasterOptimiser { //Suitable areas aggLandUse.setSuitableArea(suitableAreaSoFar + suitableAreaThisTime); - // Land covers ares + + // Land covers areas for (LandCoverType landType : LandCoverType.values()) { - double areaThisTime = landUseItem.getLandCoverArea(landType); - double areaSoFar = aggLandUse.getLandCoverArea(landType); - aggLandUse.setLandCoverArea(landType, areaSoFar + areaThisTime); - } + double areaThisTime = landUseItem.getUnprotectedLandCoverArea(landType); + double areaSoFar = aggLandUse.getUnprotectedLandCoverArea(landType); - /* - // Gams land cover areas - for (GamsLandCoverType landType : GamsLandCoverType.values()) { - double areaThisTime = landUseItem.getGamsLandCoverArea(landType); - double areaSoFar = aggLandUse.getGamsLandCoverArea(landType); - aggLandUse.setGamsLandCoverArea(landType, areaSoFar + areaThisTime); + aggLandUse.setUnprotectedLandCoverArea(landType, areaSoFar + areaThisTime); } - */ + + + } - } - + } + /* + for (Entry<RasterKey, LandUseItem> entry : rasterInputData.getPreviousLandUses().entrySet()) { + LandUseItem landUseItem = entry.getValue(); + RasterKey key = entry.getKey(); + int clusterId = mapping.get(key).getInt(); + LandUseItem aggLandUse = aggregatedAreas.lazyGet(clusterId); + + // Land covers areas + for (LandCoverType landType : LandCoverType.values()) { + double areaThisTime = landUseItem.getUnprotectedLandCoverArea(landType); + double areaSoFar = aggLandUse.getUnprotectedLandCoverArea(landType); + + aggLandUse.setLandCoverArea(landType, areaSoFar + areaThisTime); + } + } + */ +/* + LogWriter.println("potato" + rasterInputData.getCountryInput().getCountry().getName()); + if (rasterInputData.getCountryInput().getCountry().getName().equals("Central America")) { + LogWriter.println("foo"); + } +*/ LogWriter.println("YieldResponsesItem: " + rasterInputData.getCountryInput().getCountry() + ", countFound=" + countFound + ", countMissing=" + countMissing); - + + // for (Map.Entry<Integer, Set<RasterKey>> e : mapping.entrySet()) { // LogWriter.println(e.getKey() + " zone has " + e.getValue().size() + " raster areas"); diff --git a/src/ac/ed/lurg/landuse/LandUseItem.java b/src/ac/ed/lurg/landuse/LandUseItem.java index f30e7b06..ad974d34 100644 --- a/src/ac/ed/lurg/landuse/LandUseItem.java +++ b/src/ac/ed/lurg/landuse/LandUseItem.java @@ -32,6 +32,10 @@ public class LandUseItem implements InterpolatingRasterItem<LandUseItem>, Serial for (LandCoverType lcType : LandCoverType.values()) landCoverAreas.put(lcType, landCover.getLandCoverArea(lcType)); + landCoverAreas.put(LandCoverType.TIMBER_FOREST, landCover.getLandCoverArea(LandCoverType.MANAGED_FOREST)); + landCoverAreas.put(LandCoverType.CARBON_FOREST, 0.0); + landCoverAreas.put(LandCoverType.NATURAL, landCover.getLandCoverArea(LandCoverType.OTHER_NATURAL) + landCover.getLandCoverArea(LandCoverType.UNMANAGED_FOREST)); + setCropFraction(CropType.WHEAT, 0.5); // random start as don't have better data setCropFraction(CropType.MAIZE, 0.5); setUnavailableArea(landCover.getUnavailableFract()); @@ -296,7 +300,13 @@ public class LandUseItem implements InterpolatingRasterItem<LandUseItem>, Serial } public double getUnprotectedLandCoverArea(LandCoverType landType) { - return unprotectedAreas.get(landType); + Double d = unprotectedAreas.get(landType); + return d == null ? 0.0 : d; + } + + public void setUnprotectedLandCoverArea(LandCoverType landType, double d) { + unprotectedAreas.put(landType, d); + } private double getProtectedandUnavailable() { @@ -427,6 +437,11 @@ public class LandUseItem implements InterpolatingRasterItem<LandUseItem>, Serial return total; } + /* + public boolean checkForUnprotected(LandCoverType landCover) { + return unprotectedAreas.containsKey(landCover); + } + */ /* public double getGamsLandCoverArea(GamsLandCoverType landCoverType) { return gamsLandCoverAreas.get(landCoverType); -- GitLab