Пример #1
0
	def couples(self, members, nb_children=-1):
		""" Returns a set of couples that will beget newborns
			Note that a given individual may appear several times
			By default, the probability for an individual to be in a
			couple (and thus to have a child) decreases with its rank
			in 'members'
		"""

		if nb_children < 0:		# the number of children may be imposed (e.g. in s_gazelle)
			nb_children = chances(self.Parameter('ReproductionRate') / 100.0, len(members))

		candidates = [[m,0] for m in members]

		# parenthood is distributed as a function of the rank
		# it is the responsibility of the caller to rank members appropriately
		# Note: reproduction_rate has to be doubled, as it takes two parents to beget a child
		for ParentID in enumerate(members):
			candidates[ParentID[0]][1] = chances(decrease(ParentID[0],len(members),
											   self.Parameter('Selectivity')), 2 * nb_children)
		# print(self.Parameter('Selectivity'))
		# print([chances(decrease(ii,len(members), self.Parameter('Selectivity')), 2 * nb_children) for ii in range(20)])
		# print(candidates)
		Couples = []
		# print candidates[:10]
		for ii in range(nb_children):
			Couple = self.parents([p for p in candidates if p[1] > 0])	# selects two parents from the list of candidates
			if Couple:
				(mother, father) = Couple
				Couples.append((mother[0],father[0]))
				mother[1] -= 1
				father[1] -= 1
			else:	break
		return Couples
Пример #2
0
 def parenthood(self, RankedCandidates, Def_Nb_Children):
     """ Determines the number of children depending on rank
     Note: when Selectivity is 0 (as is the default), number of children does not depend on rank
     Using Selectivity instead of SelectionPressure leads to much faster albeit perhaps less realistic convergence
     (as having 1001 points is much better than having 1000...)
     """
     ValidCandidates = RankedCandidates[:]
     for Candidate in RankedCandidates:
         if Candidate.SelfSacrifice or Candidate.age < self.Parameter(
                 'AgeAdult'):
             #print('something')
             ValidCandidates.remove(
                 Candidate)  # Children and (dead) heroes cannot reproduce
     candidates = [[m, 0] for m in ValidCandidates]
     # Parenthood is distributed as a function of the rank
     # It is the responsibility of the caller to rank members appropriately
     # Note: reproduction_rate has to be doubled, as it takes two parents to beget a child
     for ParentID in enumerate(ValidCandidates):
         candidates[ParentID[0]][1] = chances(
             decrease(ParentID[0], len(ValidCandidates),
                      self.Parameter('Selectivity')), 2 * Def_Nb_Children)
     #candidates[0][1] = 100
     #print(len(candidates))
     #print('parenthood')
     #print(candidates)
     #print(candidates[0][1])
     #print(candidates[-1][1])
     return candidates
Пример #3
0
    def couples(self, members, nb_children=-1):
        """ Returns a set of couples that will beget newborns
			Note that a given individual may appear several times
			By default, the probability for an individual to be in a
			couple (and thus to have a child) decreases with its rank
			in 'members'
		"""

        if nb_children < 0:  # the number of children may be imposed (e.g. in s_gazelle)
            nb_children = chances(
                self.Parameter('ReproductionRate') / 100.0, len(members))

        candidates = self.parenthood(members, nb_children)
        # print(candidates)

        Couples = []
        # print candidates[:10]
        for ii in range(nb_children):
            Couple = self.parents([
                p for p in candidates if p[1] > 0
            ])  # selects two parents from the list of candidates
            if Couple:
                (mother, father) = Couple
                Couples.append((mother[0], father[0]))
                mother[1] -= 1
                father[1] -= 1
            else:
                break
        return Couples
