Skip to content
Snippets Groups Projects
Commit 1167a27e authored by antoined's avatar antoined
Browse files

Changes after 1D simulation has been added

parents 6d091244 5340fbcd
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 16 15:35:24 2021
@author: xavier
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text.latex', preamble=r'\usepackage{amsmath} \usepackage{amssymb}')
rc('font',**{'family':'serif','sans-serif':['Times New Roman'],'size':15})
rc('text', usetex=True)
#Value for 1 width figure
H = 3.;
L = 3.;
n_comp = 5
path = ['YuanFanChoiceA/','YuanFanChoiceB/','YuanFanChoiceC/','EisenstatChoiceA/','Classic/']
filename = ['paper2_inversion_nscgA','paper2_inversion_nscgB','paper2_inversion_nscgC','paper2_inversion_ncg','paper2_inversion_gd']
lab = ['tr (a)', 'tr (b)', 'tr (c)', 'ls', 'gd']
glob = ['trust-region','trust-region','trust-region','linesearch','linesearch']
order = [2, 2, 2, 2, 1]
col = ['C2','C2', 'C2', 'C1', 'C0']
lin = ['-','--',':','-','-']
max_forward = 180
freqf = np.array([4.,6.,9.])
n_freq = np.size(freqf)
"""
LOAD
"""
jcfs = []
n_itcf = []
n_scalecf = []
n_itcfs = []
for i in range(0,n_comp):
n_scalef = [[]]*n_freq;
jfs = [[]]*n_freq;
n_itfs = [[]]*n_freq;
n_itf = [0]*n_freq
scalefs = [[]]*n_freq;
for f in range(0,n_freq):
n_scalef[f] = np.loadtxt(path[i]+filename[i]+'_globalminimum_historyg'+str(f)+'.csv',delimiter=';',max_rows=1,skiprows=3,dtype=np.integer)[1];
scalefs[f] = np.loadtxt(path[i]+filename[i]+'_globalminimum_historyg'+str(f)+'.csv',delimiter=';',usecols=2,max_rows=np.int(n_scalef[f]),skiprows=6,dtype=np.float);
jfs[f] = [[]]*n_scalef[f];
n_itfs[f] = np.zeros(n_scalef[f],np.integer);
line = 3
for s in range(0,n_scalef[f]):
n_itfs[f][s] = np.loadtxt(path[i]+filename[i]+'_localminimum_historyg'+str(f)+'.csv',delimiter=';',usecols = 1, max_rows=1,skiprows=line,dtype=np.integer);
n_itf[f] = n_itf[f]+n_itfs[f][s]
line=line+2
jfs[f][s] = np.loadtxt(path[i]+filename[i]+'_localminimum_historyg'+str(f)+'.csv',delimiter=';',max_rows=np.int(n_itfs[f][s]+1),skiprows=line,dtype=np.float)[:,0];
line=line+n_itfs[f][s]+1+1
jcfs.append(jfs)
n_itcf.append(n_itf)
n_itcfs.append(n_itfs)
n_scalecf.append(n_scalef)
it_linecf = [[]*n_freq]*n_comp;
it_descentcf = [[]*n_freq]*n_comp;
bnd_descentcf = [[]*n_freq]*n_comp;
n_forwardcf = [[]*n_freq]*n_comp;
successcf = [[]*n_freq]*n_comp;
for i in range(0,n_comp):
it_linef = [[]]*n_freq
it_descentf = [[]]*n_freq
bnd_descentf = [[]]*n_freq
n_forwardf = [[]]*n_freq
successf = [[]]*n_freq
for f in range(0,n_freq):
n_forwardf[f] = np.zeros(n_itcf[i][f],np.integer)
if glob[i] == 'linesearch':
it_linef[f] = np.loadtxt(path[i]+filename[i]+'_linesearch_historyg'+str(f)+'.csv',delimiter=';',usecols = 5, skiprows=1,dtype=np.integer)
n_forwardf[f] += it_linef[f]
if order[i] == 2:
it_descentf[f] = np.zeros(n_itcf[i][f],np.integer)
successf[f] = np.zeros(n_itcf[i][f],np.integer)
bnd_descentf[f] = np.zeros(n_itcf[i][f],np.integer)
line = 3
for n in range(0,n_itcf[i][f]):
it_descentf[f][n] = np.loadtxt(path[i]+filename[i]+'_descent_historyg'+str(f)+'.csv',delimiter=';',usecols=3,max_rows=1,skiprows=line,dtype=np.integer);
if glob[i] == 'trust-region':
bnd_descentf[f][n] = np.loadtxt(path[i]+filename[i]+'_descent_historyg'+str(f)+'.csv',delimiter=';',usecols=6,max_rows=1,skiprows=line,dtype=np.integer);
successf[f][n] = np.loadtxt(path[i]+filename[i]+'_descent_historyg'+str(f)+'.csv',delimiter=';',usecols=0,max_rows=1,skiprows=line,dtype=np.integer);
line=line+it_descentf[f][n]+3
n_forwardf[f] += 2*it_descentf[f]
n_forwardf[f] += 2
n_forwardf[f] = np.cumsum(n_forwardf[f])
n_forwardf[f] = np.insert(n_forwardf[f],0,0)
n_forwardcf[i] = n_forwardf
it_descentcf[i] = it_descentf
bnd_descentcf[i] = bnd_descentf
it_linecf[i] = it_linef
successcf[i] = successf
"""
CONVERGENCE PLOT
"""
fig = [[]]*n_freq
for f in range(0,n_freq):
fig[f]=plt.figure(figsize=(L,H),tight_layout=False);
plt.subplots_adjust(top=0.95,right=0.98,bottom=0.09,left=0.16)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.grid(zorder=0)
plt.xlim([0,max_forward])
plt.xticks(np.arange(0,161,40))
plt.ylim([1e-3,9])
plt.yscale('log')
for i in range(0,n_comp):
n_it=0
lines = [[]]*n_scalecf[i][f]
plt.plot(n_forwardcf[i][f][n_it],jcfs[i][f][0][0],color=col[i],marker='o',zorder=3,markersize=3.)
for s in range(0,n_scalecf[i][f]):
lines[s] = plt.plot(n_forwardcf[i][f][range(n_it,n_it+n_itcfs[i][f][s]+1)],jcfs[i][f][s][0:n_itcfs[i][f][s]+1],color=col[i],linestyle=lin[i],zorder=2,label=lab[i])
plt.plot(n_forwardcf[i][f][n_it+n_itcfs[i][f][s]],jcfs[i][f][s][n_itcfs[i][f][s]],color=col[i],marker='x',zorder=3,markersize=3.)
n_it=n_it+n_itcfs[i][f][s]
n_it = 0
#plt.legend()
plt.savefig('convergence'+str(f)+'.eps')
"""
STATS
"""
print(''.rjust(30), 'Wave (tot.)'.rjust(30), 'Outer it. (tot.)'.rjust(30), 'Inner it. (mean)'.rjust(30), 'Failure (tot.)'.rjust(30), 'Bnd reached (tot.)'.rjust(30))
for i in range(0,n_comp):
outter = np.sum(n_itcf[i])
inner = 0
failure = 0
boundary = 0
call = 0
for f in range(0,n_freq):
call += n_forwardcf[i][f][-1]
inner += np.sum(it_descentcf[i][f])
if glob[i] == 'linesearch':
failure += np.count_nonzero(it_linecf[i][f])
elif glob[i] == 'trust-region':
for s in range(0,n_scalecf[i][f]):
failure += n_itcfs[i][f][s] - np.count_nonzero(np.diff(jcfs[i][f][s]))
boundary += np.sum(bnd_descentcf[i][f])
print(path[i].rjust(30), str(call).rjust(30), str(outter).rjust(30), str(inner/outter).rjust(30), str(failure/outter).rjust(30), str(boundary/outter).rjust(30))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment