/examples/03_evolutionary_optimization/pagmo1_API/example_eo_04_ownproblem.py

https://gitlab.com/wiechapeter/pyGDM2-dirty · Python · 162 lines · 123 code · 11 blank · 28 comment · 0 complexity · fa354d22c4c31d0515afd81a56485305 MD5 · raw file

  1. # encoding: utf-8
  2. """
  3. Created on May 6, 2017
  4. @author: P. R. Wiecha
  5. Example script demonstrating `pyGDM2.EO`:
  6. Demonstrate how to implement a user-defined problem.
  7. The example problem is the maximization of the scattering cross-section
  8. of a simple plasmonic nano-structure under plane wave illumination with
  9. fixed wavelength and linear polarization angle.
  10. """
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. from pyGDM2 import structures
  14. from pyGDM2 import materials
  15. from pyGDM2 import fields
  16. from pyGDM2 import linear
  17. from pyGDM2 import core
  18. from pyGDM2.EO import models as eo_models
  19. from pyGDM2.EO.core import do_eo
  20. from PyGMO import algorithm
  21. #==============================================================================
  22. # Implement a simple problem: Maximize scattering cross-section
  23. #==============================================================================
  24. from pyGDM2.EO.problems import BaseProblem
  25. class ownProblem(BaseProblem):
  26. """Problem to maximize scattering cross-section (SCS) of nano-structure"""
  27. def __init__(self, N_dim=9999, model=None, nthreads=1):
  28. """constructor"""
  29. super(self.__class__, self).__init__(N_dim, model, nthreads=nthreads)
  30. def objective_function(self, params):
  31. """evaluate directionality ratio
  32. """
  33. self.model.generate_structure(params)
  34. ## --- GDM simulation and cross-section calculation
  35. core.scatter(self.model.sim, verbose=0)
  36. ext_cs, sca_cs, abs_cs = linear.extinct(self.model.sim, 0)
  37. return float(sca_cs)
  38. def human_readable_extra(self):
  39. return "\n\tMaximization of scattering cross section"
  40. if __name__ == '__main__':
  41. #==============================================================================
  42. # Setup pyGDM part
  43. #==============================================================================
  44. ## ---------- Setup structure
  45. mesh = 'cube'
  46. step = 20
  47. material = materials.dummy(2.0) # material: constant and real dielectric function
  48. material = materials.gold() # material: gold
  49. n1, n2 = 1.0, 1.0 # constant environment
  50. ## --- Empty dummy-geometry, will be replaced on run-time by EO trial geometries
  51. geometry = []
  52. struct = structures.struct(step, geometry, material, n1,n2, structures.get_normalization(mesh))
  53. ## ---------- Setup incident field
  54. field_generator = fields.planewave # planwave excitation
  55. kwargs = dict(theta = [0.0]) # several polarizations
  56. wavelengths = [1200] # one single wavelength
  57. efield = fields.efield(field_generator, wavelengths=wavelengths, kwargs=kwargs)
  58. ## ---------- Simulation initialization
  59. sim = core.simulation(struct, efield)
  60. #==============================================================================
  61. # setup evolutionary optimization
  62. #==============================================================================
  63. ## --- output folder and file-names
  64. results_folder = 'eo_out'
  65. results_suffix = 'test_own_problem'
  66. ## --- structure model and optimizaiton problem
  67. model = eo_models.BlockModel(sim,
  68. 20, 1,1,1, [-10,10],
  69. forbidden=[], symmetric=False)
  70. problem_dimension = model.get_dim()
  71. problem = ownProblem(problem_dimension, model)
  72. ## --- algorithm configuration
  73. algo = algorithm.jde(gen=1, memory=True)
  74. islands = 4 # number of islands ( > 1: multi-processing)
  75. population = 40 # individuals per island
  76. ## --- stop criteria
  77. max_time = 1800 # seconds
  78. max_iter = 200 # max. iterations
  79. max_nonsuccess = 20 # max. consecutive iterations without improvement
  80. #==============================================================================
  81. # run evolution
  82. #==============================================================================
  83. #do_eo(results_folder, results_suffix, problem) # run with default params
  84. do_eo(results_folder, results_suffix,
  85. problem, algo,
  86. islands, population,
  87. max_time, max_iter, max_nonsuccess,
  88. ## --- flags
  89. runtime_plotting=True,
  90. save_each_generation=False,
  91. verbose=True)