@@ -303,8 +303,7 @@ def _run_optimization(self, bounds, sumo_interface, identification):
303
303
ITERATION = 1
304
304
self .iteration_results = []
305
305
self .all_iteration_results = []
306
- self ._prepare_optimization (
307
- sumo_interface , identification )
306
+ self ._prepare_optimization (sumo_interface , identification )
308
307
START_TIME = time .time ()
309
308
if self ._optimization == "differential_evolution" :
310
309
self .initial_population = random_population_from_bounds (
@@ -372,7 +371,7 @@ def _run_optimization(self, bounds, sumo_interface, identification):
372
371
# mutation_probability=[0.33, 0.10], # for adaptive
373
372
mutation_probability = self .mutation_probability ,
374
373
save_best_solutions = True ,
375
- on_fitness = fitness_callback_factory (self ))
374
+ on_fitness = fitness_callback_factory (self , "pygad" ))
376
375
ga_instance .run ()
377
376
solution , solution_fitness , _ = (
378
377
ga_instance .best_solution ())
@@ -421,7 +420,7 @@ def _run_optimization(self, bounds, sumo_interface, identification):
421
420
# mutation_probability=[0.33, 0.10], # for adaptive
422
421
mutation_probability = self .mutation_probability ,
423
422
save_best_solutions = True ,
424
- on_fitness = fitness_callback_factory_nsga2 (self ))
423
+ on_fitness = fitness_callback_factory (self , "pymoo" ))
425
424
nsga_instance .run ()
426
425
solution , solution_fitness , _ = (
427
426
nsga_instance .best_solution ())
@@ -532,10 +531,17 @@ def _prepare_optimization(self, sumo_interface, identification):
532
531
"timestep" : self .timestep ,
533
532
"project_path" : self .project_path
534
533
}
534
+ # by default, the mops's are reduced to a single value, except for the
535
+ # optimization algorithms that explicitly handle mulitple measures of performance
536
+ # by themselves
537
+ reduce = True
538
+ if self ._optimization in ["nsga2" , "gde3" , "nsde" ]:
539
+ reduce = False
535
540
objective_function = {
536
541
"identification" : identification ,
537
542
"objectives" : self .objectives ,
538
543
"weights" : self .weights ,
544
+ "reduce_mmop2smop" : reduce ,
539
545
"gof" : self .gof }
540
546
data = {
541
547
"identification" : identification ,
@@ -665,11 +671,11 @@ def random_population_from_bounds(bounds: tuple, population_size: int,
665
671
return population
666
672
667
673
668
- def fitness_callback_factory (item ):
674
+ def fitness_callback_factory (item , module_name ):
669
675
"""a factory for the fitness callback function"""
670
676
671
- def on_fitness (ga_instance : pygad .GA , solutions ):
672
- """provided callback for fitness"""
677
+ def on_fitness_pygad (ga_instance : pygad .GA , solutions ):
678
+ """provided callback for fitness on pygad iteration """
673
679
best_solution = ga_instance .best_solutions [- 1 ]
674
680
best = np .max (ga_instance .last_generation_fitness )
675
681
if best < np .max (solutions ):
@@ -679,15 +685,10 @@ def on_fitness(ga_instance: pygad.GA, solutions):
679
685
# TODO: log instead of 1 / 0
680
686
kwargs = {"weighted_error" : 1 / best }
681
687
_ = item .log_iteration (best_solution , ** kwargs )
682
- return on_fitness
683
688
684
-
685
- def fitness_callback_factory_nsga2 (item ):
686
- """a factory for the fitness callback function"""
687
-
688
- def on_fitness (nsga_instance : pygad .GA , solutions_fitness ):
689
- """provided callback for fitness"""
690
- solution , solution_fitness , _ = (
689
+ def on_fitness_pymoo (nsga_instance , solutions_fitness ):
690
+ """provided callback for fitness for pymoo iteration"""
691
+ _ , solution_fitness , _ = (
691
692
nsga_instance .best_solution (nsga_instance .last_generation_fitness ))
692
693
best_solution = nsga_instance .best_solutions [- 1 ]
693
694
best = np .sum ([1 / sol for sol in solution_fitness ])
@@ -702,4 +703,11 @@ def on_fitness(nsga_instance: pygad.GA, solutions_fitness):
702
703
kwargs = {"weighted_error" : best ,
703
704
"nondom" : pareto [0 ], "pop" : nsga_instance .population }
704
705
_ = item .log_iteration (best_solution , ** kwargs )
705
- return on_fitness
706
+
707
+ if module_name == "pygad" :
708
+ return on_fitness_pygad
709
+ elif module_name == "pymoo" :
710
+ return on_fitness_pymoo
711
+ else :
712
+ return NotImplementedError ("Module not implemented" )
713
+
0 commit comments