/
tup.cpp
164 lines (144 loc) · 4.65 KB
/
tup.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
/**
* Ant+TUP - IA Avazada
* tup.cpp
* Purpose: Implementation of Tup class.
* @author dacuna <dacuna@alumnos.inf.utfsm.cl>
* @version 1.0 10/10/2013
*/
#include <glog/logging.h>
#include <boost/tokenizer.hpp>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include "tup.h"
Tup::Tup(string filename, int home, int venue, int gamma) : home_restriction_(home), venue_restriction_(venue), gamma_(gamma)
{
//read the config file and create parameters
ifstream infile(filename);
string line;
int rowCounter = 0; //for counting the rows of distance and schedule matrix
while (getline(infile, line)) {
if(line[0] == 'n') {
//reading the number of teams
try {
string to_parse_nteams = line.substr(7,string::npos);
number_of_teams_ = stoi(to_parse_nteams, nullptr);
number_of_umpires_ = number_of_teams_ / 2;
number_of_slots_ = 2*number_of_teams_ - 2;
//resize matrix of distances and schedule
teams_schedule_.resize(extents[number_of_umpires_][number_of_slots_]);
distance_matrix_.resize(extents[number_of_teams_][number_of_teams_]);
inverted_distance_matrix_.resize(extents[number_of_teams_][number_of_teams_]);
} catch (const invalid_argument& ia) {
infile.close();
throw "Error reading instance file: the number of teams is not a number";
}
}
if(line[0] == '[') {
//reading a row from the distance matrix
tokenizer<> tok(line);
int columnCounter = 0;
for(auto elem = tok.begin(); elem != tok.end(); ++elem) {
try {
distance_matrix_[rowCounter][columnCounter] = stod(*elem, nullptr);
inverted_distance_matrix_[rowCounter][columnCounter] =
1/distance_matrix_[rowCounter][columnCounter];
columnCounter++;
} catch (const invalid_argument& ia) {
throw "Error reading distance matrix: the number in row " + to_string(rowCounter) +
" and column " + to_string(columnCounter) + " is not a number";
}
}
rowCounter = (rowCounter == number_of_teams_ -1) ? 0 : rowCounter + 1;
} if(line[3] == '[') {
//reading a row from opponents matrix
int columnCounter = 0; //OBS: in this time column gets inverted with row!
string buf;
stringstream ss(line);
vector<string> tok;
while (ss >> buf)
tok.push_back(buf);
vector<int> lecture;
vector<bool> readed;
for(auto elem = tok.begin(); elem != tok.end(); ++elem) {
try {
if((*elem).compare(tok.front()) == 0)
lecture.push_back(stoi((*elem).substr(1), nullptr));
else
lecture.push_back(stoi(*elem, nullptr));
readed.push_back(false);
} catch (const invalid_argument& ia) {
throw "Error reading opponents matrix: the number in row " + to_string(columnCounter) +
" and column " + to_string(rowCounter) + " is not a number";
}
}
//insert into the opponents matrix
for(std::vector<int>::size_type i = 0; i != lecture.size(); i++) {
int local = (lecture[i] > 0) ? i+1 : abs(lecture[i]);
int visit = (lecture[i] < 0) ? i+1 : lecture[i];
if(!readed[visit-1] && !readed[local-1]){
Game game {local, visit, columnCounter};
teams_schedule_[columnCounter][rowCounter] = game;
readed[visit-1] = readed[local-1] = true;
columnCounter++;
}
}
rowCounter++;
}
}
}
void Tup::set_problem_name(const string& problem_name_) {
this->problem_name_ = problem_name_;
}
const string& Tup::problem_name() const {
return problem_name_;
}
void Tup::set_number_of_umpires(int number_of_umpires_) {
this->number_of_umpires_ = number_of_umpires_;
}
int Tup::number_of_umpires() const {
return number_of_umpires_;
}
void Tup::number_of_slots(int number_of_slots_) {
this->number_of_slots_ = number_of_slots_;
}
int Tup::number_of_slots() const {
return number_of_slots_;
}
void Tup::set_number_of_teams(int number_of_teams_) {
this->number_of_teams_ = number_of_teams_;
}
int Tup::number_of_teams() const {
return number_of_teams_;
}
void Tup::set_home_restriction(int home_restriction){
this->home_restriction_ = home_restriction;
}
int Tup::home_restriction() const{
return home_restriction_;
}
void Tup::set_venue_restriction(int venue_restriction){
this->venue_restriction_ = venue_restriction;
}
int Tup::venue_restriction() const{
return venue_restriction_;
}
void Tup::set_gamma(int gamma){
this->gamma_ = gamma;
}
int Tup::gamma() const{
return gamma_;
}
const multi_array<double, 2>& Tup::distance_matrix() const{
return distance_matrix_;
}
const multi_array<double, 2>& Tup::inverted_distance_matrix() const{
return inverted_distance_matrix_;
}
const multi_array<Game, 2>& Tup::teams_schedule() const{
return teams_schedule_;
}
Tup::~Tup()
{
}