<s> """<STR_LIT>""" <EOL> import re <EOL> try : <EOL> import multiprocessing <EOL> multiprocessing . cpu_count ( ) <EOL> except ImportError : <EOL> pass <EOL> except NotImplementedError : <EOL> multiprocessing . cpu_count = lambda : <NUM_LIT:1> <EOL> from pyevolve import G1DList , GAllele , GenomeBase , Scaling <EOL> from pyevolve import GSimpleGA , Selectors , Initializators , Mutators , Consts <EOL> from openmdao . main . datatypes . api import Enum , Float , Int , Bool , Slot <EOL> from openmdao . main . api import Driver <EOL> from openmdao . main . hasparameters import HasParameters <EOL> from openmdao . main . hasobjective import HasObjective <EOL> from openmdao . main . hasevents import HasEvents <EOL> from openmdao . main . interfaces import IHasParameters , IHasObjective , implements , IOptimizer <EOL> from openmdao . util . decorators import add_delegate <EOL> from openmdao . util . typegroups import real_types , int_types , iterable_types <EOL> array_test = re . compile ( "<STR_LIT>" ) <EOL> @ add_delegate ( HasParameters , HasObjective , HasEvents ) <EOL> class Genetic ( Driver ) : <EOL> """<STR_LIT>""" <EOL> implements ( IHasParameters , IHasObjective , IOptimizer ) <EOL> opt_type = Enum ( "<STR_LIT>" , values = [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> iotype = "<STR_LIT>" , <EOL> desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> generations = Int ( Consts . CDefGAGenerations , iotype = "<STR_LIT>" , <EOL> desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> population_size = Int ( Consts . CDefGAPopulationSize , iotype = "<STR_LIT>" , <EOL> desc = "<STR_LIT>" ) <EOL> crossover_rate = Float ( Consts . CDefGACrossoverRate , iotype = "<STR_LIT>" , low = <NUM_LIT:0.0> , <EOL> high = <NUM_LIT:1.0> , desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> mutation_rate = Float ( Consts . CDefGAMutationRate , iotype = "<STR_LIT>" , low = <NUM_LIT:0.0> , <EOL> high = <NUM_LIT:1.0> , desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> selection_method = Enum ( "<STR_LIT>" , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> desc = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> iotype = "<STR_LIT>" ) <EOL> _selection_mapping = { "<STR_LIT>" : Selectors . GRouletteWheel , <EOL> "<STR_LIT>" : Selectors . GRankSelector , <EOL> "<STR_LIT>" : Selectors . GUniformSelector } <EOL> elitism = Bool ( False , iotype = "<STR_LIT>" , desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> best_individual = Slot ( klass = GenomeBase . GenomeBase , <EOL> desc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> seed = Int ( None , iotype = "<STR_LIT>" , <EOL> desc = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _make_alleles ( self ) : <EOL> """<STR_LIT>""" <EOL> alleles = GAllele . GAlleles ( ) <EOL> count = <NUM_LIT:0> <EOL> for param in self . get_parameters ( ) . values ( ) : <EOL> allele = None <EOL> count += <NUM_LIT:1> <EOL> val = param . evaluate ( ) [ <NUM_LIT:0> ] <EOL> low = param . low <EOL> high = param . high <EOL> metadata = param . get_metadata ( ) [ <NUM_LIT:1> ] <EOL> if ( '<STR_LIT>' in metadata or '<STR_LIT>' in metadata ) or array_test . search ( param . targets [ <NUM_LIT:0> ] ) : <EOL> if isinstance ( val , int_types ) : <EOL> allele = GAllele . GAlleleRange ( begin = low , end = high , real = False ) <EOL> elif isinstance ( val , real_types ) : <EOL> allele = GAllele . GAlleleRange ( begin = low , end = high , real = True ) <EOL> elif "<STR_LIT>" in metadata and isinstance ( metadata [ '<STR_LIT>' ] , iterable_types ) : <EOL> allele = GAllele . GAlleleList ( metadata [ '<STR_LIT>' ] ) <EOL> if allele : <EOL> alleles . add ( allele ) <EOL> else : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % param . targets [ <NUM_LIT:0> ] , ValueError ) <EOL> self . count = count <EOL> return alleles <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> self . set_events ( ) <EOL> alleles = self . _make_alleles ( ) <EOL> genome = G1DList . G1DList ( len ( alleles ) ) <EOL> genome . setParams ( allele = alleles ) <EOL> genome . evaluator . set ( self . _run_model ) <EOL> genome . mutator . set ( Mutators . G1DListMutatorAllele ) <EOL> genome . initializator . set ( Initializators . G1DListInitializatorAllele ) <EOL> ga = GSimpleGA . GSimpleGA ( genome , interactiveMode = False , <EOL> seed = self . seed ) <EOL> pop = ga . getPopulation ( ) <EOL> pop = pop . scaleMethod . set ( Scaling . SigmaTruncScaling ) <EOL> ga . setMinimax ( Consts . minimaxType [ self . opt_type ] ) <EOL> ga . setGenerations ( self . generations ) <EOL> ga . setMutationRate ( self . mutation_rate ) <EOL> if self . count > <NUM_LIT:1> : <EOL> ga . setCrossoverRate ( self . crossover_rate ) <EOL> else : <EOL> ga . setCrossoverRate ( <NUM_LIT:0> ) <EOL> ga . setPopulationSize ( self . population_size ) <EOL> ga . setElitism ( self . elitism ) <EOL> ga . selector . set ( self . _selection_mapping [ self . selection_method ] ) <EOL> ga . evolve ( freq_stats = <NUM_LIT:0> ) <EOL> self . best_individual = ga . bestIndividual ( ) <EOL> self . _run_model ( self . best_individual ) <EOL> def _run_model ( self , chromosome ) : <EOL> self . set_parameters ( [ val for val in chromosome ] ) <EOL> self . run_iteration ( ) <EOL> return self . eval_objective ( ) </s>
