Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nlopt ignores force_stop if a parameter is excluded by setting min = max #472

Closed
rolffank opened this issue Sep 15, 2022 · 6 comments · Fixed by #473
Closed

nlopt ignores force_stop if a parameter is excluded by setting min = max #472

rolffank opened this issue Sep 15, 2022 · 6 comments · Fixed by #473

Comments

@rolffank
Copy link

rolffank commented Sep 15, 2022

Hi,
I use the C API of NLopt in a C++Builder application (BCB6). My objective function runs a hydrological model of a sewer system to minimize total discharge of combined sewer overflows (CSO). Parameters are the maximal throttle flows of the CSO's.
I added a stop button on the GUI to let the user terminate the optimization if he/she doesn't want to wait for a normal termination. Optimization and manual stop works fine as long as no parameter is excluded by setting min = max for this parameter. Then NLopt ignores the demand to stop and continues until normal termination.
I tested NLOPT_LN_NELDERMEAD and NLOPT_LN_BOBYQA. I can only use derivative-free algorithms.

Here my objective function:

double ObjFunction(unsigned n, const double *x, double *grad, void *my_func_data)
{
  TOptim* optim = (TOptim*)(my_func_data);
  optim->ParamsToNetwork(x);  //  transfer x to parameters in simulation program
  optim->RunSimulation();  // runs simulation program, also checks if stop button was pressed
  double result = optim->GetSimulationResult(); 
  if (result < 0.0)   // something went wrong in simulation program
    optim->Stop();   //  nlopt_result_ = nlopt_force_stop(opt_);
  optim->AddOptimRun(x, result);
  optim->UpdateChartSeries(x, result);
  return result;
}

void TOptim::RunSimulation()
{
  try {
    info_->Text = network_->RunSimulation(false);
  }
  catch (...) {
    ShowMessage("Error during simulation!");
    *doStop_ = true;
  }
  if (AnsiContainsStr(info_->Text, "Error")) {
     ShowMessage("Error during simulation!");
     *doStop_ = true;
  }
  if (*doStop_)
    Stop();
}

TOptim is an own C++ wrapper for NLopt because I couldn't run the official C++ wrapper. I tested it with a simple console application and get an "Abnormal program termination" error.

Thanks, Rolf

@stevengj
Copy link
Owner

Good catch! Can you try the fix in #473 and verify that it solves your problem?

@rolffank
Copy link
Author

rolffank commented Sep 15, 2022

Wow! Thanks for the fast fix! I will try to build the dll on Windows or wait for the new Windows binaries.
I set nlopt_set_maxeval to 100 as workaround (if optimization does not stop before)

@rolffank
Copy link
Author

I installed MinGW on Windows in order to compile nlopt.
There is a broken link to MinGW in your docs. So, I didn't know if I should install MinGW or MinGW-w64.
I installed MinGW from https://osdn.net/projects/mingw/ but there is no pacman to install cmake as explained in your docs:
"If you want to compile NLopt on Windows with MinGW, be sure to install the MinGW version of cmake (e.g. with pacman -S mingw-w64-x86_64-cmake) and then build via cmake -G"MSYS Makefiles" . && make in order to ensure that cmake produces the correct type of makefile."
How can I install pacman on Windows or should I install MinGW-w64?

@jschueller
Copy link
Collaborator

use mingw-w64

@rolffank
Copy link
Author

I installed MSYS2 and used as described: git clone https://github.com/stevengj/nlopt.git to download newest version.

cmake output:
...
Looking for syscall - not found
Looking for isinf - not found
Looking for gettimeofday - found
Looking for qsort_r - not found
...
-- Configuring done
-- Generating done
-- Build files have been written to: C:/msys64/home/rolf/nlopt/build

Afterwards I tried: make
but make complained that there is no target. There is no Makefile.
Any idea what's wrong? Thanks!

@rolffank
Copy link
Author

I tried it successfully in Cygwin but the generated dll is 64-bit. I need a 32-bit dll for the Borland BCC32 compiler.
Is there a flag in the cmake file to generate 32-bit dll or do I need to use MinGW after all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants