GAMS Solution for Ex5.

来源:互联网 发布:mac u盘安装 mac os x 编辑:程序博客网 时间:2024/05/06 01:08

* Here is try to create gdx file

* Please notice that, rdim and cdim only tell gdx how the data store in excel,
* not for the form in theory.


$onecho > in.txt
dset=i rng=a3 rdim=1
dset=j rng=b2 cdim=1
par=d rng=A2 rdim=1 cdim=1
par=p rng=A8 rdim=1
par=a rng=A13 rdim=1
par=c rng=A19 rdim=1

$offecho

$call gdxxrw.exe transportdata.xlsx @in.txt

* Here is to display xlsx
*
* From the Excel file transportdata.xlsx read the information for the sets of
* markets and plants as well as the distance table (given in thousands of
* kilometers) and save it in a .gdx file. Read the data from the .gdx file into
* GAMS and display it.


$gdxin transportdata.gdx

set i(*) plants  ;
set j(*) markets ;
$load i j
display i,j ;

* In a new GAMS program, read the above values again using the created
* text file. Then multiply each of the distances by 1000 to convert them into
* thousands of meters. Save the new data in the transportdata.gdx file and
* write it in the Excel file transportdata.xlsx, Sheet2, starting from A1. Repeat
* the exercise by saving the data in a .gdx file called transportdata4 and
* writing the updated distance table in a new Excel file called transportdata4,
* but starting from cell F2.


parameter d(i,j) distance table ;
$load d

* close gdx
$gdxin

scalar pp multiplier to 1000 /1000/ ;
parameter xxx(i,j) distance after multiply by 1000 ;
xxx(i,j) = d(i,j) * pp ;

display xxx;

* execute_unload 'transportdata4.gdx',xxx ;
* execute 'gdxxrw.exe transportdata4.gdx par=x rng=tabelle2!A1' ;

* Implement and solve the transportation problem in GAMS by importing
* all the necessary information from the file transportdata.xlsx and writing
* the result in the file transportdata.xlsx, Sheet2, starting from A8.

* load gdx

$gdxin transportdata.gdx

parameter p(i) plants quantity ;
parameter a(j) market quantity ;
$load p a
display p,a ;

scalar c transport cost ;
$load c
display c;

* close gdx
$gdxin

parameter xc(i,j) cost table ;
xc(i,j) = d(i,j) * c / 1000;

variables
         x(i,j) quantities table
         z      total cost;
positive variable x ;

equations
         cost cost function
         supply(i) supply st.
         demand(j) demand st. ;

cost.. z =e= sum((i,j), xc(i,j) * x(i,j)) ;
supply(i).. sum(j,x(i,j)) =l= p(i) ;
demand(j).. sum(i,x(i,j)) =g= a(j) ;

model transport /all/ ;
solve transport using lp minimizing z ;
display z.l;

execute_unload 'transportdata.gdx',z ;
execute 'gdxxrw.exe transportdata.gdx var=z rng=tabelle1!B21' ;

原创粉丝点击