-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdDemand.cpp
82 lines (70 loc) · 2.18 KB
/
AdDemand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by linpingta.
//
#include "AdDemand.h"
#include <sstream>
void AdDemand::loadDemand(const std::string& file, std::unordered_map<std::string, std::vector<std::string>>& satisfyDemand) {
std::ifstream f(file);
if (!f.is_open()) {
std::cerr << "Error open demand file" << file << std::endl;
exit(1);
}
std::string line;
while (std::getline(f, line)) {
if (line.find("#") != std::string::npos) {
continue;
}
std::istringstream iss(line);
std::string demandID, ii;
int budget;
double penalty;
iss >> demandID >> budget >> penalty >> ii;
this->demandBudgetPair[demandID] = budget;
this->demandPenaltyPair[demandID] = penalty;
std::istringstream iiss(ii);
std::string supplyID;
while (std::getline(iiss, supplyID, ',')) {
this->targetSupply[demandID].push_back(supplyID);
satisfyDemand[supplyID].push_back(demandID);
}
}
}
int AdDemand::getDemandBudget(const std::string& demandID) {
auto iter = this->demandBudgetPair.find(demandID);
if (iter != this->demandBudgetPair.end()) {
return iter->second;
}
return -1;
}
double AdDemand::getPenalty(const std::string &demandID) {
auto iter = this->demandPenaltyPair.find(demandID);
if (iter != this->demandPenaltyPair.end()) {
return iter->second;
}
return -1.0;
}
std::vector<std::string> AdDemand::getTargetSupply(const std::string& demandID) {
auto iter = this->targetSupply.find(demandID);
if (iter != this->targetSupply.end()) {
return iter->second;
}
return std::vector<std::string>();
}
std::vector<std::string> AdDemand::getDemandKeys(){
std::vector<std::string> keys;
for (const auto& pair: this->demandBudgetPair) {
keys.push_back(pair.first);
}
return keys;
}
void AdDemand::print(){
std::cout << "\nDemand:" << std::endl;
std::cout << "demand_node\tdemand\tpenalty\ttarget_supply" << std::endl;
for (const auto& j : this->getDemandKeys()) {
std::cout << "demand: " << j << ", \t budget: " << this->getDemandBudget(j) << ", \t penalty: " << this->getPenalty(j) << "\t";
for (const auto& i : this->getTargetSupply(j)) {
std::cout << " \t supply_id: " << i << ",";
}
std::cout << std::endl;
}
}