runOptimization.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Feb 23 10:12:45 2021
  5. @author: Oscar Portoles
  6. """
  7. import pygmo as po
  8. import numpy as np
  9. # import optimProbs as udp
  10. import optimizationProcesses as udp
  11. import sys
  12. import time
  13. import copy
  14. def main():
  15. velocity = sys.argv[1] # velocty [m/s] given as input paramter in terminal
  16. nStage = 4
  17. nTask = 100 # nTask x nGenera = optimization iterations,
  18. nGenera = 20
  19. sizePop = 50 # number of individuals on each island
  20. fitPercentil= 99.5
  21. probMigra = 0.1 # probability of a migration happening in a task "nTask"
  22. zHist = None
  23. zSim = []
  24. simLog = []
  25. edgePulse = []
  26. pathsave = './dataSave/'
  27. namefile = 'stgSequence_Lopt_allPu1000_wE_P040_optIni_v' + str(velocity) + '_fixG0p15L0p6_Stg'
  28. # algorithms:
  29. 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]
  30. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=1)), # jDE (Brest et al.) rand-to-current/2/bin
  31. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=2)), # iDE (Elsayed et al.) rand-to-current/2/bin
  32. 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,
  33. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[2,7],variant_adptv=1)),
  34. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=1)), # jDE (Brest et al.) rand-to-current/2/bin
  35. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[15,16],variant_adptv=2)), # iDE (Elsayed et al.) rand-to-current/2/bin
  36. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=1)),
  37. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[1,3,6,8],variant_adptv=2)),
  38. po.algorithm(po.de1220(gen=nGenera,allowed_variants=[2,7],variant_adptv=1)),
  39. ]
  40. nIsla = len(algos)
  41. # My island
  42. myUDI = po.mp_island(use_pool=True)
  43. myUDI.resize_pool(nIsla)
  44. # PUT the righ file name to read the bumps topologies!
  45. for iXstage in range(nStage):
  46. # problem
  47. prob = po.problem(udp.Opt_allROIpulse_Wenecor(stageIx=iXstage,vel=velocity,zh=zHist))
  48. # empty archi
  49. archi = po.archipelago()
  50. # fill up archipiealago with islands
  51. for algo_ in algos:
  52. archi.push_back(po.island(algo=algo_, prob=prob, size=sizePop, udi=myUDI))
  53. print('All islands set for Stage ', iXstage+1)
  54. start = time.time()
  55. # set migration topology
  56. archi.set_topology( po.topology(po.ring(n=nIsla, w=probMigra)) )
  57. # Evolve (optimize)
  58. archi.evolve(n=nTask)
  59. print(archi.status)
  60. # wait for archi to end
  61. archi.wait_check()
  62. print('Stage: ', iXstage+1, ', best ftiness: ',[np.round(fIsla[0],3) for fIsla in archi.get_champions_f()] ,
  63. ', Duration: ', np.round( (time.time() - start)/3600, 4))
  64. # logs from UDP and migration
  65. logsUDP = []
  66. for isla_ in archi:
  67. logsUDP.append( isla_.get_population().problem.extract(type(udp.Opt_allROIpulse_Wenecor())).get_mylogs() )
  68. log_mig = archi.get_migration_log()
  69. logsUDP = np.asarray(logsUDP)
  70. # Find and re-run the best solution to pass the end as initial conditions
  71. udpH = udp.FindAndKeepBestSolution(stageIx=iXstage,vel=velocity,zh=zHist)
  72. udpH.getBestWeightedModel(logsUDP, fitPercentil)
  73. zHist = udpH.runBestModel()
  74. zi = np.imag(zHist)
  75. # keep simulated data
  76. zSim.append( udpH.z[:,udpH.maxDlay+1:,:] )
  77. simLog.append( udpH.log)
  78. edgePulse.append( udpH.edgePulse )
  79. # save results of current stage
  80. outfile = pathsave + namefile + str(iXstage+1) + '.npz'
  81. np.savez(outfile,logsUDP=logsUDP, nGen=nGenera, sizeP=sizePop, pMig=probMigra,
  82. log_mig=log_mig,nIsla=nIsla, allow_pickle=True)
  83. print('Done with ', namefile)
  84. zSim = np.hstack(zSim)
  85. simLog = np.asarray(simLog)
  86. outfile = pathsave + namefile + 'trialZ.npz'
  87. np.savez(outfile,sim_z=zSim, sim_log=simLog,edgePulse=edgePulse)
  88. return zSim
  89. if __name__ == "__main__":
  90. __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
  91. z = main()