File output
If we do not want the output of procedure print_tab in the previous section to be displayed on screen but to be saved in the file out.txt, we simply open the file for writing at the beginning of the procedure by adding
fopen("out.txt",F_OUTPUT)
before the first writeln statement, and close it at the end of the procedure, after the last writeln statement with
fclose(F_OUTPUT)
If we do not want any existing contents of the file out.txt to be deleted, so that the result table is appended to the end of the file, we need to write the following for opening the file (closing it the same way as before):
fopen("out.txt",F_OUTPUT+F_APPEND)
As with input of data from file, there are several ways of outputting data to a file in Mosel. The following example demonstrates three different ways of writing the contents of an array A to a file. The last section (Solution output with initializations to) shows how to proceed if the data is not readily available in the form of an array but results from the evaluation of an expression (e.g., solution values, function calls).
Data output with initializations to
The first method uses the initializations block for creating or updating a file in Mosel's initializations format.
model "Trio output (1)" declarations A: array(-1..1,5..7) of real end-declarations A :: [ 2, 4, 6, 12, 14, 16, 22, 24, 26] ! First method: use an initializations block initializations to "out_1.dat" A as "MYOUT" end-initializations end-model
File out_1.dat will contain the following:
'MYOUT': [2 4 6 12 14 16 22 24 26]
If this file contains already a data entry MYOUT, it is replaced with this output without modifying or deleting any other contents of this file. Otherwise, the output is appended at the end of it.
Note: For solution output with initializations to please see Section Solution output with initializations to below.
Data output with writeln
As mentioned above, we may create freely formatted output files by redirecting the output of write and writeln statements to a file:
model "Trio output (2)" declarations A: array(-1..1,5..7) of real end-declarations A :: [ 2, 4, 6, 12, 14, 16, 22, 24, 26] ! Second method: use the built-in writeln function fopen("out_2.dat", F_OUTPUT) forall(i in -1..1, j in 5..7) writeln('A_out(', i, ' and ', j, ') = ', A(i,j)) fclose(F_OUTPUT) end-model
The nicely formatted output to out_2.dat results in the following:
A_out(-1 and 5) = 2 A_out(-1 and 6) = 4 A_out(-1 and 7) = 6 A_out(0 and 5) = 12 A_out(0 and 6) = 14 A_out(0 and 7) = 16 A_out(1 and 5) = 22 A_out(1 and 6) = 24 A_out(1 and 7) = 26
Data output with diskdata
As a third possibility, one may use the diskdata subroutine from module mmetc to write out comma separated value (CSV) files.
model "Trio output (3)" uses "mmetc" declarations A: array(-1..1,5..7) of real end-declarations A :: [ 2, 4, 6, 12, 14, 16, 22, 24, 26] ! Third method: use diskdata diskdata(ETC_OUT+ETC_SPARSE,"out_3.dat", A) end-model
The output with diskdata simply prints the contents of the array to out_3.dat, with option ETC_SPARSE each entry is preceded by the corresponding indices:
-1,5,2 -1,6,4 -1,7,6 0,5,12 0,6,14 0,7,16 1,5,22 1,6,24 1,7,26
Without option ETC_SPARSE out_3.dat looks as follows:
2,4,6 12,14,16 22,24,26
Instead of using the diskdata subroutine, we may equally use the diskdata IO driver that is defined by the same module, mmetc. In the example above we replace the diskdata statement by the following initializations to block.
[ initializations to 'mmetc.diskdata:' A as 'sparse,out_3.dat' end-initializations
Solution output with initializations to
In the previous examples we have seen how to write out to a file the contents of a data array defined in Mosel. The free format output with write/writeln can be applied to any type of expression. However, if we wish to use the initializations to functionality for writing out, for instance, the solution values after an optimization run or the result of a Mosel function we need to proceed in a slightly different way from what we have seen so far.
There are two options:
- Save the solution values or results into a new Mosel object and work with this copy for the file output. For example,
declarations x: mpvar x_sol: real y: array(R) of mpvar y_sol: array(R) of real end-declarations ... ! Define and solve an optimization problem x_sol:= x.sol ! Retrieve the solution values forall(i in R) y_sol(i):= y(i).sol initializations to "out.txt" x_sol y_sol end-initializations
- Use the keyword evaluation in the initializations block.
declarations x: mpvar y: array(R) of mpvar end-declarations ... ! Define and solve an optimization problem initializations to "out.txt" evaluation of x.sol as "x_sol" evaluation of array(i in R) y(i).sol as "y_sol" end-initializations
The array construct is used in the model extract above to generate a new array 'on the fly'. Its use is similar to aggregate operators such as sum or union.
The use of the marker evaluation of is not restricted to solution values; it allows you to work with any type of expression directly in the initializations block, including results of Mosel functions or computations as shown in the following example initeval.mos. This model writes out detailed results for our introductory Chess example (see Section Solving the chess set problem).
model "Evaluations" uses "mmxprs" declarations small,large: mpvar ! Decision variables: produced quantities end-declarations Profit:= 5*small + 20*large ! Objective function Lathe:= 3*small + 2*large <= 160 ! Lathe-hours Boxwood:= small + 3*large <= 200 ! kg of boxwood small is_integer; large is_integer ! Integrality constraints maximize(Profit) ! Solve the problem initializations to "chessout.txt" evaluation of getparam("XPRS_mipstatus") as "Status" evaluation of getobjval as "Objective" evaluation of small.sol as "small_sol" evaluation of getsol(large) as "large_sol" evaluation of Lathe.slack as "Spare time" evaluation of Boxwood.act as "Used wood" evaluation of Boxwood.act-200 as "Spare wood" evaluation of [ small.sol, large.sol ] as "x_sol" end-initializations end-model
The resulting output file chessout.txt has the following contents:
'Status': 6 'Objective': 1330 'small_sol': 2 'large_sol': 66 'Spare time': 22 'Used wood': 200 'Spare wood': 0 'x_sol': [2 66]
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.