Book Reviews


[Day Permalink] Friday, April 30, 2004

[Item Permalink] My first try at a non-trivial Python program: solve optimization problems -- Comment()
I implemented the DE optimization algorithm (Differential Evolution) in Python. The code is available here. Here is a test run:
% /bin/ls DE
__init__.py   de.py   de_fun.py   de_rosen.py   de_test.py
% python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import DE
>>> dir(DE)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', 
'__path__', '__version__', 'arange', 'argsort', 'array', 'de', 
'initialize', 'optimize', 'randint', 'random', 'sample', 
'where', 'zeros']
>>> help(DE)
Help on package DE:
NAME
    DE - The Differential Evolution (DE) optimizer.
...
DESCRIPTION
    Written by Juha Haataja (Juha.Haataja@csc.fi).
    Based on a Fortran 90/95 version from the textbook
      http://www.csc.fi/oppaat/f95/
PACKAGE CONTENTS
    de
    de_fun
    de_rosen
    de_test
...
VERSION
    0.3
>>> import DE.de_test 
First test problem...
Initial minimum value:  42.166977660750504
Dimension:  5
Population size:  25
Coefficient:  0.69999999999999996
Crossover probability:  0.10000000000000001
Iteration and minimum:  0 42.166977660750504
Iteration and minimum:  20 2.1721386210526616
...
Iteration and minimum:  500 0.00011160431004997307
Minimization completed!
Minimum element:  array([  99.99823054,   99.98755403,  
  100.0116417 ,   99.98456   ,  100.01369348])
Minimum cost:  0.00011160431004997307
Time for the run:  5.54
Second test problem...
Initial minimum value:  70.872204463296413
Dimension:  4
Population size:  20
Coefficient:  0.40000000000000002
Crossover probability:  0.20000000000000001
Iteration and minimum:  0 70.872204463296413
Iteration and minimum:  20 2.8363178964823685
...
Iteration and minimum:  500 0.069987620807705986
Minimization completed!
Minimum element:  array([ 0.94056405,  0.88459065,  0.78161181,  0.61824012])
Minimum cost:  0.069987620807705986
Time for the run:  4.1099999999999994
This Python version of the algorithm is about 50 times slower than the Fortran 90/95 version of the code. In my first try I used the Numarray package, and that was five times slower than this version, which uses Numeric.

I like the coding style of Python, and there certainly are a lot of packages to ease programming. But I have already noticed that Python definetely is not suited for computationally heavy use.