def on_start(self): """ Start the algorithm and select an initial value for the variable when the agent is started. """ if not self._neighbors: # If a variable has no neighbors, we must select its final value immediately # as it will never receive any message. value, cost = optimal_cost_value(self._variable, self._mode) self.value_selection(value, cost) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"Select initial value {self.current_value} " f"based on cost function for var {self._variable.name}") self.finished() else: # The variable has neighbors: select a value, which might change # once we receive messages. if self.variable.initial_value is None: self.value_selection(random.choice(self.variable.domain), None) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"Select initial random value {self.current_value}") else: self.value_selection(self.variable.initial_value, None) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"Select initial value {self.current_value}") self._wait_for_values()
def on_start(self): # Select an initial value. if not self.neighbors: # If a variable has no neighbors, we must select its final value immediately # as it will never receive any message. value, cost = optimal_cost_value(self._variable, self._mode) self.value_selection(value, cost) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"Select initial value {self.current_value} " f"based on cost function for var {self._variable.name}") self.finished() else: if self.variable.initial_value is None: self.value_selection(random.choice(self.variable.domain), self.current_cost) self.logger.info( "%s gdba starts: randomly select value %s and " "send to neighbors", self.variable.name, self.current_value, ) else: self.value_selection(self.variable.initial_value, self.current_cost) self.logger.info( "%s gdba starts: select initial value %s and send to neighbors", self.variable.name, self.current_value, ) self._send_current_value() self._go_to_wait_ok_mode()
def delayed_start(self): self.remove_periodic_action(self._start_handle) if self.logger.isEnabledFor(logging.DEBUG): self.logger.debug("Remove start delayed action %s ", self._start_handle) self._start_handle = None if not self.neighbors: # If a variable has no neighbors, we must select its final value immediately. # We also do not need to setup a periodic action. if hasattr(self._variable, "cost_for_val"): current_cost, value = optimal_cost_value( self._variable, self.mode) self.value_selection(value, current_cost) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"ADSA starts: initial value {self.current_value} " f"based on cost function for var {self._variable.name}" ) else: self.value_selection(random.choice(self.variable.domain), None) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"ADSA starts: initial random value {self.current_value} " f"for unconstrained variable {self._variable.name}") self.finished() self.stop() else: self._tick_handle = self.add_periodic_action( self.period, self.tick) self.random_value_selection() if self.logger.isEnabledFor(logging.INFO): self.logger.info("ADSA starts: randomly select value %s", self.current_value) self.post_to_all_neighbors(ADsaMessage(self.current_value))
def on_start(self): if not self.neighbors: # If a variable has no neighbors, we must select its final value immediately # as it will never receive any message. value, cost = optimal_cost_value(self._variable, self.mode) self.value_selection(value, cost) if self.logger.isEnabledFor(logging.INFO): self.logger.info( f"Select initial value {self.current_value} " f"based on cost function for var {self._variable.name}") self.finished() self.stop() else: self.random_value_selection() self.logger.debug("DSA starts: randomly select value %s", self.current_value) self.post_to_all_neighbors(DsaMessage(self.current_value)) # As everything is asynchronous, we might have received our # neighbors values even before starting this algorithm. self.evaluate_cycle()