Newer
Older
import ac.ed.lurg.landuse.CropUsageData;
import ac.ed.lurg.types.CommodityType;
import ac.ed.lurg.types.LandCoverType;
import ac.ed.lurg.utils.LazyHashMap;
import com.gams.api.GAMSException;
import com.gams.api.GAMSGlobals;
import com.gams.api.GAMSJob;
import com.gams.api.GAMSOptions;
import com.gams.api.GAMSParameter;
import com.gams.api.GAMSSet;
import com.gams.api.GAMSVariable;
import com.gams.api.GAMSVariableRecord;
import com.gams.api.GAMSWorkspace;
import com.gams.api.GAMSWorkspaceInfo;
public class GamsLocationOptimiser {
// DM kg / kg
// cereals 0.86
// oilcrops 0.90
// soy 0.89
// potatoes 0.21
private GamsLocationInput inputData;
public GamsLocationOptimiser(GamsLocationInput inputData) {
public GamsLocationOutput run() {
File workingDirectory = new File(ModelConfig.TEMP_DIR, "GamsTmp");
workingDirectory.mkdir();
GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
GAMSDatabase inDB = ws.addDatabase();
setupInDB(inDB);
GAMSJob gamsJob = ws.addJobFromFile(ModelConfig.GAMS_MODEL);
GAMSOptions opt = ws.addOptions();
opt.setAllModelTypes("conopt");
opt.defines("gdxincname", inDB.getName());
long startTime = System.currentTimeMillis();
gamsJob.run(opt, inDB);
if (ModelConfig.CLEANUP_GAMS_DIR)
cleanup(ws.workingDirectory());
LogWriter.println("Took " + (System.currentTimeMillis() - startTime) + " ms to run");
return handleResults(gamsJob.OutDB());
}
private void setupInDB(GAMSDatabase inDB) {
//if (DEBUG) LogWriter.println("\nLocation set");
GAMSSet locationSet = inDB.addSet("location", 1);
for (Integer locId : inputData.getPreviousAreas().keySet()) {
//if (DEBUG) LogWriter.println(" " + locId);
locationSet.addRecord(locId.toString());
}
if (DEBUG) LogWriter.println("\nPrevious crop and land areas");
GAMSParameter prevCropP = inDB.addParameter("previousArea", 2);
GAMSParameter landP = inDB.addParameter("suitableLandArea", 1);
for (Map.Entry<Integer, ? extends AreasItem> entry : inputData.getPreviousAreas().entrySet()) {
Integer locationId = entry.getKey();
double suitableLand = areasItem.getSuitableLand();
if (DEBUG) LogWriter.println(String.format(" %d %15s,\t %.1f", locationId, "suitableLand", suitableLand));
landP.addRecord(locString).setValue(suitableLand);
double pastureArea = areasItem.getLandCoverArea(LandCoverType.PASTURE);
if (DEBUG) LogWriter.println(String.format(" %d %15s,\t %.1f", locationId, "pastureArea", pastureArea));
setPreviousArea(prevCropP, locString, CropType.PASTURE.getGamsName(), pastureArea);
totalAgriLand += pastureArea;
for (CropType cropType : CropType.getCropsLessPasture()) {
if (DEBUG && d != 0) LogWriter.println(String.format(" %d %15s,\t %.1f", locationId, cropType.getGamsName(), d));
setPreviousArea(prevCropP, locString, cropType.getGamsName(), d);
if (DEBUG) LogWriter.println(String.format(" Total agricultural\t %.1f", totalAgriLand));
if (DEBUG) LogWriter.println("\nIrrigation cost");
GAMSParameter irrigCostP = inDB.addParameter("irrigCost", 1);
for (Entry<Integer, ? extends IrrigationCostItem> entry : inputData.getIrrigationCosts().entrySet()) {
Integer locationId = entry.getKey();
IrrigationCostItem irrigCostItem = entry.getValue();
double irrigCost = irrigCostItem.getIrrigCost();
if (DEBUG) LogWriter.println(String.format(" %d %15s,\t %.1f", locationId, "irrigCost", irrigCost));
irrigCostP.addRecord(Integer.toString(locationId)).setValue(irrigCost);
}
GamsCountryInput countryInput = inputData.getCountryInput();
addCommodityMapParm(inDB.addParameter("demand", 1), countryInput.getProjectedDemand());
if (DEBUG) LogWriter.println("\nYield (fert/irrig) None/None, Max/None, None/Max, Max/Max,\t [fert p],\t [irrig p]");
GAMSParameter yNoneP = inDB.addParameter("yieldNone", 2);
GAMSParameter y_fert = inDB.addParameter("yieldFertOnly", 2);
GAMSParameter y_irrig = inDB.addParameter("yieldIrrigOnly", 2);
GAMSParameter y_both = inDB.addParameter("yieldBoth", 2);
GAMSParameter fert_p = inDB.addParameter("fertParam", 2);
GAMSParameter irrig_p = inDB.addParameter("irrigParam", 2);
for (Entry<Integer, ? extends YieldResponsesItem> entry : inputData.getYields().entrySet()) {
Integer locationId = entry.getKey();
for (CropType crop : CropType.getNonMeatTypes()) {
if (DEBUG) LogWriter.println(String.format("%d %15s,\t %.1f,\t %.1f, \t %.1f,\t %.1f,\t\t [%.2f],\t [%.2f]",
locationId, crop.getGamsName(), yresp.getYieldNone(crop), yresp.getYieldFertOnly(crop), yresp.getYieldIrrigOnly(crop), yresp.getYieldMax(crop), yresp.getFertParam(crop), yresp.getIrrigParam(crop)));
yNoneP.addRecord(v).setValue(yresp.getYieldNone(crop));
y_fert.addRecord(v).setValue(yresp.getYieldFertOnly(crop));
y_irrig.addRecord(v).setValue(yresp.getYieldIrrigOnly(crop));
y_both.addRecord(v).setValue(yresp.getYieldMax(crop));
fert_p.addRecord(v).setValue(yresp.getFertParam(crop));
irrig_p.addRecord(v).setValue(yresp.getIrrigParam(crop));
if (DEBUG) LogWriter.println("\nWorld input prices");
addItemMapParm(inDB.addParameter("worldInputPrices", 1), countryInput.getWorldInputPrices());
if (DEBUG) LogWriter.println("\nMax Net Import");
addItemMapParm(inDB.addParameter("maxNetImport", 1), countryInput.getMaxNetImport());
if (DEBUG) LogWriter.println("\nMin Net Import");
addItemMapParm(inDB.addParameter("minNetImport", 1), countryInput.getMinNetImport());
addScalar(inDB.addParameter("meatEfficency", 0), countryInput.getMeatEfficiency());
if (DEBUG) LogWriter.println("\nMeatEfficiency: " + countryInput.getMeatEfficiency());
addScalar(inDB.addParameter("landChangeEnergy", 0), countryInput.getLandChangeEnergy());
if (DEBUG) LogWriter.println("\nLandChangeEnergy: " + countryInput.getLandChangeEnergy());
addScalar(inDB.addParameter("minFeedRate", 0), countryInput.getMinFeedRate());
if (DEBUG) LogWriter.println("\nMinFeedRate: " + countryInput.getMinFeedRate());
double fitToPreviousAreas = countryInput.isCalibrateToObserved() ? 1 : 0;
if (DEBUG) LogWriter.println("\nfitToPreviousAreas: " + fitToPreviousAreas);
addScalar(inDB.addParameter("fitToPreviousAreas", 0), fitToPreviousAreas);
if (DEBUG) LogWriter.println("\ncropAdj");
addItemMapParm(inDB.addParameter("cropAdj", 1), countryInput.getCropAdjustments());
private void setPreviousArea(GAMSParameter prevCropP, String locString, String cropTypeString, double d) {
Vector<String> v = new Vector<String>();
v.add(cropTypeString);
v.add(locString);
prevCropP.addRecord(v).setValue(d);
}
@SuppressWarnings("serial")
private GamsLocationOutput handleResults(GAMSDatabase outDB) {
int modelStatus = (int) outDB.getParameter("ms").findRecord().getValue();
LogWriter.println(String.format("\n%s %s: Modelstatus %s, Solvestatus %s",
inputData.getCountryInput().getCountry(),
inputData.getTimestep().getYear(),
GAMSGlobals.ModelStat.lookup( modelStatus ),
GAMSGlobals.SolveStat.lookup((int) outDB.getParameter("ss").findRecord().getValue()) ));
GAMSVariable varAreas = outDB.getVariable("area");
GAMSVariable varFertIntensities = outDB.getVariable("fertI");
GAMSVariable varIrrigIntensities = outDB.getVariable("irrigI");
GAMSVariable varOtherIntensities = outDB.getVariable("otherIntensity");
GAMSVariable varFeedAmount = outDB.getVariable("feedAmount");
GAMSVariable varNetImports = outDB.getVariable("netImportAmount");
GAMSVariable varYields = outDB.getVariable("yield");
GAMSVariable varUnitEnergies = outDB.getVariable("unitEnergy");
GAMSParameter parmCropAdj = outDB.getParameter("cropAdj");
GAMSParameter parmProd = outDB.getParameter("totalProd");
GAMSParameter parmProdCost = outDB.getParameter("totalProdCost");
GAMSParameter parmCroplandArea = outDB.getParameter("totalCropland");
double totalCropArea = 0;
double totalPastureArea = 0;
double area, fertIntensity, irrigIntensity, otherIntensity = Double.NaN, feedAmount, netImport, yield, unitEnergy, cropAdj, prod, prodCost;
LazyHashMap<Integer, IntensitiesItem> intensities = new LazyHashMap<Integer, IntensitiesItem>() {
protected IntensitiesItem createValue() { return new IntensitiesItem(); }
};
LazyHashMap<Integer, AreasItem> cropAreas = new LazyHashMap<Integer, AreasItem>() {
protected AreasItem createValue() { return new AreasItem(); }
};
Map<CropType, CropUsageData> cropUsageData = new HashMap<CropType, CropUsageData>();
Map<CropType, Double> cropAdjs = new HashMap<CropType, Double>();
String locationName = rec.getKeys()[1];
fertIntensity = varFertIntensities.findRecord(itemName, locationName).getLevel();
irrigIntensity = varIrrigIntensities.findRecord(itemName, locationName).getLevel();
otherIntensity = varOtherIntensities.findRecord(itemName, locationName).getLevel();
yield = varYields.findRecord(itemName, locationName).getLevel();
unitEnergy = varUnitEnergies.findRecord(itemName, locationName).getLevel();
int locId = Integer.parseInt(locationName);
CropType cropType = CropType.getForGamsName(itemName);
if (!cropUsageData.containsKey(cropType)) { // then we must not have seen this crop type before, so need to do all non location specific stuff
feedAmount = varFeedAmount.findRecord(itemName).getLevel();
netImport = cropType.isImportedCrop() ? varNetImports.findRecord(itemName).getLevel() : 0;
cropAdj = getParmValue(parmCropAdj, itemName);
prod = getParmValue(parmProd, itemName);
prodCost = getParmValue(parmProdCost, itemName);
cropUsageData.put(cropType, new CropUsageData(feedAmount, netImport, prod, prodCost));
if (DEBUG) LogWriter.println(String.format("\n%s:\tfeedAmount= %.1f,\tnetImports= %.3f,\tprod= %.3f,\tprodCost= %.3f,\tcropAdj= %.3f", itemName, feedAmount, netImport, prod, prodCost, cropAdj));
if (DEBUG) LogWriter.println(String.format("\t location %s, %s:\tarea= %.1f,\tfert= %.3f,\tirrg= %.3f,\tintensity= %.3f", locationName, itemName, area, fertIntensity, irrigIntensity, otherIntensity));
IntensitiesItem intensityItem = intensities.lazyGet(locId);
intensityItem.setIntensity(cropType, new Intensity(fertIntensity, irrigIntensity, otherIntensity, yield, unitEnergy));
double croplandArea = getParmValue(parmCroplandArea, locationName);
AreasItem areasItem = cropAreas.lazyGet(locId);
areasItem.setLandCoverArea(LandCoverType.PASTURE, area);
areasItem.setLandCoverArea(LandCoverType.CROPLAND, croplandArea); // will set this multiple times, once for each arable crop, but doesn't really matter
areasItem.setCropFraction(cropType, croplandArea > 0 ? area/croplandArea : 0);
netImport = varNetImports.findRecord(CropType.MEAT.getGamsName()).getLevel();
prod = getParmValue(parmProd, CropType.MEAT.getGamsName());
prodCost = getParmValue(parmProdCost, CropType.MEAT.getGamsName());
cropUsageData.put(CropType.MEAT, new CropUsageData(0.0, netImport, prod, prodCost));
LogWriter.println(String.format("\n%s:\t\t\t\t\tnetImports= %.3f,\tprod= %.3f,\tprodCost= %.3f", CropType.MEAT.getGamsName(), netImport, prod, prodCost));
LogWriter.println(String.format("\nTotal area= %.1f (crop=%.1f, pasture %.1f)", totalCropArea+totalPastureArea, totalCropArea, totalPastureArea));
GamsLocationOutput results = new GamsLocationOutput(modelStatus, intensities, cropAreas, cropUsageData, cropAdjs);
private double getParmValue(GAMSParameter aParm, String itemName) {
try {
double d = aParm.findRecord(itemName).getValue();
return d;
}
catch (GAMSException gamsEx) {
//LogWriter.println("GAMSException thrown for " + itemName);
return 0;
}
}
private void addScalar(GAMSParameter param, double val) {
param.addRecord().setValue(val);
}
private void addItemMapParm(GAMSParameter parm, Map<CropType, Double> itemMap) {
for (Map.Entry<CropType, Double> entry : itemMap.entrySet()) {
if (DEBUG) LogWriter.println(String.format(" %15s,\t %.3f", entry.getKey().getGamsName(), d));
if (!Double.isNaN(d))
parm.addRecord(entry.getKey().getGamsName()).setValue(d);
private void addCommodityMapParm(GAMSParameter parm, Map<CommodityType, Double> itemMap) {
for (Map.Entry<CommodityType, Double> entry : itemMap.entrySet()) {
double d = entry.getValue();
if (DEBUG) LogWriter.println(String.format(" %15s,\t %.1f", entry.getKey().getGamsName(), d));
parm.addRecord(entry.getKey().getGamsName()).setValue(d);
private void cleanup(String directory) {
File directoryToDelete = new File(directory);
String files[] = directoryToDelete.list();
for (String file : files) {
File fileToDelete = new File(directoryToDelete, file);
try {
fileToDelete.delete();
} catch(Exception e){
e.printStackTrace();
}
}
try {
directoryToDelete.delete();
} catch(Exception e) {
e.printStackTrace();
}
}