This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Add hexagonal topology to pso #148
Open
sriki18
wants to merge
1
commit into
esa:master
Choose a base branch
from
sriki18:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,8 +77,8 @@ pso::pso(int gen, double omega, double eta1, double eta2, double vcoeff, int var | |
pagmo_throw(value_error,"algorithm variant must be one of 1 ... 6"); | ||
} | ||
|
||
if (neighb_type < 1 || neighb_type > 4) { | ||
pagmo_throw(value_error,"swarm topology variant must be one of 1 ... 4"); | ||
if (neighb_type < 1 || neighb_type > 5) { | ||
pagmo_throw(value_error,"swarm topology variant must be one of 1 ... 5"); | ||
} | ||
// And neighb param??? | ||
} | ||
|
@@ -177,6 +177,7 @@ void pso::evolve(population &pop) const | |
switch( m_neighb_type ){ | ||
case 1: initialize_topology__gbest( pop, best_neighb, best_fit, neighb ); break; | ||
case 3: initialize_topology__von( neighb ); break; | ||
case 5: initialize_topology__hexagonal(neighb);break; | ||
case 4: initialize_topology__adaptive_random( neighb ); | ||
best_fit = pop.champion().f; // need to track improvements in best found fitness, to know when to rewire | ||
break; | ||
|
@@ -380,6 +381,7 @@ decision_vector pso::particle__get_best_neighbor( population::size_type pidx, st | |
case 2: // { lbest } | ||
case 3: // { von } | ||
case 4: // { adaptive random } | ||
case 5: // {hexagonal} | ||
default: | ||
// iterate over indexes of the particle's neighbours, and identify the best | ||
bnidx = neighb[pidx][0]; | ||
|
@@ -565,6 +567,51 @@ void pso::initialize_topology__adaptive_random( std::vector< std::vector<int> > | |
} | ||
|
||
|
||
/** | ||
* @brief Arranges particles in a 2D lattice with alternatively staggered rows, where each interacts with its 6 nearest neighbours. | ||
|
||
*/ | ||
const int hexagonal_neighb_diff_even[6][2] = { {-1,-1}, {0,-1}, {-1,0}, {1,0}, {-1,1}, {0,1} }; | ||
const int hexagonal_neighb_diff_odd[6][2] = { {0,-1}, {1,-1}, {-1,0}, {1,0}, {0,1}, {1,1} }; | ||
|
||
void pso::initialize_topology__hexagonal( std::vector< std::vector<int> > &neighb ) const | ||
{ | ||
int swarm_size = neighb.size(); | ||
int cols, rows; // lattice structure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case there's no reason to align variable names in the column with multiple spaces, use a single space. In this method you're using 8 spaces for indentation. We try to stick with tabs for C++ and 4 spaces for Python code. |
||
int pidx, nidx; // particle and neighbour indices, in the swarm and neighbourhood vectors | ||
int p_x, p_y; // particle's coordinates in the lattice | ||
int n_x, n_y; // particle neighbor's coordinates in the lattice | ||
|
||
rows = std::sqrt( swarm_size ); | ||
while( ( (swarm_size % rows != 0) || (rows % 2 == 1)) && (rows>1) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operator |
||
rows -= 1; | ||
if (rows<2) | ||
pagmo_throw(value_error,"Hexagonal topology requires a swarm size to have an even factor > 2"); | ||
cols = swarm_size / rows; | ||
|
||
for( pidx = 0; pidx < swarm_size; pidx++ ){ | ||
p_x = pidx % cols; | ||
p_y = pidx / cols; | ||
|
||
for( nidx = 0; nidx < 6; nidx++ ){ | ||
if (p_y % 2 == 0){ | ||
n_x = ( p_x + hexagonal_neighb_diff_even[nidx][0] ) % cols; | ||
n_y = ( p_y + hexagonal_neighb_diff_even[nidx][1] ) % rows; | ||
} | ||
else { | ||
n_x = ( p_x + hexagonal_neighb_diff_odd[nidx][0] ) % cols; | ||
n_y = ( p_y + hexagonal_neighb_diff_odd[nidx][1] ) % rows; | ||
} | ||
|
||
if( n_x < 0 ) n_x = cols + n_x; // sign of remainder(%) in a division when at least one of the operands is negative is compiler implementation specific. The 'if' here ensures the same behaviour across compilers | ||
if( n_y < 0 ) n_y = rows + n_y; | ||
|
||
neighb[pidx].push_back( n_y * cols + n_x ); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/// Algorithm name | ||
std::string pso::get_name() const | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not the index order (1,3,4,5) ?
Two spaces after colon