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

Support CMA-ES with Margin. #121

Merged
merged 13 commits into from
Nov 1, 2022

Conversation

knshnb
Copy link
Contributor

@knshnb knshnb commented Oct 21, 2022

The implementation of CMA-ES with margin by #115 has the following issues.

  • It is implicit that discrete spaces must be located at the latter dimensions.
  • The argument of __init__ is inconsistent with CMA.

This PR is a continuation of #115 and changes the interface of CMAwM to address the issues. The example code would be changed as follows.

import numpy as np
from cmaes import CMAwM


def ellipsoid_onemax(x, n_zdim):
    n = len(x)
    n_rdim = n - n_zdim
    r = 10
    if len(x) < 2:
        raise ValueError("dimension must be greater one")
    ellipsoid = sum([(1000 ** (i / (n_rdim - 1)) * x[i]) ** 2 for i in range(n_rdim)])
    onemax = n_zdim - (0.0 < x[(n - n_zdim) :]).sum()
    return ellipsoid + r * onemax


def main():
    binary_dim, continuous_dim = 10, 10
    dim = binary_dim + continuous_dim
    bounds = np.concatenate(
        [
            np.tile([0, 1], (binary_dim, 1)),
            np.tile([-np.inf, np.inf], (continuous_dim, 1)),
        ]
    )
    steps = np.concatenate([np.ones(binary_dim), np.zeros(continuous_dim)])
    optimizer = CMAwM(mean=np.zeros(dim), sigma=2.0, bounds=bounds, steps=steps)
    print(" evals    f(x)")
    print("======  ==========")

    evals = 0
    while True:
        solutions = []
        for _ in range(optimizer.population_size):
            x_for_eval, x_for_tell = optimizer.ask()
            value = ellipsoid_onemax(x_for_eval, binary_dim)
            evals += 1
            solutions.append((x_for_tell, value))
            if evals % 300 == 0:
                print(f"{evals:5d}  {value:10.5f}")
        optimizer.tell(solutions)

        if optimizer.should_stop():
            break


if __name__ == "__main__":
    main()

@knshnb knshnb force-pushed the cmaes-with-margin-interface2 branch from 3c9dc05 to 9bdfb09 Compare October 21, 2022 05:45
@c-bata
Copy link
Collaborator

c-bata commented Oct 21, 2022

Thank you for your pull request!

@nomuramasahir0 Could you trigger the GitHub action on this PR?

@knshnb knshnb changed the title Cmaes with margin interface2 [WIP] CMA-ES with margin interface 2 Oct 21, 2022
@c-bata
Copy link
Collaborator

c-bata commented Oct 25, 2022

I opened #122 to fix CI problems 🙇

cmaes/_cmawm.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes overall looks good to me and the current interface looks more straightforward than #115.
We might want to consider refactoring duplicated code with CMA class, but as for this change, the current implementation looks good to me. We can work on it as follow-up tasks.

I left two minor comments.

Comment on lines +3 to +4
from scipy.stats import chi2
from scipy.stats import norm
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add scipy in extra_requires?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added cmawm = scipy. ( b3a9608)

cmaes/_cmawm.py Show resolved Hide resolved
@c-bata c-bata mentioned this pull request Oct 27, 2022
@c-bata c-bata changed the title [WIP] CMA-ES with margin interface 2 Support CMA-ES with Margin. Oct 28, 2022
@knshnb
Copy link
Contributor Author

knshnb commented Oct 31, 2022

Thanks for the review! I applied your comments.

I'm sorry for the PR with a large change. Please see cmaes/_cmawm.py and examples/cmaes_with_margin.py in https://github.com/CyberAgentAILab/cmaes/compare/3938dee69ebb707cf68ad7aaebd299001dec0ce7..1ef697199592f2e2433fa3648c95ff3d24afd3b5 for the main change from #115.

Although the behavior with the same seed is different from #115 (because I changed the order of internal representations), I checked that we can obtain similar tendencies in benchmark problems. 2000 trials, 100 runs for each sampler in each problem:
all
(_CmaEsSampler_NopPruner_0: Original, _CmaEsSampler_NopPruner_1: CMA-ES with margin)

Copy link
Collaborator

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! LGTM!

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 this pull request may close these issues.

None yet

3 participants