-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
71 lines (59 loc) · 1.74 KB
/
test.cpp
File metadata and controls
71 lines (59 loc) · 1.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
// Test sensor network files
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include "SensorNetwork.h"
// LearningType {QLEARNING, SARSA, SARSALAMBDA} ;
int main()
{
// Domain parameters, note: any negative inputs will apply the default values
int numTargets = 3 ; // number of targets, default 2
int fullEnergy = 5 ; // initial target energy, default 3
int numCells = 5 ; // number of cells, default 3
int obsRange = 1 ; // number of cells on either side of sensor that are observable, default full
SensorNetwork * testNetwork = new SensorNetwork(numTargets, fullEnergy, numCells, obsRange) ;
LearningType algorithm = QLEARNING ;
testNetwork->SetLearningAlgorithm(algorithm) ;
vector< vector<int> > allStates = testNetwork->GetAllStates() ;
// Write states to file
ofstream statesFile ;
statesFile.open("states.txt") ;
for (unsigned i = 0; i < allStates.size(); i++)
{
for (unsigned j = 0; j < allStates[i].size(); j++)
{
statesFile << allStates[i][j] ;
if (j == allStates[i].size()-1)
statesFile << "\n" ;
else
statesFile << "," ;
}
}
statesFile.close() ;
// Initialise log file
std::string logFileName = "log.txt" ;
ofstream logFile ;
// Iterate through each episode
unsigned k = 0 ;
unsigned nEps = 100 ;
while (k < nEps){
logFile.open(logFileName,ios_base::app) ;
logFile << "Episode " << k << endl ;
logFile.close() ;
cout << "Episode " << k << "..." ;
// Iterate for 1000 steps or until all targets are eliminated
unsigned i = 0 ;
while (i < 1000){
testNetwork->Iterate(logFileName) ;
if (testNetwork->GetStateID() == 0)
break ;
i++ ;
}
cout << i << " steps.\n" ;
testNetwork->ResetTargets() ;
k++ ;
}
return 0 ;
}