Reputation: 1300
Assuming a single farm problem as in the case of Mayaland example (Hazell & Norton, 1986; Chapter 2; section 2.2
), we can use the following code for optimization in GAMS
.
Data (Single farm)
resrs,Corn, Bean, Sorghum, Peanut
Land,1,1,1,1
Labor,1.42,1.87,1.92,2.64
Mules,1.45,1.27,1.16,1.45
Market,,,,0.98
resrs,resEndow
Labor,16.5
Land,5
Mules,10
Market,0.5
crop,grosMarg
Corn,1372
Bean,1219
Sorghum,1523
Peanut,4874
Code (single farm)
SETS resrs, crops;
PARAMETERS
farmData
resEndow
grosMarg
;
************************** I prefer NOT to input data like this **************************
*SETS
* resrs ressourses /labor, land, mules, market /
* crops mayaland activities /
*Corn
*Bean
*Sorghum
*Peanut
*/
*;
*PARAMETERS
*
*resEndow (resrs) ressourses endowments
* /
* Labor 16.5
* Land 5
* Mules 10.0
* Market 0.5
* /
*grosMarg (crops) gross margins
* /
* Corn 1372
* Bean 1219
* Sorghum 1523
* Peanut 4874
* /
*;
*table farmData A ressource requirements
* Corn Bean Sorghum Peanut
*Land 1 1 1 1
*Labor 1.42 1.87 1.92 2.64
*Mules 1.45 1.27 1.16 1.45
*Market 0.983
*;
*
*******************************************************************************************
******************** I prefer to load the data from csv files like this********************
$call csv2gdx Mayaland.csv id=farmData useHeader=y fieldSep=Comma index=1 values=2..lastCol trace=3
$ifE errorLevel<>0 $abort Problems reading Mayaland.csv!
$gdxIn Mayaland.gdx
$load resrs = dim1
$load crops = dim2
$load farmData
$gdxIn
;
$call csv2gdx ressourses_endowments.csv id=resEndow useHeader=y fieldSep=Comma index=1 values=2 trace=3
$ifE errorLevel<>0 $abort Problems reading ressourses_endowments.csv!
$gdxIn ressourses_endowments.gdx
$load resEndow
$gdxIn
;
$call csv2gdx activity_gross_margin.csv id=grosMarg useHeader=y fieldSep=Comma index=1 values=2 trace=3
$ifE errorLevel<>0 $abort Problems reading activity_gross_margin.csv!
$gdxIn activity_gross_margin.gdx
$load grosMarg
$gdxIn
;
*******************************************************************************************
DISPLAY resEndow, grosMarg, farmData;
VARIABLES
Prft Total gross margin
x(crops) activity levels
;
EQUATIONS
Profit definition of Z
RESCON ressouse constraint
NONNEG non-negativity condition;
Profit.. Prft =E= sum(crops, grosMarg (crops)*x(crops));
RESCON(resrs).. sum(crops, farmData(resrs, crops)*x(crops)) =L= resEndow(resrs);
NONNEG(crops).. x(crops) =G= 0;
model mayaland / Profit, RESCON, NONNEG /
;
SOLVE mayaland maximizing Prft using LP;
DISPLAY x.l, Prft.l;
Now, I would like to add several nested levels and solve the problem for each unique combination of these levels. Consider we have several farmers from different regions with different type of farming practices as in the following data
Data (Multiple farms, Multiple regions, Multiple farming practices)
farmer,region,practice,resrs,Corn, Bean, Sorghum, Peanut
farmer1,a,p1,Land,1,1,1,1
farmer1,a,p1,Labor,1.42,1.87,1.92,2.64
farmer1,a,p1,Mules,1.45,1.27,1.16,1.45
farmer1,a,p1,Market,,,,0.98
farmer2,b,p2,Land,1,1,1,1
farmer2,b,p3,Labor,1.42,1.87,1.92,2.64
farmer2,b,p4,Mules,1.45,1.27,1.16,1.45
farmer2,b,p5,Market,,,,0.98
farmer,region,practice,resrs,resEndow
farmer1,a,p1,Land,16.5
farmer1,a,p1,Labor,5
farmer1,a,p1,Mules,10
farmer1,a,p1,Market,0.5
farmer2,b,p2,Land,16.5
farmer2,b,p3,Labor,5
farmer2,b,p4,Mules,10
farmer2,b,p5,Market,0.5
farmer,region,practice,crop,grosMarg
farmer1,a,p1,Corn,1372
farmer1,a,p1,Bean,1219
farmer1,a,p1,Sorghum,1523
farmer1,a,p1,Peanut,4874
farmer2,b,p2,Corn,1372
farmer2,b,p3,Bean,1219
farmer2,b,p4,Sorghum,1523
farmer2,b,p5,Peanut,4874
Expected result (Multiple farms, Multiple regions, Multiple farming practices)
How can we make the above code work for solving the problem for each unique combination of multiple levels? In other words, each row in Mayaland.csv
(below) represents an independent observation, hence should be modelled independently.
farmer,region,practice ===================================> result 1
farmer,region,practice ===================================> result 2
farmer,region,practice ===================================> result 3
farmer,region,practice ===================================> result 4
farmer,region,practice ===================================> result 5
farmer,region,practice ===================================> result 6
farmer,region,practice ===================================> result 7
farmer,region,practice ===================================> result 8
Reference
Hazell, P. B. R., & Norton, R. D. (1986). Mathematical programming for economic analysis in agriculture. New York, US. https://www.ifpri.org/publication/mathematical-programming-economic-analysis-agriculture
Upvotes: 0
Views: 220
Reputation: 16724
Form a set frp(farmer,region,practice)
with allowed combinations. This can be read from the csv file or calculated from e.g. gross margin.
Loop over this set
parameter results(farmer,region,practice,*,*) 'collect results';
loop(frp(farmer,region,practice),
* extract data for single case
endow(res) = endowmentData(farmer,region,practice,res);
...
* solve single case
solve ....
* store results in parameter
results(farmer,region,practice,'x',crops) = x.l(crops);
results(farmer,region,practice,'profit','-') = Prft.l;
....
);
* export results to spreadsheet or csv file.
PS It may be easier to use a single spreadsheet with all data compared to multiple csv files.
Upvotes: 2