【免费】GAMS与EXCEL互动的方法【免费】-经管之家官网!

人大经济论坛-经管之家 收藏本站
您当前的位置> 会计>>

会计库

>>

【免费】GAMS与EXCEL互动的方法【免费】

【免费】GAMS与EXCEL互动的方法【免费】

发布:tylerma3223 | 分类:会计库

关于本站

人大经济论坛-经管之家:分享大学、考研、论文、会计、留学、数据、经济学、金融学、管理学、统计学、博弈论、统计年鉴、行业分析包括等相关资源。
经管之家是国内活跃的在线教育咨询平台!

经管之家新媒体交易平台

提供"微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯"等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

提供微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

无论读取还是保存,都需要使用gdx文件作为中介。读取时,先将excel的数据转换到gdx中,然后读取数据至内存。eg:seti/usa,eu,row/t/2005*2010/;parametersLandRate(i,t)CapitalRate(i,t)SkLabRate(i,t)UnskLabRate(i ...
坛友互助群


扫码加入各岗位、行业、专业交流群



无论读取还是保存,都需要使用gdx文件作为中介。


读取时,先将excel的数据转换到gdx中,然后读取数据至内存。

eg:


set

i/usa,eu,row/

t/2005*2010/

;

parameters

LandRate(i,t)

CapitalRate(i,t)

SkLabRate(i,t)

UnskLabRate(i,t)

NatResRate(i,t)

test(i)

;


$call "gdxxrw ../data/trend.xlsx output=../data/trend.gdx par=NatResRate rng=NatRes! par=LandRate rng=Land! par=CapitalRate rng=Capital!par=SkLabRate rng=SkLab! par=UnskLabRate rng=UnskLab!"


$GDXIN ../data/trend.gdx

$load LandRate CapitalRate SkLabRate UnskLabRate NatResRate

$GDXIN

display

LandRate

CapitalRate

SkLabRate

UnskLabRate

NatResRate;


将数据保存至excel时,首先要将内存中的变量或者参数保存成gdx形式,如后转到到excel。

eg:


execute_unload "../data/ReTest.gdx" NatResRate

execute 'gdxxrw.exe ../data/ReTest.gdx output=../data/ReTest.xlsx par=NatResRate rng=sheet1!'


注意:当data目录下已经存在ReTest.xlsx文件时,再次保存相同的文件名,并不会覆盖原来的文件,而是在这个文件上继续添加数据。


IntroductionThis section gives a brief overview on how to use the GDX facilities in GAMS to read data from Excel and to write data to Excel. For more detailed information see the Appendix "GDX Facilities and Tools" of the GAMS User's Guide.Sample model to write an Excel file: gms2xls.gmsSample model to read from an Excel file: xls2gms.gmsSystem Requirements: A GAMS system Distribution 21.0 or later is required.
IntroductionGAMS communicates with Excel via GDX (GAMS Data Exchange) files.A GDX file is a file that stores the values of one or more GAMS symbols such as sets, parameters variables and equations. GDX files can be used to prepare data for a GAMS model, present results of a GAMS model, store results of the same model using different parameters etc. A GDX file does not store a model formulation or executable statements.
GDX files are binary files that are portable between different platforms. They are written using the byte ordering native to the hardware platform they are created on, but can be read on a platform using a different byte ordering.
In order to write data from GAMS to Excel, the user writes a GDX file and then writes the Excel file from the GDX file.
GAMS ----> GDX ----> Excel This is practically seamless for the user and requires few commands. The process to import data from an Excel file to GAMS is similar: Excel ----> GDX ----> GAMS Example: GAMS to ExcelWe will build on the simple transportation model from the GAMS Model library and write the solution x and the marginals of x to an Excel file.After the solve statement, we unload the data (x.L and x.M) to a GDX file using the execute_unload command:
execute_unload "results.gdx" x.L x.M Note that the execute_unload command is executed during the actual execution phase (not during compilation time as $ control options) and creates a GDX file called results.gdx.Now let us write the data from the GDX file to an Excel file called results.xls. We do this using the GDXXRW utility
execute 'gdxxrw.exe results.gdx var=x.L' execute 'gdxxrw.exe results.gdx var=x.M rng=NewSheet!f1:i4' For the first call for x.L, there is no range specified and the data is written in cell A1 and beyond in the first available sheet. For the marginals x.M data will be written to cells F1:I4 in the sheet NewSheet. Note that we specified var=x.L and var=x.M. If the user wishes to write parameters to the Excel file, the relevant command is par. See the GDX facilities in GAMS document for further details.The complete GAMS model: gms2xls.gmsThe resulting GDX file: results.gdxThe resulting Excel file: results.xlsExample: Excel to GAMSAgain, we will use the transportation model and make use of the results.xls file created by the previous model. First we will create the GDX file from the Excel file. We will make use of the GDXXRW utility: $CALL GDXXRW.EXE results.xls par=Level rng=A1:D3 Note that since we are using the $CALL command, this occurs during the compilation phase and not during execution time. We specify that the data in the range A1:D3 is read in as a parameter called Level. The resulting GDX file will be called results.gdxBefore we can read in the data, we must define a parameter called Level over the appropriate sets:
Parameter Level(i,j); $GDXIN results.gdx $LOAD Level $GDXIN The GDXIN results.gdx command specifies that data will be read in from the appropriate GDX file. We then import data structures values using the $LOAD command. When we are finished, we terminate with $GDXIN.In the example below, we then fix the level values of the variable x to the parameter Level so that solving results in a trivial fixed model.The complete GAMS model: xls2gms.gms

扫码或添加微信号:坛友素质互助


「经管之家」APP:经管人学习、答疑、交友,就上经管之家!
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
本文关键词:

本文论坛网址:https://bbs.pinggu.org/thread-2696939-1-1.html

人气文章

1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
数据分析师 人大经济论坛 大学 专业 手机版