Commit 78545b59 authored by Colby Green's avatar Colby Green
Browse files

update

parent 8b8457a4
......@@ -12,100 +12,72 @@ using namespace std;
class OPTTSP {
public:
struct Vertex {
size_t index;
bool visited;
double minDistanceTo;
short precedingPokeIndex;
};
class mst2 {
public:
double pokeDistance(size_t pokeIndex1, size_t pokeIndex2, vector<Poke> &pokeDex){
Poke poke1 = pokeDex[pokeIndex1];
Poke poke2 = pokeDex[pokeIndex2];
double x = poke1.xcord - poke2.xcord;
double y = poke1.ycord - poke2.ycord;
double dist = (x * x) + (y * y);
dist = sqrt(dist);
return dist;
bool promising(size_t permLength){
if ((path.size() - permLength) <= 5){
return true;
}
double getDistance(){
return distance;
vector<Vertex> v;
for (size_t i = permLength - 1; i <= path.size(); i++){
Vertex vertex;
vertex.index = (i == path.size()) ? 0 : size_t(path[i]);
if (i == 0){
vertex.minDistanceTo = 0;
}else {
vertex.minDistanceTo = numeric_limits<double>::infinity();
}
vertex.precedingPokeIndex = -1;
vertex.visited = false;
v.push_back(vertex);
}
mst2(vector<int> &x, vector<Poke> &pokeDex, vector<vector<double>> &distanceMatrix){
for (size_t i = 0; i <= x.size(); i++){
Vertex vertex;
vertex.index = (i == x.size()) ? 0 : size_t(x[i]);
if (i == 0){
vertex.minDistanceTo = 0;
}else {
vertex.minDistanceTo = numeric_limits<double>::infinity();
size_t numVisited = 0;
size_t numToVisit = v.size();
while (numVisited != numToVisit){
double minDistance = numeric_limits<double>::infinity();
size_t minIndex = numeric_limits<size_t>::infinity();
for (size_t i = 0; i < v.size(); i++){
Vertex &vertex = v[i];
if (!vertex.visited && vertex.minDistanceTo < minDistance){
minDistance = vertex.minDistanceTo;
minIndex = i;
}
vertex.precedingPokeIndex = -1;
vertex.visited = false;
v.push_back(vertex);
}
size_t numVisited = 0;
size_t numToVisit = v.size();
while (numVisited != numToVisit){
double minDistance = numeric_limits<double>::infinity();
size_t minIndex = numeric_limits<size_t>::infinity();
for (size_t i = 0; i < v.size(); i++){
Vertex &vertex = v[i];
if (!vertex.visited && vertex.minDistanceTo < minDistance){
minDistance = vertex.minDistanceTo;
minIndex = i;
}
}
v[minIndex].visited = true;
numVisited++;
for (size_t i = 0; i < v.size(); i++){
if (!v[i].visited){
double distance = distanceMatrix[v[minIndex].index][v[i].index];
if (distance == 0){
distance = pokeDistance(v[minIndex].index, v[i].index, pokeDex);
distanceMatrix[v[minIndex].index][v[i].index] = distance;
}
if (distance < v[i].minDistanceTo){
v[i].minDistanceTo = distance;
v[i].precedingPokeIndex = short(minIndex);
}
}
}
} //while
v[minIndex].visited = true;
numVisited++;
double total = 0;
for (size_t i = 0; i < v.size(); i++){
if (v[i].precedingPokeIndex != -1){
total += v[i].minDistanceTo;
if (!v[i].visited){
double distance = distanceMatrix[v[minIndex].index][v[i].index];
if (distance == 0){
distance = pokeDistance(v[minIndex].index, v[i].index);
distanceMatrix[v[minIndex].index][v[i].index] = distance;
}
if (distance < v[i].minDistanceTo){
v[i].minDistanceTo = distance;
v[i].precedingPokeIndex = short(minIndex);
}
}
}
distance = total;
}
} //while
struct Vertex {
size_t index;
bool visited;
double minDistanceTo;
short precedingPokeIndex;
};
private:
double distance;
vector<Vertex> v;
};
bool promising(size_t permLength){
if ((path.size() - permLength) <= 5){
return true;
double distance = 0;
for (size_t i = 0; i < v.size(); i++){
if (v[i].precedingPokeIndex != -1){
distance += v[i].minDistanceTo;
}
}
auto first = path.begin() + int(permLength - 1);
auto last = path.end();
vector<int> newVec(first, last);
mst2 mst(newVec, pokeDex, distanceMatrix);
double totalDistanceLowerBound = curPathDistance + mst.getDistance();
double totalDistanceLowerBound = curPathDistance + distance;
return totalDistanceLowerBound < bestPathDistance;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment