Forum Discussion

FICO's avatar
FICO
Community Manager
8 years ago

Sum duplicate entries from input data file

Is there a command to sum duplicate entries from an input data file in an "initializations from..." block?

For example, if I have a file input.dat:

Cost: [

(A) 1

(A) 2

(B) 5

]

and my Mosel code contains:

declarations

Materials: set of string

Cost: array(Materials) of real

end-declarations

initializations from "input.dat"

Cost

end-initializations

forall(m in Materials) writeln(m + " costs: " + Cost(m))

I'd like to see that Material "A" costs 3 ( = 1+2 ), but will instead see that it costs 2 (only the last duplicate value in the data file overwrites all previous duplicates). I understand that this is intended behavior, and that I could seek to do the summation on my data source, but is there an easy way to do this in Mosel?

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    The 'initializations' format does not allow you to use the same index multiple times for the same table. If you can distribute the multiple occurrences within your data into several tables you could read these into some temporary structures and later add them.

    Otherwise, the only possibility I see for implementing this behaviour would be to write your own data reading routine, such as in these examples:

    https://examples.xpress.fico.com/example.pl?id=readfile

    FICO Xpress Optimization Examples Repository: Parsing a CSV-format data file (https://examples.xpress.fico.com/example.pl?id=readcsv)