A short Python intro # start Python (python -E, ipython --quick) python -E # don't load rc file for the time being # use as a calculator a = 3 + 4 2**4 _ + 5 a, b = 1, 2 a, b = b, a # swap a, b = c, d = 11, 22 # check out Ctrl-p ! Type "2" and "Ctrl-p" t = (33, 44) type(t) a, b = t # unpack a tuple 2/3 # I don't like it and Guido also doesn't like it # get rid of /-integer division: from __future__ import division 2/3, 2//3 # "real" vs integer division exp(2) # we will solve this problem later # to have mathematical functions (sum, sin, cos) s = 'abc' type(s) s + 'def' dir() s # Crtl-p is the magic key to get back matching previous commands # TAB-completion will work in ipython (see below) l = [1,2] + [3,4] # a list of length 4 range(5) for i in range(1, 10): # caveat: doesn't print 10 print(i) for c in 'hello world': print(c) # dict data type: dictionary, look-up table, key-value-map, associative array, # (like a list container, but with freely chosen keys and no fixed order) d = {} d[1] = (2, 3) d['2'] = 44 d[(3,4)] = 55 # keys must be immutable (tuples are) d d2 = d # d and d2 now refer to the same object d2 is d d.copy() d3 = d.copy() d3 == d d3 is d # in the OS system shell (ctrl-z might work to get there): echo "print(' ---printing in test.py--- ')" > test.py # back in the Python shell: exec(open('test.py')) # in ipython also: %run test.py or just run test.py # (clean way: with open('test.py') as fh: exec(fh)) # we can "import test", but there is nothing interesting defined in test import numpy as np # np is just an alias, like setting np = numpy # check also `reload` for modules which (we) have changed after the last import np.exp(2) # this is how to use math functions in code exp = np.exp exp(2) # "from numpy import *" let us use everything "in" numpy, see below # defining a function is as easy as this and can be done everywhere: def f(x): """this is the help for f: f returns x**2""" return x**2 f(3) help(f) quit() # ctrl-d works as well ipython # from OS shell # same things work, ipython is a python shell plus other useful stuff who whos # back to math... # ipython magic commands start with %, often also work without it %pylab # switching the "(mat)lab"-mode on, see http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.pylab.html # pylab mode executes also "from numpy import *" sin(1) exp(2) 2 * pi x = arange(0, 2, 0.01) # x is a numpy array, not a list plot(x, sin(x)) # CAVEATs: a = [1,2,3] # == range(1, 4) a + a # len(a+a) == 6 a = array([1,2,3]) # == arange(1, 4) a + a # len(a+a) == 3 a = array([1,2,3]) b = [1,2,3] a + a a + b # a.__add__(b) b + b b + a # a.__radd__(b), because b.__add__(a) fails 3 * a 3 * b min is not np.min # they have different user interfaces! import cma # use "pip install cma" if necessary (and sudo if necessary) cma? # same as ?cma or help(cma) cma.CMAEvolutionStrategy # implementation: object-oriented approach cma.OOOptimizer? cma.OOOptimizer?? # shows the source code quit() pip install appnope # can be useful for Mac in ipython notebook to remain responsive ipython notebook # notebook file should be in the current folder # see the copenhagen-cma.ipynb notebook file for the rest of it # solving an "unknown" function f = cma.FF2.Unknown() # run from typical x0 # large sigma: worse results # small sigma: same, sometimes better results # conclusion: getting the same results: seeing the same local optimum # investigate plots and rescale -> about 4 times faster runs # Sections, zoom, then semilogy, then different do(locations=...) # Sections with rescaling # What now? popsize as means to keep the initial ss large without # becoming extremely slow or diverge # what else: debug # into debug mode to the point where the last error occurred # l=list, u=up, d=down, q=quit are useful commands in debug mode