diff --git a/.github/workflows/lint_hosted.yml b/.github/workflows/lint_hosted.yml index 98455013a2..46f3a28a82 100644 --- a/.github/workflows/lint_hosted.yml +++ b/.github/workflows/lint_hosted.yml @@ -66,7 +66,7 @@ jobs: - name: Set up MATLAB id: set-up-matlab if: ${{ matrix.linter == 'mlint' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: latest cache: false # We should not cache MATLAB; otherwise, an empty directory will be cached since we remove $MATLABROOT in the next step diff --git a/.github/workflows/parallel_test_matlab.yml b/.github/workflows/parallel_test_matlab.yml index 3c9bbb4f99..d8b6d4550b 100644 --- a/.github/workflows/parallel_test_matlab.yml +++ b/.github/workflows/parallel_test_matlab.yml @@ -81,7 +81,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB with optimization toolbox - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} products: Optimization_Toolbox Parallel_Computing_Toolbox diff --git a/.github/workflows/profile_all.yml b/.github/workflows/profile_all.yml index 0483e6c9ac..5a7ec57e44 100644 --- a/.github/workflows/profile_all.yml +++ b/.github/workflows/profile_all.yml @@ -115,7 +115,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox == 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true @@ -123,7 +123,7 @@ jobs: - name: Set up MATLAB without optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox != 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_all_sq.yml b/.github/workflows/profile_all_sq.yml index 7fdb3ad2b6..f3644cab99 100644 --- a/.github/workflows/profile_all_sq.yml +++ b/.github/workflows/profile_all_sq.yml @@ -111,7 +111,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_bobyqa_small.yml b/.github/workflows/profile_bobyqa_small.yml index 85a29364f0..252e7f939d 100644 --- a/.github/workflows/profile_bobyqa_small.yml +++ b/.github/workflows/profile_bobyqa_small.yml @@ -77,7 +77,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_bobyqa_small_sq.yml b/.github/workflows/profile_bobyqa_small_sq.yml index 31a12d3a3a..ad6b0368f6 100644 --- a/.github/workflows/profile_bobyqa_small_sq.yml +++ b/.github/workflows/profile_bobyqa_small_sq.yml @@ -93,7 +93,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_cobyla_small.yml b/.github/workflows/profile_cobyla_small.yml index f908225158..30364853dc 100644 --- a/.github/workflows/profile_cobyla_small.yml +++ b/.github/workflows/profile_cobyla_small.yml @@ -84,7 +84,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox == 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true @@ -92,7 +92,7 @@ jobs: - name: Set up MATLAB without optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox != 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_cobyla_small_sq.yml b/.github/workflows/profile_cobyla_small_sq.yml index 5d4378d20c..0646f00ff3 100644 --- a/.github/workflows/profile_cobyla_small_sq.yml +++ b/.github/workflows/profile_cobyla_small_sq.yml @@ -96,7 +96,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_compiler_options.yml b/.github/workflows/profile_compiler_options.yml index 0ea1aa7d86..09c17a0222 100644 --- a/.github/workflows/profile_compiler_options.yml +++ b/.github/workflows/profile_compiler_options.yml @@ -90,7 +90,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true'}} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_infnan.yml b/.github/workflows/profile_infnan.yml index c7404c0cd9..e583f151b0 100644 --- a/.github/workflows/profile_infnan.yml +++ b/.github/workflows/profile_infnan.yml @@ -90,7 +90,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_integer_kind.yml b/.github/workflows/profile_integer_kind.yml index e794cb57e9..267f92a0f3 100644 --- a/.github/workflows/profile_integer_kind.yml +++ b/.github/workflows/profile_integer_kind.yml @@ -91,7 +91,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_lincoa_small.yml b/.github/workflows/profile_lincoa_small.yml index bc59484b11..92d4b93803 100644 --- a/.github/workflows/profile_lincoa_small.yml +++ b/.github/workflows/profile_lincoa_small.yml @@ -78,7 +78,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox == 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true @@ -86,7 +86,7 @@ jobs: - name: Set up MATLAB without optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox != 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_lincoa_small_sq.yml b/.github/workflows/profile_lincoa_small_sq.yml index 51f24a3700..d8392f3710 100644 --- a/.github/workflows/profile_lincoa_small_sq.yml +++ b/.github/workflows/profile_lincoa_small_sq.yml @@ -93,7 +93,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_newuoa_small.yml b/.github/workflows/profile_newuoa_small.yml index 13706000d9..3c498dc9c8 100644 --- a/.github/workflows/profile_newuoa_small.yml +++ b/.github/workflows/profile_newuoa_small.yml @@ -77,7 +77,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_newuoa_small_sq.yml b/.github/workflows/profile_newuoa_small_sq.yml index c66e9e468c..8dde7861fb 100644 --- a/.github/workflows/profile_newuoa_small_sq.yml +++ b/.github/workflows/profile_newuoa_small_sq.yml @@ -93,7 +93,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_npt.yml b/.github/workflows/profile_npt.yml index 6c9d8f4657..263b712260 100644 --- a/.github/workflows/profile_npt.yml +++ b/.github/workflows/profile_npt.yml @@ -97,7 +97,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true'}} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_prima_small.yml b/.github/workflows/profile_prima_small.yml index 6ed8cf1c1f..74b76ddd19 100644 --- a/.github/workflows/profile_prima_small.yml +++ b/.github/workflows/profile_prima_small.yml @@ -73,7 +73,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox == 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true @@ -81,7 +81,7 @@ jobs: - name: Set up MATLAB without optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' && matrix.with_optim_toolbox != 'yes' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_quadruple.yml b/.github/workflows/profile_quadruple.yml index 8d6059b53f..e2ae29252d 100644 --- a/.github/workflows/profile_quadruple.yml +++ b/.github/workflows/profile_quadruple.yml @@ -87,7 +87,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_rescue_idz_classical.yml b/.github/workflows/profile_rescue_idz_classical.yml index 66c058791f..09a4992701 100644 --- a/.github/workflows/profile_rescue_idz_classical.yml +++ b/.github/workflows/profile_rescue_idz_classical.yml @@ -88,7 +88,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_rescue_idz_modernized.yml b/.github/workflows/profile_rescue_idz_modernized.yml index 58cae01298..bacfa8ca14 100644 --- a/.github/workflows/profile_rescue_idz_modernized.yml +++ b/.github/workflows/profile_rescue_idz_modernized.yml @@ -88,7 +88,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_single.yml b/.github/workflows/profile_single.yml index 89e1f58166..d9aadeed5a 100644 --- a/.github/workflows/profile_single.yml +++ b/.github/workflows/profile_single.yml @@ -88,7 +88,7 @@ jobs: - name: Set up MATLAB with optimization toolbox if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_uobyqa_small.yml b/.github/workflows/profile_uobyqa_small.yml index d42975913c..8d4d53e47a 100644 --- a/.github/workflows/profile_uobyqa_small.yml +++ b/.github/workflows/profile_uobyqa_small.yml @@ -76,7 +76,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/profile_uobyqa_small_sq.yml b/.github/workflows/profile_uobyqa_small_sq.yml index 40010ee59a..5f288052de 100644 --- a/.github/workflows/profile_uobyqa_small_sq.yml +++ b/.github/workflows/profile_uobyqa_small_sq.yml @@ -95,7 +95,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/recursive_test_matlab.yml b/.github/workflows/recursive_test_matlab.yml index 61a994abf3..7e15a533b7 100644 --- a/.github/workflows/recursive_test_matlab.yml +++ b/.github/workflows/recursive_test_matlab.yml @@ -139,7 +139,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB with optimization toolbox - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/stress_test_matlab.yml b/.github/workflows/stress_test_matlab.yml index 2020c246e6..1cba5b677a 100644 --- a/.github/workflows/stress_test_matlab.yml +++ b/.github/workflows/stress_test_matlab.yml @@ -140,7 +140,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB with optimization toolbox - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/test_matlab.yml b/.github/workflows/test_matlab.yml index cd37c43312..e3a9b5ac46 100644 --- a/.github/workflows/test_matlab.yml +++ b/.github/workflows/test_matlab.yml @@ -153,7 +153,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/test_matlab_linux.yml b/.github/workflows/test_matlab_linux.yml index 32c0100bbb..d9a7e0a34f 100644 --- a/.github/workflows/test_matlab_linux.yml +++ b/.github/workflows/test_matlab_linux.yml @@ -86,7 +86,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/test_matlab_mac_intel.yml b/.github/workflows/test_matlab_mac_intel.yml index 4940ee5e26..cfd5670c7a 100644 --- a/.github/workflows/test_matlab_mac_intel.yml +++ b/.github/workflows/test_matlab_mac_intel.yml @@ -86,7 +86,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/test_matlab_windows.yml b/.github/workflows/test_matlab_windows.yml index aa4ff15ac7..9f8de23e2e 100644 --- a/.github/workflows/test_matlab_windows.yml +++ b/.github/workflows/test_matlab_windows.yml @@ -86,7 +86,7 @@ jobs: run: cmd.exe "/K" '".github\scripts\install_oneapi_windows.bat 2023"' - name: Set up MATLAB - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/verify_archiva.yml b/.github/workflows/verify_archiva.yml index 2b369cccc8..01354742c3 100644 --- a/.github/workflows/verify_archiva.yml +++ b/.github/workflows/verify_archiva.yml @@ -91,7 +91,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/verify_big.yml b/.github/workflows/verify_big.yml index 179b526d89..783744a429 100644 --- a/.github/workflows/verify_big.yml +++ b/.github/workflows/verify_big.yml @@ -70,7 +70,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/verify_large.yml b/.github/workflows/verify_large.yml index 9291e27726..a690235f2c 100644 --- a/.github/workflows/verify_large.yml +++ b/.github/workflows/verify_large.yml @@ -70,7 +70,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/.github/workflows/verify_small.yml b/.github/workflows/verify_small.yml index d87ba7f247..3840b2c2ce 100644 --- a/.github/workflows/verify_small.yml +++ b/.github/workflows/verify_small.yml @@ -69,7 +69,7 @@ jobs: - name: Set up MATLAB if: ${{ steps.check_matlab.outputs.has_matlab != 'true' }} - uses: matlab-actions/setup-matlab@v2.1.0 + uses: matlab-actions/setup-matlab@v2.1.1 with: release: ${{ matrix.matlab }} cache: true diff --git a/c/include/prima/prima.h b/c/include/prima/prima.h index bc8ce3c729..6a041816d8 100644 --- a/c/include/prima/prima.h +++ b/c/include/prima/prima.h @@ -245,6 +245,9 @@ int prima_init_options(prima_options_t *const options); // Structure to hold the result +// prima_minimize will allocate the memory for x and nlconstr (if needed), +// and as such the user is expected to free the memory using prima_free_result +// once they are done using the contents of the result (or have saved the contents). typedef struct { // x: returned point @@ -277,11 +280,14 @@ int prima_free_result(prima_result_t *const result); /* - * The function that does the minimization using a PRIMA solver - * algorithm : optimization algorithm (see prima_algorithm) - * problem : optimization problem (see prima_problem) - * options : optimization options (see prima_options) - * result : optimization result (see prima_result) + * The function that does the minimization using a PRIMA solver. + * After using (or saving) the result, the user is expected to call + * prima_free_result, regardless of the return value of prima_minimize, + * in order to avoid memory leaks. + * algorithm : optimization algorithm (see prima_algorithm_t) + * problem : optimization problem (see prima_problem_t) + * options : optimization options (see prima_options_t) + * result : optimization result (see prima_result_t) * return : see prima_rc_t enum for return codes */ PRIMAC_API diff --git a/c/prima.c b/c/prima.c index 8cea9e7c78..ce16e276da 100644 --- a/c/prima.c +++ b/c/prima.c @@ -93,32 +93,12 @@ int prima_init_result(prima_result_t *const result, const prima_problem_t proble memset(result, 0, sizeof(prima_result_t)); - // x: returned point - result->x = (double*)malloc(problem.n * sizeof(double)); - if (!result->x) - return PRIMA_MEMORY_ALLOCATION_FAILS; - for (int i = 0; i < problem.n; i++) - result->x[i] = problem.x0[i]; - // f: objective function value at the returned point result->f = NAN; // cstrv: constraint violation at the returned point (COBYLA and LINCOA only) result->cstrv = NAN; - // nlconstr: nonlinear constraint values at the returned point, of size m_nlcon (COBYLA only) - if (problem.m_nlcon <= 0) - result->nlconstr = NULL; - else { - result->nlconstr = (double*)malloc(problem.m_nlcon * sizeof(double)); - if (!result->nlconstr) { - free(result->x); - return PRIMA_MEMORY_ALLOCATION_FAILS; - } - for (int i = 0; i < problem.m_nlcon; i++) - result->nlconstr[i] = NAN; - } - // nf: number of function evaluations result->nf = INT_MIN; @@ -128,6 +108,22 @@ int prima_init_result(prima_result_t *const result, const prima_problem_t proble // message: exit message result->message = NULL; + // x: returned point + result->x = (double*)malloc(problem.n * sizeof(double)); + if (!result->x) + return PRIMA_MEMORY_ALLOCATION_FAILS; + for (int i = 0; i < problem.n; i++) + result->x[i] = NAN; + + // nlconstr: nonlinear constraint values at the returned point, of size m_nlcon (COBYLA only) + result->nlconstr = (double*)malloc(problem.m_nlcon * sizeof(double)); + if (!result->nlconstr) { + free(result->x); + return PRIMA_MEMORY_ALLOCATION_FAILS; + } + for (int i = 0; i < problem.m_nlcon; i++) + result->nlconstr[i] = NAN; + return 0; } @@ -238,12 +234,19 @@ int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t prob { int use_constr = (algorithm == PRIMA_COBYLA); - int info = prima_check_problem(problem, use_constr, algorithm); + int info = prima_init_result(result, problem); if (info == 0) - info = prima_init_result(result, problem); + info = prima_check_problem(problem, use_constr, algorithm); if (info == 0) { + // We copy x0 into result->x only after prima_check_problem has succeeded, + // so that if prima_check_problem failed, result->x will not contained a + // seemingly valid value. + for (int i = 0; i < problem.n; i++) { + result->x[i] = problem.x0[i]; + } + switch (algorithm) { case PRIMA_BOBYQA: bobyqa_c(problem.calfun, options.data, problem.n, result->x, &(result->f), problem.xl, problem.xu, &(result->nf), options.rhobeg, options.rhoend, options.ftarget, options.maxfun, options.npt, options.iprint, options.callback, &info); @@ -275,10 +278,10 @@ int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t prob default: return PRIMA_INVALID_INPUT; } - - result->status = info; - result->message = prima_get_rc_string(info); } + result->status = info; + result->message = prima_get_rc_string(info); + return info; }