optimizationjuliawritetofileipopt

Adding lines to allready exisiting file


I am currently working on an optimization problem where I want to add some information from each IPOPT iteration to a file by using a callback function. I am able to collect the information I need but when I try to add it to the file it only adds a few weird iterations. This is what I am doing (simplified)

I have an outer iteration loop (k) and an inner (the IPOPT iterations)

thefile = "output.txt".      # Create a new file 
f = open(thefile, "w").      # The header to my new file 
@printf(f,"%-10s %-10s %-10s\n ", "outer", "inner", "objval" )

k = 0
while k <= 100 
      iter = []
      objValVector = []

      function my_callback( alg_mod::Cint,
        iter_count::Cint,
        obj_value::Float64,
        inf_pr::Float64,
        inf_du::Float64,
        mu::Float64,
        d_norm::Float64,
        regularization_size::Float64,
        alpha_du::Float64,
        alpha_pr::Float64,
        ls_trials::Cint)                # Using the call back function to get the obj.val
         append!(objValvector, obj_value)
         append!(iter, iter_count)
         return true
      end 
 MOI.set(model, Ipopt.CallbackFunction(), my_callback)
 optimize!(model); 

 f = open(thefile, "a");         # Open the file in append "mode" to add to the existing file
 for i in 1:length(iter)
     @printf(f, "%-10s %-10s %-10s\n", 
            k, iter[i], objValvector[i])
 end

Do something...
k += 1; 


end

I really don't understand why this should not work? The file looks like this: The file


Solution

  • You open the same file twice (which should not happen). And you do not flush the buffer. Hence you are seeing a mix of what made to the disk and what did not make to the disk.

    close(f) in the fourth line and at the end of the code. Or just open it once. Depending on your other code and scenario you might want to flush(f) as well.