Forum Discussion
FICO
10 years agoCommunity Manager
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 o...
FICO
10 years agoCommunity 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_integerct:=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

