Skip to content

Commit

Permalink
fixed the moving of plotting .py files
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Benazera committed Feb 14, 2015
1 parent cfe5ed7 commit 328bd00
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
115 changes: 115 additions & 0 deletions python/cma_multiplt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python
"""In a OS shell::
python cma_multiplt.py data_file_name
or in a python shell::
import cma_multiplt as lcmaplt
lcmaplt.plot(data_file_name)
"""
# CMA-ES, Covariance Matrix Adaptation Evolution Strategy
# Copyright (c) 2014 Inria
# Author: Emmanuel Benazera <emmanuel.benazera@lri.fr>
#
# This file is part of libcmaes.
#
# libcmaes is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libcmaes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with libcmaes. If not, see <http://www.gnu.org/licenses/>.
##

import sys, pylab, csv
import numpy as np
from matplotlib.pylab import figure, ioff, ion, subplot, semilogy, hold, grid, axis, title, text, xlabel, isinteractive, draw, gcf
# TODO: the above direct imports clutter the interface in a Python shell

# number of static variables at the head of every line (i.e. independent of problem dimension)
single_values = 4

def plot(filename):
# read data into numpy array
dat = np.loadtxt(filename,dtype=float)

dim = int(np.ceil(np.shape(dat)[1] - single_values) / 3) # we estimate the problem dimension from the data
#print dim

fvalue = np.absolute(dat[:,0])
fevals = dat[:,1]
sigma = dat[:,2]
kappa = dat[:,3]
if dim > 0:
eigenvc = []
for c in range(single_values,single_values+dim):
eigenvc.append(c)
eigenv = dat[:,eigenvc]
stdsc = []
for c in range(single_values+dim,single_values+2*dim):
stdsc.append(c)
stds = dat[:,stdsc]
minstds = np.amin(stds,axis=1)
maxstds = np.amax(stds,axis=1)
xmeanc = []
for c in range(single_values+2*dim,single_values+3*dim):
xmeanc.append(c)
xmean = dat[:,xmeanc]

# plot data.
pylab.rcParams['font.size'] = 10
xlab = "function evaluations"

# plot fvalue, sigma, kappa
if dim > 0:
subplot(221)
semilogy(fevals,fvalue,'b')
semilogy(fevals,sigma,'g')
semilogy(fevals,kappa,'r')
if dim > 0:
semilogy(fevals,sigma*minstds,'y')
semilogy(fevals,sigma*maxstds,'y')
title('f-value (blue), sigma (green), kappa (red)')
grid(True)

if dim == 0:
pylab.xlabel(xlab)
pylab.show();
msg = ' --- press return to continue --- '
raw_input(msg) if sys.version < '3' else input(msg)
sys.exit(1)

# plot xmean
subplot(222)
pylab.plot(fevals,xmean)
title('Object Variables (mean, ' + str(dim) + '-D)')
grid(True)

# plot eigenvalues
subplot(223)
semilogy(fevals,eigenv,'-b')
pylab.xlabel(xlab)
title('Eigenvalues')
grid(True)

# plot std deviations
subplot(224)
semilogy(fevals,stds)
pylab.xlabel(xlab)
title('Standard Deviation in all coordinates')
grid(True)

pylab.show()

if __name__ == "__main__":
plot(sys.argv[1])
msg = ' --- press return to continue --- '
raw_input(msg) if sys.version < '3' else input(msg)
104 changes: 104 additions & 0 deletions python/cma_multiplt_surr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
##
# CMA-ES, Covariance Matrix Adaptation Evolution Strategy
# Copyright (c) 2014 Inria
# Author: Emmanuel Benazera <emmanuel.benazera@lri.fr>
#
# This file is part of libcmaes.
#
# libcmaes is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libcmaes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with libcmaes. If not, see <http://www.gnu.org/licenses/>.
##

import sys, pylab, csv
import numpy as np
from matplotlib.pylab import figure, ioff, ion, subplot, semilogy, hold, plot, grid, axis, title, text, xlabel, isinteractive, draw, gcf
from numpy import *

# number of static variables at the head of every line (i.e. independent of problem dimension)
single_values = 4

# read data into numpy array
dat = loadtxt(sys.argv[1],dtype=float)

dim = int(ceil(np.shape(dat)[1] - single_values - 3) / 3) # we estimate the problem dimension from the data
#print dim

fvalue = np.absolute(dat[:,0])
fevals = dat[:,1]
sigma = dat[:,2]
kappa = dat[:,3]
eigenvc = []
for c in range(single_values,single_values+dim):
eigenvc.append(c)
eigenv = dat[:,eigenvc]
stdsc = []
for c in range(single_values+dim,single_values+2*dim):
stdsc.append(c)
stds = dat[:,stdsc]
minstds = np.amin(stds,axis=1)
maxstds = np.amax(stds,axis=1)
xmeanc = []
for c in range(single_values+2*dim,single_values+3*dim):
xmeanc.append(c)
xmean = dat[:,xmeanc]
trainerr = dat[:,single_values+3*dim+1]
testerr = dat[:,single_values+3*dim+2]
stesterr = dat[:,single_values+3*dim+3]

# plot data.
pylab.rcParams['font.size'] = 10
xlab = "function evaluations"

# plot fvalue, sigma, kappa
subplot(321)
semilogy(fevals,fvalue,'b')
semilogy(fevals,sigma,'g')
semilogy(fevals,kappa,'r')
semilogy(fevals,sigma*minstds,'y')
semilogy(fevals,sigma*maxstds,'y')
title('f-value (blue), sigma (green), kappa (red)')
grid(True)

# plot xmean
subplot(322)
plot(fevals,xmean)
title('Object Variables (mean, ' + str(dim) + '-D)')
grid(True)

# plot eigenvalues
subplot(323)
semilogy(fevals,eigenv,'-b')
pylab.xlabel(xlab)
title('Eigenvalues')
grid(True)

# plot std deviations
subplot(324)
semilogy(fevals,stds)
pylab.xlabel(xlab)
title('Standard Deviation in all coordinates')
grid(True)

# plot std deviations
subplot(325)
pylab.ylim(0,1)
plot(fevals,trainerr,'r')
plot(fevals,testerr,'g')
plot(fevals,stesterr,'b')
title('Surrogate error: train (red), test (green), smoothed test (blue)')
grid(True)

pylab.show()

msg = ' --- press return to continue --- '
raw_input(msg) if sys.version < '3' else input(msg)

0 comments on commit 328bd00

Please sign in to comment.