New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ant colony optimization algorithm for traveling salesman problem #3914

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@Jimmyzs
Copy link

Jimmyzs commented Dec 9, 2018

Fixes issue:

Issue #3631

Changes:

Adds Ant Colony Optimization algorithm for Traveling Salesman Problem

#include <vector>
#include <ctime>

using namespace std;

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Don't use this.


using namespace std;

const double g_rho = 0.6; // Evaporation constant

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Make these constexpr.

double doubleRand()
{
// Generate random double within the interval [0.0, 1.0)
return double(rand()) / (double(RAND_MAX) + 1.0);

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Is it possible that you could instead use something from the random header instead, which is more modern.

int chooseNextNode (int ant, int startOfTour, int N, vector<vector<double>> &distance,
vector<vector<double>> &pheromone, vector<vector<int>> &parents)
{ // Choose the next vertex for "ant" to go
double denominator = 0, prob, interval = 0;

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Please space out your code a bit, it's very hard to read right now.

}
}
}
else

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

No need for the else.

{
double t = 0;
for (int k = 0; k < N; k++)
if ((parents[k][i]==j) || (parents[k][j]==i) ) // kth ant uses edge (i,j)

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Spaces around all operators.

double calculateDeltaPheromone (int i, int j, int N, vector<vector<int>> &parents, vector<double> &len)
{
double t = 0;
for (int k = 0; k < N; k++)

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member

Please use pre-increment/decrement whenever possible.

cout << k << "->";
k = cheapestPath[k];
}
cout << 0 <<"\nCost is: " << lowestLen<<endl;

This comment has been minimized.

@arnavb

arnavb Jan 10, 2019

Member
Suggested change Beta
cout << 0 <<"\nCost is: " << lowestLen<<endl;
cout << 0 << "\nCost is: " << lowestLen << '\n';

Make sure other lines also have spacing around operators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment