-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
180 lines (150 loc) · 4.46 KB
/
main.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
//g++ -g -DDEBUG -I ../DATASTRUCTURES/HEAP -I ../DATASTRUCTURES/ -I . -c main.cpp; g++ -o spc main.o general_includes.o permutation.o
//g++ -pg -O3 -DNDEBUG -I ../DATASTRUCTURES/HEAP -I ../DATASTRUCTURES/ -I . -c main.cpp; g++ -pg -o spc main.o general_includes.o permutation.o
//g++ -O3 -DNDEBUG -I ../DATASTRUCTURES/HEAP -I ../DATASTRUCTURES/ -I . -c main.cpp; g++ -o spc main.o general_includes.o permutation.o
// ./scp input/rail1.txt 1 1 asd.txt 10
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <scp.h>
#include <general_includes.h>
using namespace std;
///PARAMETERS
double numTrial; //Default 10
double time_limit; //Default 10
bool unicost; //Default unicost
int seed; //Default 0
///PROBLEM DATA
int m; //#rows
int n; //#columns
float* c;//cost for each set
int* setCard;//cardinality for each set
int** set;//list of elements for each set
///STATS
float totalTime=0;
int totalSum=0;
float totalTimeOfBest = 0.0;
float stdDevTime =0.0;
float stdDevSum =0.0;
float currTime=0;
int currSum=0;
float t = 0;
int main(int argc, const char** argv)
{
if(argc < 3)
{
cerr<<"Usage: "<<argv[0]<<" <Input_File> <Number_of_Runs> <Search_Time_Limit>"<<endl;
exit(0);
}
numTrial = atoi(argv[2]);
time_limit = atof(argv[3]);
int i,j,k;
string filename = argv[1];
ifstream read;
read.open(filename.c_str());
if(read.is_open())
{
/*The format of these test problems is:
number of rows (m), number of columns (n)
the cost of each column c(j),j=1,...,n
for each row i (i=1,...,m): the number of columns which cover
row i followed by a list of the columns which cover row i
*/
//Read size
read >> m;
read >> n;
// don't read costs, assume it is unicost
c=new float[n];
for(i=0;i<n;i++)
c[i] = 1;
setCard=new int[n];
for(i=0;i<n;i++)
setCard[i] = 0;
signed char** data=new signed char*[n];
for (i=0; i<n; i++)
data[i]=new signed char[m];
int count=0;
int colIndex=0;
for(i=0;i<m;i++)
{
read>>count;
for(j=0;j<count;j++)
{
read >> colIndex;
data[colIndex-1][i] = 1; //input starts from 1..!!!
setCard[colIndex-1]++;
}
}
set=new int*[n];
for (i=0; i<n; i++)
set[i]=new int[setCard[i]];
int elt =0;
for (i=0; i<n; i++)
{
elt=0;
for(j=0;j<m;j++)
{
if(data[i][j] == 1)
{
set[i][elt] = j;
elt++;
}
}
}
for (int j=0; j<n; j++)
delete [] data[j];
delete [] data;
vector<double> timeOfBestRecord;
timeOfBestRecord.resize(numTrial);
vector<double> solutionRecord;
solutionRecord.resize(numTrial);
for (i=0; i<numTrial; i++)
{
t=now();
srand(i);
SCP scp(n,m,c,set,setCard);
solutionRecord[i] = scp.dialektic(timeOfBestRecord[i], time_limit);
currTime = now()-t;
// cout << " time: " << currTime << endl;
totalTimeOfBest += timeOfBestRecord[i];
totalSum += solutionRecord[i];
totalTime += currTime;
}
sort(timeOfBestRecord.begin(),timeOfBestRecord.end());
sort(solutionRecord.begin(),solutionRecord.end());
double minBestTime = timeOfBestRecord[0];
double maxBestTime = timeOfBestRecord[numTrial-1];
double minSum = solutionRecord[0];
double maxSum = solutionRecord[numTrial-1];
//Standard Deviation
double totalDevTime = 0.0;
double totalDevSum = 0.0;
for (i=0; i<numTrial; i++)
{
timeOfBestRecord[i] -= totalTimeOfBest/numTrial;
timeOfBestRecord[i] = timeOfBestRecord[i]*timeOfBestRecord[i];
totalDevTime += timeOfBestRecord[i];
solutionRecord[i] -= totalSum/numTrial;
solutionRecord[i] = solutionRecord[i]*solutionRecord[i];
totalDevSum += solutionRecord[i];
}
cout << "Input: " << filename << endl;
cout << "Total runtime: " << totalTime <<endl; //Total run time of N runs
cout << "Avg. runtime: " << totalTime/numTrial<<endl; //Runtime per instance - Cutoff
// cout << "Max runtime: " << maxBestTime <<endl;
// cout << "Min runtime: " << minBestTime <<endl;
cout << "Avg. solution quality: " << totalSum/numTrial <<endl; //Average solution quality
cout << "StDev solution quality: " << sqrt(totalDevSum/numTrial) <<endl; //Std Dev in solution quality
cout << "Best solution quality: " << minSum <<endl; //Best solution quality
cout << "Worst solution quality: " << maxSum <<endl; //Best solution quality
}
else{cout<<"Error: can not open input file "<<argv[1]<<endl;}
for (int j=0; j<n; j++)
delete [] set[j];
delete [] set;
delete [] setCard;
delete [] c;
return 0;
}