Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Pries, Jason
Oersted
Commits
485f392a
Commit
485f392a
authored
Jan 23, 2018
by
p7k
Browse files
Implements text file data logging for particle swarm optimization
parent
494c8e74
Changes
7
Hide whitespace changes
Inline
Side-by-side
apps/Switched Winding Synchrel/designspace.cpp
View file @
485f392a
...
...
@@ -11,7 +11,7 @@ void limit_angular_thickness(CoordinateSpace &d, Particle &p) {
for
(
size_t
iter
=
0
;
iter
!=
3
;
++
iter
)
{
double_t
scale
{
0.93
/
sum_at
};
sum_at
=
0
;
for
(
size_t
i
=
0
;
i
!=
10
;
++
i
)
{
for
(
size_t
i
=
0
;
i
!=
6
;
++
i
)
{
std
::
string
key
=
"at"
+
std
::
to_string
(
i
);
if
(
iter
==
0
)
{
...
...
apps/Switched Winding Synchrel/main.cpp
View file @
485f392a
...
...
@@ -2,7 +2,7 @@
#include
"globalobjective.h"
#include
"model.h"
#define SAVE_DIR "./"
#define SAVE_DIR "./
output/
"
int
main
(
int
argc
,
char
**
argv
)
{
MPI_Init
(
NULL
,
NULL
);
...
...
@@ -15,7 +15,7 @@ int main(int argc, char** argv) {
// Arguments
size_t
swarm_size
{
31
};
double_t
objective_tolerance
{
1e-2
*
22.0
};
size_t
maximum_iterations
{
3
};
size_t
maximum_iterations
{
20
};
CoordinateSpace
ds
=
design_space
();
...
...
@@ -27,6 +27,10 @@ int main(int argc, char** argv) {
Swarm
swarm
{
ds
,
go
,
swarm_size
,
objective_tolerance
,
maximum_iterations
,
std
::
greater
<
double_t
>
()};
if
(
rank
==
0
)
{
swarm
.
set_output_file
(
SAVE_DIR
,
"sws"
);
}
for
(
auto
&
p
:
swarm
.
Particles
)
{
p
.
local_objective
()
=
[](
ObjectiveMap
m
)
{
if
(
m
[
"power"
]
<
55.0
)
{
...
...
src/Optimization/src/Swarm.cpp
View file @
485f392a
#include
"Swarm.h"
#include
<experimental/filesystem>
void
Swarm
::
run
()
{
std
::
uniform_real_distribution
<>
dist
(
0.0
,
1.0
);
...
...
@@ -14,6 +15,10 @@ void Swarm::run() {
}
}
for
(
size_t
i
=
0
;
i
!=
Particles
.
size
();
++
i
)
{
append_particle_to_output_file
(
iter
,
i
,
Particles
[
i
]);
}
all_converged
=
true
;
for
(
auto
&
p
:
Particles
)
{
all_converged
&=
p
.
is_converged
(
Space
,
CoordinateTolerance
,
ObjectiveTolerance
);
...
...
@@ -41,6 +46,40 @@ void Swarm::run() {
}
}
void
Swarm
::
set_output_file
(
std
::
string
path
,
std
::
string
file_name
)
{
if
(
!
std
::
experimental
::
filesystem
::
exists
(
path
))
{
std
::
experimental
::
filesystem
::
create_directories
(
path
);
}
OutputFile
=
path
+
file_name
+
".txt"
;
std
::
fstream
fs
;
fs
.
open
(
OutputFile
,
std
::
fstream
::
out
);
fs
<<
"iteration,particle,pbest,gbest"
;
for
(
auto
key_ind
:
Particles
[
0
].
state
().
map
())
{
fs
<<
","
<<
key_ind
.
first
;
}
fs
<<
"
\n
"
;
fs
.
close
();
};
void
Swarm
::
append_particle_to_output_file
(
double_t
iter
,
size_t
pnum
,
Particle
&
p
)
{
if
(
OutputFile
.
size
()
>
4
)
{
std
::
fstream
fs
;
fs
.
open
(
OutputFile
,
std
::
fstream
::
out
|
std
::
fstream
::
app
);
fs
<<
iter
<<
","
<<
pnum
<<
","
<<
p
.
personal_best
()
<<
","
<<
p
.
global_best
();
for
(
auto
state
:
p
.
state
().
vector
())
{
fs
<<
","
<<
state
.
Position
;
}
fs
<<
"
\n
"
;
fs
.
close
();
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
Swarm
&
s
)
{
for
(
auto
&
p
:
s
.
Particles
)
{
os
<<
p
;
...
...
@@ -115,6 +154,8 @@ void Swarm::mpi_master_receive(std::deque<int> &process_queue, std::deque<int> &
// Update personal best
Particles
[
particle
].
update_personal_best
(
objective
[
particle
],
Comparator
);
append_particle_to_output_file
(
iter
,(
size_t
)
particle
,
Particles
[
particle
]);
// Perturb
if
(
!
Particles
[
particle
].
has_improved
())
{
Space
.
perturb
(
Particles
[
particle
],
RNG
);
...
...
src/Optimization/src/Swarm.h
View file @
485f392a
#ifndef OERSTED_SWARM_H
#define OERSTED_SWARM_H
#include
<fstream>
#include
"Particle.h"
#include
"CoordinateSpace.h"
...
...
@@ -41,6 +43,10 @@ public:
void
mpi_run
();
void
set_output_file
(
std
::
string
path
,
std
::
string
file_name
);
void
append_particle_to_output_file
(
double_t
iter
,
size_t
pnum
,
Particle
&
p
);
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
Swarm
&
s
);
std
::
vector
<
Particle
>
Particles
;
...
...
@@ -62,7 +68,9 @@ protected:
std
::
mt19937
RNG
;
protected:
// MPI Inforomation
std
::
string
OutputFile
;
protected:
// MPI Information
void
mpi_master
();
void
mpi_master_send
(
std
::
deque
<
int
>
&
processes
,
std
::
deque
<
int
>
&
particles
);
void
mpi_master_receive
(
std
::
deque
<
int
>
&
process_queue
,
std
::
deque
<
int
>
&
particle_queue
,
double_t
&
iter
);
...
...
@@ -83,7 +91,6 @@ protected: // MPI Inforomation
MPI_Datatype
MPI_CoordinateState
;
MPI_Datatype
MPI_ParticleState
;
};
#endif //OERSTED_SWARM_H
test/Optimization/MPI_Particle_Swarm_Optimization.cpp
View file @
485f392a
...
...
@@ -22,6 +22,10 @@ TEST(Particle_Swarm_Optimization, mpi_swarm) {
Swarm
swarm
{
cs
,
go
,
21
,
1e-2
,
50
};
if
(
rank
==
0
)
{
swarm
.
set_output_file
(
SAVE_DIR
,
"mpi_sos"
);
}
for
(
size_t
i
=
0
;
i
!=
swarm
.
size
();
++
i
)
{
swarm
.
Particles
[
i
].
local_objective
()
=
[](
ObjectiveMap
go
)
{
return
go
[
"z0"
];
...
...
test/Optimization/test_Optimization.hpp
View file @
485f392a
...
...
@@ -4,4 +4,6 @@
#include
"Optimization.hpp"
#include
"gtest.h"
#define SAVE_DIR "./test/output/Optimization/"
#endif //OERSTED_TEST_OPTIMIZATION_HPP
test/Optimization/test_ParticleSwarmOptimization.cpp
View file @
485f392a
...
...
@@ -27,7 +27,7 @@ TEST(Particle_Swarm_Optimization, particle_initialization) {
std
::
random_device
rd
;
std
::
mt19937
rng
(
rd
());
Particle
p
=
cs
.
new_particle
(
rng
);
Particle
p
=
cs
.
new_particle
(
rng
,
DBL_MAX
);
for
(
std
::
string
key
:
{
"x"
,
"y"
})
{
EXPECT_GE
(
p
.
position
(
key
),
cs
[
key
].
lower_bound
());
...
...
@@ -78,6 +78,8 @@ TEST(Particle_Swarm_Optimization, single_objective_swarm) {
Swarm
swarm
{
cs
,
go
,
10
,
1e-2
,
20
};
swarm
.
set_output_file
(
SAVE_DIR
,
"sos"
);
for
(
size_t
i
=
0
;
i
!=
swarm
.
size
();
++
i
)
{
swarm
.
Particles
[
i
].
local_objective
()
=
[](
ObjectiveMap
go
)
{
return
go
[
"z0"
];
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment