Linker bug in code that was working

Genetic algorithm program worked a few times, but the very same code stopped compiling a minute later. Only one constant value was changed in the code (from 50 to 250), and then it kept bugging when changed back (from 250 to 50).
System: Windows 7 x64 Enterprise
IDE: MS Visual Studio 2010 Premium

Mostly, I'm just asking for help on the compiling error. I've been bashing my head at it for a while trying to figure out what was repeated, but I don't see any degenercies. It looks like that the second error is pointing towards my random function in the header file, "iRandCust", but I can't seem to fix it. That function was "iRand" when I first got the error, and I had hoped that renaming it would fix the bug, but it hasn't.

Errors:
1: error LNK1169: one or more multiply defined symbols found
2: error LNK2005: "int __cdecl iRandCust(int,int)" (?iRandCust@@YAH@Z) already defined in FunctionsNSuch.obj

The program never compiles.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//main.cpp
// Genetic Algorithm CPP program 'n such.
// For the Math final project.
#include "FunctionsNSuch.h"

using namespace std;

int main(){
	cout << "Welcome to the Genetic Algorithm test lab.\n";

	cout << "Creating new set of chromosomes...\n";
	Chromosomes *mainSet = new Chromosomes();

	srand( (unsigned)time(NULL) );
	for (int i = 0; i < 100; i++){rand();}  //Did this because my first few rand() values kept coming back as the same thing every time.
	

	mainSet->PrintBest();
	mainSet->AdvanceWithPrint(50);
	cin.ignore();
	return 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//FunctionsNSuch.h
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>


const int geneSize = 10;
const int chromosomeCount = 100;
const int keepCount = 5;
const int mutantRatio = 3;
const int mutantGeneCount = 4;
const double minLimits[] = {10, 1, 1, 1, 1, 1, 1, 1, 1, 1};
const double maxLimits[] = {100, 10, 10, 10, 10, 10, 10, 10, 10, 10};

static long nextFreeID = 1;

#include <iostream>

using namespace std;

struct Chromosome;
class Chromosomes;

int iRandCust(int lowerBound, int upperBound){
	return (lowerBound + rand() / RAND_MAX * (lowerBound + upperBound));
}

struct Chromosome{
	double g[geneSize];
	double result;
	bool hasResult, elite;
	long ID;
	Chromosome *next;
	void RandomizeGene(int geneIndex){
		g[geneIndex] = minLimits[geneIndex] + (double)rand() / RAND_MAX * (maxLimits[geneIndex] - minLimits[geneIndex]);
		return;
	}
	Chromosome(){
		next = NULL;
		hasResult = false;
		elite = false;
		ID = nextFreeID;
		nextFreeID++;
		for(int i = 0; i < geneSize; i++){
			RandomizeGene(i);
			//cout << "For gene #" << i << ", value: " << g[i] << ".\n";
		}
		//cout << "Made #" << ID << " (" << this << ").\n";
	}
	Chromosome(Chromosome *toDuplicate){
		next = NULL;
		hasResult = false;
		elite = false;
		ID = nextFreeID;
		nextFreeID++;
		for(int i = 0; i < geneSize; i++){
			g[i] = toDuplicate->g[i];
			//cout << "For gene #" << i << ", value: " << g[i] << ".\n";
		}
		//cout << "Made #" << ID << " (" << this << ").\n";
	}
	void Evaluate(){
		hasResult = true;
		result = (g[1] + g[2] * g[3] / g[4] - g[5] / g[6] + g[7] * g[8] * g[9]) / g[0];
	}
};

class Chromosomes{
	//Chromosome *first[2], *last[2], *toDelete;
	Chromosome *first, *last, *toDelete, *throwAway;
	int chromCount;
	//double maxFitness;
	Chromosome *elites[keepCount];
	int weakElite;
	long generationIndex;
	long deleteCount;
	int deletedList[1000];
	void DeleteChr(Chromosome *toDelete){
		/*cout << "Deleting #" << toDelete->ID << " (" << deleteCount << ").  (" << toDelete << ")\n";
		for(int i = 0; i < deleteCount; i++){
			if (toDelete->ID == deletedList[i]){
				cout << "Duplicate deletion: " << toDelete->ID << "!\n";
			}
		}
		deletedList[deleteCount] = toDelete->ID;
		deleteCount++;*/
		delete toDelete;
	}
	void UpdateWeakElite(){
		weakElite = 0;
		for (int i = 0; i < keepCount; ++i){
			if(elites[i]->result < elites[weakElite]->result){
				weakElite = i;
			}
		}
	}
	void AddNewChromosome(){
		if(first==NULL){
			throwAway = new Chromosome();
			DeleteChr(throwAway);
			first = new Chromosome();
			last = first;
		} else {
			last->next = new Chromosome();
			last = last->next;
		}
		chromCount++;
		return;
	}
	void AddDuplicantChromosome(Chromosome *parent){
		if(first==NULL){
			throwAway = new Chromosome();
			DeleteChr(throwAway);
			first = new Chromosome();
			last = first;
		} else {
			last->next = new Chromosome();
			last = last->next;
		}
		chromCount++;
		return;
	}
	void AddMutants(){
		Chromosome *current;
		int toMutate;
		
		current = first;
		for (int i = 0; i < keepCount; i++){
			for (int j = 0; j < mutantRatio; j++){
				AddDuplicantChromosome(current);
				for (int k = 0; k < mutantGeneCount; k++){
					toMutate = iRandCust(0, geneSize - 1);
					last->RandomizeGene(toMutate);
				}
			}
			current = current->next;
		}
	}
public:
	Chromosomes(){
		deleteCount = 0;
		first = NULL;
		last = NULL;
		chromCount = 0;
		while(chromCount < chromosomeCount){
			//cout << "Adding...\n";
			AddNewChromosome();
		}
		generationIndex = 1;
	}
	~Chromosomes(){
		Chromosome *current, *toDelete;
		current = first;
		while(current){
			toDelete = current;
			current = current->next;
			DeleteChr(toDelete);
		}
	}

	void EvaluateAll(){
		Chromosome *current;
		current = first;
		while(current){
			if(!current->hasResult){
				current->Evaluate();
			}
			current = current->next;
		}
	}
	void ToNextGeneration(){	//Move to the next generation
		generationIndex++;
		
		//Evaluate
		EvaluateAll();
		
		//First, ID and keep the best from the prior generation
		Chromosome *current = first;
		for(int i = 0; i < keepCount; i++){		//Count first few chromosomes as tenative elites
			elites[i] = current;
			//cout << "Temp elite, default:  " << current->result << endl;
			current = current->next;
		}
		UpdateWeakElite();						//Find the weak elite
		//cout << "Chromosome count = " << chromCount << ".\n";
		for (int i = keepCount; i < chromosomeCount; ++i){		//Go through keeping high values as the elites
			if (current->result > elites[weakElite]->result){
				//cout << "Better elite found:  " << current->result << " (higher than " << elites[weakElite]->result << ").\n";
				elites[weakElite] = current;
				UpdateWeakElite();
			}
			current = current->next;
		}
		for (int i = 0; i < keepCount; i++){		//Mark elites
			elites[i]->elite = true;
			//cout << "Mark elite loop iteration #" << i << endl;
		}
		current = first;
		while(!current->elite){		//Delete until the first elite
			toDelete = current;
			current = current->next;
			DeleteChr(toDelete);
		}
		first = current;
		last = first;
		first->elite = false;
		//cout << "Found elite (" << current->result << ").\n";
		current = current->next;
		while (current) {
			if (current->elite) {
				last->next = current;
				last = current;
				last->elite = false;
				//cout << "Found elite (" << current->result << ").\n";
				current = current->next;
			} else {
				toDelete = current;
				//cout << "Found non-elite (" << current->result << ").\n";
				current = current->next;
				DeleteChr(toDelete);
			}
		}
		chromCount = keepCount;
		// Now only the elite chromosomes are left.
		
		// Generate new chromosomes
		while (chromCount < chromosomeCount){
			AddNewChromosome();
		}
	};

	void Advance (int numberOfGenerations){
		for (int i = 0; i < numberOfGenerations; i++){
			ToNextGeneration();
		}
	};
	void AdvanceWithPrint (int numberOfGenerations){
		for (int i = 0; i < numberOfGenerations; i++){
			ToNextGeneration();
			PrintBest();
		}
	};
	void PrintBest(){
		EvaluateAll();

		Chromosome *current, *currentElite;
		
		currentElite = first;
		current = first->next;

		while(current){
			if(current->result > currentElite->result){
				currentElite = current;
			}
			current = current->next;
		}

		//cout << "\nOut of " << chromCount << " chromosomes, the current best value is " << currentElite->result << " from the genes:\n";
		cout << "\nBest value in generation #" << generationIndex << " is " << currentElite->result << " from the genes:\n";
		for (int i = 0; i < (geneSize - 1); i++){
			//cout << " Gene #" << (i + 1) << ":  " << currentElite->g[i] << ", \n";
			cout << currentElite->g[i] << ", ";
		}
		//cout << " Gene #" << geneSize << ":  " << currentElite->g[(geneSize - 1)] << ".\n";
		cout << currentElite->g[(geneSize - 1)] << ".\n";
	};
};
Last edited on
Update:

I took out
1
2
3
int iRandCust(int lowerBound, int upperBound){
	return (lowerBound + rand() / RAND_MAX * (lowerBound + upperBound));
}

and just replaced the one place where I used this function with the plain code. The program works now, so I'm guessing that this was the issue.

Can anyone tell me why that function was messing it up?
Topic archived. No new replies allowed.