@@ -530,28 +530,6 @@ def mutate(self, problem, rt, **kw):
530530 return rt
531531
532532
533- # ----------
534- class RandomMutationsOrchestrator (MutationBase ):
535- """Select a mutation operator at random and apply it to the population.
536-
537- :param opts: List of Mutation classes.
538- :type opts: list[Mutation]
539- """
540-
541- def __init__ (self , opts , ** kw ):
542- super ().__init__ (** kw )
543-
544- self .opts = opts
545-
546- def _do (self , problem , X , ** kw ):
547- opt = self .opts [np .random .randint (0 , len (self .opts ))]
548- return opt ._do (problem , X , ** kw )
549-
550- def print_mutation_statistics (self ):
551- for opt in self .opts :
552- opt .print_mutation_statistics ()
553-
554-
555533class RandomPercentageChangeSpeedMutation (MutationConstraintRejection ):
556534 """
557535 Ship speed mutation class.
@@ -579,10 +557,10 @@ def mutate(self, problem, rt, **kw):
579557 op = random .choice (ops )
580558 change_percent = random .uniform (0.0 , self .change_percent_max )
581559 new = op (rt [i ][2 ], change_percent * rt [i ][2 ])
582- if new < 0 :
583- new = 0
584- elif new > self .config .BOAT_SPEED_MAX :
585- new = self .config .BOAT_SPEED_MAX
560+ if new < self . config . BOAT_SPEED_BOUNDARIES [ 0 ] :
561+ new = self . config . BOAT_SPEED_BOUNDARIES [ 0 ]
562+ elif new > self .config .BOAT_SPEED_BOUNDARIES [ 1 ] :
563+ new = self .config .BOAT_SPEED_BOUNDARIES [ 1 ]
586564 rt [i ][2 ] = new
587565 return rt
588566
@@ -604,22 +582,60 @@ def __init__(self, n_updates: int = 10, **kw):
604582 self .n_updates = n_updates
605583 # FIXME: these numbers should be carefully evaluated
606584 # ~99.7 % in interval (0, BOAT_SPEED_MAX)
607- self .mu = 0.5 * self .config .BOAT_SPEED_MAX
608- self .sigma = self .config .BOAT_SPEED_MAX / 6
585+ self .mu = 0.5 * self .config .BOAT_SPEED_BOUNDARIES [ 1 ]
586+ self .sigma = self .config .BOAT_SPEED_BOUNDARIES [ 1 ] / 6
609587
610588 def mutate (self , problem , rt , ** kw ):
589+ rt_new = copy .deepcopy (rt )
611590 try :
612591 indices = random .sample (range (0 , rt .shape [0 ] - 1 ), self .n_updates )
613592 except ValueError :
614593 indices = range (0 , rt .shape [0 ] - 1 )
615594 for i in indices :
616- new = random .normalvariate (self .mu , self .sigma )
617- if new < 0 :
618- new = 0
619- elif new > self .config .BOAT_SPEED_MAX :
620- new = self .config .BOAT_SPEED_MAX
621- rt [i ][2 ] = new
622- return rt
595+ old_speed = rt [i ][2 ]
596+ new = random .normalvariate (old_speed , self .sigma )
597+ if new < self .config .BOAT_SPEED_BOUNDARIES [0 ]:
598+ new = old_speed
599+ elif new > self .config .BOAT_SPEED_BOUNDARIES [1 ]:
600+ new = old_speed
601+ rt_new [i ][2 ] = new
602+
603+ return rt_new
604+
605+
606+ # ----------
607+ class RandomMutationsOrchestrator (MutationBase ):
608+ """Select a mutation operator at random and apply it to the population.
609+
610+ :param speed_opts: List of Mutation classes for mutating speed.
611+ :type speed_opts: list[Mutation]
612+ :param waypoint_opts: List of Mutation classes for mutating waypoints.
613+ :type waypoint_optsgit s: list[Mutation]
614+ """
615+
616+ def __init__ (self , speed_opts , waypoint_opts , ** kw ):
617+ super ().__init__ (** kw )
618+
619+ self .speed_opts = speed_opts
620+ self .waypoint_opts = waypoint_opts
621+
622+ def _do (self , problem , X , ** kw ):
623+ if self .speed_opts :
624+ speed_opt = self .speed_opts [np .random .randint (0 , len (self .speed_opts ))]
625+ X = speed_opt ._do (problem , X , ** kw )
626+ if self .waypoint_opts :
627+ waypoint_opt = self .waypoint_opts [np .random .randint (0 , len (self .waypoint_opts ))]
628+ X = waypoint_opt ._do (problem , X , ** kw )
629+ return X
630+
631+ def print_mutation_statistics (self ):
632+ if self .speed_opts :
633+ for opt in self .speed_opts :
634+ opt .print_mutation_statistics ()
635+
636+ if self .waypoint_opts :
637+ for opt in self .waypoint_opts :
638+ opt .print_mutation_statistics ()
623639
624640
625641# factory
@@ -638,11 +654,34 @@ def get_mutation(
638654 if config .GENETIC_MUTATION_TYPE == "random" :
639655 logger .debug ('Setting mutation type of genetic algorithm to "random".' )
640656 return RandomMutationsOrchestrator (
641- opts = [
657+ waypoint_opts = [
642658 RandomPlateauMutation (config = config , constraints_list = constraints_list ),
643- RouteBlendMutation (config = config , constraints_list = constraints_list )
659+ RouteBlendMutation (config = config , constraints_list = constraints_list ),
660+ ],
661+ speed_opts = [
662+ # RandomPercentageChangeSpeedMutation(config=config, constraints_list=constraints_list),
663+ GaussianSpeedMutation (config = config , constraints_list = constraints_list )
664+ ], )
665+
666+ if config .GENETIC_MUTATION_TYPE == "speed" :
667+ logger .debug ('Setting mutation type of genetic algorithm to "rndm_speed".' )
668+ return RandomMutationsOrchestrator (
669+ waypoint_opts = None ,
670+ speed_opts = [
671+ # RandomPercentageChangeSpeedMutation(config=config, constraints_list=constraints_list),
672+ GaussianSpeedMutation (config = config , constraints_list = constraints_list )
644673 ], )
645674
675+ if config .GENETIC_MUTATION_TYPE == "waypoints" :
676+ logger .debug ('Setting mutation type of genetic algorithm to "rndm_waypoints".' )
677+ return RandomMutationsOrchestrator (
678+ waypoint_opts = [
679+ RandomPlateauMutation (config = config , constraints_list = constraints_list ),
680+ RouteBlendMutation (config = config , constraints_list = constraints_list ),
681+ ],
682+ speed_opts = None
683+ )
684+
646685 if config .GENETIC_MUTATION_TYPE == "rndm_walk" :
647686 logger .debug ('Setting mutation type of genetic algorithm to "random_walk".' )
648687 return RandomWalkMutation (config = config , constraints_list = constraints_list )
0 commit comments