A problem about .Net API and GAMS Topic is solved

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
arozzy
User
User
Posts: 2
Joined: 2 weeks ago

A problem about .Net API and GAMS

Post by arozzy » 2 weeks ago

Hi, I am new at .NET API of Gams. I have a problem for the code below

Code: Select all

                GAMSWorkspace ws;
                        if (Environment.GetCommandLineArgs().Length > 1)
                            ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
                        else
                            ws = new GAMSWorkspace();
                            GAMSJob t = ws.AddJobFromString(GetModelText());
                            t.Run();
                            Console.WriteLine(t.OutDB.GetVariable("z").FindRecord().Level);

Code: Select all

       static String GetModelText()
        {
            String model = @"
Sets
i /1*100/
j /1*5/
t /t0*t434/;
parameter p(i,j);
parameter C(i,t,j);
scalar xx /0/;
parameters TT(t);
loop(t,
  TT(t) = xx;
  xx = xx +1;
);
$Call GDXXRW.EXE data.xls  par=p rng=100_5!A1 Rdim=1 Cdim=1
$GDXIN data.gdx
$LOAD p
$GDXIN
scalar d/69/;
scalar a/2/;
scalar b/3/;
loop(i,loop(j,loop(t,
                    C(i,t,j)$(d>=TT(t)) =  (a/P(i,j))* ((d- P(i,j)/2)-(TT(t)-0.5));
                    C(i,t,j)$(d<TT(t)) =  (b/P(i,j))* ((TT(t)-0.5)-(d- P(i,j)/2));
)));
positive variables X(i,t,j);
positive variable Y(i,j);
variable z;
equations c1, c2, c3, c4, c5;
c1.. z=e= sum(i, sum(t, sum(j, C(i,t,j)* X(i,t,j))));
c2(i,j).. sum(t, X(i,t,j)) =e= P(i,j)*Y(i,j);
c3(j,t).. sum(i, X(i,t,j)) =l= 1;
c4(i).. sum(j, Y(i,j)) =e= 1;
c5(i,j).. Y(i,j) =l= 1;
option RESLIM = 1000000000000000000;
option ITERLIM = 100000000;
model project /all/;
solve project using MIP minimize z;
display z.l;
  
";

            return model;
        }
The error message is below.
An unhandled exception of type 'GAMS.GAMSExceptionExecution' occurred in GAMS.net4.dll
Additional information: GAMS return code not 0 (2), set GAMSWorkspace.Debug to KeepFiles or higher or define the GAMSWorkspace.WorkingDirectory to receive a listing file with more details

How can I fix this ?

Thanks.

User avatar
bussieck
Moderator
Moderator
Posts: 208
Joined: 2 years ago

Re: A problem about .Net API and GAMS

Post by bussieck » 2 weeks ago

I suggest you do what the error message tells you: "...set GAMSWorkspace.Debug to KeepFiles or higher or define the GAMSWorkspace.WorkingDirectory to receive a listing file with more details". The exception means the GAMS compiler had compilation errors. I guess the call the gdxxrw did not work (probably because it could not find the Excel file). By default the GAMS job is executed in a temporary directory (not in your current directory where you execute your .Net application). So you probably have to use a full path to point to your Excel workbook. It could be also other things. The LST will tell you.

-Michael

arozzy
User
User
Posts: 2
Joined: 2 weeks ago

Re: A problem about .Net API and GAMS

Post by arozzy » 2 weeks ago

By following your suggestion, I have solved my problem. I added only the line below.

GAMSWorkspace ws = new GAMSWorkspace(workingDirectory: @"L:\GAM_NET\DATA");

Thank you.

Post Reply