/
parser.cpp
executable file
·181 lines (151 loc) · 4.79 KB
/
parser.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "parser.h"
BuyerParser::BuyerParser() : data()
{ }
BuyerParser::BuyerParser(std::istream& file) : data()
{
readFile(file);
}
BuyerParser::BuyerParser(const char* filename) : data()
{
std::ifstream file(filename);
readFile(file);
file.close();
}
void BuyerParser::readFile(std::istream& file)
{
CSVRow row;
while( file >> row )
data.push_back(row);
}
std::string const& BuyerParser::getCell(const Cell& c) {
return data[c.second-1][c.first-'a'];
}
float BuyerParser::getBudget(void) {
return std::atof( getCell(Cell('b',4)).c_str()+3 );
}
std::vector<float> BuyerParser::getBrands(BuyerParser::InputType t) {
std::vector<float> v;
Cell c;
size_t off = 0; //offset
switch(t)
{
case BuyerParser::PRICE: c = Cell('c', 3); off = 3; break;
case BuyerParser::TRUST: c = Cell('l', 15); off = 0; break;
case BuyerParser::FIT: c = Cell('u', 15); off = 0; break;
case BuyerParser::FASH: c = Cell('c', 15); off = 0; break;
default: throw
"BuyerParser::getBrands(BuyerParser::InputType): unknown type requested";
}
for( int i = 0; i < N_BRANDS; i++ ) {
v.push_back( std::atof(getCell(c).c_str()+off) );
c.first++;
}
return v;
}
float BuyerParser::getModelParam(BuyerParser::InputType t, size_t i, size_t j) {
Cell c;
switch(t)
{
case BuyerParser::rho: c = Cell('b', 5); break;
case BuyerParser::RHO: c = Cell('b', 6); break;
case BuyerParser::lambda: c = Cell('b', 9); break;
case BuyerParser::LAMBDA: c = Cell('b',10); break;
case BuyerParser::phi: c = Cell('t',21); break;
case BuyerParser::PHI: c = Cell('t',22); break;
case BuyerParser::tau: c = Cell('k',21); break;
case BuyerParser::TAU: c = Cell('k',22); break;
case BuyerParser::gamma: c = Cell('b',21); break;
case BuyerParser::GAMMA: c = Cell('b',22); break;
case BuyerParser::DELTA: c = Cell('b',40); break;
case BuyerParser::alpha:
throw "BuyerParser::getModelParam(): implementation missing for alpha param";
case BuyerParser::ALPHA:
throw "BuyerParser::getModelParam(): implementation missing for ALPHA param";
default:
throw "BuyerParser::getModelParam(): unknown param requested";
}
float v = std::atof( getCell(c).c_str() );
return v;
}
void BuyerParser::getAdCampaign(std::vector<std::vector<float> >& v) {
v.clear();
Cell start_c('c',41);
for( int b=0; b<N_BRANDS; b++ ) {
v.push_back(std::vector<float>(N_TIME_INTERVALS));
Cell c = Cell(start_c.first + b, start_c.second);
for( int t=0; t<N_TIME_INTERVALS; t++ ) {
v[b][t] = std::atof( getCell(c).c_str() );
c.second++;
}
}
}
void BuyerParser::getInteractions(std::vector<std::vector<std::pair<float,float> > >& v,
size_t x) {
v.clear();
Cell st_a('k',42);
Cell st_A('p',42);
for( int i=0; i<x; i++ ) {
v.push_back(std::vector<std::pair<float,float> >(x));
for( int j=0; j<x; j++ ) {
std::pair<float,float> p( std::atof( getCell(Cell(st_a.first+j,
st_a.second+i)).c_str() ),
std::atof( getCell(Cell(st_A.first+j,
st_A.second+i)).c_str() )
);
v[i][j] = p;
}
}
}
void BuyerParser::getWeakInteractions(std::vector<std::vector<std::pair<float,float> > >& v, size_t agents, size_t ties) {
v.clear();
Cell st_b('k', 54);
Cell st_B('p', 54);
for( int i=0; i<agents; i++ ) {
v.push_back(std::vector<std::pair<float,float> >(ties));
for( int k=0; k<ties; k++ ) {
std::pair<float,float> p( std::atof( getCell( Cell(st_b.first+i,
st_b.second+k) ).c_str() ),
std::atof( getCell( Cell(st_B.first+i,
st_B.second+k) ).c_str() ) );
v[i][k] = p;
}
}
}
void BuyerParser::getWeakTiesPreferences(std::vector<std::vector<float> >& ties,
size_t ties_count) {
ties.clear();
Cell start_c('c', 54);
for( int k=0; k<ties_count; k++ ) {
ties.push_back(std::vector<float>(N_BRANDS));
Cell c = Cell(start_c.first, start_c.second+k);
for( int b=0; b<N_BRANDS; b++ ) {
ties[k][b] = std::atof( getCell(c).c_str() );
c.first++;
}
}
}
std::string BuyerParser::getBrandName(size_t b_ind) {
const std::string& s = getCell( Cell('c' + b_ind, 1) );
return s.substr(1, s.length()-2);
}
///////////////////////////////////////////////////////////////////////////////
std::string const& CSVRow::operator[](std::size_t index) const
{ return m_data[index]; }
std::size_t CSVRow::size(void)
{ return m_data.size(); }
void CSVRow::readNextRow(std::istream& str)
{
std::string line;
std::getline(str,line);
std::stringstream lineStream(line);
std::string cell;
m_data.clear();
while(std::getline(lineStream,cell,',')) {
m_data.push_back(cell);
}
}
std::istream& operator>>(std::istream& str, CSVRow& data)
{
data.readNextRow(str);
return str;
}