-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOcontroller.h
More file actions
197 lines (182 loc) · 7.74 KB
/
Copy pathIOcontroller.h
File metadata and controls
197 lines (182 loc) · 7.74 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Created by sbian on 2019/9/5.
//
#ifndef BIM_IOCONTROLLER_H
#define BIM_IOCONTROLLER_H
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#endif
class IOcontroller
{
public:
static void mkdir_absence(const char* outFolder)
{
#if defined(_WIN32)
CreateDirectoryA(outFolder, nullptr); // can be used on Windows
#else
mkdir(outFolder, 0733); // can be used on non-Windows
#endif
}
/// Save a serialized file
template <class T>
static void save_file(const std::string filename, const T& output)
{
std::ofstream outfile(filename, std::ios::binary);
if (!outfile.eof() && !outfile.fail())
{
StreamType res;
serialize(output, res);
outfile.write(reinterpret_cast<char*>(&res[0]), res.size());
outfile.close();
res.clear();
std::cout << "Save file successfully: " << filename << '\n';
}
else
{
std::cout << "Save file failed: " + filename << '\n';
exit(1);
}
}
/// Load a serialized file
template <class T>
static void load_file(const std::string filename, T& input)
{
std::ifstream infile(filename, std::ios::binary);
if (!infile.eof() && !infile.fail())
{
infile.seekg(0, std::ios_base::end);
const std::streampos fileSize = infile.tellg();
infile.seekg(0, std::ios_base::beg);
std::vector<uint8_t> res(fileSize);
infile.read(reinterpret_cast<char*>(&res[0]), fileSize);
infile.close();
input.clear();
auto it = res.cbegin();
input = deserialize<T>(it, res.cend());
res.clear();
}
else
{
std::cout << "Cannot open file: " + filename << '\n';
exit(1);
}
}
/// Save graph structure to a file
static void save_graph_struct(const std::string graphName, const Graph& vecGraph, const bool isReverse)
{
std::string postfix = ".vec.graph";
if (isReverse) postfix = ".vec.rvs.graph";
const std::string filename = graphName + postfix;
save_file(filename, vecGraph);
}
/// Load graph structure from a file
static void load_graph_struct(const std::string graphName, Graph& vecGraph, const bool isReverse)
{
std::string postfix = ".vec.graph";
if (isReverse) postfix = ".vec.rvs.graph";
const std::string filename = graphName + postfix;
load_file(filename, vecGraph);
}
/// Get out-file name
static std::string get_out_file_name(const std::string graphName, const std::string algName, const int seedsize,
const std::string probDist, const float probEdge)
{
if (probDist == "UNI")
{
return graphName + "_" + algName + "_k" + std::to_string(seedsize) + "_" + probDist + std::
to_string(probEdge);
}
return graphName + "_" + algName + "_k" + std::to_string(seedsize) + "_" + probDist;
}
/// New methods get out-file name
static std::string get_out_file_name_budget(const std::string graphName, const std::string algName,
const double budpercent, const double normalpha, const double percent, const double eps,
const double feps, const std::string probDist, const float probEdge) {
if (probDist == "UNI") {
return graphName + "_" + algName + "_" + std::to_string(budpercent) +
"_" + std::to_string(normalpha) + "_" + std::to_string(percent) +
"_" + std::to_string(eps) + "_" + std::to_string(feps) +
"_" + probDist + std::to_string(probEdge);
}
return graphName + "_" + algName + "_" + std::to_string(budpercent) +
"_" + std::to_string(normalpha) + "_" + std::to_string(percent) +
"_" + std::to_string(eps) + "_" + std::to_string(feps) + "_" + probDist;
}
/// Print the results
static void write_result(const std::string& outFileName, const TResult& resultObj, const std::string& outFolder)
{
const auto approx = resultObj.get_approximation();
const auto runTime = resultObj.get_running_time();
const auto influence = resultObj.get_influence();
const auto influenceOriginal = resultObj.get_influence_original();
const auto seedSize = resultObj.get_seed_size();
const auto RRsetsSize = resultObj.get_RRsets_size();
const auto Boundmin_influence = resultObj.get_boundmin_inf();
const auto Boundmin_cost_influence = resultObj.get_boundmin_inf_cost();
std::cout << " --------------------" << std::endl;
std::cout << " |Approx.: " << approx << std::endl;
std::cout << " |Time (sec): " << runTime << std::endl;
std::cout << " |Influence: " << influence << std::endl;
std::cout << " |Self-estimated influence: " << influenceOriginal << std::endl;
std::cout << " |#Seeds: " << seedSize << std::endl;
std::cout << " |#RR sets: " << RRsetsSize << std::endl;
std::cout << " |BoundMin Influence: " << Boundmin_influence << std::endl;
std::cout << " |BoundMinCost Influence: " << Boundmin_cost_influence << std::endl;
std::cout << " --------------------" << std::endl;
mkdir_absence(outFolder.c_str());
std::ofstream outFileNew(outFolder + "/" + outFileName);
if (outFileNew.is_open())
{
outFileNew << "Approx.: " << approx << std::endl;
outFileNew << "Time (sec): " << runTime << std::endl;
outFileNew << "Influence: " << influence << std::endl;
outFileNew << "Self-estimated influence: " << influenceOriginal << std::endl;
outFileNew << "#Seeds: " << seedSize << std::endl;
outFileNew << "#RR sets: " << RRsetsSize << std::endl;
outFileNew << "BoundMin Influence: " << Boundmin_influence << std::endl;
outFileNew << "BoundMinCost Influence: " << Boundmin_cost_influence << std::endl;
outFileNew.close();
}
}
/// Print the seeds
static void write_order_seeds(const std::string& outFileName, const TResult& resultObj,
const std::string& outFolder, const Graph & graph)
{
auto vecSeed = resultObj.get_seed_vec();
mkdir_absence(outFolder.c_str());
const auto outpath = outFolder + "/seed";
mkdir_absence(outpath.c_str());
std::ofstream outFile(outpath + "/seed_" + outFileName);
for (auto i = 0; i < vecSeed.size(); i++)
{
outFile << vecSeed[i] << " " << graph[i].size() << '\n';
}
outFile.close();
}
/// Print budget the budget seeds
static void write_budget_order_seeds(const std::string & outFileName, const TResult & resultObj,
const std::string & outFolder, const Graph & graph) {
auto vecSeed_inf = resultObj.get_seed_inf_vec();
auto vecSeed_inf_cost = resultObj.get_seed_inf_cost_vec();
mkdir_absence(outFolder.c_str());
const auto outpath = outFolder + "/seed";
mkdir_absence(outpath.c_str());
std::ofstream outFile_inf(outpath + "/seed_" + outFileName + "_inf");
for (auto i = 0; i < vecSeed_inf.size(); i++)
{
outFile_inf << vecSeed_inf[i] << " " << graph[i].size() << '\n';
}
outFile_inf.close();
std::ofstream outFile_inf_cost(outpath + "/seed_" + outFileName + "_inf_cost");
for (auto i = 0; i < vecSeed_inf_cost.size(); i++)
{
outFile_inf_cost << vecSeed_inf_cost[i] << " " << graph[i].size() << '\n';
}
outFile_inf_cost.close();
}
};
using TIO = IOcontroller;
using PIO = std::shared_ptr<IOcontroller>;
#endif //BIM_IOCONTROLLER_H