-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssignment.cpp
More file actions
148 lines (120 loc) · 3.55 KB
/
Copy pathAssignment.cpp
File metadata and controls
148 lines (120 loc) · 3.55 KB
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
#include "Assignment.h"
Matrix
Assignment::GenerateFromTwoSections(const section& s1, const section& s2)const {
size_t nrows = s1.size();
size_t ncols = s2.size();
//update data members
this->num_agents = nrows;
this->num_tasks = ncols;
//define a matrix
Matrix matrix;
matrix.resize(nrows);
for(unsigned int i=0; i<nrows; i++)
matrix[i].resize(ncols);
//randomly generate
for(unsigned int i=0; i<nrows; i++)
for(unsigned int j=0; j<ncols; j++){
int rdm=rand()%MAX-1;
if(rdm<0) rdm = 0;
matrix[i][j].SetWeight(distance_util::Jaccard(s1[i],s2[j]));
}
return matrix;
}
Matrix
Assignment::RandomGenerate(size_t nrows, size_t ncols, int MAX, unsigned int _seed){
//accept new seed for random generator
if(_seed != SEED)
srand(_seed);
else
srand(this->seed);
//update data members
this->num_agents = nrows;
this->num_tasks = ncols;
//define a matrix
Matrix matrix;
matrix.resize(nrows);
for(unsigned int i=0; i<nrows; i++)
matrix[i].resize(ncols);
//randomly generate
for(unsigned int i=0; i<nrows; i++)
for(unsigned int j=0; j<ncols; j++){
int rdm=rand()%MAX-1;
if(rdm<0) rdm = 0;
matrix[i][j].SetWeight(rdm);
}
return matrix;
}
Matrix
Assignment::ImportAssignment(ifstream& input_file){
Matrix matrix;
string line;
vector<double> numstream;
size_t num_rows = 0;
size_t num_cols = 0;
if (input_file.is_open())
{
while (!input_file.eof() )
{
getline (input_file,line);
size_t local_num_cols=0;
vector<double> local_numstream;
local_numstream.clear();
string word;
stringstream parse(line);
while(parse >> word){
//if comment line, ignore it
if(word[0] == '#')
break;
//numstream.push_back(atoi(word.c_str())); //if not number, then convert to zero
numstream.push_back(atof(word.c_str())); //if not number, then convert to zero
//local_numstream.push_back(atoi(word.c_str()));
local_numstream.push_back(atof(word.c_str()));
//matrix(num_rows, num_cols)= atoi(word.c_str());
local_num_cols++;
} //end inner while
//judge if the matrix format is correct or not
if(num_cols && local_num_cols && num_cols!=local_num_cols){
cerr<<endl<<"Please input a correct matrix format!"<<endl<<endl;
exit(-1);
}
//update column number if everything looks normal
if(local_num_cols)
num_cols = local_num_cols;
//update row number if everything looks normal
if(line.length()&&local_numstream.size())
num_rows++;
} //end out layer while
input_file.close();
//update class data members
this->num_agents = num_rows;
this->num_tasks = num_cols;
//put elements into matrix
//matrix.resize(num_rows, num_cols);
matrix.resize(num_rows);
for(unsigned int i=0; i<num_rows; i++)
matrix[i].resize(num_cols);
vector<double>::iterator itr = numstream.begin();
for(unsigned int i=0; i<num_rows; i++)
for(unsigned int j=0; j<num_cols; j++)
matrix[i][j].SetWeight(*itr++);
} //end outmost if
else{
cerr <<endl<<"Error: Unable to open file! Stopped."<<endl<<endl;
exit(-1);
}
return matrix;
}
void
Assignment::DisplayMatrix(Matrix& m) const{
if(m[0].size()>30){
cout<<endl<<"Queried assignment matrix is big, not displaying."<<endl;
return;
}
cout<<endl<<"The assignment problem (matrix) you queried is:"<<endl<<endl;
for(unsigned int i=0; i<m.size(); i++){
for(unsigned int j=0; j<m[0].size(); j++)
cout<<" "<<m[i][j].GetWeight()<<"\t";
cout<<endl;
}
cout<<endl;
}