Пример #4
0
    def couples(self, RankedMembers):
        """	Lions and gazelles should not attempt to mate
			(because the selection of both subspecies operates on different scales)
		"""
        if len(RankedMembers) == 0: return []
        livingGazelles = [
            G for G in RankedMembers if self.gazelle(G) and G.score() >= 0
        ]
        lions = [L for L in RankedMembers if self.lion(L)]
        # print([G.score() for G in gazelles])
        # print([L.score() for L in lions])
        Desired_ratio = self.Parameter('GazelleToLionRatio')
        # global number of children
        nb_children = chances(
            self.Parameter('ReproductionRate') / 100.0,
            len(livingGazelles) + len(lions))
        # Distribution:
        nb_gazelle_target = int(round(Desired_ratio * nb_children / 100.0))
        nb_lion_target = 1 + int(
            round((100 - Desired_ratio) * nb_children / 100.0))
        # Correction
        nb_gazelle_target += len(lions) * Desired_ratio / (
            100.0 - Desired_ratio) - len(livingGazelles)
        # print(len(RankedMembers), len(livingGazelles), Desired_ratio, nb_gazelle_target, nb_lion_target)
        Couples = Default_Scenario.couples(
            self, livingGazelles,
            int(nb_gazelle_target)) + Default_Scenario.couples(
                self, lions, int(nb_lion_target))
        # print(len(livingGazelles), len(lions))
        # print(["%d%d (%.01f/%0.1f)" % (1*self.gazelle(C[0]),1*self.gazelle(C[1]), C[0].score(), C[1].score(), ) for C in Couples], end="\n")
        # print()
        return Couples
Пример #5
0
 def parenthood(self, RankedCandidates, Def_Nb_Children):
     " Determines the number of children depending on rank "
     candidates = [[m, 0] for m in RankedCandidates]
     # parenthood is distributed as a function of the rank
     # it is the responsibility of the caller to rank members appropriately
     # Note: reproduction_rate has to be doubled, as it takes two parents to beget a child
     for ParentID in enumerate(RankedCandidates):
         candidates[ParentID[0]][1] = chances(
             decrease(ParentID[0], len(RankedCandidates),
                      self.Parameter('Selectivity')), 2 * Def_Nb_Children)
     # print(self.Parameter('Selectivity'))
     # print([chances(decrease(ii,len(RankedCandidates), self.Parameter('Selectivity')), 2 * Def_Nb_Children) for ii in range(20)])
     return candidates
Пример #6
0
 def parenthood(self, RankedCandidates, Def_Nb_Children):
     """ Determines the number of children depending on rank
         Here, Selectivity is non-null by default, leading to rapid (non-linear) convergence
         (contrary to a purely SelectionPressure mode)
     """
     ValidCandidates = RankedCandidates[:]
     for Candidate in RankedCandidates:
         if Candidate.SelfSacrifice or Candidate.age < self.Parameter(
                 'AgeAdult'):
             ValidCandidates.remove(
                 Candidate
             )  # Children and (dead) heroes cannot reproduce (AgeAdult is null by default)
     candidates = [[m, 0] for m in ValidCandidates]
     # Parenthood is distributed as a function of the rank
     # It is the responsibility of the caller to rank members appropriately
     # Note: reproduction_rate has to be doubled, as it takes two parents to beget a child
     for ParentID in enumerate(ValidCandidates):
         candidates[ParentID[0]][1] = chances(
             decrease(ParentID[0], len(ValidCandidates),
                      self.Parameter('Selectivity')), 2 * Def_Nb_Children)
     return candidates
Пример #7
0
	def couples(self, RankedMembers):						
		"""	Lions and gazelles should not attempt to mate
			(because the selection of both subspecies operates on different scales)
		"""
		if len(RankedMembers) == 0:	return []
		livingGazelles = [G for G in RankedMembers if self.gazelle(G) and G.score() >= 0]
		lions = [L for L in RankedMembers if self.lion(L)]
		# print([G.score() for G in gazelles])
		# print([L.score() for L in lions])
		Desired_ratio = self.Parameter('GazelleToLionRatio')
		# global number of children
		nb_children = chances(self.Parameter('ReproductionRate') / 100.0, len(livingGazelles) + len(lions))
		# Distribution:
		nb_gazelle_target =  int(round(Desired_ratio * nb_children / 100.0))
		nb_lion_target =  1 + int(round((100 - Desired_ratio) * nb_children / 100.0))
		# Correction
		nb_gazelle_target += len(lions) * Desired_ratio / (100.0-Desired_ratio) - len(livingGazelles)
		# print(len(RankedMembers), len(livingGazelles), Desired_ratio, nb_gazelle_target, nb_lion_target)
		Couples = Default_Scenario.couples(self, livingGazelles, int(nb_gazelle_target)) + Default_Scenario.couples(self, lions, int(nb_lion_target))
		# print(len(livingGazelles), len(lions))
		# print(["%d%d (%.01f/%0.1f)" % (1*self.gazelle(C[0]),1*self.gazelle(C[1]), C[0].score(), C[1].score(), ) for C in Couples], end="\n")
		# print()
		return Couples