Пример #1
0
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 allocate():
	'reads and stores the input from the boys.csv and girls.csv files and then makes the valid couples'

	B = []
	G = []
	
	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()

	BN = []
	for b in B:
		BN.append(b.name)
	a = allocator()
	k = randint(1, len(BN))
	GB = random.sample(BN, k)
	print 'Given Boys list:'
	for b in GB:
		print b
	print '\n'

	print 'Choose your Allocator method:\n1 - List\n2 - List(sorted)\n3 - Hash table'

	choice = randint(1, 3)
	print choice
	print '\nGirlfriends for given boys:'
	if (choice == 2):
		a.allocator2(B, G, GB, k)
	elif (choice == 3):
		a.allocator3(B, G, GB, k)
	else:
		a.allocator1(B, G, GB, k)
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)