Forum Discussion

FICO's avatar
FICO
Community Manager
3 years ago

A problem with the readln function

Hello, I am having a problem with the readln() function.

In fact, I want to read an instance file to start the optimization in a subsequent moment.

In particular, I get the following error:

Mosel: E-27 at line 5 of `istanza_di_prova.txt': Number expected.

Mosel: E-27 at (10,1) of `istanza_di_prova.txt': Number expected.

A subsequent problem of this error message, is that only the first part of the input file is read properly.

Data starting from row 7 of the input file, is not read at all.

However, I don't see anything wrong neither in my input file (istanza_di_prova.txt) nor in the sintax of my script.

I am attaching both of them.

Thank you very much in advance for your help.

With kind regards,

Mauro

3 Replies

  • FICO's avatar
    FICO
    Community Manager

    Hello,

    I cannot reproduce your problem here. When I run your master_ufficale.mos then I get

    Mosel: E-22: I cannot open file `slave.bim' for reading (No such file or directory).

    I guess this is expected, because you did not provide a slave.bim. However, this error happens a long time after the readln() calls you mentioned.

    All the reading of data seems to work as expected.

    What version of Mosel are you using?

    Can you try to reduce your mos/txt to a minimal example that reproduces the issue?

  • FICO's avatar
    FICO
    Community Manager

    Dear Daniel,

    thank you for your kind reply.

    I am copying next a reduced version of the code that does not require the bim file.

    I have added a loop that prints the arrays Wcat, Ccat, L, and U.

    However, if I go to the panel on the left, it looks like they have no content (please see the attached screenshot).

    I am using Xpress 9.0.2 on a Windows 10 computer, while my colleague is using Xpress 8.13.

    I have tried to upload both the screenshot and the reduced version of the code, but it looks like it is possible to attach just one file here, so I have to copy and paste the reduced version of the code next.

    Thank you for your patience and time you are spending to help me.

    Best regards,

    Mauro

    model ModelName

    !uses "mmxprs", "mmjobs", "mmsystem";

    parameters

       input="istanza_di_prova.txt"

    end-parameters

    declarations

    Nt: integer !#type of bins

    Ni: integer !#items

    Nf: integer !#families

    U: array(range) of integer !upper bound on the #bins of type t

    L: array(range) of integer !lower bound on the #bins of type t

    Nj: integer !#bins

    sigma: array(range) of integer !bin j type

    gamma: array(range) of integer !item i category

    Ncat: array(range) of integer !#items belonging to category k

    Ccat: array(range) of integer !cost of a bin of category k

    Wcat: array(range) of integer !capacity of a bin of category k

    C: array(range) of integer !cost of bin j

    W: array(range) of integer !capacity of bin j

    w: array(range) of integer !item i weight

    p: array(range) of integer !p(k) profit of family k

    wk: array(range) of integer !category k weight

    flag: array(range) of integer !1 if item i is compulsory, 0 otherwise

    family: array(range) of integer

    id: integer

    V: array(range) of integer !auxiliary number to count bins

    Ic: set of integer

    Inc: set of integer

    take: array(range)of integer

    epsilon: array(range) of real

    best: real

    Sfix: array(range) of integer

    Zfix: array(range,range) of integer

    comp: array(range,range) of integer !1 category k1 and k2 are compatible, 0 otherwise

    fileName: string

    end-declarations

    fopen(input,F_INPUT)

    !readln("NAME", fileName)

    !readln("COMMENT")

    readln("BINTYPES", Nt)

    readln("ITEMS", Ni)

    readln("FAMILIES", Nf)

    readln("BINS_SECTION")

    Nj:=0

    U(0):=0

    V(0):=0

    forall(t in 1..Nt)do

       readln(id, Wcat(t), Ccat(t), L(t), U(t))

       Nj:=Nj+U(t)

       V(t):=V(t-1)+U(t)

    end-do

    writeln("Nt ", Nt)

    writeln("Ni ", Ni)

    writeln("Nf ", Nf)

    writeln("CHECK")

    forall(t in 1..Nt)do

       writeln(t, ' ', Wcat(t),' ', Ccat(t),' ', L(t),' ', U(t))

    end-do

  • FICO's avatar
    FICO
    Community Manager

    When you are working with 'readln'/'read' the contents of the file that you are reading must match exactly what you are trying to read from your Mosel model: uncommenting the lines

    !readln("NAME", fileName)

    !readln("COMMENT")

    in your latest code example above will match the format of your input file and input values are read in correctly.

    You may want to add a test after the individual read statements, such as:

    procedure testread(num: integer)
      numread:=getparam("nbread")
      if numread<>num then
        writeln("Read error (expected:", num, " items read:", numread, ")")
        exit(1)
      end-if
    end-procedure
     
    !...
    readln("FAMILIES", Nf)
    testread(2)
     
    readln("BINS_SECTION")
    testread(1)
     
    Nj:=0
    U(0):=0
    V(0):=0
     
    forall(t in 1..Nt)do
       readln(id, Wcat(t), Ccat(t), L(t), U(t))
       testread(5)