@@ -162,18 +162,30 @@ def optimize(
162162
163163 # FIXME temporary consistency check
164164 def consistency_check (self , res , problem ):
165+ """
166+ Temporary consistency check to uncover memory issues.
167+ """
165168 X = res .X
169+ res_objs = res .F
166170 i_route = 0
171+
172+ # solve shape issue in case there is only one objective
173+ if self .n_objs == 1 :
174+ res_objs = np .array ([res .F ])
175+ X = [X ]
176+
167177 for route in X :
168178 fuel_dict = problem .get_power (route [0 ])
169179
170- i_obj = 0
180+ # ordering of objective values in res.F is defined by RoutingProblem.get_objectives()
171181 for obj_str in self .objectives :
172182 if obj_str == "fuel_consumption" :
173- np .testing .assert_equal (fuel_dict ["fuel_sum" ].value , res .F [i_route , i_obj ], 5 )
183+ i_obj = 1
184+ if self .n_objs == 1 :
185+ i_obj = 0
186+ np .testing .assert_equal (fuel_dict ["fuel_sum" ].value , res_objs [i_route , i_obj ], 5 )
174187 else :
175- np .testing .assert_equal (fuel_dict ["time_obj" ], res .F [i_route , i_obj ], 5 )
176- i_obj += 1
188+ np .testing .assert_equal (fuel_dict ["time_obj" ], res_objs [i_route , 0 ], 5 )
177189 i_route += 1
178190
179191 def terminate (self , res : Result , problem : RoutingProblem ):
@@ -196,6 +208,7 @@ def terminate(self, res: Result, problem: RoutingProblem):
196208
197209 self .plot_running_metric (res )
198210 self .plot_population_per_generation (res , best_route )
211+ self .plot_speed_per_generation (res , best_route )
199212 self .plot_convergence (res )
200213 self .plot_coverage (res , best_route )
201214 self .plot_objective_space (res , best_index )
@@ -343,8 +356,62 @@ def plot_running_metric(self, res):
343356 plt .cla ()
344357 plt .close ()
345358
359+ def plot_speed_per_generation (self , res , best_route ) -> None :
360+ """Plot line diagrams of speed vs. travel distance for each individual in one generation.
361+
362+ :param res: Result of GA minimization
363+ :type res: pymoo.core.result.Result
364+ :param best_route: Optimum route
365+ :type best_route: np.ndarray
366+ """
367+ history = res .history
368+
369+ for igen in range (len (history )):
370+ plt .clf ()
371+ plt .close ('all' )
372+
373+ fig , ax = plt .subplots (figsize = graphics .get_standard ('fig_size' ))
374+ plt .rcParams ['font.size' ] = graphics .get_standard ('font_size' )
375+
376+ last_pop = history [igen ].pop .get ('X' )
377+ objs = []
378+ for iroute in range (0 , last_pop .shape [0 ]):
379+ hist_values = utils .get_hist_values_from_route (last_pop [iroute , 0 ], self .departure_time )
380+
381+ new_line = ax .plot (
382+ hist_values ["bin_centres" ].to (u .km ).value ,
383+ hist_values ["bin_contents" ].to (u .m / u .second ).value ,
384+ color = "blue" ,
385+ alpha = 0.3 ,
386+ linestyle = '-' ,
387+ zorder = 2
388+ )
389+ objs .append (new_line )
390+
391+ if igen == (self .n_generations - 1 ):
392+ hist_values_best_route = utils .get_hist_values_from_route (best_route , self .departure_time )
393+ ax .plot (
394+ hist_values_best_route ["bin_centres" ].to (u .km ).value ,
395+ hist_values_best_route ["bin_contents" ].to (u .m / u .second ).value ,
396+ color = "firebrick" ,
397+ linewidth = 3
398+ )
399+ left , right = plt .xlim ()
400+ ax .set_xlim (- 100 , right )
401+ ax .set_ylim (0 , 10 )
402+
403+ plt .ylabel ("speed (m/s)" )
404+ plt .xlabel ('travel distance (km)' )
405+ plt .xticks ()
406+ plt .tight_layout ()
407+ ax .legend ()
408+
409+ figname = f"genetic_algorithm_speed { igen :02} .png"
410+ plt .savefig (os .path .join (self .figure_path , figname ))
411+ plt .close (fig )
412+
346413 def plot_population_per_generation (self , res , best_route ):
347- """Plot figures and save them in WRT_FIGURE_PATH
414+ """Plot routes for each individual in one generation on a map.
348415
349416 :param res: Result of GA minimization
350417 :type res: pymoo.core.result.Result
@@ -413,7 +480,6 @@ def plot_population_per_generation(self, res, best_route):
413480 cbar = fig .colorbar (route_lc , ax = ax , orientation = 'vertical' , pad = 0.15 , shrink = 0.7 )
414481 cbar .set_label ('Geschwindigkeit ($m/s$)' )
415482 plt .tight_layout ()
416-
417483 ax .legend ()
418484
419485 figname = f"genetic_algorithm_generation { igen :02} .png"
0 commit comments