123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Tue Feb 23 10:12:45 2021
- @author: Oscar Portoles
- """
- import pygmo as po
- import numpy as np
- # import optimProbs as udp
- import optimizationProcesses as udp
- import sys
- import time
- import copy
- def main():
- velocity = sys.argv[1] # velocty [m/s] given as input paramter in terminal
- nStage = 4
- nTask = 100 # nTask x nGenera = optimization iterations,
- nGenera = 20
- sizePop = 50 # number of individuals on each island
- fitPercentil= 99.5
- probMigra = 0.1 # probability of a migration happening in a task "nTask"
- zHist = None
- zSim = []
- simLog = []
- edgePulse = []
-
- pathsave = './dataSave/'
- namefile = 'stgSequence_Lopt_allPu1000_wE_P040_optIni_v' + str(velocity) + '_fixG0p15L0p6_Stg'
-
- # algorithms:
- algos = [po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=1)), # jDE (Brest et al.) [rand-to-best/1/exp, rand-to-best/1/bin, rand-to-current/2/bin, rand-to-current/2/exp]
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=1)), # jDE (Brest et al.) rand-to-current/2/bin
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=2)), # iDE (Elsayed et al.) rand-to-current/2/bin
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=2)), # iDE (Elsayed et al.) best/1/bin, rand/1/bin, rand-to-current/2/bin,
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[2,7],variant_adptv=1)),
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=1)), # jDE (Brest et al.) rand-to-current/2/bin
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=2)), # iDE (Elsayed et al.) rand-to-current/2/bin
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=1)),
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=2)),
- po.algorithm(po.de1220(gen=nGenera,allowed_variants=[2,7],variant_adptv=1)),
- ]
- nIsla = len(algos)
- # My island
- myUDI = po.mp_island(use_pool=True)
- myUDI.resize_pool(nIsla)
- # PUT the righ file name to read the bumps topologies!
- for iXstage in range(nStage):
- # problem
- prob = po.problem(udp.Opt_allROIpulse_Wenecor(stageIx=iXstage,vel=velocity,zh=zHist))
- # empty archi
- archi = po.archipelago()
- # fill up archipiealago with islands
- for algo_ in algos:
- archi.push_back(po.island(algo=algo_, prob=prob, size=sizePop, udi=myUDI))
- print('All islands set for Stage ', iXstage+1)
- start = time.time()
- # set migration topology
- archi.set_topology( po.topology(po.ring(n=nIsla, w=probMigra)) )
- # Evolve (optimize)
- archi.evolve(n=nTask)
- print(archi.status)
- # wait for archi to end
- archi.wait_check()
- print('Stage: ', iXstage+1, ', best ftiness: ',[np.round(fIsla[0],3) for fIsla in archi.get_champions_f()] ,
- ', Duration: ', np.round( (time.time() - start)/3600, 4))
- # logs from UDP and migration
- logsUDP = []
- for isla_ in archi:
- logsUDP.append( isla_.get_population().problem.extract(type(udp.Opt_allROIpulse_Wenecor())).get_mylogs() )
- log_mig = archi.get_migration_log()
- logsUDP = np.asarray(logsUDP)
- # Find and re-run the best solution to pass the end as initial conditions
- udpH = udp.FindAndKeepBestSolution(stageIx=iXstage,vel=velocity,zh=zHist)
- udpH.getBestWeightedModel(logsUDP, fitPercentil)
- zHist = udpH.runBestModel()
- zi = np.imag(zHist)
-
- # keep simulated data
- zSim.append( udpH.z[:,udpH.maxDlay+1:,:] )
- simLog.append( udpH.log)
- edgePulse.append( udpH.edgePulse )
- # save results of current stage
- outfile = pathsave + namefile + str(iXstage+1) + '.npz'
- np.savez(outfile,logsUDP=logsUDP, nGen=nGenera, sizeP=sizePop, pMig=probMigra,
- log_mig=log_mig,nIsla=nIsla, allow_pickle=True)
- print('Done with ', namefile)
- zSim = np.hstack(zSim)
- simLog = np.asarray(simLog)
- outfile = pathsave + namefile + 'trialZ.npz'
- np.savez(outfile,sim_z=zSim, sim_log=simLog,edgePulse=edgePulse)
- return zSim
- if __name__ == "__main__":
- __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
- z = main()
-
|