PPL Assignment
IRM2015006
couple_utils.h
Go to the documentation of this file.
1 /* Some utilities for couple operations
2  */
3 
4 #include "../couples/couple.h"
5 #include "../boys/allboys.h"
6 #include "../girls/allgirls.h"
7 #include <algorithm>
8 
9 #include "logger/logger.h"
10 
14 std::vector<Couple> makeCouples (std::vector<Boy*> boys,
15  std::vector<Girl*> girls, Logger *logger)
16 {
17  std::vector<Couple> couples;
18 
19  int i, j;
20  for (i = 0; i < (int)girls.size(); i++) {
21  if (girls[i]->committed) continue;
22 
23  /* Sort the boys as required */
24  switch (girls[i]->boy_type) {
25  case attractive:
26  std::sort(boys.begin(), boys.end(), compareOnAttractiveness);
27  break;
28  case rich:
29  std::sort(boys.begin(), boys.end(), compareOnRichness);
30  break;
31  case intelligent:
32  std::sort(boys.begin(), boys.end(), compareOnIntelligence);
33  break;
34  }
35 
36  /* Find an appropriate boy */
37  for (j = 0; j < (int)boys.size(); j++) {
38  if (boys[j]->committed) continue;
39  logger->log("Match", "Checking compatibility of "+boys[j]->name+" and "+girls[i]->name, false);
40  if (boys[j]->isCompatible( girls[i] ) &&
41  girls[i]->isCompatible( boys[j] ) ) {
42 
43  couples.push_back( Couple((boys)[j], girls[i]) );
44  boys[j]->committed = true;
45  girls[i]->committed = true;
46  logger->log("Committed", boys[j]->name+" and "+girls[i]->name + " committed", false);
47  break;
48  }
49  }
50  }
51 
52  return couples;
53 }
54 
58 std::vector<Couple> makeCouplesAlternatively (std::vector<Boy*> boys,
59  std::vector<Girl*> girls, Logger *logger)
60 {
61  std::sort(girls.begin(), girls.end(), compareOnMaintenanceCost);
62  std::sort(boys.begin(), boys.end(), compareOnAttractiveness);
63  std::vector<Girl*> girlsCopy = girls;
64  std::vector<Boy*> boysCopy = boys;
65  std::vector<Couple> couples;
66 
67  int i, j;
68  for (i = 0; i < (int)girls.size(); i++) {
69  if (girls[i]->committed) continue;
70  std::sort(boysCopy.begin(), boysCopy.end(), compareOnAttractiveness);
71 
72  if (i % 2 == 0) {
73 
74  /* Sort the boys as required */
75  switch (girls[i]->boy_type) {
76  case attractive:
77  std::sort(boysCopy.begin(), boysCopy.end(), compareOnAttractiveness);
78  break;
79  case rich:
80  std::sort(boysCopy.begin(), boysCopy.end(), compareOnRichness);
81  break;
82  case intelligent:
83  std::sort(boysCopy.begin(), boysCopy.end(), compareOnIntelligence);
84  break;
85  }
86 
87  /* Find an appropriate boy */
88  for (j = 0; j < (int)boysCopy.size(); j++) {
89  if (boysCopy[j]->committed) continue;
90  logger->log("Match", "Checking compatibility of "+boysCopy[j]->name+" and "+girls[i]->name, false);
91  if (boysCopy[j]->isCompatible( girls[i] ) &&
92  girls[i]->isCompatible( boys[j] ) ) {
93 
94  couples.push_back( Couple((boysCopy)[j], girls[i]) );
95  boysCopy[j]->committed = true;
96  girls[i]->committed = true;
97  logger->log("Committed", boysCopy[j]->name+" and "+girls[i]->name + " committed", false);
98  break;
99  }
100  }
101 
102  } else {
103 
104  /* find first single boy */
105  Boy *singleBoy = boys[0];
106  for (int i = 0; i < (int)boys.size() && boys[i]->committed; i++)
107  singleBoy = boys[i];
108 
109  /* Sort the girls with attractiveness */
110  std::sort(girlsCopy.begin(), girlsCopy.end(), compareOnMaintenanceCost);
111 
112  for (auto it = girlsCopy.begin(); it != girlsCopy.end(); it++) {
113  if ((*it)->committed) continue;
114  couples.push_back( Couple(singleBoy, *it) );
115  singleBoy->committed = true;
116  (*it)->committed = true;
117  logger->log("Committed", singleBoy->name+" and "+(*it)->name+" committed", false);
118  break;
119  }
120 
121  }
122  }
123 
124  return couples;
125 }
126 
129 void performGifting (std::vector<Couple> *couples, std::vector<Gift> *giftlist, Logger *logger)
130 {
131  for (auto it = couples->begin(); it != couples->end(); it++)
132  it->makeGiftBasket(*giftlist, logger);
133 }
134 
138 void performGifting2 (std::vector<Couple> *couples, std::vector<Gift> *giftlist, Logger *logger)
139 {
140  for (auto it = couples->begin(); it != couples->end(); it++)
141  it->makeGiftBasket2(*giftlist, logger);
142 }
143 
147 std::vector<Couple> getKHappiestCouples (std::vector<Couple> couples, int k)
148 {
149  std::sort(couples.begin(), couples.end(), compareOnHappiness);
150  std::vector<Couple> happyCouples (couples.begin(), couples.begin() + k);
151  return happyCouples;
152 }
153 
157 std::vector<Couple> getKCompatibleCouples (std::vector<Couple> couples, int k)
158 {
159  std::sort(couples.begin(), couples.end(), compareOnCompatibility);
160  std::vector<Couple> happyCouples (couples.begin(), couples.begin() + k);
161  return happyCouples;
162 }
163 
167 std::vector<Couple> getKUnhappiestCouples (std::vector<Couple> couples, int k)
168 {
169  std::sort(couples.rbegin(), couples.rend(), compareOnHappiness);
170  std::vector<Couple> unhappyCouples (couples.begin(), couples.begin() + k);
171  return unhappyCouples;
172 }
173 
174 
178 std::vector<Couple> getCouplesWithHappinessLessThan (std::vector<Couple>couples, int t)
179 {
180  std::vector<Couple> retvalue;
181  retvalue.clear();
182  for (auto it = couples.begin(); it != couples.end(); it++) {
183  if (it->findHappiness() < t)
184  retvalue.push_back(*it);
185  }
186  return retvalue;
187 }
188 
189 
193 void performBreakupAndPairAgain (std::vector<Couple>happyCouples,
194  std::vector<Couple> *couples,
195  std::vector<Boy*> *boys,
196  std::vector<Girl*> *girls,
197  Logger *logger)
198 {
199  std::vector<Couple> newCouples;
200  newCouples.clear();
201  for (std::vector<Couple>::iterator it = happyCouples.begin(); it != happyCouples.end(); it++) {
202  auto girl = it->girl;
203  auto boy = it->boy;
204  it->breakup();
205  /* Sort the boys as required */
206  switch (girl->boy_type) {
207  case attractive:
208  std::sort(boys->begin(), boys->end(), compareOnAttractiveness);
209  break;
210  case rich:
211  std::sort(boys->begin(), boys->end(), compareOnRichness);
212  break;
213  case intelligent:
214  std::sort(boys->begin(), boys->end(), compareOnIntelligence);
215  break;
216  }
217 
218  /* Find an appropriate boy */
219  for (int j = 0; j < (int)boys->size(); j++) {
220  if ((*boys)[j]->committed ||
221  (*boys)[j]->name == boy->name)
222  continue;
223  logger->log("Match", "Checking compatibility of "+(*boys)[j]->name+" and "+(*boys)[j]->name, false);
224  if ((*boys)[j]->isCompatible( girl ) &&
225  girl->isCompatible( (*boys)[j] ) ) {
226 
227  newCouples.push_back( Couple((*boys)[j], girl) );
228  (*boys)[j]->committed = true;
229  girl->committed = true;
230  logger->log("Committed", (*boys)[j]->name+" and "+girl->name + " committed", false);
231  break;
232  }
233  }
234  }
235 
236  auto newCouplesItr = newCouples.begin();
237  for (auto it = happyCouples.begin(); it != happyCouples.end(); it++) {
238  for (auto jt = couples->begin(); jt != couples->end() && newCouplesItr != newCouples.end(); jt++) {
239  if (it->boy->name == jt->boy->name) {
240  couples->erase(jt);
241  couples->push_back(*newCouplesItr);
242  newCouplesItr++;
243  }
244  }
245  }
246 }
247 
252 void performBreakupOnHappinessAndPairAgain (std::vector<Couple> sadCouples,
253  std::vector<Couple> *couples,
254  std::vector<Boy*> *boys,
255  std::vector<Girl*> *girls,
256  int t,
257  Logger *logger)
258 {
259  std::vector<Couple> newCouples;
260  newCouples.clear();
261  for (std::vector<Couple>::iterator it = sadCouples.begin(); it != sadCouples.end(); it++) {
262  auto girl = it->girl;
263  auto boy = it->boy;
264  it->breakup();
265  /* Sort the boys as required */
266  switch (girl->boy_type) {
267  case attractive:
268  std::sort(boys->begin(), boys->end(), compareOnAttractiveness);
269  break;
270  case rich:
271  std::sort(boys->begin(), boys->end(), compareOnRichness);
272  break;
273  case intelligent:
274  std::sort(boys->begin(), boys->end(), compareOnIntelligence);
275  break;
276  }
277 
278  /* Find an appropriate boy */
279  for (int j = 0; j < (int)boys->size(); j++) {
280  if ((*boys)[j]->committed ||
281  (*boys)[j]->name == boy->name)
282  continue;
283  logger->log("Match", "Checking compatibility of "+(*boys)[j]->name+" and "+(*boys)[j]->name, false);
284  if ((*boys)[j]->isCompatible( girl ) &&
285  girl->isCompatible( (*boys)[j] ) ) {
286 
287  newCouples.push_back( Couple((*boys)[j], girl) );
288  (*boys)[j]->committed = true;
289  girl->committed = true;
290  logger->log("Committed", (*boys)[j]->name+" and "+girl->name + " committed", false);
291  break;
292  }
293  }
294  }
295 
296  auto newCouplesItr = newCouples.begin();
297  for (auto it = sadCouples.begin(); it != sadCouples.end(); it++) {
298  for (auto jt = couples->begin(); jt != couples->end() && newCouplesItr != newCouples.end(); jt++) {
299  if (it->boy->name == jt->boy->name) {
300  couples->erase(jt);
301  couples->push_back(*newCouplesItr);
302  newCouplesItr++;
303  }
304  }
305  }
306 }
bool committed
Definition: boy.h:32
void log(const std::string type, const std::string msg, bool print=false)
Definition: logger.cpp:27
Definition: logger.h:8
std::vector< Couple > getKCompatibleCouples(std::vector< Couple > couples, int k)
Definition: couple_utils.h:157
Definition: girl_type.h:14
bool compareOnIntelligence(Boy *b1, Boy *b2)
Definition: boy.cpp:3
std::vector< Couple > makeCouplesAlternatively(std::vector< Boy * > boys, std::vector< Girl * > girls, Logger *logger)
Definition: couple_utils.h:58
void performBreakupOnHappinessAndPairAgain(std::vector< Couple > sadCouples, std::vector< Couple > *couples, std::vector< Boy * > *boys, std::vector< Girl * > *girls, int t, Logger *logger)
Definition: couple_utils.h:252
Definition: couple.h:10
void performGifting(std::vector< Couple > *couples, std::vector< Gift > *giftlist, Logger *logger)
Definition: couple_utils.h:129
std::vector< Couple > getKHappiestCouples(std::vector< Couple > couples, int k)
Definition: couple_utils.h:147
Definition: boy.h:20
std::string name
Definition: boy.h:24
std::vector< Couple > getKUnhappiestCouples(std::vector< Couple > couples, int k)
Definition: couple_utils.h:167
std::vector< Couple > makeCouples(std::vector< Boy * > boys, std::vector< Girl * > girls, Logger *logger)
Definition: couple_utils.h:14
bool compareOnAttractiveness(Boy *b1, Boy *b2)
Definition: boy.cpp:8
bool compareOnMaintenanceCost(Girl *g1, Girl *g2)
Definition: girl.cpp:6
void performBreakupAndPairAgain(std::vector< Couple >happyCouples, std::vector< Couple > *couples, std::vector< Boy * > *boys, std::vector< Girl * > *girls, Logger *logger)
Definition: couple_utils.h:193
void performGifting2(std::vector< Couple > *couples, std::vector< Gift > *giftlist, Logger *logger)
Definition: couple_utils.h:138
bool compareOnRichness(Boy *b1, Boy *b2)
Definition: boy.cpp:13
bool compareOnHappiness(Couple c1, Couple c2)
Definition: couple.cpp:8
std::vector< Couple > getCouplesWithHappinessLessThan(std::vector< Couple >couples, int t)
Definition: couple_utils.h:178
bool compareOnCompatibility(Couple c1, Couple c2)
Definition: couple.cpp:13