## This file is part of the federated_learning_p2p (p2pfl) distribution# (see https://github.com/pguijas/p2pfl).# Copyright (c) 2024 Pedro Guijas Bravo.## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, version 3.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#"""Node state."""importthreadingfromtypingimportDict,List,Optionalfromp2pfl.experimentimportExperiment
[docs]defset_experiment(self,exp_name:str,total_rounds:int)->None:""" Start a new experiment. Attributes: exp_name: The name of the experiment. total_rounds: The total rounds of the experiment. """self.status="Learning"self.experiment=Experiment(exp_name,total_rounds)
[docs]defincrease_round(self)->None:""" Increase the round number. Raises: ValueError: If the experiment is not initialized. """ifself.experimentisNone:raiseValueError("Experiment not initialized")self.experiment.increase_round()self.models_aggregated={}
[docs]defclear(self)->None:"""Clear the state."""type(self).__init__(self,self.addr)
def__str__(self)->str:"""Return a String representation of the node state."""return(f"NodeState(addr={self.addr}, status={self.status}, exp_name={self.exp_name}, "f"round={self.round}, total_rounds={self.total_rounds}, simulation={self.simulation}, "f"models_aggregated={self.models_aggregated}, nei_status={self.nei_status}, "f"train_set={self.train_set}, train_set_votes={self.train_set_votes})")