def setPartner(b, g): b.partner = g g.partner = b b.status = "Committed" g.status = "Committed" b.Couple = Couple(b, g) g.Couple = Couple(b, g)
def generate_Children(self): i = 0 children = Population(self.numberOfIndividuals) while (i < self.numberOfIndividuals): firstParent, secondParent = self.select_Parents() couple = Couple(firstParent, secondParent) child = couple.crossover() children.chromosomes.append(Chromosome()) children.chromosomes[i].string = copy.deepcopy(child) i += 1 return children
def allocator1(self, B, G, GB, k): 'allocates and stores in List' CP = [] logging.warning('Girls are checking out boys ahead:\n') for g in G: for b in B: logging.info('Commitment: Girl: ' + g.name + ' is checking out Boy: ' + b.name) if (b.is_elligible(g.mbudget, g.atr)) and (g.is_elligible( b.gfbudget )) and g.status == 'single' and b.status == 'single': g.status = 'commited' b.status = 'commited' g.bfname = b.name b.gfname = g.name logging.info('Commitment: Girl: ' + g.name + ' got commited with Boy: ' + b.name) CP = CP + [(b, g)] break C = [Couple(c[0], c[1]) for c in CP] CB = [] for c in C: CB.append(c.boy.name) for b in GB: if (b not in CB): print b + ' - ' + 'Error 404: No girlfriend found' else: for c in C: if (c.boy.name == b): print b + ' - ' + c.girl.name break
def main(): boys = open('./boys_list.csv') getBoy = csv.reader(boys, delimiter=',') girls = open('./girls_list.csv') getGirls = csv.reader(girls, delimiter=',') boy_list = [] girl_list = [] couple_list = [] virtual_couple_list = [] for i in getBoy: boy_list += [ Boys(i[0], int(i[1]), int(i[2]), int(i[3]), int(i[4]), i[5]) ] for i in getGirls: girl_list += [Girls(i[0], int(i[1]), int(i[2]), int(i[3]), i[4])] # Create gift list to be sent to happiness calculator with open('./gift_list.csv', 'r') as gift_file: gift_list = csv.reader(gift_file, delimiter=',') for row in gift_list: gifts = [Gift(row[0], int(row[1]), int(row[2]), row[3])] gift_file.close() gifts = sorted(gifts, key=lambda item: item.cost) for girl in girl_list: for boy in boy_list: # Create Log create_log(boy.name + ' is trying for ' + girl.name) if boy.status == 'Single' and girl.status == 'Single' and \ boy.check_eligibility(girl.maintenance_budget, girl.attractiveness): boy.gf = girl.name girl.bf = boy.name # Update relationship status boy.status = 'Committed' girl.status = 'Committed' text = girl.name + ' is in relationship with ' + boy.name print text # Create Log create_log(text) couple_list += [(girl, boy)] break for i in couple_list: # Create couples first to be sent to happiness calculator virtual_couple_list += [Couple(i[0], i[1])] print "Couples in relationship are : " for couple in virtual_couple_list: print couple.girl.name + " is in a relationship with " + couple.boy.name calc_happiness(virtual_couple_list, gifts)
def allocate(): 'reads and stores the input from the boys.csv and girls.csv files and then makes the valid couples' B = [] G = [] CP = [] with open('boys.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for row in reader: if (row[5] == 'Miser'): B.append(MiserBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) elif (row[5] == 'Generous'): B.append(GenerousBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) else: B.append(GeekBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) csvfile.close() with open('girls.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for row in reader: if (row[4] == 'Choosy'): G.append(ChoosyGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) elif (row[4] == 'Normal'): G.append(NormalGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) else: G.append(DesperateGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) csvfile.close() k = randint(1, 10) logging.warning('Girls are checking out boys ahead:\n') for g in G: KB = best_k(B, k, 'gfbudget') for b in KB: logging.info('Commitment: Girl: ' + g.name + ' is checking out Boy: ' + b.name) if (b.is_elligible(g.mbudget)) and (g.is_elligible(b.gfbudget)) and g.status == 'single' and b.status == 'single': g.status = 'commited' b.status = 'commited' g.bfname = b.name b.gfname = g.name logging.info('Commitment: Girl: ' + g.name + ' got commited with Boy: ' + b.name) CP = CP+[(b, g)] break print 'Couples formed:\n' for g in G: if g.status == 'single': print 'Girl: ' + g.name + ' is not commited to anyone' else: print 'Girl: ' + g.name + ' is commited with Boy: ' + g.bfname print '\n' C = [Couple(c[0], c[1]) for c in CP] calculate_happiness(C, k)
def find_girl(boy, girls, gifts, couples): """Find a suitable girl for a boy and make a couple. Sort the girls in decreasing order of attractiveness. Find the most attractive girl who satisfies the constraints and form couple and do gifting. Arguments: boy: Boy from the list of all boys. girls: List of all girls. gifts: List of all gifts. couples: List of all couples. """ girls.sort(key=itemgetter('attrac'), reverse=True) male = ObjectAlloc.find_object(boy) fem = None for girl in girls: g = ObjectAlloc.find_object(girl) if g.status == False and g.cost <= male.budget and g.attrac >= male.min_attrac_req: fem = g break if fem == None: return couple = Couple(male, fem) couple.happy_gift(male, fem, gifts) couple.calc_happiness(male, fem) couple.calc_compatibility(male, fem) couples.append(vars(couple)) log = Logger() log.couple_logger(vars(couple)) for girl in girls: if girl['name'] == fem.name: girl['status'] = 'True' girl['partner'] = male.name girl['happiness'] = fem.happiness girl['gift_recv'] = fem.gift_recv break boy['status'] = 'True' boy['partner'] = fem.name boy['amount_spent'] = male.amount_spent boy['happiness'] = male.happiness
def Couplify(girl_list, boy_list): couple_list=[] for g in girl_list: for b in boy_list: newC = 0 for m in range(10, 0, -1): if g.status == 'single' and b.status=='single' and g.is_eligible(b, m) and b.is_eligible(g): setPartner(b,g) couple_list +=[Couple(b, g)] Store_Log(g.name +' and ' + b.name + ' are commited now.') newC = 1 break else: break if(newC): break return couple_list
b.partner = g g.partner = b b.status = "Committed" g.status = "Committed" b.Couple = Couple(b, g) g.Couple = Couple(b, g) couple_list=[] for k in range(0, max(len(boy_list),len(girl_list))): if(k<len(girl_list)): for b in boy_list: newC = 0 for m in range(10, 0, -1): if girl_list[k].status == 'single' and b.status=='single' and girl_list[k].is_eligible(b, m) and b.is_eligible(girl_list[k]): setPartner(b,girl_list[k]) couple_list +=[Couple(b, girl_list[k])] Store_Log(girl_list[k].name +' and ' + b.name + ' are commited now.') newC = 1 break else: break if(newC): break if(k<len(boy_list)): for g in girl_list: newC = 0 if g.status == 'single' and boy_list[k].status=='single' and g.is_eligible(boy_list[k]) and boy_list[b].is_eligible(g): setPartner(boy_list[k],g) couple_list +=[Couple(boy_list[k], g)] Store_Log(g.name +' and ' + boy_list[k].name + ' are commited now.') newC = 1
def main(): boys = open('./boys_list.csv') getBoy = csv.reader(boys, delimiter=',') girls = open('./girls_list.csv') getGirls = csv.reader(girls, delimiter=',') boy_list = [] girl_list = [] couple_list = [] virtual_couple_list = [] after_breakup_girl_list = [] for i in getBoy: boy_list += [ Boys(i[0], int(i[1]), int(i[2]), int(i[3]), int(i[4]), i[5]) ] for i in getGirls: girl_list += [Girls(i[0], int(i[1]), int(i[2]), int(i[3]), i[4])] # Create gift list to be sent to happiness calculator with open('./gift_list.csv', 'r') as gift_file: gift_list = csv.reader(gift_file, delimiter=',') for row in gift_list: gifts = [Gift(row[0], int(row[1]), int(row[2]), row[3])] gift_file.close() gifts = sorted(gifts, key=lambda item: item.cost) for girl in girl_list: for boy in boy_list: # Create Log create_log(boy.name + ' is trying for ' + girl.name) if boy.status == 'Single' and girl.status == 'Single' and \ boy.check_eligibility(girl.maintenance_budget, girl.attractiveness): boy.gf = girl.name girl.bf = boy.name # Update relationship status boy.status = 'Committed' girl.status = 'Committed' text = girl.name + ' is in relationship with ' + boy.name print(text) # Create Log create_log(text) couple_list += [(girl, boy)] break for i in couple_list: # Create couples first to be sent to happiness calculator virtual_couple_list += [Couple(i[0], i[1])] print("Couples in relationship are : ") for couple in virtual_couple_list: print(couple.girl.name + " is in a relationship with " + couple.boy.name) calc_happiness(virtual_couple_list, gifts) for girl in girl_list: for i in range(0, 10): if girl == virtual_couple_list[i].girl: # Add the boy to girl's blacklist , thus removing any chance # of their matching up again girl.blacklist = virtual_couple_list[i].boy.name # Create the new list of girls after breakup after_breakup_girl_list.append(girl) # Remove them from couple list virtual_couple_list.remove(virtual_couple_list[i]) # Now rerun the matchup loop after breakup for girl in after_breakup_girl_list: for boy in boy_list: # Create Log create_log(boy.name + ' is trying for ' + girl.name) if boy.status == 'Single' and girl.status == 'Single' and \ boy.check_eligibility(girl.maintenance_budget, girl.attractiveness): boy.gf = girl.name girl.bf = boy.name # Update relationship status boy.status = 'Committed' girl.status = 'Committed' text = girl.name + ' is in relationship with ' + boy.name print(text) # Create Log create_log(text) virtual_couple_list.append(Couple(girl, boy)) break
def main(): boys = open('./boys_list.csv') getBoy = csv.reader(boys, delimiter=',') girls = open('./girls_list.csv') getGirls = csv.reader(girls, delimiter=',') boy_list = [] girl_list = [] couple_list = [] virtual_couple_list = [] for i in getBoy: boy_list += [ Boys(i[0], int(i[1]), int(i[2]), int(i[3]), int(i[4]), i[5]) ] for i in getGirls: girl_list += [Girls(i[0], int(i[1]), int(i[2]), int(i[3]), i[4])] # Create gift list to be sent to happiness calculator with open('./gift_list.csv', 'r') as gift_file: gift_list = csv.reader(gift_file, delimiter=',') for row in gift_list: gifts = [Gift(row[0], int(row[1]), int(row[2]), row[3])] gift_file.close() gifts = sorted(gifts, key=lambda item: item.cost) for girl in girl_list: for boy in boy_list: # Create Log create_log(boy.name + ' is trying for ' + girl.name) if boy.status == 'Single' and girl.status == 'Single' and \ boy.check_eligibility(girl.maintenance_budget, girl.attractiveness): boy.gf = girl.name girl.bf = boy.name # Update relationship status boy.status = 'Committed' girl.status = 'Committed' text = girl.name + ' is in relationship with ' + boy.name print(text) # Create Log create_log(text) couple_list += [(girl, boy)] break for i in couple_list: # Create couples first to be sent to happiness calculator virtual_couple_list += [Couple(i[0], i[1])] print("Couples in relationship are : ") for couple in virtual_couple_list: print(couple.girl.name + " is in a relationship with " + couple.boy.name) calc_happiness(virtual_couple_list, gifts) virtual_couple_list = sorted(virtual_couple_list, key=lambda x: x.happiness, reverse=True) print('\n') print('10 most happy couple : ') for i in range(0, 10): print(virtual_couple_list[i].boy.name + " and " + virtual_couple_list[i].girl.name) print("\n") # print(virtual_couple_list[i].happiness) print('10 most compatible couple : ') virtual_couple_list = sorted(virtual_couple_list, key=lambda x: x.compatibility, reverse=True) for i in range(0, 10): print(virtual_couple_list[i].boy.name + ' and ' + virtual_couple_list[i].girl.name) print('The 1st day : ') for couple in virtual_couple_list: print(couple.boy.name + ' and ' + couple.girl.name) t = input() for i in range(0, t - 1): for couple in virtual_couple_list: if couple.happiness < t: couple.girl.status = 'Single' girl = couple.girl for boy in boy_list: create_log(girl.name + ' is looking guy ' + boy.name) if boy.status == girl.status and boy.status == 'Single' and \ boy.check_eligibility(girl.maintenance_budget, girl.attractiveness) \ and girl.check_eligibility(boy.budget): boy.gf = girl.name girl.bf = boy.name # Update relationship status boy.status = 'Committed' girl.status = 'Committed' text = girl.name + ' is in relationship with ' + boy.name print(text) # Create Log create_log(text) virtual_couple_list.append(Couple(boy, girl)) break couple.boy.status = 'Single' virtual_couple_list.remove(couple) calc_happiness(virtual_couple_list, gifts) print('\n') print('After ' + str(i + 2) + ' day ') for couple in virtual_couple_list: print(couple.boy.name + ' and ' + couple.girl.name)
Store_Log(g.name +' and ' + b.name + ' are commited now.') newC = 1 break else: break if(newC): break return couple_list couples=Couplify(girl_list, boy_list) print ('\n\n\n') for c in couples: print ('The given Gift to '+c.girl.name+' by '+c.boy.name+' are: ') Couple.Gifting(c, gift_list) print ('\n\n\n') k = random.randint(1, n/10) def printHappyCouple(C, k): A = sorted(C, key=lambda item: item.happiness, reverse=True) print (str(k) + ' most Happy couples:') for i in range(k): print (A[i].boy.name + ' and ' + A[i].girl.name) print ('\n') def printCompatibleCouple(C, k): B = sorted(C, key=lambda item: item.compatibility, reverse=True)
def allocate(): 'reads and stores the input from the boys.csv and girls.csv files and then makes the valid couples' B = [] G = [] CP = [] with open('boys.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for row in reader: if (row[5] == 'Miser'): B.append(MiserBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) elif (row[5] == 'Generous'): B.append(GenerousBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) else: B.append(GeekBoy(row[0], int(row[1]), int(row[2]), int(row[3]), int(row[4]), row[5])) csvfile.close() with open('girls.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for row in reader: if (row[4] == 'Choosy'): G.append(ChoosyGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) elif (row[4] == 'Normal'): G.append(NormalGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) else: G.append(DesperateGirl(row[0], int(row[1]), int(row[2]), int(row[3]), row[4])) csvfile.close() B1 = sorted(B, key=lambda item: item.atr, reverse=True) B2 = sorted(B, key=lambda item: item.atr, reverse=True) G1 = sorted(G, key=lambda item: item.mbudget, reverse=True) SG = sorted(G, key=lambda item: item.atr, reverse=True) logging.warning('Check-out session going on ahead:\n') for i in range(5): if (i % 2 == 0): for g in G1: if (g.status == 'single'): break for b in B1: logging.info('Commitment: Girl: ' + g.name + ' is checking out Boy: ' + b.name) if (b.is_elligible(g.mbudget, g.atr)) and (g.is_elligible(b.gfbudget)) and b.status == 'single': g.status = 'commited' b.status = 'commited' g.bfname = b.name b.gfname = g.name logging.info('Commitment: Girl: ' + g.name + ' got commited with Boy: ' + b.name) CP = CP+[(b, g)] break G1.remove(g) else: for b in B2: if (b.status == 'single'): break for g in SG: logging.info('Commitment: Boy: ' + b.name + ' is checking out Girl: ' + g.name) if (b.is_elligible(g.mbudget, g.atr)) and (g.is_elligible(b.gfbudget)) and g.status == 'single': g.status = 'commited' b.status = 'commited' g.bfname = b.name b.gfname = g.name logging.info('Commitment: Boy: ' + b.name + ' got commited with Girl: ' + g.name) CP = CP+[(b, g)] break B2.remove(b) print 'Couples formed (using new mechanism given in question 5):\n' for g in G: if g.status == 'single': print 'Girl: ' + g.name + ' is not commited to anyone' else: print 'Girl: ' + g.name + ' is commited with Boy: ' + g.bfname print '\n' C = [Couple(c[0], c[1]) for c in CP] calculate_happiness(C)
def make_couple(girl, boys, gifts, couples): """Find a suitable boy for a girl and initialize a couple. Find a suitable boy from the list of all boys and initialize a couple. Then perform gifting and calculate happiness, compatibility according to the type of boy and girl in the couple. Update the data in the list of all boys and list of all girls. Arguments : girl : A girl from the list of girls. boys : List of all boys. gifts : List of all gifts. couples : List of all couples. """ fem = None male = None if girl['category'] == 'c': fem = GirlChoosy(girl) elif girl['category'] == 'n': fem = GirlNormal(girl) elif girl['category'] == 'd': fem = GirlDesperate(girl) suit_boy = {} if fem.criterion == 'a': """For maximum attractiveness.""" suit_boy = {'name': None, 'attrac': 0} for boy in boys: if Utility.str_to_bool(boy['status']) is False and \ int(boy['budget']) >= fem.cost and \ fem.attrac >= int(boy['min_attrac_req']) and \ int(boy['attrac']) > suit_boy['attrac']: suit_boy['name'] = boy['name'] suit_boy['attrac'] = int(boy['attrac']) elif fem.criterion == 'r': """For maximum girlfriend budget.""" suit_boy = {'name': None, 'budget': 0} for boy in boys: if Utility.str_to_bool(boy['status']) is False and \ int(boy['budget']) >= fem.cost and \ fem.attrac >= int(boy['min_attrac_req']) and \ int(boy['budget']) > suit_boy['budget']: suit_boy['name'] = boy['name'] suit_boy['budget'] = int(boy['budget']) elif fem.criterion == 'i': """For maximum intelligence.""" suit_boy = {'name': None, 'intel': 0} for boy in boys: if Utility.str_to_bool(boy['status']) is False and \ int(boy['budget']) >= fem.cost and \ fem.attrac >= int(boy['min_attrac_req']) and \ int(boy['intel']) > suit_boy['intel']: suit_boy['name'] = boy['name'] suit_boy['intel'] = int(boy['intel']) if suit_boy['name'] is None: return else: boy = {} for boy in boys: if boy['name'] == suit_boy['name']: break if boy['category'] == 'mi': male = BoyMiser(boy) elif boy['category'] == 'gk': male = BoyGeek(boy) elif boy['category'] == 'gs': male = BoyGenerous(boy) couple = Couple(male, fem) couple.happy_gift(male, fem, gifts) couple.calc_happiness(male, fem) couple.calc_compatibility(male, fem) couples.append(vars(couple)) log = Logger() log.couple_logger(vars(couple)) """Log couple information in csv file format.""" girl['status'] = 'True' girl['partner'] = male.name girl['happiness'] = fem.happiness girl['gift_recv'] = fem.gift_recv for boy in boys: if boy['name'] == male.name: boy['status'] = 'True' boy['partner'] = fem.name boy['amount_spent'] = male.amount_spent boy['happiness'] = male.happiness break
def make_couple(girl, boys, gifts, couples): """Find a suitable boy for a girl and initialize a couple. Find a suitable boy from the list of all boys and initialize a couple. Then perform gifting and calculate happiness, compatibility according to the type of boy and girl in the couple. Update the data in the list of all boys and list of all girls. Arguments : girl : A girl from the list of girls. boys : List of all boys. gifts : List of all gifts. couples : List of all couples. """ fem = ObjectAlloc.find_object(girl) suit_boy = {} if fem.criterion == 'a': """For maximum attractiveness.""" suit_boy = {'name': None, 'attrac': 0} for boy in boys: b = ObjectAlloc.find_object(boy) if b.status is False and b.budget >= fem.cost and b.name != fem.partner and \ fem.attrac >= b.min_attrac_req and b.attrac > suit_boy['attrac']: suit_boy['name'] = b.name suit_boy['attrac'] = b.attrac elif fem.criterion == 'r': """For maximum girlfriend budget.""" suit_boy = {'name': None, 'budget': 0} for boy in boys: b = ObjectAlloc.find_object(boy) if b.status is False and b.budget >= fem.cost and b.name != fem.partner and \ fem.attrac >= b.min_attrac_req and b.budget > suit_boy['budget']: suit_boy['name'] = boy['name'] suit_boy['budget'] = int(boy['budget']) elif fem.criterion == 'i': """For maximum intelligence.""" suit_boy = {'name': None, 'intel': 0} for boy in boys: b = ObjectAlloc.find_object(boy) if b.status is False and b.budget >= fem.cost and b.name != fem.partner and \ fem.attrac >= b.min_attrac_req and b.intel > suit_boy['intel']: suit_boy['name'] = boy['name'] suit_boy['intel'] = int(boy['intel']) if suit_boy['name'] is None: return else: boy = {} for boy in boys: if boy['name'] == suit_boy['name']: break male = ObjectAlloc.find_object(boy) couple = Couple(male, fem) couple.happy_gift(male, fem, gifts) couple.calc_happiness(male, fem) couple.calc_compatibility(male, fem) couples.append(vars(couple)) log = Logger() log.couple_logger(vars(couple)) """Log couple information in csv file format.""" girl['status'] = 'True' girl['partner'] = male.name girl['happiness'] = fem.happiness girl['gift_recv'] = fem.gift_recv for boy in boys: if boy['name'] == male.name: boy['status'] = 'True' boy['partner'] = fem.name boy['amount_spent'] = male.amount_spent boy['happiness'] = male.happiness break