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