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

Results of a minimization are out of bound #244

Closed
marco-palmiotto opened this issue Oct 23, 2024 · 6 comments
Closed

Results of a minimization are out of bound #244

marco-palmiotto opened this issue Oct 23, 2024 · 6 comments

Comments

@marco-palmiotto
Copy link

marco-palmiotto commented Oct 23, 2024

Hi all, I started using CMA-ES recently, and I wanted to thank you for this remarkable work!

I have a question about bounds to parameters. I am now doing a minimization of a function in 1 variable, imposing a bound on it. I followed the guide, and my code (at least its relevant part) is the following:

constexpr const int npar=1;
const double lbounds[1] ={1.0e-20},
             ubounds[1] ={1.0};

constexpr const double init_seed_g_chi=0.3;
std::vector<double> x0;
x0.reserve(npar);
x0.emplace_back(init_seed_g_chi);

GenoPheno<pwqBoundStrategy> gp(lbounds,ubounds,npar); // genotype / phenotype transform associated to bounds.

// -1 for automatically decided lambda, 0 is for random	seeding	of the internal generator.           
CMAParameters<GenoPheno<pwqBoundStrategy>> cmaparams(x0,sigma,-1,0,gp);     

cmaparams.set_algo(aCMAES);
FitFunc f = function_to_minimize;
CMASolutions cmasols = cmaes<GenoPheno<pwqBoundStrategy>>(f, cmaparams);

std::cout << "seed g_chi is\n";
for(int i=0; i < npar ; i++) std::cout <<  x0[i] << '\n';
std::cout << "\nbest solution: ";
cmasols.print(std::cout,0,gp);
std::cout << '\n';
std::cout << "Expected Distance from Minimum: " << cmasols.edm() << '\n';
std::cout << "optimization took " << cmasols.elapsed_time() / 1000.0 << " seconds\n";

Candidate bcand = cmasols.best_candidate();
double fmin = bcand.get_fvalue(); // min objective function value the optimizer converged to
std::vector<double> x_stdv = bcand.get_x(); // vector of objective function parameters at minimum.
const double* x_dptr = bcand.get_x_ptr(); // vector of objective function parameters at minimum, as C-style double array
Eigen::VectorXd x_ev = bcand.get_x_dvec(); // vector of objective function parameters at minimum, as Eigen vector
double edm = cmasols.edm(); // expected distance to the minimum.

std::cout << "# m_chi   m_phi    g_chi   pull-Oh2\nRESULT=";
std::cout << input.m_chi.get() << '\t' << input.m_phi.get() << '\t' << x_dptr[0] << '\t' << fmin << '\n';

where function_to_minimize is defined as a lambda function.

The problem is that the result I have is 1.1 for the minimum, which is out of the bound [1.0e-20, 1.0] that I provided. So I wanted to ask you what am I missing.

I have also another question, in case I will open another topic, but it is very simple. Is there a way to "impose" the value of the function at minimum? For instance, I want this function to be 0.0 (because I expect 0 to be its minimum), but instead the f-value is around 30.

Thanks in advance for your answers :)

EDIT The output is:

best solution: best solution => f-value=0.345947 / fevals=280 / sigma=0.00112601 / iter=70 / elaps=351247ms / x=1
Expected Distance from Minimum: 0
optimization took 351.247 seconds
# m_chi   m_phi    g_chi   pull-Oh2
RESULT=10000    22000   1.1     0.345947

I notice now a mismatch between the parameter in the last line and the one in the best solution line.

EDIT 2 I tried to "free" another parameter of the function (it actually has 3 parameters, but I wanted to fix 2 and vary just one), and by minimising with 2 parameters I get consistent points (i.e. withn the bounds, and with a value close to 0)

@beniz
Copy link
Collaborator

beniz commented Oct 23, 2024

Hi, can you show the exact code output ? what value is 1.1, the best solution or the edm ? Also look at this comment and issue #231 (comment)

@marco-palmiotto
Copy link
Author

marco-palmiotto commented Oct 23, 2024

Hi, can you show the exact code output ? what value is 1.1, the best solution or the edm ? Also look at this comment and issue #231 (comment)

Hi, I updated the text of the question, with the output. I looked at the issue you linked, and it appears to me that I should try as well best_seen_candidate, and gp.pheno(best_candidate().get_x_dvec()).transpose() to display the output. Am I correct?

Also, it seems there's a discrepancy between x shown in the best solution and xdptr[0] printed later. I wonder if I did something wrong there, because 1 can be an acceptable result: it would mean that the function has a minimum at the extremum of the interval (which can make sense).

@beniz
Copy link
Collaborator

beniz commented Oct 23, 2024

I notice now a mismatch between the parameter in the last line and the one in the best solution line.

You may want to show the code for this last line.

@marco-palmiotto
Copy link
Author

I notice now a mismatch between the parameter in the last line and the one in the best solution line.

You may want to show the code for this last line.

My bad, it was in the updated message, but unformatted. Now it should appear properly.

@beniz
Copy link
Collaborator

beniz commented Oct 23, 2024

Then I believe you have found the answer in the linked comment above: apply pheno to the best_candidate() output.

@marco-palmiotto
Copy link
Author

Then I believe you have found the answer in the linked comment above: apply pheno to the best_candidate() output.

Thanks! I mark the issue as closed

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

No branches or pull requests

2 participants