How to write conditional statements in Gams - modeling

I'm new in the Gams and I try to solve a problem for my term project. Although the code does not give errors, I am not sure of its correctness.
kisit5(i,h,k)$( (ord(i)>=2) and (ord(h)<ord(i)) and (ord(h) < ord(k)) )
In the conditional part, I wanted to write that expression: ∀i=2,...,n and h<i,k.
Is it correct or should I write it in a different form?

This looks correct. You can just write some dummy code to verify it yourself:
Set i /1*4/
h /1*4/
k /1*4/
kisit5(i,h,k);
kisit5(i,h,k)$( (ord(i)>=2) and (ord(h)<ord(i)) and (ord(h) < ord(k)) ) = yes;
display kisit5;
The result will be printed to the lst file. Or you run it with GDX creation (F10 in GAMS Studio), and look at it in the GDX Viewer of GAMS Studio.

Related

Matlab writematrix in Excel gives error: Name cannot be the same as built-in name

I want to copy an Excel file to a different path through Matlab and then write in it also using Matlab. Somehow I get the error: Name cannot be the same as built-in name.
As I want to write multiple times in the file, I don't want to solve this problem manuelly each time, I want the code to run through without me having to do something constantly.
Is there any way I can solve this problem all at once through code? Does this happen because I copy the Excel file first?
The code looks like this:
path_source_template1 = 'Blabla1\Template1.xlsx';
timestamp = datestr(now);
timestamp = strrep(timestamp, ':', '-');
timestamp = strrep(timestamp, ' ', '-');
path_output = fullfile('Blabla2\',timestamp);
mkdir(fullfile(path_output));
path_output_template1 = strcat(path_output,'\Template1.xlsx');
copyfile(path_source_template1,path_output_template1);
Then I want to write in the Template1.xlsx:
writematrix(test,path_output_template1,'Sheet','Test','Range','A1',UseExcel=true,AutoFitWidth=false);
Then I get this error:
enter image description here
The input to the writematrix file uses the name, value format, so in this line:
writematrix(test,path_output_template1,'Sheet','Test','Range','A1',UseExcel=true,AutoFitWidth=false);
you should have:
..., 'Sheet','Test','Range','A1','UseExcel', true,'AutoFitWidth', false);
Disclaimer: I haven't tested this, but I'm fairly certain this will fix your problem.
The correct call to the writematrix would be:
writematrix(test,path_output_template1, 'Sheet','Test','Range','A1', 'UseExcel', true,'AutoFitWidth',false);
When writing datetime data to a spreadsheet file, you must set both 'PreserveFormat' and the 'UseExcel' Name-Value pair to true to preserve the existing cell formatting. You can check the documentation writematrix.
In order to answer to the error I tested the code locally in Matlab 2019b and works well setting the test variable in this example way: test = magic(5);.
Maybe the error could be in the data that you use in test variable or in that if you run the code iteratively very fast the path_output could exist, One way to improve this could be with a more accurate timestamp.

phpspreadsheet merge error with variables

When I'm merging cells with phpspreadsheet using a variable I have an issue.
On opening in MS Excel (2019), it says that the program can try to recover the document if I'm sure it's a reliable one.
When I say yes, the document is ok and the merging worked fine.
Why do I have that message?
I don't have this message on this way :
$spreadsheet->getActiveSheet()->mergeCells('B2:F2');
But on this way I have this message :
$cellRange = 'B2:F2';
$spreadsheet->getActiveSheet()->mergeCells($cellRange);
MergeCells is a sensible function. If you try to/accidentally make overlap cell groups this kind of error. Be sure that your code not do something like this:
for($i=1; $i<3; $i++){
$cellRange = 'B'.$i.':F'.$i;
$spreadsheet->getActiveSheet()->mergeCells($cellRange);
}
My mistake was :
I was using that merging tool on a "for" loop and I was trying to merge an already merged cell with another one.

export data from gams to excel

I have a mathematical model and I want to run it 10 times automatically with different data sets which are generated randomly(when the first model stops, start another model automatically). for this purpose I have written the solve statement in a loop . I want to see the result of these 10 models in different sheets of an excel file. how can I do it? is it possible to correct my code?
set k/sheet1*sheet10/;
loop(k,
data generation....
solve statement....
execute_unload 'RESULT.gdx'
execute 'gdxxrw.exe RESULT.gdx o=RESULT.xlsx var=x.l rng='k.t1:0'!a1'
);
I really appreciate your kind helps
Actually, you are pretty close already, you just need to use 'put_utility' instead of 'execute' to make use of the 'k.tl' syntax (note: it is .tl, not .t1):
*dummy put file
file fx; put fx;
set k/sheet1*sheet10/;
loop(k,
solve statement....
execute_unload 'RESULT.gdx'
put_utility 'exec' / 'gdxxrw.exe RESULT.gdx o=RESULT.xlsx var=x.l rng='k.tl:0'!a1';
);
I hope that helps!
Best regards,
Lutz

Extract the ANOVA table to Excel/.csv

I'm wondering if it's possible to extract the table that results when running ANOVA to an Excel or .csv file. I'm running a repeated measures two-way ANOVAs with RMAOV2 (http://uk.mathworks.com/matlabcentral/fileexchange/5578-rmaov2). Here is the code I'm using, which works fine, and it produces a table with the ANOVA results.
dir ='/Users/Documents/folder';
cd(dir)
file = readtable('file.csv');
toAnalyse = table2array(file);
RMAOV2(toAnalyse);
However, when I tried to save the ANOVA results in order to then export them to Excel or in a .csv file, this doesn't work:
ANOVAresults = RMAOV2(toAnalyse);
Error:
Output argument "RMAOV2" (and maybe others) not assigned during call to "RMAOV2".
Any suggestion would be very appreciated.
If you take a look into the source code of the file, you will notice that it never assigns anything to the return variable. Instead it only prints data to the command window.
To resolve this problem you have to edit the source code and assign the data you want to return. Alternatively you can contact the Autor.

Not using colnames when reading .xls files with RODBC

I have another puzzling problem.
I need to read .xls files with RODBC. Basically I need a matrix of all the cells in one sheet, and then use greps and strsplits etc to get the data out. As each sheet contains multiple tables in different order, and some text fields with other options inbetween, I need something that functions like readLines(), but then for excel sheets. I believe RODBC the best way to do that.
The core of my code is following function :
.read.info.default <- function(file,sheet){
fc <- odbcConnectExcel(file) # file connection
tryCatch({
x <- sqlFetch(fc,
sqtable=sheet,
as.is=TRUE,
colnames=FALSE,
rownames=FALSE
)
},
error = function(e) {stop(e)},
finally=close(fc)
)
return(x)
}
Yet, whatever I tried, it always takes the first row of the mentioned sheet as the variable names of the returned data frame. No clue how to get that solved. According to the documentation, colnames=FALSE should prevent that.
I'd like to avoid the xlsReadWrite package. Edit : and the gdata package. Client doesn't have Perl on the system and won't install it.
Edit:
I gave up and went with read.xls() from the xlsReadWrite package. Apart from the name problem, it turned out RODBC can't really read cells with special signs like slashes. A date in the format "dd/mm/yyyy" just gave NA.
Looking at the source code of sqlFetch, sqlQuery and sqlGetResults, I realized the problem is more than likely in the drivers. Somehow the first line of the sheet is seen as some column feature instead of an ordinary cell. So instead of colnames, they're equivalent to DB field names. And that's an option you can't set...
Can you use the Perl-based solution in the gdata instead? That happens to be portable too...

Resources