Exemple #1
0
 def pickInitialJobs(self):
     self.jobs = {}
     self.jobs["king"] = Job(name="king",level=1000,prefWeights=[1],attribWeights=[["age",1]])
     self.jobs["queen"] = Job(name="queen",level=1000,prefWeights=[1],attribWeights=[["age",1]])  
     
     men = [p for p in self.pop if p.gender == "M"]
     women = [p for p in self.pop if p.gender == "F"]
     print "Check, out of %d people we had %d men and %d women" % (len(self.pop),len(men),len(women))
     #TODO: do we want to have women "kings" or how to handle?
     self.king = pickBest(men,makeWeightedRule([[1,"charisma"],[2,"intelligence"],[3,"strength"]]))[0]
     self.king.setJob(self.jobs["king"])
     men.remove(self.king)
     self.queen = pickBest(women,makeWeightedAddRule([[2,"charisma"],[2,"intelligence"],[-0.1,"age"],[1,"body"]]))[0]
     self.queen.setJob(self.jobs["queen"])
     women.remove(self.queen)
     self.king.marry(self.queen)
     #TODO: allow king to order priorites based on his traits
     for name,attribsZipped  in [
             ["commander",[[1,"charisma"],[2,"strength"],[2,"body"],[1,"intelligence"]]],
             ["priest",[[2,"charisma"],[2,"intelligence"],[1,"age"]]],
             ["foreman",[[1,"charisma"],[2,"strength"],[2,"body"],[1,"intelligence"]]]]:
         guy = pickBest(men,makeWeightedRule(attribsZipped))[0]
         self.__dict__[name] = guy
         men.remove(guy)
         newJob = Job(name=name,level=500,prefWeights=[],attribWeights=attribsZipped)
         self.jobs[name] = newJob
         guy.setJob(newJob)
     court(men,women)
Exemple #2
0
def court(men, women):
    #filter out marriaged people
    men = dict([[man, []] for man in men if not man.spouse])
    women = [man for man in women if not man.spouse]
    wooed = defaultdict(list)
    for man in men:
        men[man] = pickBest(
            women,
            makeWeightedRule([[man.__dict__[a], a] for a in attribNames]),
            len(women))
        wooed[men[man][0]].append(man)
    rejected = []
    previousRejected = []
    toReject = max(0, len(men) - len(women))
    while True:

        #print "Debug: men's ranking:"
        #for m in men:
        #    print m.name,[f.name for f in men[m]]
        #print "Debug: wooed list:"
        #for w in wooed:
        #    print w.name,[m.name for m in wooed[w]]
        #print "We will expect %d men to get rejected" % (toReject)

        for woman in wooed:
            if len(wooed[woman]) <= 1:
                #print "This woman (%s) has %d suitor(s)" % (woman.name,len(wooed[woman]))
                continue
            best = pickBest(
                wooed[woman],
                makeWeightedRule([[woman.__dict__[a], a]
                                  for a in attribNames]))[0]
            for m in wooed[woman]:
                if m != best:
                    men[m].pop(0)
                    rejected.append(m)
            wooed[woman] = [best]
        rejected = set(rejected)
        #NOTE: bug ... should modify so it requires that the rejected list be the same as the previous iteration
        if len(rejected) <= toReject and rejected == previousRejected:
            break
        for r in rejected:
            if len(men[r]) > 0:
                wooed[men[r][0]].append(r)
                #print "Debug... rejected:",r.name
            #else:
            #print "%s ran out of folks to ask" % r.name
        previousRejected = rejected
        rejected = []
    for woman in wooed:
        if wooed[woman]:
            wooed[woman][0].marry(woman)
