Skip to content
/ CMA-ES Public

A short implementation and demonstration of the Covariance Matrix Adaptation algorithm in numpy

Notifications You must be signed in to change notification settings

jenkspt/CMA-ES

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Covariance Matrix Adaptation - Evolutionary Strategy (CMA-ES)

Surface Plot of Rastrigin Function

"Rastrigin Function"

def rastrigin(X, A=10):
    return A + np.sum((X**2 - A * np.cos(2 * np.pi * X)), -1)

The Core Algorithm

  • Evaluate the function on the sampled points X
    • d is the dimentions of the optimization space
    • n is the population size
X = np.random.normal(0,1.24, (d, n))
fitness = function(X.T)
  • Calculate the covariance matrix of top k samples, but centered using the mean of the entire population
    • k is the size of the elite population
arg_topk = np.argsort(fitness)[:k]
topk = X[:,arg_topk]

centered = topk - X.mean(1, keepdims=True)
C = (centered @ centered.T)/(k-1)
  • Sample the new population for the next iteration from this covariance matrix using the mean of the top k
w, E = la.eigh(C)
N = np.random.normal(size=(d,n))
X = topk.mean(1,keepdims=True) + (E @ np.diag(np.sqrt(w)) @ N)
  • Repeat!

"CMA-ES Rastrigin"

Initialization:

n = 100     # Population size
d = 2       # Dimensions
k = 25      # Size of elite population

# Initial random sample
X = np.random.normal(0,1.24, (d, n))

*Note the official implementation of CMA-ES should be used for any actual use cases.

About

A short implementation and demonstration of the Covariance Matrix Adaptation algorithm in numpy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages