This repository was archived by the owner on Jun 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwarmGenerator.cpp
More file actions
83 lines (74 loc) · 2.12 KB
/
SwarmGenerator.cpp
File metadata and controls
83 lines (74 loc) · 2.12 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
#include "SwarmGenerator.h"
using namespace std;
string generateSwarm(const SwarmGenerationProperties &props, long seed)
{
//|[attr1,...,attrN]|[v1,...,vN]|w|c1|c2|
srand(seed);
string result = "";
for (int i = 0; i < props.numParticles; i++)
{
result += "|[";
string position = "";
string velocity = "";
for (int j = 0; j < props.numAttributes; j++)
{
double pJ = getRandomNumber(props.minPositionValue, props.maxPositionValue);
double vJ = getRandomNumber(props.minVelocity, props.maxVelocity);
if (j + 1 >= props.numAttributes)
{
position += fromDoubleToString(pJ);
velocity += fromDoubleToString(vJ);
}
else
{
position += fromDoubleToString(pJ) + ",";
velocity += fromDoubleToString(vJ) + ",";
}
}
result += position + "]|[" + velocity + "]|" +
fromDoubleToString(getRandomNumber(props.minW, props.maxW)) + "|" +
fromDoubleToString(getRandomNumber(props.minC1, props.maxC1)) + "|" +
fromDoubleToString(getRandomNumber(props.minC2, props.maxC2)) + "|\n";
}
return result;
}
double getRandomNumber(double min, double max)
{
long max_rand = 1000000L;
if (min > max)
{
double temp;
temp = max;
max = min;
min = temp;
}
int randVal = rand();
return min + (max - min) * (randVal % (int)floor(max_rand)) / max_rand;
}
string fromDoubleToString(double v)
{
stringstream ss;
ss << v;
return ss.str();
}
void defaultSettings(SwarmGenerationProperties &props)
{
props.maxC1 = 0.5;
props.maxC2 = 0.5;
props.maxPositionValue = 100;
props.maxVelocity = 1;
props.maxW = 0.5;
props.minC1 = 0.1;
props.minC2 = 0.1;
props.minPositionValue = -100;
props.minVelocity = 0.1;
props.minW = 0.1;
props.numAttributes = 2;
props.numParticles = 10;
}
void toFile(string fileName, string content)
{
ofstream file(fileName.c_str());
file << content;
file.close();
}