Skip to content

Instantly share code, notes, and snippets.

@cshjin
Created April 18, 2014 17:15
Show Gist options
  • Save cshjin/11054755 to your computer and use it in GitHub Desktop.
Save cshjin/11054755 to your computer and use it in GitHub Desktop.
AMPL learning
data;
set Crops := wheat corn beets;
param TotalArea:=500;
param BeetsQuota:=6000;
param Yield :=
wheat 2.5
corn 3.0
beets 20.0;
param MinRequirement :=
wheat 200
corn 240
beets 0;
param PlantingCost :=
wheat 150
corn 230
beets 260;
param SellingPrice :=
wheat 170
corn 150
beets 36;
param ExcessSellingPrice := 10;
param BuyingPrice :=
wheat 238
corn 210
beets 100;
reset;
model;
set Crops;
param TotalArea;
param Yield{Crops};
param PlantingCost{Crops};
param SellingPrice{Crops};
param ExcessSellingPrice;
param BuyingPrice{Crops};
param MinRequirement{Crops};
param BeetsQuota;
var area{c in Crops} >= 0;
var sell{c in Crops} >= 0;
var sellExcess >=0;
var buy{c in Crops} >=0;
maximize profit:
ExcessSellingPrice * sellExcess +
sum{c in Crops} (SellingPrice[c] * sell[c] -
BuyingPrice[c] * buy[c] -
PlantingCost[c] * area[c]);
subject to totalArea:
sum {c in Crops} area[c] <= TotalArea;
subject to requirement{c in Crops}:
Yield[c] * area[c] - sell[c] + buy[c]
>=MinRequirement[c];
subject to quota: sell['beets'] <=BeetsQuota;
subject to sellBeets: sell['beets'] + sellExcess
<=Yield['beets'] * area['beets'];
#----------------------------------------
# FARMAR RUN COMMEND
#----------------------------------------
reset;
option solver CPLEX;
option presolve 1;
option cplex_options 'sensitivity';
include farmer1.mod;
include farmer1.dat;
solve;
display _objname, _obj;
# Print out the value of the objective function
display _varname, _var, _var.lb,_var.ub, _var.rc ;
# Print out names, values and reduced costs
display _varname, _var.down, _var.current, _var.up ;
# Print out names, objective function coefficients
# together with what can be changed without causing
# a base change
# down = lower limit, up = upper limit
display _conname, _con.slack, _con.dual;
# Print out constraint name, slack and shadow price
display _conname,_con.down,_con.current,_con.up ;
# Print out constraint name, right-hand side coefficient
# together with what can be changed without causing
# a base change
@cshjin
Copy link
Author

cshjin commented Apr 18, 2014

_var can have a bunch of suffixes, the following list is from help doc

Possible suffix values for _var.suffix:
        astatus   current   defeqn   derstage
        down      dual      init     init0
        int       lb        lb0      lb1
        lb2       lrc       lslack   no
        rc        relax     slack    sno
        sstatus   stage     status   ub
        ub0       ub1       ub2      up
        urc       uslack    val

For _con which is the constraints suffix:

Possible suffix values for _con.suffix:
        astatus    body     current   defvar
        derstage   dinit    dinit0    down
        dual       lb       lbs       lbs1
        lbs2       ldual    lslack    no
        relax      slack    sno       sstatus
        stage      status   ub        ubs
        ubs1       ubs2     udual     up
        uslack

for _obj, which is object suffix:

Possible suffix values for _obj.suffix:
        astatus   current   down      exitcode
        message   no        relax     result
        sense     sno       sstatus   stage
        up        val

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment