Display solution in 2d vector

Hi everyone.. newbie here.. I seeking some advice regarding my c++ project. I got a problem in reading a solution that I copied from (populationP_shortR & populationP_longR) to a new vector (populationP_shortR_copy & populationP_longR_copy). When I try to display or print vector (populationP_shortR_copy & populationP_shortR_copy) wrong solution displayed. Hopefully you understand what I'm trying to explain here.
Hoping someone can help me. Thank you :)

The solution for populationP_shortR_copy should be in 2d vector consists of 1 row. And, the solution for populationP_longR_copy should be in 2d vector consists of 9 row.


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
198
199
200
201
202
203
204
205
206
207
208
#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;

int main()
{
	// Initialize random seed
	srand(time(NULL));

	// Read and store input data from text file into vector
	// Read IS route from text file
	ifstream IS_route("IS_p102.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 IS from text file
	ifstream IS_data("IS_p102_startnode_Zvalue_routeID.txt");
	while (IS_data >> startnode >> objfunction >> routeID)
	{
		start_node.push_back(startnode);
		obj_function.push_back(objfunction);
		route_id.push_back(routeID);
	}

	// Creating new population P
	for (size_t i = 0; i < generation; i++)
	{
		cout << "Generation: " << i + 1 << "\n";
		
		// 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";

		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;
				}
				cout << endl;
			}
			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;
			}

			
			//Print route for shortR
			cout << "List route will used short runner: " << "\n";
			for (size_t i = 0; i < populationP_shortR_copy.size(); i++)
			{
				for (size_t j = 0; j < populationP_shortR_copy[i].size(); j++)
				{
					cout << populationP_shortR_copy[i][j] << "    ";
				}
				cout << endl;
			}
			cout << endl;

			//Print route for longR
			cout << "List route will used long runner: " << "\n";
			for (size_t i = 0; i < populationP_longR_copy.size(); i++)
			{
				for (size_t j = 0; j < populationP_longR_copy[i].size(); j++)
				{
					cout << populationP_longR_copy[i][j] << "    ";
				}
				cout << endl;
			}
			cout << endl;
		
		}
		cout << endl;
	}
	cout << "\n";
}
Please post a small sample of your input file.

Edit:
Make that input files.

Also since you only have one function you really should move all those global variables into that one function, global variables should rarely if ever used.
Last edited on
Your code is in dire need of reorganization. You should stop using globals, organize your data into structs, and break main up into functions.

What exactly is your code supposed to do?
What is the input?
What is the expected output?
Is there an online description? If so, post a link.
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";
}
This code is even worse than the code in the first post. With all of those "unrelated" global variables it is going to be almost impossible to follow the logic of the program.

As already stated stop with all the global variables, think about how your data is "related" and then consider classes/structures to hold the related data.

Once you have removed the global variables slowly start breaking the code into functions. These functions should probably have their own local variables as well as variables passed into the function and returned from the functions.

Looking at your sample files please explain what the data actually represents.

The same with your output files, what do all those numbers actually mean?


Just factoring out the functions from main is the easiest thing to do, and not useful if you leave the globals. Some of your code doesn't make sense. For example you never use populationP_shortR_copy. You assign to it over and over again in a loop but never use it anywhere.

You need to think about the structure of your data and create a struct (or two or three) to hold it.

What is the purpose of your program?
What is a population?
What is a generation?
What is a route?
What is a runner?
What is short and long?
What in the world is happening???
This code is even worse than the code in the first post. With all of those "unrelated" global variables it is going to be almost impossible to follow the logic of the program.


Sorry for the inconvenience. I'm not so familiar with the use of function but I'll try to learn it.

Looking at your sample files please explain what the data actually represents.



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


small data represent set of route. for example, row 1 represent set of route 1. row 2 represent set of route 2. same as others.

meanwhile, data2 consists of 3 column. column 1 represent startnode of routes in (small data). column 2 represent objective function of route in (small data). column 3 represent route id in (small data). for example row 1, startnode 44, obj function 245.073 and route id 1. route id 1 referring to row 1 in small data.

small data:
0 44 83 30 2 29 61 88 1 93 91 90 59 58 1 0


The same with your output files, what do all those numbers actually mean?


Actually, I have done it. Line 143-152 & 161-171. But, when i want to print whole vector of populationP_shortR_copy and populationP_longR_copy, the solution keep repeating 10 times (line 175-193).

Hopefully you understand what I'm trying to explain..

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <vector>
#include <cstdlib>		// srand, rand()
#include <algorithm>    // find(), iter_swap()
#include <iterator>

using namespace std;

int main()
{
	// Initialize random seed
	srand(time(NULL));

	// Read and store input data from text file into vector
	int population_size = 10;
	vector<vector<int>> populationP;
	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
	vector<int> start_node;
	vector<int> route_id;
	vector<double> obj_function;
	int startnode;
	double objfunction;
	int routeID;
	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);
	}

	// Creating new population P
	int generation = 1;
	for (size_t i = 0; i < generation; i++)
	{
		cout << "Generation: " << i + 1 << "\n";

		// Copying IS route into new vector 
		vector<vector<int>> populationP_copy;
		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
		vector<double> fitnessFX;
		double fitness;
		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
		vector<double> mappingfitnessNX;
		double mappingFitness;
		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
		vector<vector<int>> populationP_sorted;
		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";

		double short_runner_proportional = 0.1;
		vector<vector<int>> populationP_shortR;
		vector<vector<int>> populationP_longR;
		vector<vector<int>> populationP_shortR_copy;
		vector<vector<int>> populationP_longR_copy;

		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;
			}
		}
		cout << "\n";

                for (size_t i = 0; i < populationP_shortR_copy.size(); i++)
		{
			for (size_t j = 0; j < populationP_shortR_copy[i].size(); j++)
			{
				cout << populationP_shortR_copy[i][j] << "    ";
			}
			cout << endl;
		}
		cout << endl;

		for (size_t i = 0; i < populationP_longR_copy.size(); i++)
		{
			for (size_t j = 0; j < populationP_longR_copy[i].size(); j++)
			{
				cout << populationP_longR_copy[i][j] << "    ";
			}
			cout << endl;
		}
		cout << endl;

	}
}

Last edited on

Just factoring out the functions from main is the easiest thing to do, and not useful if you leave the globals. Some of your code doesn't make sense. For example you never use populationP_shortR_copy. You assign to it over and over again in a loop but never use it anywhere.

You need to think about the structure of your data and create a struct (or two or three) to hold it.


Okay, i will try to do it..
Topic archived. No new replies allowed.