-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk.cpp
More file actions
120 lines (113 loc) · 3.65 KB
/
Copy pathwalk.cpp
File metadata and controls
120 lines (113 loc) · 3.65 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
#include "walk.h"
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iostream>
void Walk::run(Solution & init, Solution &best){
best = init;
step = 0;
}
bool Walk::stoppingConditions(Solution& solution){
if(step >= walkLengthLmt) return true;
if(runtimeLmt != 0){
double currentTime = double(clock() - start) / CLOCKS_PER_SEC;
if(currentTime >= (double)runtimeLmt){
solution.setCensored();
return true;
}
}
if(solution.isTargetReached()) return true;
if(cntProbeLmt != 0 && Solution::getCntProbe() >= cntProbeLmt){
solution.setCensored();
return true;
}
return false;
}
void Walk::writeWalkLine(const Solution &solution, const Solution & old,
const unsigned int neighbSize) const{
stringstream ss;
ss<<solution.getType()<<"-"<<getType()<<"-"<<solution.getL()<<"-"<<seed<<"-walk.txt";
string filename = ss.str();
ofstream file;
static bool header = true;
if(!writeWalk) return;
if(header){
file.open(filename,ofstream::out);
if(!file.good()){
cerr<<"Error opening file ("<<filename<<")"<<endl;
exit(1);
}
header = false;
time_t now = time(0);
tm *ltm = localtime(&now);
file<<"# From "<<getType()<<endl;
file<<"# fileName = "<<filename<<endl;
file<<"# timeStamp = "<<1900 + ltm->tm_year;
if(1 + ltm->tm_mon < 10) file<<"0";
file<<1 + ltm->tm_mon;
if(ltm->tm_mday < 10) file<<"0";
file<<ltm->tm_mday;
if(1 + ltm->tm_hour < 10) file<<"0";
file<<1 + ltm->tm_hour;
if(1 + ltm->tm_min < 10) file<<"0";
file<<1 + ltm->tm_min;
if(1 + ltm->tm_sec < 10) file<<"0";
file<<1 + ltm->tm_sec<<endl;
file<<"# dateStamp = "<<ctime(&now);
file<<"#"<<endl;
file<<"# .. this file can be read in R."<<endl;
file<<"#"<<endl;
file<<setw(5)<<"step";
file<<setw(11)<<"cntRestart";
file<<setw(9)<<"distance";
file<<setw(solution.getL()+1)<<"coord";
file<<setw(6)<<"value";
file<<setw(11)<<"neighbSize";
file<<setw(9)<<"cntProbe";
file<<setw(14)<<"targetReached";
file<<endl;
}
else{
file.open(filename, ofstream::out| ofstream::app);
if(!file.good()){
cerr<<"Error opening file ("<<filename<<")"<<endl;
exit(1);
}
}
file<<setw(5)<<step;
file<<setw(11)<<cntRestart;
file<<setw(9)<<solution.distance(old);
file<<setw(solution.getL()+1)<<solution;
file<<setw(6)<<solution.getValue();
file<<setw(11)<<neighbSize;
file<<setw(9)<<Solution::getCntProbe();
file<<setw(14)<<solution.isTargetReached();
file<<endl;
file.close();
}
void Walk::writeTraceLine(const Solution &solution, const Solution & old,
const unsigned int neighbSize) const{
static bool header = true;
if(!writeTrace) return;
if(header){
header = false;
cout<<setw(5)<<"step";
cout<<setw(11)<<"cntRestart";
cout<<setw(9)<<"distance";
cout<<setw(solution.getL()+1)<<"coord";
cout<<setw(6)<<"value";
cout<<setw(11)<<"neighbSize";
cout<<setw(9)<<"cntProbe";
cout<<setw(14)<<"targetReached";
cout<<endl;
}
cout<<setw(5)<<step;
cout<<setw(11)<<cntRestart;
cout<<setw(9)<<solution.distance(old);
cout<<setw(solution.getL()+1)<<solution;
cout<<setw(6)<<solution.getValue();
cout<<setw(11)<<neighbSize;
cout<<setw(9)<<Solution::getCntProbe();
cout<<setw(14)<<solution.isTargetReached();
cout<<endl;
}