From 4f61ff2ea173ed8eed51337846289bc02de1d31e Mon Sep 17 00:00:00 2001 From: Peter Alexander <peter@blackhillock.co.uk> Date: Wed, 10 Dec 2014 17:46:59 +0000 Subject: [PATCH] no message --- src/ac/ed/lurg/country/CountryAgent.java | 48 ++---------- .../lurg/country/gams/GamsCountryInput.java | 75 ++++++++++++++++--- src/ac/ed/lurg/country/gams/GamsInput.java | 25 ++++++- .../country/gams/GamsLandUseOptimiser.java | 2 +- .../ed/lurg/country/gams/GamsRasterInput.java | 27 ++++++- .../country/gams/GamsRasterOptimiser.java | 20 ++--- src/ac/ed/lurg/country/gams/GamsTest.java | 50 ++----------- 7 files changed, 131 insertions(+), 116 deletions(-) diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java index 8bcdf334..c0a79f62 100644 --- a/src/ac/ed/lurg/country/CountryAgent.java +++ b/src/ac/ed/lurg/country/CountryAgent.java @@ -13,7 +13,7 @@ import ac.ed.lurg.types.CropType; import ac.ed.lurg.utils.LogWriter; import ac.ed.lurg.yield.YieldResponses; -public class CountryAgent implements GamsInput, GamsCountryInput { +public class CountryAgent { private ModelContext modelContext; private Country country; @@ -58,19 +58,20 @@ public class CountryAgent implements GamsInput, GamsCountryInput { currentRefYield = modelContext.getYieldManager().getRefYield(country, year); // optimise areas and intensity - GamsLandUseOptimiser opti = new GamsLandUseOptimiser(this); + GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); + GamsInput gamsInput = new GamsInput(getYields(), getPreviousCropArea(), countryLevelInputs); + + GamsLandUseOptimiser opti = new GamsLandUseOptimiser(gamsInput); LogWriter.println("Running " + country.getCountryName() + ", year " + year); GamsOutput result = opti.run(); // cropAreasTimeseries.put(timestep, result); } - @Override public Map<CropType, Double> getProjectedDemand() { return currentProjectedDemand; } - @Override public Map<Integer, YieldResponses> getYields() { // Map<Integer, Map<CropType, YieldResponse>> returnMap = new HashMap<Integer, Map<CropType, YieldResponse>>(); @@ -80,7 +81,6 @@ public class CountryAgent implements GamsInput, GamsCountryInput { return null; // this should be from LPJ data } - @Override public Map<Integer, Map<CropType, Double>> getPreviousCropArea() { Map<CropType, Double> previousCropAreas = new HashMap<CropType, Double>(); int previousTimestep = currentTimestep==0 ? 0 : currentTimestep-1; @@ -97,7 +97,6 @@ public class CountryAgent implements GamsInput, GamsCountryInput { return returnMap; } - @Override public Map<CropType, Double> getWorldInputEnergy() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); dummyMap.put(CropType.CEREALS, 9.0); @@ -111,7 +110,6 @@ public class CountryAgent implements GamsInput, GamsCountryInput { return dummyMap; } - @Override public Map<CropType, Double> getMaxNetImport() { Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); dummyMaxExports.put(CropType.CEREALS, 24.0); @@ -125,7 +123,6 @@ public class CountryAgent implements GamsInput, GamsCountryInput { return dummyMaxExports; } - @Override public Map<CropType, Double> getMinNetImport() { Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); dummyMaxExports.put(CropType.CEREALS, -24.0); @@ -138,39 +135,4 @@ public class CountryAgent implements GamsInput, GamsCountryInput { dummyMaxExports.put(CropType.TREENUTS, -5.0); return dummyMaxExports; } - - @Override - public double getMeatEfficiency() { - return 1; // this is already handled by the feed conversion efficiency for each animal product - } - - @Override - public double getMaxLandUseChange() { - return 0.05; - } - - @Override - public double getMaxIntensity() { - return 29.0; - } - - @Override - public double getTradeBarrier() { - return 2; - } - - @Override - public double getLandChangeEnergy() { - return 0.1; - } - - @Override - public double getMinFeedRate() { - return 0.15; - } - - @Override - public GamsCountryInput getCountryInput() { - return this; - } } \ No newline at end of file diff --git a/src/ac/ed/lurg/country/gams/GamsCountryInput.java b/src/ac/ed/lurg/country/gams/GamsCountryInput.java index eca14dab..2c0111d3 100644 --- a/src/ac/ed/lurg/country/gams/GamsCountryInput.java +++ b/src/ac/ed/lurg/country/gams/GamsCountryInput.java @@ -4,20 +4,73 @@ import java.util.Map; import ac.ed.lurg.types.CropType; -public interface GamsCountryInput { +public class GamsCountryInput { - Map<CropType, Double> getProjectedDemand(); - Map<CropType, Double> getWorldInputEnergy(); - Map<CropType, Double> getMaxNetImport(); - Map<CropType, Double> getMinNetImport(); + private Map<CropType, Double> projectedDemand; + private Map<CropType, Double> worldInputEnergy; + private Map<CropType, Double> maxNetImport; + private Map<CropType, Double> minNetImport; // limits to areas for each location and crop - double getMaxLandUseChange(); - double getMeatEfficiency(); - double getMaxIntensity(); - double getMinFeedRate(); - double getTradeBarrier(); - double getLandChangeEnergy(); +/* private double maxLandUseChange; + private double meatEfficiency; + private double maxIntensity; + private double minFeedRate; + private double tradeBarrier; + private double landChangeEnergy;*/ + + + public GamsCountryInput(Map<CropType, Double> projectedDemand, Map<CropType, Double> worldInputEnergy, + Map<CropType, Double> maxNetImport, Map<CropType, Double> minNetImport) { + super(); + this.projectedDemand = projectedDemand; + this.worldInputEnergy = worldInputEnergy; + this.maxNetImport = maxNetImport; + this.minNetImport = minNetImport; + } + + + public Map<CropType, Double> getProjectedDemand() { + return projectedDemand; + } + + + public Map<CropType, Double> getWorldInputEnergy() { + return worldInputEnergy; + } + + + public Map<CropType, Double> getMaxNetImport() { + return maxNetImport; + } + + + public Map<CropType, Double> getMinNetImport() { + return minNetImport; + } + + public double getMeatEfficiency() { + return 1; // this is already handled by the feed conversion efficiency for each animal product + } + + public double getMaxLandUseChange() { + return 0.05; + } + + public double getMaxIntensity() { + return 29.0; + } + + public double getTradeBarrier() { + return 4; + } + + public double getLandChangeEnergy() { + return 0.1; + } + public double getMinFeedRate() { + return 0.15; + } } diff --git a/src/ac/ed/lurg/country/gams/GamsInput.java b/src/ac/ed/lurg/country/gams/GamsInput.java index c2b77650..787b9910 100644 --- a/src/ac/ed/lurg/country/gams/GamsInput.java +++ b/src/ac/ed/lurg/country/gams/GamsInput.java @@ -5,10 +5,27 @@ import java.util.Map; import ac.ed.lurg.types.CropType; import ac.ed.lurg.yield.YieldResponses; -public interface GamsInput { +public class GamsInput { - Map<Integer, YieldResponses> getYields(); - Map<Integer, Map<CropType, Double>> getPreviousCropArea(); + private Map<Integer, ? extends YieldResponses> yields; + private Map<Integer, Map<CropType, Double>> previousCropArea; + private GamsCountryInput countryInput; + + public GamsInput(Map<Integer, ? extends YieldResponses> yields, Map<Integer, Map<CropType, Double>> previousCropArea, GamsCountryInput countryInput) { + super(); + this.yields = yields; + this.previousCropArea = previousCropArea; + this.countryInput = countryInput; + } - GamsCountryInput getCountryInput(); + public Map<Integer, ? extends YieldResponses> getYields() { + return yields; + } + public Map<Integer, Map<CropType, Double>> getPreviousCropArea() { + return previousCropArea; + } + + public GamsCountryInput getCountryInput() { + return countryInput; + } } diff --git a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java index d5a5835c..ad0ff148 100644 --- a/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsLandUseOptimiser.java @@ -73,7 +73,7 @@ public class GamsLandUseOptimiser { GAMSParameter fert_p = inDB.addParameter("fertParam", 2); GAMSParameter irrig_p = inDB.addParameter("irrigParam", 2); - for (Entry<Integer, YieldResponses> cropsForALocation : inputData.getYields().entrySet()) { + for (Entry<Integer, ? extends YieldResponses> cropsForALocation : inputData.getYields().entrySet()) { Integer locationId = cropsForALocation.getKey(); YieldResponses yresp = cropsForALocation.getValue(); diff --git a/src/ac/ed/lurg/country/gams/GamsRasterInput.java b/src/ac/ed/lurg/country/gams/GamsRasterInput.java index 1292d3b0..a2a7fdc3 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterInput.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterInput.java @@ -4,10 +4,29 @@ import ac.ed.lurg.landuse.LandUse; import ac.ed.lurg.yield.YieldRaster; import ac.sac.raster.RasterSet; -public interface GamsRasterInput { +public class GamsRasterInput { - YieldRaster getYields(); - RasterSet<LandUse> getPreviousLandUse(); + private YieldRaster yields; + private RasterSet<LandUse> previousLandUse; + private GamsCountryInput countryInput; - GamsCountryInput getCountryInput(); + public GamsRasterInput(YieldRaster yields, + RasterSet<LandUse> previousLandUse, GamsCountryInput countryInput) { + super(); + this.yields = yields; + this.previousLandUse = previousLandUse; + this.countryInput = countryInput; + } + + public YieldRaster getYields() { + return yields; + } + + public RasterSet<LandUse> getPreviousLandUse() { + return previousLandUse; + } + + public GamsCountryInput getCountryInput() { + return countryInput; + } } diff --git a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java index 74877b74..39bea158 100644 --- a/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java +++ b/src/ac/ed/lurg/country/gams/GamsRasterOptimiser.java @@ -17,22 +17,24 @@ import ac.sac.raster.RasterKey; public class GamsRasterOptimiser { - private GamsRasterInput inputData; + private GamsRasterInput rasterInputData; - public GamsRasterOptimiser(GamsRasterInput inputData) { - this.inputData = inputData; + public GamsRasterOptimiser(GamsRasterInput rasterInputData) { + this.rasterInputData = rasterInputData; } public GamsRasterOutput run() { - // workout similar areas - clustering multi-dimensional data - YieldRaster yieldRaster = inputData.getYields(); - - Map<Integer, AveragingYieldResponses> mapping = findSimilarAreas(yieldRaster); + // workout similar areas + Map<Integer, AveragingYieldResponses> mapping = findSimilarAreas(rasterInputData.getYields()); + Map<Integer, Map<CropType, Double>> dummy = null; // create GamsInput using mapping - + GamsInput gamsInput = new GamsInput(mapping, dummy, rasterInputData.getCountryInput()); + // run optimizer - + GamsLandUseOptimiser opti = new GamsLandUseOptimiser(gamsInput); + GamsOutput gamsOutput = opti.run(); + // map results back to raster return null; diff --git a/src/ac/ed/lurg/country/gams/GamsTest.java b/src/ac/ed/lurg/country/gams/GamsTest.java index e5e1c8d4..c72ca4b3 100644 --- a/src/ac/ed/lurg/country/gams/GamsTest.java +++ b/src/ac/ed/lurg/country/gams/GamsTest.java @@ -8,7 +8,7 @@ import ac.ed.lurg.types.CropType; import ac.ed.lurg.types.YieldType; import ac.ed.lurg.yield.YieldResponses; -public class GamsTest implements GamsInput, GamsCountryInput { +public class GamsTest { public static void main(String[] args) { GamsTest aGamsTest = new GamsTest(); @@ -16,11 +16,13 @@ public class GamsTest implements GamsInput, GamsCountryInput { } private void run() { - GamsLandUseOptimiser opti = new GamsLandUseOptimiser(this); - opti.run(); + GamsCountryInput countryLevelInputs = new GamsCountryInput(getProjectedDemand(), getWorldInputEnergy(), getMaxNetImport(), getMinNetImport()); + GamsInput gamsInput = new GamsInput(getYields(), getPreviousCropArea(), countryLevelInputs); + + GamsLandUseOptimiser opti = new GamsLandUseOptimiser(gamsInput); + GamsOutput gamsOutput = opti.run(); } - @Override public Map<CropType, Double> getProjectedDemand() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); dummyMap.put(CropType.CEREALS, 300.0); @@ -35,7 +37,6 @@ public class GamsTest implements GamsInput, GamsCountryInput { } - @Override public Map<Integer, YieldResponses> getYields() { YieldResponses yresp = new YieldResponses(); @@ -57,7 +58,6 @@ public class GamsTest implements GamsInput, GamsCountryInput { return returnMap; } - @Override public Map<Integer, Map<CropType, Double>> getPreviousCropArea() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); dummyMap.put(CropType.CEREALS, 9.0); @@ -77,7 +77,6 @@ public class GamsTest implements GamsInput, GamsCountryInput { return returnMap; } - @Override public Map<CropType, Double> getWorldInputEnergy() { Map<CropType, Double> dummyMap = new HashMap<CropType, Double>(); dummyMap.put(CropType.CEREALS, 0.4); @@ -91,7 +90,6 @@ public class GamsTest implements GamsInput, GamsCountryInput { return dummyMap; } - @Override public Map<CropType, Double> getMaxNetImport() { Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); dummyMaxExports.put(CropType.CEREALS, 24.0); @@ -105,7 +103,6 @@ public class GamsTest implements GamsInput, GamsCountryInput { return dummyMaxExports; } - @Override public Map<CropType, Double> getMinNetImport() { Map<CropType, Double> dummyMaxExports = new HashMap<CropType, Double>(); dummyMaxExports.put(CropType.CEREALS, -24.0); @@ -118,39 +115,4 @@ public class GamsTest implements GamsInput, GamsCountryInput { dummyMaxExports.put(CropType.TREENUTS, -5.0); return dummyMaxExports; } - - @Override - public double getMeatEfficiency() { - return 1; // this is already handled by the feed conversion efficiency for each animal product - } - - @Override - public double getMaxLandUseChange() { - return 0.05; - } - - @Override - public double getMaxIntensity() { - return 29.0; - } - - @Override - public double getTradeBarrier() { - return 4; - } - - @Override - public double getLandChangeEnergy() { - return 0.1; - } - - @Override - public double getMinFeedRate() { - return 0.15; - } - - @Override - public GamsCountryInput getCountryInput() { - return this; - } } -- GitLab