PPL Assignment
IRM2015006
q9.cpp
Go to the documentation of this file.
1 /* allocate boyfriends to all girls using KBest template : question 9
2  */
3 
4 #include "../boys/allboys.h"
5 #include "../girls/allgirls.h"
6 #include "../gifts/gift.h"
7 #include "../couples/couple.h"
8 #include "../utils/read_data.h"
9 #include "../utils/logger/logger.h"
10 #include "../templates/kbest.h"
11 #include "../configs.h"
12 
13 std::vector<Couple> makeCouples(int k, std::vector<Boy*>, std::vector<Girl*>, Logger *logger);
14 void performGifting (int k, std::vector<Couple> *couples, std::vector<Gift> *giftlist, Logger *logger);
15 
16 int main(int argc, char **argv)
17 {
18  int k = K;
19  if (argc >= 2)
20  k = atoi(argv[1]);
21  Logger logger(LOG_FILE);
22 
23  std::vector<Boy*> boys = readAllBoys(BOY_FILE);
24  std::vector<Girl*> girls = readAllGirls(GIRL_FILE);
25  std::vector<Gift> gifts = readAllGifts(GIFT_FILE);
26 
27  logger.info("Processing for question 3", true);
28  logger.log("boys", std::to_string(boys.size()) + " record read", true);
29  logger.log("girls", std::to_string(girls.size()) + " record read", true);
30  logger.log("gifts", std::to_string(gifts.size()) + " record read", true);
31 
32  std::vector<Couple> couples = makeCouples(k, boys, girls, &logger);
33 
34  logger.log("couples", std::to_string(couples.size()) + " formed", true);
35 
36  logger.info("Couples list", true);
37  for (int i = 0; i < (int)couples.size(); i++)
38  logger.log("couple"+std::to_string(i+1),
39  couples[i].boy->name+" and "+couples[i].girl->name, true);
40 
41  performGifting(k, &couples, &gifts, &logger);
42 
43  return 0;
44 }
45 
46 
48 std::vector<Couple> makeCouples(int k, std::vector<Boy*> boys, std::vector<Girl*> girls, Logger *logger)
49 {
50  std::vector<Couple> couples;
51  couples.clear();
52 
53  for (auto girl : girls) {
54  if (girl->committed) continue;
55 
56  std::vector<Boy*> topBoys;
57  switch (girl->boy_type) {
58  case attractive:
59  topBoys = KBest<Boy*>(boys).getKBest(k, compareOnAttractiveness);
60  break;
61  case rich:
62  topBoys = KBest<Boy*>(boys).getKBest(k, compareOnRichness);
63  break;
64  case intelligent:
65  topBoys = KBest<Boy*>(boys).getKBest(k, compareOnIntelligence);
66  break;
67  }
68 
69  for (auto boy : boys) {
70  if (boy->committed) continue;
71  if (boy->isCompatible(girl) &&
72  girl->isCompatible(boy)) {
73 
74  couples.push_back(Couple(boy, girl));
75  logger->log("Couple", boy->name+" partenered with "+girl->name, true);
76  boy->committed = true;
77  girl->committed= true;
78  break;
79  }
80  }
81  }
82 
83  return couples;
84 }
85 
87 void performGifting (int k, std::vector<Couple> *couples, std::vector<Gift> *giftlist, Logger *logger)
88 {
89  for (auto it = couples->begin(); it != couples->end(); it++)
90  it->makeGiftBasket(*giftlist, logger);
91 }
#define K
Definition: configs.h:14
std::vector< Gift > readAllGifts(std::string filename)
Definition: read_data.h:86
void info(const std::string msg, bool print=false)
Definition: logger.cpp:18
void log(const std::string type, const std::string msg, bool print=false)
Definition: logger.cpp:27
Definition: logger.h:8
Definition: girl_type.h:14
bool compareOnIntelligence(Boy *b1, Boy *b2)
Definition: boy.cpp:3
#define BOY_FILE
Definition: configs.h:2
void performGifting(int k, std::vector< Couple > *couples, std::vector< Gift > *giftlist, Logger *logger)
Definition: q9.cpp:87
std::vector< Girl * > readAllGirls(std::string filename)
Definition: read_data.h:50
Definition: couple.h:10
std::vector< Boy * > readAllBoys(std::string filename)
Definition: read_data.h:14
#define LOG_FILE
Definition: configs.h:11
bool compareOnAttractiveness(Boy *b1, Boy *b2)
Definition: boy.cpp:8
Definition: kbest.h:13
#define GIFT_FILE
Definition: configs.h:8
#define GIRL_FILE
Definition: configs.h:5
bool compareOnRichness(Boy *b1, Boy *b2)
Definition: boy.cpp:13
int main(int argc, char **argv)
Definition: q9.cpp:16
std::vector< Couple > makeCouples(int k, std::vector< Boy * >, std::vector< Girl * >, Logger *logger)
Definition: q9.cpp:48