-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneighborimprovement.h
128 lines (94 loc) · 4.22 KB
/
neighborimprovement.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef NEIGHBORIMPROVEMENT
#define NEIGHBORIMPROVEMENT
#include <iostream>
#include "TSP.h"
#include "TSPSolution.h"
#include "solver.h"
using namespace std;
class NeigthborImprovement
{
public:
/**
* explore the neighbouhood
* @param tsp TSP data
* @param currSol center solution
* @return (into param move) the selected move (stepest descent strategy)
* @return the incremental cost with respect to currSol
*/
virtual double execute(const TSP& tsp, const TSPSolution& currSol, TSPMove& move) = 0;
virtual const string getName() const = 0;
};
class FirstImprovement : public NeigthborImprovement
{
public:
double execute(const TSP& tsp , const TSPSolution& currSol, TSPMove& move) {
//cout << endl << endl << "-------------- find neighbor --------------";
double bestCostVariation = tsp.infinite;
for ( uint a = 1 ; a < currSol.sequence.size() - 2 ; a++ ) {
int h = currSol.sequence[a-1];
int i = currSol.sequence[a];
for ( uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++ ) {
int j = currSol.sequence[b];
int l = currSol.sequence[b+1];
// incremental evaluation --> bestCostVariation (instead of best cost)
double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
+ tsp.cost[h][j] + tsp.cost[i][l];
if (neighCostVariation < bestCostVariation ) {
// neighbor better of precedent neighbors
bestCostVariation = neighCostVariation;
move.from = a;
move.to = b;
double currentBestValueFound = currSol.evaluateObjectiveFunction(tsp);
// on first improvement exit
//cout << endl << "Move from " << a << " to " << b;
//cout << "\tNeighbor Value = " << currentBestValueFound + bestCostVariation << " - CurrentBestValue = " << currentBestValueFound;
if (currentBestValueFound + bestCostVariation < currentBestValueFound) {
//cout << "\tIt's an improvement, chose it!" << endl;
//cout << "------------------------" << endl;
return bestCostVariation;
}
}
}
}
//cout << endl << "------------------------------" << endl;
return bestCostVariation;
}
const string getName() const {
return "First Improvement";
}
};
class BestImprovement : public NeigthborImprovement
{
public:
double execute(const TSP &tsp, const TSPSolution &currSol, TSPMove &move) {
//cout << endl << endl << "------------- find neighbor --------------";
// Determine the *move* yielding the best 2-opt neigbor solution
double bestCostVariation = tsp.infinite;
// initial and final position are fixed (initial/final node remains 0)
for ( uint a = 1 ; a < currSol.sequence.size() - 2 ; a++ ) {
int h = currSol.sequence[a-1];
int i = currSol.sequence[a];
for ( uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++ ) {
int j = currSol.sequence[b];
int l = currSol.sequence[b+1];
// incremental evaluation --> bestCostVariation (instead of best cost)
double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
+ tsp.cost[h][j] + tsp.cost[i][l];
//cout << endl << "Move from " << a << " to " << b;
//cout << "\tNeighbor Variation = " << neighCostVariation << " - BestCostVariation = " << bestCostVariation;
if ( neighCostVariation < bestCostVariation ) {
//cout << "\tSave! Best neighbor found until now";
bestCostVariation = neighCostVariation;
move.from = a;
move.to = b;
}
}
}
//cout << endl << "-----------------------------" << endl;
return bestCostVariation;
}
const string getName() const {
return "Best Improvement";
}
};
#endif // NEIGHBORIMPROVEMENT