Forum Discussion

FICO's avatar
FICO
Community Manager
10 years ago

Reading input file line by line in Xpress

Reading input file line by line in Xpress

I am trying to iteratively solve a given model as follows

forall(c in 1..|C|) do

     min_x {c'x | A(c)x=b}

end-do

where A(c) is a single line of the matrix A. Matriz A can be huge and I have found out that just by loading it, the entire solving process is slowed down considerably. So what I am trying to do is instead of initializing the whole matrix, I am trying to read line by line and solving the model with it. 

I have tried using fopen and read/readln commands but without any success.

Any thoughts?

Thank you very much

2 Replies

  • FICO's avatar
    FICO
    Community Manager

    Here is a small example how to combine linewise reading with constraint redefinition (for simplicity's sake all data are contained in the model, to read some text data file you just need to remove the "text:" I/O driver prefix in the file name in "fopen"). I am assuming dense data for the coefficient matrix A - if this is not the case, you could define A as "dynamic array" and add delcell(A) before reading in a new line of data, and adapt the reading to sparse-format (that is, index+value) data.

    model "test linewise read"

    uses "mmxprs" uses "mmsystem"

    declarations

      I=1..5   Ctrs=1..3   x: array(I) of mpvar   A: array(I) of real   B: array(Ctrs) of real   C: array(I) of real   public testdata: text end-declarations

    ! Some test data (usually read from file)

    C::(1..5)[1,2,3,4,5] B::(1..3)[10,51,92]

    testdata:=

    ` 1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 `

    ! Invariable portion of the problem definition

    Obj:=sum(i in I) C(i)*x(i) forall(i in I) x(i) is_integer

    ct:=0

    fopen("text:testdata",F_INPUT) while (not iseof) do    forall(i in I) read(A(i))    readln    ct+=1   ! Redefine the constraint at every iteration    Ctr:=sum(i in I) A(i)*x(i) = B(ct)     minimize(Obj)    if getprobstat=XPRS_OPT then     writeln("Solution: ", getobjval)     forall(i in I) write("  ", i, ":", x(i).sol)     writeln    else     writeln("No solution")    end-if end-do fclose(F_INPUT)

    end-model

  • FICO's avatar
    FICO
    Community Manager

    Thank you very much! Exactly what I was looking for.