def __init__(self, prog = None, gp = None, lCap = []): if gp is not None: self.__stock = gp elif lCap == []: self.__stock = None else: self.__stock = GeneratePercept(lCap, objetsStatiques) if prog is None: if lCap == []: self.__chromosome = ProgramGenetic(1, 8, "A G D R".split(), plagiatUnaire) else: self.__chromosome = ProgramGenetic(1, self.__stock.howMany, "A G D R".split(), plagiatUnaire) elif isinstance(prog, ProgramGenetic): if lCap != []: assert (len(prog) == self.__stock.howMany), "pas compatible" self.__chromosome = prog else: raise AssertionError("{} expected got {}" .format(ProgramGenetic, type(prog))) lAct = list(self.__chromosome.actions) super().__init__(lCap, lAct) self.__energy = 100 self.__cpt = 0 self.reset()
class Aspirateur_PG(Aspirateur): """ prog: un programme genetique, par défaut None gp: un GeneratePercept, par défaut None lCap: valeur par défaut [] """ def __init__(self, prog = None, gp = None, lCap = []): if gp is not None: self.__stock = gp elif lCap == []: self.__stock = None else: self.__stock = GeneratePercept(lCap, objetsStatiques) if prog is None: if lCap == []: self.__chromosome = ProgramGenetic(1, 8, "A G D R".split(), plagiatUnaire) else: self.__chromosome = ProgramGenetic(1, self.__stock.howMany, "A G D R".split(), plagiatUnaire) elif isinstance(prog, ProgramGenetic): if lCap != []: assert (len(prog) == self.__stock.howMany), "pas compatible" self.__chromosome = prog else: raise AssertionError("{} expected got {}" .format(ProgramGenetic, type(prog))) lAct = list(self.__chromosome.actions) super().__init__(lCap, lAct) self.__energy = 100 self.__cpt = 0 self.reset() def reset(self): """ initialisation de certaines variables pour chaque simulation """ self.vivant = True self.cpt = 0 self.repos = 0 self.energie = 100 self.__cptalive = 0 @property def stock(self): return self.__stock @property def energie(self): return self.__energy @energie.setter def energie(self,v): assert isinstance(v, int), "int expected found {}".format(type(v)) self.__energy = max(0, min(100, v)) # force la valeur entre 0 et 100 if self.__energy == 0: self.vivant = False @property def vivant(self): return self.__vivant @vivant.setter def vivant(self, v): if isinstance(v, bool): self.__vivant = v @property def cpt(self): return self.__cpt @cpt.setter def cpt(self, v): assert isinstance(v,int) self.__cpt = min(max(0,v % len(self.program)),len(self.program)-1) @property def nbTours(self): """ renvoie le nombre d'itérations pendant lesquelles aspi est vivant """ return self.__cptalive @property def program(self): return self.__chromosome def getEvaluation(self): """ renvoie l'évaluation de l'agent """ score = (self.nettoyage / self.dirty) * 10 if self.dirty != 0 else 0 index = int(self.energie / 25) index = min(index, 3) link = [1/2 * self.energie / 100, 2/3 * self.energie / 100, 3/4 * self.energie / 100, self.energie / 100] if not self.vivant: score -= 100 else: score += link[index] return score def getDecision(self, percepts): """ deux cas à traiter suivant que percepts = [] ou pas """ self.__cptalive += 1 if percepts == []: return self.program.decoder(self.cpt) return self.program.decoder(self.__stock.find(percepts))