Skip to content
Snippets Groups Projects
Commit 2661b436 authored by Peter Alexander's avatar Peter Alexander
Browse files

initial

parents
No related branches found
No related tags found
No related merge requests found
<?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.7"/>
<classpathentry kind="lib" path="/Applications/GAMS/gams24.3_osx_x64_64_sfx/apifiles/Java/api/GAMSJavaAPI.jar">
<attributes>
<attribute name="javadoc_location" value="http://www.gams.com/dd/docs/api/javadoc"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>UNPLUM</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
File added
package ac.ed.lurg;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.gams.api.GAMSDatabase;
import com.gams.api.GAMSJob;
import com.gams.api.GAMSOptions;
import com.gams.api.GAMSParameter;
import com.gams.api.GAMSVariable;
import com.gams.api.GAMSVariableRecord;
import com.gams.api.GAMSWorkspace;
import com.gams.api.GAMSWorkspaceInfo;
public class GamsTest {
public static void main(String[] args) {
// create GAMSWorkspace "ws" with default working directory
// (the directory named with current date and time under System.getProperty("java.io.tmpdir"))
System.out.println(System.getProperty("user.dir"));
File workingDirectory = new File(System.getProperty("user.dir"), "GamsTest");
workingDirectory.mkdir();
// create a workspace
GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
GAMSDatabase db = ws.addDatabase();
// prepare input data
Map<String, Double> previous_area = new HashMap<String, Double>();
{
previous_area.put("cropland", Double.valueOf(250.0));
previous_area.put("pasture", Double.valueOf(400.0));
}
Map<String, Double> demand = new HashMap<String, Double>();
{
demand.put("cereals", Double.valueOf(550.0));
demand.put("fruits", Double.valueOf(60.0));
demand.put("oilcrops", Double.valueOf(120.0));
demand.put("starchyRoots", Double.valueOf(100.0));
demand.put("treenuts", Double.valueOf(20.0));
demand.put("vegetables", Double.valueOf(60.0));
}
Map<String, Double> yield_ref = new HashMap<String, Double>();
{
yield_ref.put("cereals", Double.valueOf(3));
yield_ref.put("fruits", Double.valueOf(1));
yield_ref.put("oilcrops", Double.valueOf(1));
yield_ref.put("starchyRoots", Double.valueOf(10));
yield_ref.put("treenuts", Double.valueOf(2));
yield_ref.put("vegetables", Double.valueOf(7));
yield_ref.put("pasture", Double.valueOf(0.7));
}
// add a database and add input data into the database
GAMSParameter a = db.addParameter("previous_area", 1, "previous area");
for(String c : previous_area.keySet())
a.addRecord(c).setValue(previous_area.get(c));
GAMSParameter b = db.addParameter("demand", 1, "demand for crop");
for(String c : demand.keySet())
b.addRecord(c).setValue(demand.get(c));
GAMSParameter y = db.addParameter("yield_ref", 1, "ref yield t per ha");
for(String c : yield_ref.keySet())
y.addRecord(c).setValue(yield_ref.get(c));
// create GAMSJob "t2" from the "model" string variable
GAMSJob t2 = ws.addJobFromFile("/Users/peteralexander/Documents/R_Workspace/landalloc/GAMS/IntExtOpt.gms");
GAMSOptions opt = ws.addOptions();
System.out.println(db.getName());
opt.defines("gdxincname", db.getName());
t2.run(opt, db);
GAMSVariable varAreas = t2.OutDB().getVariable("area");
GAMSVariable varIntensities = t2.OutDB().getVariable("intensity");
for (GAMSVariableRecord rec : varAreas) {
System.out.println(String.format("%s: \tarea= %.0f, \tintensity= %.3f", rec.getKeys()[0], rec.getLevel(), varIntensities.findRecord(rec.getKeys()[0]).getLevel()));
// + " marginal=" + rec.getMarginal());
}
// cleanup GAMSWorkspace's working directory
cleanup(ws.workingDirectory());
// terminate program
System.exit(0);
}
static 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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment