Hi
jb &
dutch. Sorry for my unorganised code.
Here is the small sample of input.
small data:
0 44 83 30 2 29 61 88 1 93 91 90 59 58 1 0
0 0 8 50 75 87 82 1 80 69 14 65 28 1 0
0 100 84 17 51 72 68 2 85 78 47 45 15 2 0
0 0 8 50 75 87 82 1 80 69 14 65 28 1 0
0 65 87 82 80 69 14 1 75 8 50 28 42 2 0
0 85 78 47 45 68 2 15 52 8 50 75 87 1 0
0 96 97 11 27 25 1 24 63 26 13 40 81 2 0
0 57 5 74 41 1 42 28 50 2 8 75 87 82 2 0
0 0 8 50 75 2 87 82 80 69 14 65 28 1 0
0 14 87 82 80 1 69 65 75 1 8 50 28 42 2 0
data2:
44 245.073 1
56 206.798 2
100 222.541 3
70 206.798 4
65 206.879 5
85 219.329 6
96 184.308 7
57 202.072 8
37 206.798 9
14 206.95 10
Referring code line 132 until 174 (runner_selection function). Here, I suppose to create two new 2d vector which are populationP_shortR and populationP_longR. This was created by looking at mappingfitnessNX (line 85-93). If the mappingfitnessNX lower than 0.1, than the particular route will be in new vector populationP_shortR. Else, more than 0.1, will be in new vector populationR_longR.
Actually, I would like to print this two new vector and using it for the next process. But, I don't know how to print it. I try to print it, but the solution replicated 10 times for both vector.
The expected output should be.
PopulationP_shortR
0 44 83 30 2 29 61 88 1 93 91 90 59 58 1 0
PopulationP_longR
0 100 84 17 51 72 68 2 85 78 47 45 15 2 0
0 85 78 47 45 68 2 15 52 8 50 75 87 1 0
0 14 87 82 80 1 69 65 75 1 8 50 28 42 2 0
0 65 87 82 80 69 14 1 75 8 50 28 42 2 0
0 0 8 50 75 87 82 1 80 69 14 65 28 1 0
0 0 8 50 75 2 87 82 80 69 14 65 28 1 0
0 0 8 50 75 87 82 1 80 69 14 65 28 1 0
0 57 5 74 41 1 42 28 50 2 8 75 87 82 2 0
0 96 97 11 27 25 1 24 63 26 13 40 81 2 0
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <vector>
#include <cstdlib> // srand, rand()
#include <algorithm> // find(), iter_swap()
#include <iterator>
using namespace std;
// Declare vectors
vector<vector<int>> populationP;
vector<vector<int>> populationP_copy;
vector<vector<int>> populationP_sorted;
vector<vector<int>> populationP_shortR;
vector<vector<int>> populationP_longR;
vector<vector<int>> populationP_shortR_copy;
vector<vector<int>> populationP_longR_copy;
vector<int> start_node;
vector<int> route_id;
vector<double> obj_function;
vector<double> fitnessFX;
vector<double> mappingfitnessNX;
// Declare variables
int startnode;
int routeID;
int population_size = 10;
int generation = 1;
double objfunction;
double fitness;
double mappingFitness;
double short_runner_proportional = 0.1;
void input()
{
// Read route from text file
ifstream IS_route("small data.txt");
for (string line; getline(IS_route, line); )
{
populationP.push_back(vector<int>());
istringstream iss(line);
for (int n; iss >> n; )
populationP.back().push_back(n);
}
// Read solution input from text file
ifstream IS_data("data2.txt");
while (IS_data >> startnode >> objfunction >> routeID)
{
start_node.push_back(startnode);
obj_function.push_back(objfunction);
route_id.push_back(routeID);
}
}
void new_populationP()
{
// Copying IS route into new vector
populationP_copy = populationP;
// Finding maximum & minimum value of obj function
double Zmax = *max_element(obj_function.begin(), obj_function.end());
double Zmin = *min_element(obj_function.begin(), obj_function.end());
cout << "Max obj function in current population P : " << Zmax << '\n';
cout << "Min obj function in current population P : " << Zmin << '\n';
cout << "\n";
// Calculate fitness value of each plant
cout << "Fitness :" << "\n";
const double diff = Zmax - Zmin;
if (diff > 0)
{
for (size_t i = 0; i < populationP_copy.size(); i++)
{
fitness = (Zmax - obj_function[i]) / diff;
fitnessFX.push_back(fitness);
cout << start_node[i] << " " << fitness << "\n";
}
cout << "\n";
}
cout << "\n";
// Calculate mapping fitness value of each plant
cout << "Mapping Fitness :" << endl;
for (size_t i = 0; i < populationP_copy.size(); i++)
{
mappingFitness = (0.5 *(tanh(4 * fitnessFX[i] - 2) + 1));
mappingfitnessNX.push_back(mappingFitness);
cout << start_node[i] << " " << mappingFitness << "\n";
}
cout << "\n";
// Sort population P in ascending order of mapping fitness values NX (for minimization)
// Sort using bubble sort
for (size_t i = 0; i < population_size; i++)
{
for (size_t j = i + 1; j <= population_size - 1; j++)
{
if (mappingfitnessNX[i] > mappingfitnessNX[j])
{
//swapping two elements as parallel arrays
swap(mappingfitnessNX[i], mappingfitnessNX[j]);
swap(start_node[i], start_node[j]);
swap(route_id[i], route_id[j]);
swap(populationP_copy[i], populationP_copy[j]);
}
}
}
cout << "Sorted mapping fitness :" << "\n";
for (size_t i = 0; i < population_size; i++)
{
cout << start_node[i] << " " << mappingfitnessNX[i] << " " << route_id[i] << "\n";
}
cout << "\n";
cout << "Sorted route :" << "\n";
for (size_t i = 0; i < populationP_copy.size(); i++)
{
for (size_t j = 0; j < populationP_copy[i].size(); j++)
{
cout << populationP_copy[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void runner_selection()
{
for (size_t i = 0; i < population_size; i++)
{
if (short_runner_proportional >= mappingfitnessNX[i])
{
cout << "\n";
cout << "Short runner " << i + 1 << "\n";
cout << "Found at route [" << route_id[i] << "]: "
<< mappingfitnessNX[i]
<< "\n";
cout << "Route: " << "\n";
for (size_t j = 0; j < populationP_copy[i].size(); j++)
{
cout << populationP_copy[i][j] << " ";
populationP_shortR.push_back(populationP_copy[i]);
// Copying short runner route into new vector
populationP_shortR_copy = populationP_shortR;
}
}
else
{
cout << "\n";
cout << "Long runner " << i << "\n";
cout << "Found at route [" << route_id[i] << "]: "
<< mappingfitnessNX[i]
<< "\n";
cout << "Route: " << "\n";
for (size_t j = 0; j < populationP_copy[i].size(); j++)
{
cout << populationP_copy[i][j] << " ";
populationP_longR.push_back(populationP_copy[i]);
// Copying long runner route into new vector
populationP_longR_copy = populationP_longR;
}
cout << endl;
}
}
}
int main()
{
// Initialize random seed
srand(time(NULL));
// Read and store input data from text file into vector
input();
// Creating new population P
for (size_t i = 0; i < generation; i++)
{
cout << "Generation: " << i + 1 << "\n";
new_populationP();
runner_selection();
}
cout << "\n";
}
|