Snopt.jl icon indicating copy to clipboard operation
Snopt.jl copied to clipboard

Allow printing of output files to be suppressed and terminal output

Open ajshephard opened this issue 1 year ago • 10 comments

This allows the print and/or summary file output to be suppressed whenever Print file and Summary file are set to empty strings. E.g. options = Dict("Print file" => "", "Summary file" => "").

ajshephard avatar Sep 18 '23 23:09 ajshephard

I will make a few more changes to this PR to address #17 . To avoid adding more options "Print file" => nothing can be used to completely suppress print output, while "Print file" => "" can be used to print to the REPL. (And similarly for the summary file.) The latter can be achieved by calling a Fortran function that returns the stdout unit defined in iso_fortran_env.

ajshephard avatar Sep 20 '23 14:09 ajshephard

The default value of printfile is snopt-print.out so this should not break any existing programs. If "Print file" => nothing, output is suppressed, if "Print file" => "" output is directed to the terminal (which should close #17). I have also made the option non-case sensitive, so Print file, Print File, PRINT FILE, etc., are all valid. Everything here also applies to the summary file.

ajshephard avatar Sep 23 '23 21:09 ajshephard

@ajshephard I tried this, but the behavior appears to only occur once per REPL instance. For example, if I try to run the optimization call a second time, but within the same REPL instance, then the terminal output disappears again.

martinozimek avatar Nov 27 '23 20:11 martinozimek

@martinozimek I can not reproduce this. If I run the following (based on code in examples.jl)

using Snopt

function rosenbrock(g, df, dg, x, deriv)
    f = (1 - x[1])^2 + 100*(x[2] - x[1]^2)^2
    fail = false

    if deriv
        df[1] = -2*(1 - x[1]) + 200*(x[2] - x[1]^2)*-2*x[1]
        df[2] = 200*(x[2] - x[1]^2)
    end

    return f, fail
end


x0 = [4.0; 4.0]
lx = [-5.0; -5.0]
ux = [5.0; 5.0]
lg = []
ug = []
rows = []
cols = []
options = Dict(
    "Print file" => ""
)
println("First")
xopt, fopt, info, out = snopta(rosenbrock, x0, lx, ux, lg, ug, rows, cols, options)

println("Second")
x0 = [4.0; 4.0]
xopt, fopt, info, out = snopta(rosenbrock, x0, lx, ux, lg, ug, rows, cols, options)

Then I get the same print output for both first and second call. Note that snopta is actually overwriting x0, so xopt and x0 are the same at the solution. If you call snopta without setting x0 again then it will converge immediately (so there is no iteration output to print).

ajshephard avatar Dec 13 '23 19:12 ajshephard

AJ are you able to merge your changes into a branch we can pull? I tried to grab yours manually but maybe missed some changes resulting in the one print per terminal error.

davegard1 avatar Jan 08 '24 22:01 davegard1

AJ are you able to merge your changes into a branch we can pull? I tried to grab yours manually but maybe missed some changes resulting in the one print per terminal error.

I'm not able to merge this. I think @andrewning can if he is happy with changes.

ajshephard avatar Jan 09 '24 16:01 ajshephard

Since we seem to be having conflicting behavior probably would be best if we had some additional testing before pulling into the main branch. But note that there is already a branch you can pull from though to test these changes, the branch the pull request is coming from: https://github.com/ajshephard/Snopt.jl/tree/disable_printing

andrewning avatar Jan 09 '24 17:01 andrewning

Got the branch, thanks both.

AJ here are screenshots of my repl output, using your test file: test_problem

repl_output_p1 repl_output_p2

I am on your branch so that should all be aligned. Also I followed instructions in the snopt repo for making changes to the snopt src: "You will need to make a couple of changes to subroutine sn02lib.f. Function snSet, snSeti, and snSetr require the following changes:" "sn27lu.f, sn27lu77.f, and sn27lu90.f contain duplicate symbols. You'll need to keep only one file. I deleted the latter two files. If you are building with SNOPT v7.7 and do not define any user functions, you will also need to delete snopth.f."

I did both steps, which are working fine. I also found I am able to build/use snopt without doing the changes to snSet/Seti/Setr though. I am using snopt v7.7, I did not test the deletion of the files.

I am using WSL and Julia 1.9.2, could it be a WSL or julia version difference? I also had a few others on my team repeat the first print second no print to make sure it wasn't just myself.

Thank you a ton for your time on this! It's been a big help.

davegard1 avatar Jan 10 '24 22:01 davegard1

You all probably know this, but to view progress in realtime I use tail (e.g., tail -f filename). I prefer that over the repl anyway as I can check in on progress in real time (then close it when I don't want to keep monitoring the whole time), and that history is saved so I can compare different runs. Anyway, just an option.

andrewning avatar Jan 11 '24 00:01 andrewning

I can't reproduce this issue.

Can you add the following to the end of the test file, and let me know the output each time you run this?

println(Snopt.STDOUTNUM, ",", Snopt.get_stdout())

ajshephard avatar Feb 06 '24 15:02 ajshephard