Exemple #3
0
    def pickInitialJobs(self):
        self.jobs = {}
        self.jobs["king"] = Job(name="king",
                                level=1000,
                                prefWeights=[1],
                                attribWeights=[["age", 1]])
        self.jobs["queen"] = Job(name="queen",
                                 level=1000,
                                 prefWeights=[1],
                                 attribWeights=[["age", 1]])

        men = [p for p in self.pop if p.gender == "M"]
        women = [p for p in self.pop if p.gender == "F"]
        print "Check, out of %d people we had %d men and %d women" % (len(
            self.pop), len(men), len(women))
        #TODO: do we want to have women "kings" or how to handle?
        self.king = pickBest(
            men,
            makeWeightedRule([[1, "charisma"], [2, "intelligence"],
                              [3, "strength"]]))[0]
        self.king.setJob(self.jobs["king"])
        men.remove(self.king)
        self.queen = pickBest(
            women,
            makeWeightedAddRule([[2, "charisma"], [2, "intelligence"],
                                 [-0.1, "age"], [1, "body"]]))[0]
        self.queen.setJob(self.jobs["queen"])
        women.remove(self.queen)
        self.king.marry(self.queen)
        #TODO: allow king to order priorites based on his traits
        for name, attribsZipped in [[
                "commander",
            [[1, "charisma"], [2, "strength"], [2, "body"],
             [1, "intelligence"]]
        ], ["priest", [[2, "charisma"], [2, "intelligence"], [1, "age"]]],
                                    [
                                        "foreman",
                                        [[1, "charisma"], [2, "strength"],
                                         [2, "body"], [1, "intelligence"]]
                                    ]]:
            guy = pickBest(men, makeWeightedRule(attribsZipped))[0]
            self.__dict__[name] = guy
            men.remove(guy)
            newJob = Job(name=name,
                         level=500,
                         prefWeights=[],
                         attribWeights=attribsZipped)
            self.jobs[name] = newJob
            guy.setJob(newJob)
        court(men, women)
Exemple #4
0
 def __init__(self,name,level,prefWeights,attribWeights):
     self.name = name
     self.level = level
     self.attribWeights = attribWeights
     self.prefWeights = prefWeights
     self.workers = []
     self.rule = makeWeightedRule(attribWeights)
Exemple #5
0
 def testMakeSingleAttribRule(self):
     m = makeWeightedRule([[2,'foo']])
     d = Dummy()
     d.foo = 0
     self.assertEqual(0,m(d))
     d.foo = 3
     self.assertEqual(6,m(d))
Exemple #6
0
def court(men,women):
    #filter out marriaged people
    men = dict([[man,[]] for man in men if not man.spouse])
    women = [man for man in women if not man.spouse]
    wooed = defaultdict(list)
    for man in men:
        men[man] = pickBest(women,makeWeightedRule([[man.__dict__[a],a] for a in attribNames]),len(women))
        wooed[men[man][0]].append(man)
    rejected = []
    previousRejected = []
    toReject = max(0,len(men) - len(women))
    while True:
        
        #print "Debug: men's ranking:"
        #for m in men:
        #    print m.name,[f.name for f in men[m]]
        #print "Debug: wooed list:"
        #for w in wooed:
        #    print w.name,[m.name for m in wooed[w]]
        #print "We will expect %d men to get rejected" % (toReject)
    

        for woman in wooed:
            if len(wooed[woman]) <= 1:
                #print "This woman (%s) has %d suitor(s)" % (woman.name,len(wooed[woman]))
                continue
            best = pickBest(wooed[woman],makeWeightedRule([[woman.__dict__[a],a] for a in attribNames]))[0]
            for m in wooed[woman]:
                if m != best:
                    men[m].pop(0)
                    rejected.append(m)
            wooed[woman] = [best]
        rejected = set(rejected)
        #NOTE: bug ... should modify so it requires that the rejected list be the same as the previous iteration
        if len(rejected) <= toReject and rejected == previousRejected:
            break
        for r in rejected:
            if len(men[r]) > 0:
                wooed[men[r][0]].append(r)
                #print "Debug... rejected:",r.name
            #else:
                #print "%s ran out of folks to ask" % r.name
        previousRejected = rejected
        rejected = []
    for woman in wooed:
        if wooed[woman]:
            wooed[woman][0].marry(woman)
Exemple #7
0
 def testMakeMultiAttribRule(self):
     m = makeWeightedRule([[1,'foo'],[2,'bar']])
     d = Dummy()
     d.foo = 0
     self.assertEqual(0,m(d))
     d.foo = 3
     self.assertEqual(0,m(d))
     d.bar = 4
     self.assertEqual(24,m(d))
Exemple #8
0
 def rejectWorst(self,maxToKeep):
     worstList = pickBest(self.workers,makeWeightedRule([(-1,"rank")]),n=len(self.workers)-maxToKeep)
     for fired in worstList:
         fired.worker.setJob(None)