-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleVisualMain.cpp
155 lines (137 loc) · 5.35 KB
/
SimpleVisualMain.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
#include "random.h"
#include "VisualAgent.h"
#include <iostream>
#include <fstream>
#include <chrono>
// Global constants
// const double StepSize = 0.1;
// const double RunDuration = 200;
const long RandomSeed = 1;
// The main program
void run_for_line(ofstream &outFile, double StepSize, int X1, int Y1, int X2, int Y2, bool showDetails=false){
VisualAgent Agent;
Line Object;
// Circle Object;
// Load the CTRNN into the agent
char fname[] = "categorize.ns";
ifstream ifs;
ifs.open(fname);
if (!ifs) {
cerr << "File not found: " << fname << endl;
exit(EXIT_FAILURE);
}
ifs >> Agent.NervousSystem;
// Run the agent
SetRandomSeed(RandomSeed);
Agent.Reset(0, Y1);
Agent.SetPositionX(X1);
Object.SetPositionX(X2);
Object.SetPositionY(Y2);
int timer = 0;
int status = 0;
auto start = std::chrono::system_clock::now();
if (showDetails) Agent.NervousSystem.PrintModelAbstract();
for (double t = 0; Object.PositionY() > BodySize/2; t += StepSize) {
if (showDetails){
cout << "------------------\n";
cout << Agent.PositionX() << " " << Agent.PositionY() << " \n";
cout << Object.PositionX() << " " << Object.PositionY() << " \n";
}
outFile << 1 << ',' << ++timer << ',' << StepSize << ',' << X1 << ',' << Y1 << ',' << X2 << ',' << Y2 << ',';
outFile << Agent.PositionX() << "," << Agent.PositionY() << "," << Object.PositionX() << "," << Object.PositionY() << "," << status <<" \n";
status = 1;
Agent.Step(StepSize, Object, showDetails );
Object.Step(StepSize);
if (showDetails) Agent.NervousSystem.PrintModelAbstract();
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
if (showDetails){
cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
}
outFile << 1 << ',' << ++timer << ',' << StepSize << ',' << X1 << ',' << Y1 << ',' << X2 << ',' << Y2 << ',';
outFile << Agent.PositionX() << "," << Agent.PositionY() << "," << Object.PositionX() << "," << Object.PositionY() << "," << ++status <<" \n";
}
void run_for_Circle(ofstream &outFile, double StepSize, int X1, int Y1, int X2, int Y2, bool showDetails=false){
VisualAgent Agent;
// Line Object;
Circle Object;
// Load the CTRNN into the agent
char fname[] = "categorize.ns";
ifstream ifs;
ifs.open(fname);
if (!ifs) {
cerr << "File not found: " << fname << endl;
exit(EXIT_FAILURE);
}
ifs >> Agent.NervousSystem;
// Run the agent
SetRandomSeed(RandomSeed);
Agent.Reset(0, Y1);
Agent.SetPositionX(X1);
Object.SetPositionX(X2);
Object.SetPositionY(Y2);
int timer = 0;
int status = 0;
auto start = std::chrono::system_clock::now();
if (showDetails) Agent.NervousSystem.PrintModelAbstract();
for (double t = 0; Object.PositionY() > BodySize/2; t += StepSize) {
if (showDetails){
cout << "------------------\n";
cout << Agent.PositionX() << " " << Agent.PositionY() << " \n";
cout << Object.PositionX() << " " << Object.PositionY() << " \n";
}
outFile << 2 << ',' << ++timer << ',' << StepSize << ',' << X1 << ',' << Y1 << ',' << X2 << ',' << Y2 << ',';
outFile << Agent.PositionX() << "," << Agent.PositionY() << "," << Object.PositionX() << "," << Object.PositionY() << "," << status <<" \n";
status = 1;
Agent.Step(StepSize, Object, showDetails );
Object.Step(StepSize);
if (showDetails) Agent.NervousSystem.PrintModelAbstract();
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
if (showDetails){
cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
}
outFile << 2 << ',' << ++timer << ',' << StepSize << ',' << X1 << ',' << Y1 << ',' << X2 << ',' << Y2 << ',';
outFile << Agent.PositionX() << "," << Agent.PositionY() << "," << Object.PositionX() << "," << Object.PositionY() << "," << ++status <<" \n";
}
void showModel(string fname){
CTRNN NervousSystem;
ifstream ifs;
ifs.open(fname);
if (!ifs) {
cerr << "File not found: " << fname << endl;
exit(EXIT_FAILURE);
}
ifs >> NervousSystem;
cout << "------------------------------------\n";
NervousSystem.PrintModel();
}
int main(int argc, char* argv[])
{
ofstream outFile;
outFile.open ("output.csv");
outFile << "obj_type,timer,step_size,X1,Y1,X2,Y2,agent_X,agent_Y,obj_X,obj_Y,status\n";
// int X1, Y1, X2, Y2;
for (int j1=150; j1 < 275; j1+=20){
for (int j2=-30; j2 <= 30; j2+=15){
for (double i = 0.1; i <= 0.2; i+= 0.05){
cout << "------------------------------------\n";
cout << i << " " << j2<< " " << 0<< " " <<-32 + int((275-j1)/2)<< " " << j1 << "\n";
run_for_line(outFile, i, j2, 0, -32 + int((275-j1)/2), j1);
run_for_Circle(outFile, i, j2, 0, -32 + int((275-j1)/2), j1);
}
}
}
// run_for_line(outFile, 0.1, -20, 0, 0, 200);
// cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
// run_for_Circle(outFile, 0.1, -20, 0, 0, 200);
outFile.close();
showModel("categorize.ns");
return 0;
}