Giter VIP home page Giter VIP logo

krzysztofarendt / modestga Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 1.0 697 KB

Optimized and benchmarked parallel Genetic Algorithm with inequality constraints, and a scipy-like interface

License: BSD 2-Clause "Simplified" License

Python 98.17% Shell 1.83%
constrained-optimization optimization genetic-algorithm parallel parallel-computing multiprocessing nonlinear-optimization nonconvex-optimization

modestga's People

Contributors

dependabot[bot] avatar krzysztofarendt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

manasdas17

modestga's Issues

Serialization of exotic functions fails on Windows

The following unit test fails on Windows:

======================================================================
ERROR: test_exotic_pickling (__main__.TestModestga)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "modestga\test\test.py", line 233, in test_exotic_pickling
    workers=2)
  File "d:\code\modestga\modestga\ga.py", line 167, in minimize
    p.start()
  File "D:\Anaconda3\envs\modestga\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "D:\Anaconda3\envs\modestga\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:\Anaconda3\envs\modestga\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "D:\Anaconda3\envs\modestga\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "D:\Anaconda3\envs\modestga\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'TestModestga.test_exotic_pickling.<locals>.FunWrapper'

----------------------------------------------------------------------

On Linux it works fine.

Examples fail on Windows

When running examples using multiprocessing on Windows, the following errors pops out:

RuntimeError:                                                                                                                                                                                                                                                           An attempt has been made to start a new process before the                                                                                                                                                                                                      current process has finished its bootstrapping phase.                                                                                                                                                                                                                                                                                                                                                                                                                                                                           This probably means that you are not using fork to start your                                                                                                                                                                                                   child processes and you have forgotten to use the proper idiom                                                                                                                                                                                                  in the main module:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 if __name__ == '__main__':                                                                                                                                                                                                                                          freeze_support()                                                                                                                                                                                                                                                ...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     The "freeze_support()" line can be omitted if the program                                                                                                                                                                                                       is not going to be frozen to produce an executable.          

It is because Windows requires the code using multiprocessing to be enclosed with the condition:
if __name__ == '__main__'.

The examples work fine on Linux.

There should warning about this also in README.

Rastrigin funcion example doesn't work

krza@DESKTOP-3738N1E:~/code/modestga$ python modestga/examples/min_rastrigin_fun.py
Traceback (most recent call last):
  File "modestga/examples/min_rastrigin_fun.py", line 37, in <module>
    res = minimize(fun, bounds, args=args, callback=callback, options=options)
  File "/home/krza/code/modestga/modestga/ga.py", line 171, in minimize
    pop = population.Population(opts['pop_size'], bounds, fun, args=args, evaluate=True)
  File "/home/krza/code/modestga/modestga/population.py", line 28, in __init__
    val=val
  File "/home/krza/code/modestga/modestga/individual.py", line 51, in __init__
    self.val = self.evaluate()
  File "/home/krza/code/modestga/modestga/individual.py", line 68, in evaluate
    self.val = self.fun(self.get_estimates(), *self.args)
TypeError: rastrigin() takes 1 positional argument but 3 were given

Rewrite benchmark framework

Multiple benchmark cases are likely to be added in the future. They all should be put in some kind of a framework with YAML configuration files.

Argument passing fails with workers > 1

Failing test:

======================================================================
ERROR: test_args_passing_2workers (__main__.TestModestga)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "modestga/test/test.py", line 209, in test_args_passing_2workers
    workers=2)
  File "/home/krza/code/modestga/modestga/ga.py", line 258, in minimize
    data_from.append(pipes[i].recv())
  File "/usr/lib/python3.6/multiprocessing/connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "/usr/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
    buf = self._recv(4)
  File "/usr/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError

----------------------------------------------------------------------

Function evaluation counter in GA seems to be broken

Results from 70 cases using Rastrigin function with varying number of parameters:

ng_vs_nfev

Used settings (those other than default included only):

    options = {
        'generations': 1000,  # Max. number of generations
        'pop_size': 100,      # Population size
        'tol': 1e-3           # Solution tolerance
    }

Exception handling in function evaluation

We cannot guarantee that the cost function will work for the entire range of parameters. In example in ModestPy FMU solvers sometimes fail when unphysical parameters are used (see: sdu-cfei/modest-py#68).

It should be fairly easy to implement some generic exception handling. I think it is sufficient to enclose the following line with a try... except... block:

self.val = self.fun(self.get_estimates(), *self.args)

When exception is caught the function value could be +infinity or some very large number.

Remove jupyter notebooks

Inter-method comparisons should be implemented in pure Python scripts. Remove notebooks from modestga/benchmark.

Exception handling or time out needed in the parallel mode

When an exception happens in one of the threads, the main process waits indefinitely for its result. A timeout or exception handling should be implemented.

An example from one of the users of ModestPy:

Traceback (most recent call last):

  File "C:\Users\lucjsar\Documents\ModestPy\lucile_GBM_max_flows.py", line 151, in <module>
    estimates = session.estimate()

  File "C:\Users\lucjsar\Documents\ModestPy\modestpy\estimation.py", line 289, in estimate
    m_estimates = m_inst.estimate()

  File "C:\Users\lucjsar\Documents\ModestPy\modestpy\estim\ga_parallel\ga_parallel.py", line 315, in estimate
    workers=self.workers)

  File "C:\Users\lucjsar\AppData\Local\Continuum\anaconda3\lib\site-packages\modestga\ga.py", line 257, in minimize
    if pipes[i].poll(0.001):

  File "C:\Users\lucjsar\AppData\Local\Continuum\anaconda3\lib\multiprocessing\connection.py", line 257, in poll
    return self._poll(timeout)

 File "C:\Users\lucjsar\AppData\Local\Continuum\anaconda3\lib\multiprocessing\connection.py", line 330, in _poll
    return bool(wait([self], timeout))

  File "C:\Users\lucjsar\AppData\Local\Continuum\anaconda3\lib\multiprocessing\connection.py", line 868, in wait
    ready_handles = _exhaustive_wait(waithandle_to_obj.keys(), timeout)

  File "C:\Users\lucjsar\AppData\Local\Continuum\anaconda3\lib\multiprocessing\connection.py", line 800, in _exhaustive_wait
    res = _winapi.WaitForMultipleObjects(L, False, timeout)

Benchmark GA against other methods

Compare the performance of the implemented GA with other available methods, especially Monte Carlo (own implementation) and Differential Evolution (from SciPy).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.