Skip to content

optimize() can return failure, even when the underlying fit is succesful #517

Closed
@martinmodrak

Description

@martinmodrak

Describe the bug
Using $optimize() I can reliably (on my computer) get an error message even for the most trivial models, that work when called from command line. It appears there is some sort of race condition as forcing cmdstanr to reread the process output once more after the while (procs$active_procs() == 1) loop in .run_other() finishes makes the fit succeed.

To Reproduce

m_test <- cmdstan_model(write_stan_file(r"(
parameters {
  real x;
}
model {
  x ~ normal(0,1);
}
                                        )"))

fit_test <- m_test$optimize()
# Fitting finished unexpectedly! Use the $output() method for more information.

fit_test$output()
# method = optimize

Expected behavior

A succesful optimization fit.

Operating system
Windows 10.

CmdStanR version number
0.4.0.9000 (just downloaded from GitHub)

Additional context

There is no problem when runnning the model from the command line with the exact same arguments cmdstanr uses.
In fact, copying and pasting the output processing code

  output <- procs$get_proc(id)$read_output_lines()
  procs$process_output(output, id)
  error_output <- procs$get_proc(id)$read_error_lines()
  procs$process_error_output(error_output, id)

just below the loop that waits for the process to finish (at

successful_fit <- FALSE
) makes the problem disappear.

I verified that upon last execution at

procs$process_output(output, id)
the output only contains one line: method = optimize, but once the loop finishes, the output contains the whole result, including the crucial "Optimization terminated normally".

I don't think I understand the code enough to know whether this copy-paste of code is a good solution to the underlying problem, but it lets me sidestep the issue for now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions