Repository navigation
Open
Description
Use AMPL .dat format files, it is possible to read a subset of pairs and an associated parameter from the same table. For example, with
set NUTR;
set FOOD;
set LINKS within {NUTR, FOOD};
param amt {LINKS} >= 0;
and data
set NUTR := A B1 B2 C ;
set FOOD := BEEF CHK FISH HAM MCH MTL SPG TUR ;
param :LINKS: amt (tr):
A C B1 B2 :=
BEEF 60 20 . 15
CHK . . 20 20
FISH . . 15 .
HAM 40 40 35 .
MCH 15 35 15 15
MTL 70 30 15 15
SPG 25 50 25 15
TUR 60 20 15 . ;
neither the values marked . or the corresponding members of LINKS are included in the data that is read:
ampl: display LINKS, amt;
set LINKS :=
(A,BEEF) (B2,CHK) (B1,HAM) (B2,MCH) (B2,MTL) (B2,SPG)
(C,BEEF) (B1,FISH) (A,MCH) (A,MTL) (A,SPG) (A,TUR)
(B2,BEEF) (A,HAM) (C,MCH) (C,MTL) (C,SPG) (C,TUR)
(B1,CHK) (C,HAM) (B1,MCH) (B1,MTL) (B1,SPG) (B1,TUR);
amt [*,*] (tr)
: A B1 B2 C :=
BEEF 60 . 15 20
CHK . 20 20 .
FISH . 15 . .
HAM 40 35 . 40
MCH 15 15 15 35
MTL 70 15 15 30
SPG 25 25 15 50
TUR 60 15 . 20
;
Reading from spreadsheet data should work the same, with the role of . played by a blank cell. But currently the entire set {NUTR,FOOD} is read rather than just the pairs corresponding to cells that are non-blank. In this example, reading
with table definition
table dietAmts IN "amplxl" "dietIssue.xlsx" "Amounts" "2D":
LINKS <- [NUTR, FOOD], amt;
gives the same values but the wrong LINKS set:
display LINKS, amt;
set LINKS :=
(A,BEEF) (B2,CHK) (A,HAM) (B2,MCH) (A,SPG) (B2,TUR)
(B1,BEEF) (C,CHK) (B1,HAM) (C,MCH) (B1,SPG) (C,TUR)
(B2,BEEF) (A,FISH) (B2,HAM) (A,MTL) (B2,SPG)
(C,BEEF) (B1,FISH) (C,HAM) (B1,MTL) (C,SPG)
(A,CHK) (B2,FISH) (A,MCH) (B2,MTL) (A,TUR)
(B1,CHK) (C,FISH) (B1,MCH) (C,MTL) (B1,TUR);
amt [*,*] (tr)
: A B1 B2 C :=
BEEF 60 . 15 20
CHK . 20 20 .
FISH . 15 . .
HAM 40 35 . 40
MCH 15 15 15 35
MTL 70 15 15 30
SPG 25 25 15 50
TUR 60 15 . 20
;
Metadata
Metadata
Assignees
Labels
No labels

Activity
nfbvs commentedon May 22, 2020
There seems to be a difference between the .dat reader and the table handler reader. I was able to reproduce the issue with a relational table in amplxl, odbc and the .tab handler.
To reproduce the issue with the .tab handler copy the following data to a file named amounts.tab
and the following to a .run script
the display in AMPL shows
mingodadampl commentedon May 22, 2020
Based on Nicolau's comment I looked at the escrow/acl/tables.c and it seems that in the Add_Rows function we are inserting the the keys before check the rest of values.
The behavior Robert expect is not present in any table handler, we can rework the code to achieve that behavior and it'll break (change behavior) for existing ampl scripts.
mingodadampl commentedon May 22, 2020
After a while I'm revisiting my comment because it was not correct, on Nicolau's example it's not equivalent with Robert example, to be equivalent it should be like this:
And then running it will get this output (that somehow match Robert example):
It seems that if there isn't data in a cell we should not include it in a call to
AddRow.mingodadampl commentedon May 22, 2020
With this modification to initial Robert example we can see the equivalent "LINKS' table:
mingodadampl commentedon May 22, 2020
I'm still in doubt about my second comment, it seems that there is no way from the table handler to know if it's updating a
setlike ampl can when readingdatfiles.I tried modify escrow/acl/tables.c::Add_Rows but could not get it to work as expected so far.
I'm again inclined to say the problem is inside ampl not the table handler.
mingodadampl commentedon May 24, 2020
Finally I could find a possible fix for this issue with this commit https://github.com/ampl/escrow/commit/f85bf5c67cedf8d39cbbce5fe10e33e16f354f0c, I was correct on my first comment but I didn't understood the code at that time to be able to move the insertion on the indexed set after checking for
'.'values.nfbvs commentedon May 24, 2020
Can you test the fix with the following files?
dat_data.dat
tab_data.tab
example.run
expected output:
mingodadampl commentedon May 25, 2020
Good catch !
Thanks for the sample script I extended the initial fix here https://github.com/ampl/escrow/commit/a8aed66684fcfd74abdc63c8358a6756f602e5d7 , I could not identify where the "escrow/acl/read.c" manage this case and came with this not elegant/clean fix.
It now produces the expected output for both of Nicolau's examples, I could not reproduce Robert example because I could not create the xlsx file that could be accepted by the table handler definition.
A peer review is welcome !
nfbvs commentedon May 27, 2020
The behavior seems to be different for different dimensions, for example with the followinf script
reading the data
AMPL does not skip the missing values and displays
mingodadampl commentedon May 27, 2020
There is also a confuse or not clear way to explain this behavior where we have a set declared with
dimenand check (what really change in the data format is having a 2D table or not, independent of thewithinclause):4er4er4er commentedon Jun 18, 2020
It seems that the rules for reading set and parameter values together from a
.dattable are as follows:.entries..entries.I believe that these rules were chosen to correspond to the cases that are most likely to be useful. It's possible to think up situations where the opposites of the above rules would be desirable, but they are less likely cases and can be handled in other ways.
Given that these are indeed the rules for
.dattables, I propose to use analogous rules for amplxl tables in spreadsheets.