Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PLUM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Land Use Lab
PLUM
Commits
c64eb11b
Commit
c64eb11b
authored
10 years ago
by
Peter Alexander
Browse files
Options
Downloads
Patches
Plain Diff
Working, if you forget about imports/exports
parent
5adb831e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
GAMS/IntExtOpt.gms
+26
-22
26 additions, 22 deletions
GAMS/IntExtOpt.gms
src/ac/ed/lurg/GamsTest.java
+87
-93
87 additions, 93 deletions
src/ac/ed/lurg/GamsTest.java
with
113 additions
and
115 deletions
GAMS/IntExtOpt.gms
+
26
−
22
View file @
c64eb11b
SET crop crop types including pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables, pasture /
SET crop_less_pasture(crop) arable crops types includes treenuts but not pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables /
SET feed_crop(crop) / cereals, oilcrops /
SET landuse land use type / cropland, pasture /
SET crop crop types including pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables, pasture /
;
SET crop_less_pasture(crop) arable crops types includes treenuts but not pasture / cereals, fruits, oilcrops, starchyRoots, treenuts, vegetables /
;
SET feed_crop(crop) / cereals, oilcrops /
;
SET landuse land use type / cropland, pasture /
;
PARAMETER previous_area(landuse) areas for previous timestep in ha
PARAMETER demand(crop) yield in t per ha
PARAMETER yield_ref(crop) yield in t per ha
PARAMETER previous_area(landuse) areas for previous timestep in ha;
PARAMETER demand(crop) yield in t per ha;
PARAMETER yield_ref(crop) yield in t per ha;
SCALAR meatDemand;
SCALAR meatEfficency;
SCALAR maxLandUseChange;
SCALAR maxIntensity;
$gdxin %gdxincname%
$load previous_area, demand, yield_ref
$load previous_area, demand, yield_ref
, meatDemand, meatEfficency, maxLandUseChange, maxIntensity
$gdxin
PARAMETER
feedEnergy(crop) energy from feed in MJ per t
PARAMETER feedEnergy(crop) energy from feed in MJ per t
/ cereals 1
oilcrops 1.5
pasture 0.4
/ ;
SCALAR meatDemand /200/;
SCALAR meatEfficency /0.1/;
SCALAR maxLandUseChange /0.1/;
VARIABLES
area(crop) total area for each crop
feedArea(crop) area used for feed for each crop
intensity(crop) intensity for each crop
yield(crop)
energy
per area for each crop
yield(crop)
yield
per area for each crop
unit_energy(crop) energy per area for each crop
energy total input energy
;
energy total input energy;
POSITIVE VARIABLE area, intensity, feedArea;
...
...
@@ -40,7 +38,8 @@ $gdxin
YIELD_EQ(crop) yield given chosen intensity
CROP_DEMAND_CONSTRAINT(crop_less_pasture) satisfy demand for crop
MEAT_DEMAND_CONSTRAINT satisfy demand for crop
INTENSITY_CONSTRAINT(crop) intensity constraint
MIN_INTENSITY_CONSTRAINT(crop) intensity constraint
MAX_INTENSITY_CONSTRAINT(crop) constraint on maximum intensity which will tend to rise over time
CROPLAND_CHANGE_CONSTRAINT constraint on the rate of land use change
PASTURE_CHANGE_CONSTRAINT constraint on the rate of land use change
AGRI_LAND_CHANGE_CONSTRAINT constraint expansion of agricultural land
...
...
@@ -55,7 +54,9 @@ $gdxin
MEAT_DEMAND_CONSTRAINT .. meatEfficency*sum(feed_crop, feedEnergy(feed_crop) * feedArea(feed_crop) * yield(feed_crop)) +
feedEnergy('pasture') * area('pasture') * yield('pasture') =G= meatDemand;
INTENSITY_CONSTRAINT(crop) .. intensity(crop) =G= 1;
MIN_INTENSITY_CONSTRAINT(crop) .. intensity(crop) =G= 1;
MAX_INTENSITY_CONSTRAINT(crop) .. intensity(crop) =L= maxIntensity;
CROPLAND_CHANGE_CONSTRAINT .. abs(sum(crop_less_pasture, area(crop_less_pasture))/previous_area('cropland') - 1) =L= maxLandUseChange;
...
...
@@ -67,4 +68,7 @@ $gdxin
MODEL LAND_USE /ALL/ ;
SOLVE LAND_USE USING DNLP MINIMIZING energy;
Scalar ms 'model status', ss 'solve status';
ms=LAND_USE.modelstat;
ss=LAND_USE.solvestat;
This diff is collapsed.
Click to expand it.
src/ac/ed/lurg/GamsTest.java
+
87
−
93
View file @
c64eb11b
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.GAMSGlobals
;
import
com.gams.api.GAMSJob
;
import
com.gams.api.GAMSOptions
;
import
com.gams.api.GAMSParameter
;
...
...
@@ -14,97 +13,92 @@ import com.gams.api.GAMSWorkspace;
import
com.gams.api.GAMSWorkspaceInfo
;
public
class
GamsTest
{
private
final
static
String
WORKING_DIR
=
"/Users/peteralexander/Documents/R_Workspace/tempDir"
;
public
static
void
main
(
String
[]
args
)
{
File
workingDirectory
=
new
File
(
WORKING_DIR
,
"GamsTest"
);
workingDirectory
.
mkdir
();
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/UNPLUM/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
());
}
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
();
}
}
private
final
static
String
WORKING_DIR
=
"/Users/peteralexander/Documents/R_Workspace/tempDir"
;
public
static
void
main
(
String
[]
args
)
{
GamsTest
aGamsTest
=
new
GamsTest
();
aGamsTest
.
run
();
}
private
void
run
()
{
File
workingDirectory
=
new
File
(
WORKING_DIR
,
"GamsTest"
);
workingDirectory
.
mkdir
();
GAMSWorkspaceInfo
wsInfo
=
new
GAMSWorkspaceInfo
();
wsInfo
.
setWorkingDirectory
(
workingDirectory
.
getAbsolutePath
());
GAMSWorkspace
ws
=
new
GAMSWorkspace
(
wsInfo
);
GAMSDatabase
db
=
ws
.
addDatabase
();
GAMSParameter
parm
=
db
.
addParameter
(
"previous_area"
,
1
,
"the previous area for each land use"
);
parm
.
addRecord
(
"cropland"
).
setValue
(
200.0
);
parm
.
addRecord
(
"pasture"
).
setValue
(
450.0
);
parm
=
db
.
addParameter
(
"demand"
,
1
,
"demand for crop"
);
parm
.
addRecord
(
"cereals"
).
setValue
(
550.0
);
parm
.
addRecord
(
"fruits"
).
setValue
(
60.0
);
parm
.
addRecord
(
"oilcrops"
).
setValue
(
120.0
);
parm
.
addRecord
(
"starchyRoots"
).
setValue
(
100.0
);
parm
.
addRecord
(
"treenuts"
).
setValue
(
20.0
);
parm
.
addRecord
(
"vegetables"
).
setValue
(
50.0
);
parm
=
db
.
addParameter
(
"yield_ref"
,
1
,
"ref yield t per ha"
);
parm
.
addRecord
(
"cereals"
).
setValue
(
3.0
);
parm
.
addRecord
(
"fruits"
).
setValue
(
1.0
);
parm
.
addRecord
(
"oilcrops"
).
setValue
(
1.0
);
parm
.
addRecord
(
"starchyRoots"
).
setValue
(
10
);
parm
.
addRecord
(
"treenuts"
).
setValue
(
1.0
);
parm
.
addRecord
(
"vegetables"
).
setValue
(
1
);
parm
.
addRecord
(
"pasture"
).
setValue
(
0.7
);
db
.
addParameter
(
"meatDemand"
,
0
,
"demand for meat milk and eggs"
).
addRecord
().
setValue
(
160
);
db
.
addParameter
(
"meatEfficency"
,
0
,
"efficency of converting feed and pasture into animal products"
).
addRecord
().
setValue
(
0.1
);
db
.
addParameter
(
"maxLandUseChange"
,
0
,
"max rate of land use change"
).
addRecord
().
setValue
(
0.1
);
db
.
addParameter
(
"maxIntensity"
,
0
,
"max intensity which will rise over time"
).
addRecord
().
setValue
(
3
);
// create GAMSJob "t2" from the "model" string variable
GAMSJob
gamsJob
=
ws
.
addJobFromFile
(
"/Users/peteralexander/Documents/R_Workspace/UNPLUM/GAMS/IntExtOpt.gms"
);
GAMSOptions
opt
=
ws
.
addOptions
();
opt
.
defines
(
"gdxincname"
,
db
.
getName
());
gamsJob
.
run
(
opt
,
db
);
System
.
out
.
println
(
"Modelstatus: "
+
GAMSGlobals
.
ModelStat
.
lookup
(
(
int
)
gamsJob
.
OutDB
().
getParameter
(
"ms"
).
findRecord
().
getValue
()
)
+
", Solvestatus: "
+
GAMSGlobals
.
SolveStat
.
lookup
((
int
)
gamsJob
.
OutDB
().
getParameter
(
"ss"
).
findRecord
().
getValue
())
);
GAMSVariable
varAreas
=
gamsJob
.
OutDB
().
getVariable
(
"area"
);
GAMSVariable
varIntensities
=
gamsJob
.
OutDB
().
getVariable
(
"intensity"
);
GAMSVariable
varFeedArea
=
gamsJob
.
OutDB
().
getVariable
(
"feedArea"
);
for
(
GAMSVariableRecord
rec
:
varAreas
)
{
System
.
out
.
println
(
String
.
format
(
"%s: \tarea= %.0f, \tintensity= %.3f, \tfeed_area= %.0f"
,
rec
.
getKeys
()[
0
],
rec
.
getLevel
(),
varIntensities
.
findRecord
(
rec
.
getKeys
()[
0
]).
getLevel
(),
rec
.
getKeys
()[
0
].
equals
(
"pasture"
)
?
Double
.
NaN
:
varFeedArea
.
findRecord
(
rec
.
getKeys
()[
0
]).
getLevel
()));
// + " marginal=" + rec.getMarginal());
}
//cleanup(ws.workingDirectory());
}
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
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment