Exemplo n.º 1
0
    def getAverageFitness(self):
        """
        INPUT       : None
        OUTPUT      : Returns the current avegare of the fitness value
                      of all the agents in the session.

        DESCRIPTION : Self explanatory from OUTPUT

        This function is used to get the average fitness value of
        agents in the current session, this uses daemon functions
        getCurrGen, getAgent
        """

        try:
            totalFitness = 0
            currGen = daemon.getCurrGen(self)
            for agentID in currGen:
                totalFitness += daemon.getAgent(self, agentID).fitness

        except Exception as e:
            print(
                "gaDisc.getAverageFitness failed with error as : {}".format(e))
            totalFitness = 0

        return (totalFitness / self.agentCount)
Exemplo n.º 2
0
    def getBestAgent(self):
        """
        INPUT       : None
        OUTPUT      : Returns the Agent with the best fitness value
                      at present , and None on error

        DESCRIPTION : Self explanatory from OUTPUT

        This is used to retrieve the best agent in the current
        pool of agents based on their fitness value with
        higher fitness corresponding to being better
        uses daemon functions getCurrGen, getAgent
        """

        try:

            bestAgent = None
            currGen = daemon.getCurrGen(self)
            for agentID in currGen:
                agent = daemon.getAgent(self, agentID)
                if ((bestAgent is None)
                        or (bestAgent.fitness < agent.fitness)):
                    bestAgent = agent

        except Exception as e:
            print("gaDisc.getBestAgent failed with error as : {}".format(e))
            bestAgent = None

        return (bestAgent)
Exemplo n.º 3
0
    def getAgent(self, agentID):
        """
        INPUT       : AgentID
        OUTPUT      : Returns the agent with the given AgentId, 
                      None on error

        DESCRIPTION : Self explanatory from OUTPUT
        """

        try:
            agentObj = daemon.getAgent(self, agentID)
            return (agentObj)

        except Exception as e:
            print("gaDisc.getAgent failed with error as : {}".format(e))
            return (None)
Exemplo n.º 4
0
    def createNextGen(self):
        """
        INPUT       : None
        OUTPUT      : Returns 1 on success, None on failure

        DESCRIPTION : Creates the next generation based on the session
                      conf that is set in the beginning of the session

        This is the main attraction in this session file XD
        the algorithm follows as this :
        
        a 'numberOfSurvivors' number is selected randomly between
        5 - 10 % of the population size. These many top agents will
        qualify for next generation.
        
        now, 1 % of the agents from the unselected agents pool is selected
        in random and made to come into the survivor agents group as 
        wildcard entry
        
        all the unselected agents are deleted (killed ?)
        now the current population is used to create new children
        by calling a daemon function aSexualRep, which returns a
        new agentObj as a child when a parent agentObj is sent
        this new child-agentObj will inherit the fitness value and
        slightly modified from its parent, (Needs optimisation).
        """

        try:
            if (self.survival > 0.95):
                survivalPerc = 0.95

            numberOfSurvivors = int(
                ((self.survival + 0.05 * random.random()) * self.agentCount))
            if (numberOfSurvivors < 1):
                numberOfSurvivors = 1

            listOfAgents = [(agent.agentID, agent.fitness) for agent in [
                daemon.getAgent(self, agentID)
                for agentID in daemon.getCurrGen(self)
            ]]

            listOfAgents.sort(key=lambda x: x[1], reverse=True)
            unselected = listOfAgents[numberOfSurvivors:]
            # wildcard zero
            wildCardEntries = random.sample(unselected,
                                            int(len(unselected) * 0.00))
            finalUnselected = [
                agent for agent in unselected if (agent not in wildCardEntries)
            ]

            for agent in finalUnselected:
                daemon.deleteAgent(self, agent[0])

            currPop = list(daemon.getCurrGen(self))
            itr = 0
            while (len(daemon.getCurrGen(self)) < self.agentCount):
                if (self.generateMode == 'A'):
                    self.updateAgent(
                        daemon.aSexualRep(self, self.getAgent(currPop[itr])))
                else:
                    self.updateAgent(
                        daemon.sexualRep(
                            self, self.getAgent(currPop[itr]),
                            self.getAgent(currPop[(itr + 1) %
                                                  (len(currPop))])))
                itr = (itr + 1) % (len(currPop))

        except Exception as e:
            print("gaDisc.createNextGen failed with error as : {}".format(e))