(argv=None)
to install and/or test from the command line use::

        python cma.py [options | func dim sig0 [optkey optval][optkey optval]...]

    --test (or -t) to run the doctest, ``--test -v`` to get (much) verbosity
    and ``--test -q`` to run it quietly with output only in case of errors.

    install to install cma.py (uses setup from distutils.core).

    --fcts and --doc for more infos or start ipython --pylab.

    Examples
    --------
    First, testing with the local python distribution::

        python cma.py --test

    If succeeded install (uses setup from distutils.core)::

        python cma.py install

    A single run on the ellipsoid function::

        python cma.py elli 10 1
to install and/or test from the command line use::
to install and / or test from the command line use ::
def main(argv=None):
    """to install and/or test from the command line use::

        python cma.py [options | func dim sig0 [optkey optval][optkey optval]...]

    --test (or -t) to run the doctest, ``--test -v`` to get (much) verbosity
    and ``--test -q`` to run it quietly with output only in case of errors.

    install to install cma.py (uses setup from distutils.core).

    --fcts and --doc for more infos or start ipython --pylab.

    Examples
    --------
    First, testing with the local python distribution::

        python cma.py --test

    If succeeded install (uses setup from distutils.core)::

        python cma.py install

    A single run on the ellipsoid function::

        python cma.py elli 10 1

    """
    if argv is None:
        argv = sys.argv  # should have better been sys.argv[1:]

    # uncomment for unit test
    # _test()
    # handle input arguments, getopt might be helpful ;-)
    if len(argv) >= 1:  # function and help
        if len(argv) == 1 or argv[1].startswith('-h') or argv[1].startswith('--help'):
            print(main.__doc__)
            fun = None
        elif argv[1].startswith('-t') or argv[1].startswith('--test'):
            import doctest
            if len(argv) > 2 and (argv[2].startswith('--v') or argv[2].startswith('-v')):  # verbose
                print('doctest for cma.py: due to different platforms and python versions')
                print('and in some cases due to a missing unique random seed')
                print('many examples will "fail". This is OK, if they give a similar')
                print('to the expected result and if no exception occurs. ')
                # if argv[1][2] == 'v':
                doctest.testmod(report=True)  # this is quite cool!
            else:  # was: if len(argv) > 2 and (argv[2].startswith('--qu') or argv[2].startswith('-q')):
                print('doctest for cma.py: launching (it might be necessary to close a few pop up windows to finish)')
                fn = '__cma_doctest__.txt'
                stdout = sys.stdout
                try:
                    with open(fn, 'w') as f:
                        sys.stdout = f
                        doctest.testmod(report=True)  # this is quite cool!
                finally:
                    sys.stdout = stdout
                process_test(fn)
                print('doctest for cma.py: finished (no other output should be seen after launching)')
            return
        elif argv[1] == '--doc':
            print(__doc__)
            print(CMAEvolutionStrategy.__doc__)
            print(fmin.__doc__)
            fun = None
        elif argv[1] == '--fcts':
            print('List of valid function names:')
            print([d for d in dir(fcts) if not d.startswith('_')])
            fun = None
        elif argv[1] in ('install', '--install'):
            from distutils.core import setup
            setup(name = "cma",
                  version = __version__,
                  author = "Nikolaus Hansen",
                  #    packages = ["cma"],
                  py_modules = ["cma"],
                  )
            fun = None
        elif argv[1] in ('plot',):
            plot()
            raw_input('press return')
            fun = None
        elif len(argv) > 3:
            fun = eval('fcts.' + argv[1])
        else:
            print('try -h option')
            fun = None

    if fun is not None:

        if len(argv) > 2:  # dimension
            x0 = np.ones(eval(argv[2]))
        if len(argv) > 3:  # sigma
            sig0 = eval(argv[3])

        opts = {}
        for i in xrange(5, len(argv), 2):
            opts[argv[i-1]] = eval(argv[i])

        # run fmin
        if fun is not None:
            tic = time.time()
            fmin(fun, x0, sig0, **opts)  # ftarget=1e-9, tolfacupx=1e9, verb_log=10)
            # plot()
            # print ' best function value ', res[2]['es'].best[1]
            print('elapsed time [s]: + %.2f', round(time.time() - tic, 2))

    elif not len(argv):
        fmin(fcts.elli, np.ones(6)*0.1, 0.1, ftarget=1e-9)
def main ( argv = None ) : if argv is None : argv = sys . argv # should have better been sys.argv[1:] # uncomment for unit test # _test() # handle input arguments, getopt might be helpful ;-) if len ( argv ) >= 1 : # function and help if len ( argv ) == 1 or argv [ 1 ] . startswith ( '-h' ) or argv [ 1 ] . startswith ( '--help' ) : print ( main . __doc__ ) fun = None elif argv [ 1 ] . startswith ( '-t' ) or argv [ 1 ] . startswith ( '--test' ) : import doctest if len ( argv ) > 2 and ( argv [ 2 ] . startswith ( '--v' ) or argv [ 2 ] . startswith ( '-v' ) ) : # verbose print ( 'doctest for cma.py: due to different platforms and python versions' ) print ( 'and in some cases due to a missing unique random seed' ) print ( 'many examples will "fail". This is OK, if they give a similar' ) print ( 'to the expected result and if no exception occurs. ' ) # if argv[1][2] == 'v': doctest . testmod ( report = True ) # this is quite cool! else : # was: if len(argv) > 2 and (argv[2].startswith('--qu') or argv[2].startswith('-q')): print ( 'doctest for cma.py: launching (it might be necessary to close a few pop up windows to finish)' ) fn = '__cma_doctest__.txt' stdout = sys . stdout try : with open ( fn , 'w' ) as f : sys . stdout = f doctest . testmod ( report = True ) # this is quite cool! finally : sys . stdout = stdout process_test ( fn ) print ( 'doctest for cma.py: finished (no other output should be seen after launching)' ) return elif argv [ 1 ] == '--doc' : print ( __doc__ ) print ( CMAEvolutionStrategy . __doc__ ) print ( fmin . __doc__ ) fun = None elif argv [ 1 ] == '--fcts' : print ( 'List of valid function names:' ) print ( [ d for d in dir ( fcts ) if not d . startswith ( '_' ) ] ) fun = None elif argv [ 1 ] in ( 'install' , '--install' ) : from distutils . core import setup setup ( name = "cma" , version = __version__ , author = "Nikolaus Hansen" , #    packages = ["cma"], py_modules = [ "cma" ] , ) fun = None elif argv [ 1 ] in ( 'plot' , ) : plot ( ) raw_input ( 'press return' ) fun = None elif len ( argv ) > 3 : fun = eval ( 'fcts.' + argv [ 1 ] ) else : print ( 'try -h option' ) fun = None if fun is not None : if len ( argv ) > 2 : # dimension x0 = np . ones ( eval ( argv [ 2 ] ) ) if len ( argv ) > 3 : # sigma sig0 = eval ( argv [ 3 ] ) opts = { } for i in xrange ( 5 , len ( argv ) , 2 ) : opts [ argv [ i - 1 ] ] = eval ( argv [ i ] ) # run fmin if fun is not None : tic = time . time ( ) fmin ( fun , x0 , sig0 , * * opts ) # ftarget=1e-9, tolfacupx=1e9, verb_log=10) # plot() # print ' best function value ', res[2]['es'].best[1] print ( 'elapsed time [s]: + %.2f' , round ( time . time ( ) - tic , 2 ) ) elif not len ( argv ) : fmin ( fcts . elli , np . ones ( 6 ) * 0.1 , 0.1 , ftarget = 1e-9 )