def notify_solution(self, incumbents): super(KpiRecorder, self).notify_solution(incumbents) publish_name_fn = self.publish_name_fn if self._last_accept: self._report_count += 1 # build a name/value dictionary with builtin values k = self._model.kpis_as_dict(self.current_solution, use_names=True) name_values = {publish_name_fn(kn): kv for kn, kv in iteritems(k)} # This must be this value for the current objective name_values['PROGRESS_CURRENT_OBJECTIVE'] = self.current_solution.objective_value # predefined keys, not KPIs # name_values[publish_name_fn('_objective')] = self.current_solution.objective_value name_values[publish_name_fn('_time')] = self._last_time self._kpis.append(name_values) # usually publish kpis in environment... if self.publish_hook is not None: self.publish_hook(name_values) # save kpis.csv table context = self._model.context if auto_publising_kpis_table_names(context) is not None: write_kpis_table(env=get_environment(), context=context, model=self._model, solution=self.current_solution)
def notify_solution(self, sol): pdata = self.current_progress_data publish_name_fn = self.publish_name_fn # 1. build a dict from formatted names to kpi values. name_values = { publish_name_fn(kp.name): kp.compute(sol) for kp in self.model.iter_kpis() } # 2. add predefined keys for obj, time. name_values['PROGRESS_CURRENT_OBJECTIVE'] = sol.objective_value name_values[publish_name_fn('_time')] = pdata.time # 3. store it (why???) self._kpi_dicts.append(name_values) # usually publish kpis in environment... if self.publish_hook is not None: self.publish_hook(name_values) # save kpis.csv table context = self._context if auto_publising_kpis_table_names(context) is not None: write_kpis_table(env=get_environment(), context=context, model=self.model, solution=sol)
def write_kpis_table(env, context, model, solution): # import from context here otherwise we have cyclic inclusions between # context and utils from docplex.mp.context import auto_publising_kpis_table_names names = auto_publising_kpis_table_names(context) kpis_table = [] for k in model.iter_kpis(): kpis_table.append([k.name, k.compute(solution)]) if kpis_table: # do not create the kpi tables if there are no kpis to be written for name in names: write_kpis(env, kpis_table, name)